1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452
|
# -*- coding: utf-8 -*-
"""
Code for creating and editing "fake directories" within the `seedir` package;
i.e. Python folder tree objects. This module can be used
to make example folder tree diagrams, read folder tree strings, or convert
abstract folder trees into real directories on a computer.
"""
__pdoc__ = {'get_random_int': False,
'recursive_add_fakes': False}
import os
import string
import re
import random
from seedir.errors import FakedirError
from seedir.folderstructure import FakeDirStructure, RealDirStructure
from seedir.folderstructure import listdir_fullpath
from seedir.printing import words
class FakeItem:
'''Parent class for representing fake folders and files.'''
def __init__(self, name, parent=None):
'''
Initialize the fake diretory or file object.
Parameters
----------
name : str
Name for the folder or file.
parent : seedir.fakedir.FakeDir or None, optional
Parent of `self`. The default is `None`, meaning the object will
be the "root" of the directory.
Returns
-------
None.
'''
self.name = name
self._parent = None
self.parent = parent
self.depth = 0
self.set_depth()
@property
def parent(self):
'''
Getter for the `parent` attribute.
Returns
-------
seedir.fakedir.FakeDir
The parent folder of the object.
'''
return self._parent
@parent.setter
def parent(self, other):
'''
Setter for the `parent` attribute.
When a new parent is assigned for an object, this method
verifies that the other object is `seedir.fakedir.FakeDir`, and that the other
objects children do not contain a fake item with the same name.
If those conditions are met, `self` is removed from the children
of its current parent, and its parent attribute is reassigned.
Depths are also reset.
Parameters
----------
other : seedir.fakedir.FakeDir
Fake directory to become the new `parent` for self.
Raises
------
TypeError
other is not a `seedir.fakedir.Fakedir`.
FakedirError
Name collision: `self.name` is in the child names of other.
Returns
-------
None.
'''
if other:
if not isinstance(other, FakeDir):
raise TypeError('other parameter must be instance of FakeDir')
if self.name in other.get_child_names():
raise FakedirError('FakeDirs must have unique file/folder names')
other._children.append(self)
if self.parent is not None:
self.parent._children.remove(self)
self._parent = other
self.set_depth()
if isinstance(self, FakeDir):
self.set_child_depths()
def get_path(self):
'''Return a "path" string of self, from the head `seedir.fakedir.FakeDir`
(which has `parent == None`).
```
>>> import seedir as sd
>>> f = sd.randomdir(seed=5) # create a random FakeDir
>>> f
MyFakeDir/
├─senor.txt
├─verb.txt
├─takeoff.txt
├─monastic/
│ ├─paddy.txt
│ ├─ewe.txt
│ ├─advantage.txt
│ ├─hooves/
│ └─registrar/
└─zag/
├─thematic.txt
├─inelastic.txt
├─fierce.txt
├─gout/
└─stein/
├─vector.txt
├─sora.txt
└─proviso.txt
>>> x = sd.FakeDir('newfolder', parent=f['zag/stein'])
>>> x.get_path()
'MyFakeDir/zag/stein/newfolder'
```
'''
parents = [self.name]
on = self
while on.parent is not None:
on = on.parent
parents.append(on.name)
return '/'.join(parents[::-1])
def set_depth(self):
'''Set the depth attribute of `self`, based on the depth of parent.
Automatically called when setting new parents.'''
if self.parent is None:
self.depth = 0
else:
self.depth = self.parent.depth + 1
def isfile(self):
"""Returns `True` if instance is a `seedir.fakedir.FakeFile` object"""
return isinstance(self, FakeFile)
def isdir(self):
"""Returns True if instance is a `seedir.fakedir.FakeDir` object"""
return isinstance(self, FakeDir)
def siblings(self):
"""Returns all the other children of `self.parent`."""
if self.parent is None:
return []
else:
return [f for f in self.parent.listdir() if f is not self]
class FakeFile(FakeItem):
'''Class to represent files in `seedir.fakedir.FakeDir` objects.
Files in a `seedir.fakedir.FakeDir` have this type.
```
>>> import seedir as sd
>>> f = sd.randomdir(seed=5) # create a random FakeDir
>>> f
MyFakeDir/
├─senor.txt
├─verb.txt
├─takeoff.txt
├─monastic/
│ ├─paddy.txt
│ ├─ewe.txt
│ ├─advantage.txt
│ ├─hooves/
│ └─registrar/
└─zag/
├─thematic.txt
├─inelastic.txt
├─fierce.txt
├─gout/
└─stein/
├─vector.txt
├─sora.txt
└─proviso.txt
>>> f['senor.txt']
FakeFile(MyFakeDir/senor.txt)
```
'''
def __init__(self, name, parent=None):
'''Same as `seedir.fakedir.FakeItem` initialization, but adds
`filename` and `extension` attributes.
'''
super().__init__(name, parent)
self.filename, self.extension = os.path.splitext(self.name)
def __str__(self):
return 'FakeFile({})'.format(self.get_path())
def __repr__(self):
return 'FakeFile({})'.format(self.get_path())
class FakeDir(FakeItem):
'''Class to represent fake folders. Can be used to create
custom folder tree diagrams. See `seedir.fakedir.fakedir()` for converting
a real directory into a FakeDir, `seedir.fakedir.fakedir_fromstring()` for
converting a text folder diagram into a `FakeDir`, and `seedir.fakedir.randomdir()`
for creating a random one.
To make a `FakeDir` from scratch, use this class:
```
>>> import seedir as sd
>>> x = sd.FakeDir('myfakedir')
>>> x.seedir()
myfakedir/
```
There are various ways to add to it:
```
# using methods; the created items are returned
>>> x.create_file(['__init__.py', 'main.py', 'styles.txt'])
[FakeFile(myfakedir/__init__.py), FakeFile(myfakedir/main.py), FakeFile(myfakedir/styles.txt)]
>>> x.create_folder('docs')
docs/
# initializing new objects and setting the parent
>>> y = sd.FakeDir('resources', parent=x)
# changing the parent of existing objects
>>> z = sd.FakeDir('images')
>>> z.parent = y
>>> for n in ['a', 'b', 'c']:
... z.create_file(n + '.png')
FakeFile(myfakedir/resources/images/a.png)
FakeFile(myfakedir/resources/images/b.png)
FakeFile(myfakedir/resources/images/c.png)
>>> x.seedir(sort=True, first='folders')
myfakedir/
├─docs/
├─resources/
│ └─images/
│ ├─a.png
│ ├─b.png
│ └─c.png
├─__init__.py
├─main.py
└─styles.txt
```
You can index with path-like strings:
```
>>> x['resources/images/a.png']
FakeFile(myfakedir/resources/images/a.png)
```
'''
def __init__(self, name, parent=None):
'''Same as `seedir.fakedir.FakeItem` initialization, but adds
the `_children` attribute for keeping track of items inside the fake dir.
'''
# alter children through FakeDir methods!
self._children = []
super().__init__(name, parent)
def __str__(self):
'''String conversion of `FakeDir`'''
return 'FakeDir({})'.format(self.get_path())
def __repr__(self):
'''Representation of `FakeDir` (shown as a folder diagram).'''
return self.seedir(printout=False)
def __getitem__(self, path):
"""Use path-like strings to index `FakeDir` objects."""
if not isinstance(path, str):
raise TypeError("Can only index FakeDir with int or str, "
"not {}".format(type(path)))
paths = path.split('/')
current = self
for p in paths:
for f in current._children:
if p == f.name:
current = f
break
else:
raise(FakedirError('Path "{}" not found through {}'.format(path, self)))
return current
def create_folder(self, name):
"""
Create a new folder (`seedir.fakedir.FakeDir`) as a child.
```
>>> import seedir as sd
>>> x = sd.FakeDir('Test')
>>> x.create_folder("new_folder")
new_folder/
>>> x.seedir()
Test/
└─new_folder/
```
Parameters
----------
name : str
Name of the new folder. Can also be a collection of names to create
multiple folders.
Returns
-------
FakeDir or list
The new object or objects (as a list) are returned.
"""
if isinstance(name, str):
return FakeDir(name, parent=self)
else:
return [FakeDir(s, parent=self) for s in name]
def create_file(self, name):
"""
Create a new file (`seedir.fakedir.FakeFile`) as a child.
```
>>> import seedir as sd
>>> x = sd.FakeDir('Test')
>>> x.create_file("new_file.txt")
FakeFile(Test/new_file.txt)
>>> x.seedir()
Test/
└─new_file.txt
```
Parameters
----------
name : str
Name of the new file. Can also be a collection of names to create
multiple files.
Returns
-------
FakeFile or list
The new object or objects (as a list) are returned.
"""
if isinstance(name, str):
return FakeFile(name, parent=self)
else:
return [FakeFile(s, parent=self) for s in name]
def copy(self):
'''
Generate a totally unlinked copy object. The root of the new FakeDir
will be a copy of this folder (and all its subfolders). Calling this
method does not alter self at all.
Returns
-------
seedir.fakedir.FakeDir
A copy FakeDir.
'''
def recurse_build(f, other):
'''Recursive helper for building the new copy.'''
if f.isfile():
new = FakeFile(name=f.name, parent=other)
elif f.isdir():
new = FakeDir(name=f.name, parent=other)
for child in f.listdir():
recurse_build(child, other=new)
return new
return recurse_build(self, other=None)
def delete(self, child):
'''
Delete items from a `seedir.fakedir.FakeDir`.
```
>>> import seedir as sd
>>> r = sd.randomdir(seed=5)
>>> r
MyFakeDir/
├─senor.txt
├─verb.txt
├─takeoff.txt
├─monastic/
│ ├─paddy.txt
│ ├─ewe.txt
│ ├─advantage.txt
│ ├─hooves/
│ └─registrar/
└─zag/
├─thematic.txt
├─inelastic.txt
├─fierce.txt
├─gout/
└─stein/
├─vector.txt
├─sora.txt
└─proviso.txt
>>> r['zag'].delete(['thematic.txt', 'inelastic.txt']) # delete with string names
>>> r.delete(r['monastic']) # delete with objects
>>> r
MyFakeDir/
├─senor.txt
├─verb.txt
├─takeoff.txt
└─zag/
├─fierce.txt
├─gout/
└─stein/
├─vector.txt
├─sora.txt
└─proviso.txt
```
Parameters
----------
child : str, FakeDir, FakeFile or list-like
Child or children to remove. Can be a string name, actual
`seedir.fakedir.FakeDir` / `seedir.fakedir.FakeFile` object,
or a collection of names or items.
Raises
------
FakedirError
No item found to delete.
Returns
-------
None.
'''
target = None
if type(child) in [FakeDir, FakeFile]:
target = child.name
elif isinstance(child, str):
target = child
if target is not None:
try:
to_del = next(f for f in self._children if f.name == target)
to_del.parent = None
except StopIteration:
raise FakedirError('{} has no child with name "{}"'.format(self, target))
else:
child_copy = [c for c in child]
for c in child_copy:
target = None
if type(c) in [FakeDir, FakeFile]:
target = c.name
elif isinstance(c, str):
target = c
if target is not None:
try:
to_del = next(f for f in self._children if
f.name == target)
to_del.parent = None
except StopIteration:
raise FakedirError('{} has no child with name "{}"'.format(self, target))
def get_child_names(self):
'''Return a list of child names.
```
>>> import seedir as sd
>>> r = sd.randomdir(seed=5)
>>> r
MyFakeDir/
├─senor.txt
├─verb.txt
├─takeoff.txt
├─monastic/
│ ├─paddy.txt
│ ├─ewe.txt
│ ├─advantage.txt
│ ├─hooves/
│ └─registrar/
└─zag/
├─thematic.txt
├─inelastic.txt
├─fierce.txt
├─gout/
└─stein/
├─vector.txt
├─sora.txt
└─proviso.txt
>>> r.get_child_names()
['senor.txt', 'verb.txt', 'takeoff.txt', 'monastic', 'zag']
```
'''
return [c.name for c in self._children]
def listdir(self):
'''Return the list of `seedir.fakedir.FakeFile` and `seedir.fakedir.FakeDir`
objects that are children of `self` (like `os.listdir`).
```
>>> import seedir as sd
>>> r = sd.randomdir(seed=1)
>>> r
MyFakeDir/
├─churchmen.txt
└─exposure/
>>> print([str(i) for i in r.listdir()])
['FakeFile(MyFakeDir/churchmen.txt)', 'FakeDir(MyFakeDir/exposure)']
```
'''
return self._children
def realize(self, path=None):
'''
Convert a fake file tree into a real one by creating a folder at a
given path, and populating it with files and sub-directories.
All files will be empty.
```
import os
import seedir as sd
r = sd.randomdir(seed=1)
# MyFakeDir/
# ├─churchmen.txt
# └─exposure/
r.realize()
os.path.isdir('MyFakeDir/exposure')
# True
```
Parameters
----------
path : str, optional
System path where to create the folder. The default is `None`,
in which the current working directory is used.
Returns
-------
None.
'''
def create(f, root):
fpath = f.get_path()
joined = os.path.join(root, fpath)
if isinstance(f, FakeDir):
os.mkdir(joined)
elif isinstance(f, FakeFile):
with open(joined, 'w'): pass;
if path is None:
path = os.getcwd()
self.walk_apply(create, root=path)
def seedir(self, style='lines', printout=True, indent=2, uniform=None,
anystart=None, anyend=None, depthlimit=None, itemlimit=None,
beyond=None, first=None, sort=False, sort_reverse=False, sort_key=None,
include_folders=None, exclude_folders=None, include_files=None,
exclude_files=None, regex=False, mask=None,
formatter=None, sticky_formatter=False,
acceptable_listdir_errors=None, denied_string=' [ACCESS DENIED]', **kwargs):
'''
Create a folder tree diagram for `self`. `seedir.fakedir.FakeDir` version of
`seedir.realdir.seedir()` (see its documentation for examples).
Parameters
----------
style : 'lines', 'dash', 'arrow', 'spaces', 'plus', or 'emoji', optional
Style to use. The default is `'lines'`. A style determines the set
of characters ("tokens") used to represent the base structure of
the directory (e.g. which items belong to which folders, when items
are the last member of a folder, etc.). The actual tokens being used
by each style can be viewed with `seedir.printing.get_styleargs()`.
printout : bool, optional
Print the folder structure in the console. The default is `True`. When
`False`, the folder diagram is returned as a string.
indent : int (>= 0), optional
Number of spaces separating items from their parent folder.
The default is `2`.
uniform : str or None, optional
Characters to use for all tokens when creating the tree diagram.
The default is `None`. When not `None`, the extend, space, split, and
final tokens are replaced with `uniform` (the `'spaces'` style is
essentially `uniform = ' '`).
anystart : str or None, optional
Characters to append before any item (i.e. folder or file). The
default is `None`. Specific starts for folders and files can be
specified (see `**kwargs`).
anyend : str or None, optional
Characters to append after any item (i.e. folder or file). The
default is `None`. Specific ends for folders and files can be
specified (see `**kwargs`).
depthlimit : int or None, optional
Limit the depth of folders to traverse. Folders at the `depthlimit` are
included, but their contents are not shown (with the exception of the
beyond parameter being specified). The default is `None`, which can
cause exceptionally long runtimes for deep or extensive directories.
itemlimit : int or None, optional
Limit the number of items in a directory to show. Items beyond the
`itemlimit` can be expressed using the `beyond` parameter. The files and
folders left out are determined by the sorting parameters
(`sort`, `sort_reverse`, `sort_key`). The default is `None`.
beyond : str ('ellipsis', 'content' or a string starting with an underscore) or None, optional
String to indicate directory contents beyond the `itemlimit` or the
`depthlimit`. The default is `None`. Options are: `'ellipsis'` (`'...'`),
`'content'` or `'contents'` (the number of files and folders beyond), or
a string starting with `'_'` (everything after the leading underscore
will be returned)
first : 'files', 'folders', or None, optional
Sort the directory so that either files or folders appear first.
The default is `None`.
sort : bool, optional
Sort the directory. With no other specifications, the sort will be a
simple alphabetical sort of the item names, but this can be altered
with the `first`, `sort_reverse`, and `sort_key parameters`.
The default is `False`.
sort_reverse : bool, optional
Reverse the sorting determined by `sort` or `sort_key`.
The default is `False`.
sort_key : function, optional
Key to use for sorting file or folder names, akin to the `key` parameter
of the builtin `sorted()` or `list.sort()`. The function should take a
string as an argument. The default is `None`.
include_folders, exclude_folders, include_files, exclude_files : str, list-like, or None, optional
Folder / file names to include or exclude. The default is `None`. By
default, these are interpreted literally. Pass `regex=True` for
using regular expressions.
regex : bool, optional
Interpret the strings of include/exclude file/folder arguments as
regular expressions. The default is `False`.
mask : function, optional
Function for filtering items. Each individual item object
is passed to the mask function. If `True` is returned, the
item is kept. The default is `None`.
formatter : function, optional
Function for customizing the directory printing logic and style
based on specific folders & files. When passed, the formatter
is called on each item in the file tree, and the current arguments
are updated based what is returned.
The formatter function should accept a FakeItem as a
single argument (either relative or absolute, depending on what is passed
to the `path` argument), and it should return either a dictionary or None.
The dictionary should have names of arguments as keys and their respective
setting as values.
The following options can meaningfully be toggled by passing a formatter
function: `depthlimit`, `itemlimit`, `beyond`, `first`, `sort`, `sort_reverse`,
`sort_key`, `include_folders`, `regex`, `mask`, as well as any seedir token
keywords (`extend`, `space`, `split`, `final`, `folderstart`, `filestart`,
`folderend`, `fileend`).
Note that in version 0.3.0, formatter could only be used to update
the style tokens. It can now be used to udpate those as well as the other
arguments listed above.
If None is returned by formatter, the tokens will be set by `style`.
Note that items exlcuded by the inclusion/exclusion arguments (or the
`mask`) *will not* be seen by formatter. Similarly, any folder tree
entries created by the `beyond` argument *will not* be seen by formatter.
sticky_formatter : bool, optional
When True, updates to argumnts made by the `formatter` (see above)
will be permanent. Thus, if arguments are updated when the `formatter`
is called on a folder, its children will (recursively) inherit
those new arguments.
acceptable_listdir_errors : Exception, tuple, or None
**New in v0.5.0**
Set errors which, when raised when listing the contents of a folder,
do not halt traversal. This parameter was added to allow
folders to be skipped when a `PermissionError` is raised. For folders
which raise an acceptable error, an additional string is added to their
entry in the diagram (see `denied_string`).
Providing one Exception causes only that type of Exception to be ignored.
Multiple Exception types can be handled by passing mutple Exceptions
as a tuple. Pass `None` to not allow any Exceptions (in this case,
an error is raised).
Exception types which are not provided will still be raised.
This parameter is mostly meaningless for FakeDir objects, except
for testing purposes.
denied_string : str
**New in v0.5.0**
String tag to signify that a folder was not able to be traversed
due to one of the `acceptable_listdir_errors` being raised. This
is a string added after the folder name (and `folderend`) strings.
The default is `" [ACCESS DENIED]"`.
**kwargs : str
Specific tokens to use for creating the file tree diagram. The tokens
use by each builtin style can be seen with `seedir.printing.get_styleargs()`.
Valid options are `extend` (characters to show the extension of a directory
while its children are traversed), `space` (character to provide the
correct indentation of an item when some of its parent / grandparent
directories are completely traversed), `split` (characters to show a
folder or file within a directory, with more items following),
`final` (characters to show a folder or file within a directory,
with no more items following), `folderstart` (characters to prepend
before any folder), `filestart` (characters to preppend before any
file), `folderend` (characters to append after any folder), and
`fileend` (characters to append after any file). The following shows
the default tokens for the `'lines'` style:
>>> import seedir as sd
>>> sd.get_styleargs('lines')
{'split': '├─', 'extend': '│ ', 'space': ' ', 'final': '└─', 'folderstart': '', 'filestart': '', 'folderend': '/', 'fileend': ''}
All default style tokens are 2 character strings, except for
the file/folder start/end tokens. Style tokens from `**kwargs` are not
affected by the indent parameter. The `uniform`, `anystart`, and
`anyend` parameters can be used to affect multiple style tokens.
Notes
-------
The parameter `slash` was deprecated in 0.5.0. Pass `folderend` as
an additional keyword argument instead.
Returns
-------
s (str) or None
The tree diagram (as a string) or None if prinout = True, in which
case the tree diagram is printed in the console.
'''
# call
args = dict(style=style,
printout=printout,
indent=indent,
uniform=uniform,
anystart=anystart,
anyend=anyend,
depthlimit=depthlimit,
itemlimit=itemlimit,
beyond=beyond,
first=first,
sort=sort,
sort_reverse=sort_reverse,
sort_key=sort_key,
include_folders=include_folders,
exclude_folders=exclude_folders,
include_files=include_files,
exclude_files=exclude_files,
regex=regex,
mask=mask,
formatter=formatter,
sticky_formatter=sticky_formatter,
acceptable_listdir_errors=acceptable_listdir_errors,
denied_string=denied_string,
**kwargs)
FDS = FakeDirStructure()
return FDS(self, **args)
def set_child_depths(self):
'''Recursively set depths of `self` and its children.
Called automatically when a new `parent` is assigned.'''
def apply_setdepth(FD):
FD.set_depth()
self.walk_apply(apply_setdepth)
def trim(self, depthlimit):
"""
Remove items beyond the `depthlimit`.
```
>>> import seedir as sd
>>> r = sd.randomdir(seed=456)
>>> r
MyFakeDir/
├─Vogel.txt
├─monkish.txt
├─jowly.txt
├─scrooge/
│ ├─light.txt
│ ├─reliquary.txt
│ ├─sandal/
│ ├─paycheck/
│ │ ├─electrophoresis.txt
│ │ └─Pyongyang/
│ └─patrimonial/
├─Uganda/
└─pedantic/
└─cataclysmic.txt
>>> r.trim(1)
>>> r
MyFakeDir/
├─Vogel.txt
├─monkish.txt
├─jowly.txt
├─scrooge/
├─Uganda/
└─pedantic/
```
Parameters
----------
depthlimit : non-negative int
Files beyond this depth will be cut. The root has depth `0`.
Raises
------
ValueError
`depthlimit` is not a non-negative int
Returns
-------
None.
"""
depthlimit = int(depthlimit)
if depthlimit < 0:
raise ValueError('depthlimit must be non-negative int')
depthlimit += self.depth
def trim_apply(f, depthlimit):
if depthlimit is not None and f.depth == depthlimit:
if isinstance(f, FakeDir):
f.delete(f.listdir())
if depthlimit == self.depth:
self.delete(self.listdir())
else:
self.walk_apply(trim_apply, depthlimit=depthlimit)
def walk_apply(self, foo, *args, **kwargs):
"""
Recursively apply a function the children of self (and so on)
```
>>> import seedir as sd
>>> r = sd.randomdir(seed=5)
>>> r
MyFakeDir/
├─senor.txt
├─verb.txt
├─takeoff.txt
├─monastic/
│ ├─paddy.txt
│ ├─ewe.txt
│ ├─advantage.txt
│ ├─hooves/
│ └─registrar/
└─zag/
├─thematic.txt
├─inelastic.txt
├─fierce.txt
├─gout/
└─stein/
├─vector.txt
├─sora.txt
└─proviso.txt
>>> def replace_txt(f):
... f.name = f.name.replace('txt', 'pdf')
>>> r.walk_apply(replace_txt)
>>> r
MyFakeDir/
├─senor.pdf
├─verb.pdf
├─takeoff.pdf
├─monastic/
│ ├─paddy.pdf
│ ├─ewe.pdf
│ ├─advantage.pdf
│ ├─hooves/
│ └─registrar/
└─zag/
├─thematic.pdf
├─inelastic.pdf
├─fierce.pdf
├─gout/
└─stein/
├─vector.pdf
├─sora.pdf
└─proviso.pdf
```
Parameters
----------
foo : function
Function to apply.
*args :
Additional positional arguments for `foo`.
**kwargs :
Additional keyword arguments for `foo`.
Returns
-------
None.
"""
foo(self, *args, **kwargs)
for f in self._children:
if isinstance(f, FakeDir):
f.walk_apply(foo, *args, **kwargs)
else:
foo(f, *args, **kwargs)
def get_random_int(collection, seed=None):
'''
Helper function for selecting a random integer, used by seedir.populate().
Parameters
----------
collection : list-like
Collection of integers to select from.
seed : int or float, optional
Random seed. The default is None.
Raises
------
TypeError
Non-integer found.
Returns
-------
r : int
Randomly chosen int from collection.
'''
r = random.Random(seed).choice(collection)
if not isinstance(r, int):
raise TypeError('non int found')
return r
def populate(fakedir, depth=3, folders=2, files=2, stopchance=.5, seed=None,
extensions=['txt']):
'''
Function for populating `seedir.fakedir.FakeDir` objects with random files and folders.
Used by `seedir.fakedir.randomdir()`. Random dictionary names are chosen
for file and folder names.
Parameters
----------
fakedir : seedir.fakedir.FakeDir
Fake directory to populate.
depth : int, optional
Maximum depth to create folders and files. The default is `3`.
folders : int or collection of integers, optional
Parameter for setting the number of folders per directory.
The default is `2`. If `int`, represents the number of folders
per directory. If collection of integers, a random value will be
chosen from the collection each time a directory is popualted.
files : int or collection of integers, optional, optional
Same as the folders parameter, but for files.
stopchance : float between 0 and 1, optional
Chance that an added folder will not be populated. The default is `.5`.
seed : int or float, optional
Random seed. The default is `None`.
extensions : list-likie, optional
Collection of extensions to randomly select from for files. The
default is `['txt']`. Leading period can be included or omitted.
Raises
------
ValueError
Issue selecting int from folders or files.
Returns
-------
None, input is modified in place.
'''
random.seed(seed)
if not isinstance(folders, int):
try:
fold_num = get_random_int(folders, seed=seed)
except:
raise ValueError('folders must be an int or collection of int')
else:
fold_num = folders
if not isinstance(files, int):
try:
file_num = get_random_int(files, seed=seed)
except:
raise ValueError('files must be an int or collection of int')
else:
file_num = files
for i in range(file_num):
name = random.choice(words) + random.choice(extensions)
while name in [f.name for f in fakedir._children]:
name = random.choice(words) + random.choice(extensions)
fakedir.create_file(name)
for i in range(fold_num):
name = random.choice(words)
while name in [f.name for f in fakedir._children]:
name = random.choice(words)
fakedir.create_folder(name)
for f in fakedir._children:
if isinstance(f, FakeDir):
if f.depth <= depth and random.uniform(0, 1) > stopchance:
if seed is not None:
seed += random.random()
populate(f, depth=depth, folders=folders, files=files,
seed=seed, stopchance=stopchance,
extensions=extensions)
def randomdir(depth=2, files=range(1,4), folders=range(0,4),
stopchance=.5, seed=None, name='MyFakeDir', extensions=['txt']):
'''
Create a randomized `seedir.fakedir.FakeDir`, initialized with random
dictionary words.
```
>>> import seedir as sd
>>> sd.randomdir(seed=7)
MyFakeDir/
├─Hardin.txt
├─Kathleen.txt
├─berserk/
└─pineapple/
├─visceral.txt
├─gestural.txt
├─plenty.txt
├─discoid/
│ ├─eclat.txt
│ └─milord/
├─Offenbach/
│ ├─Trianon.txt
│ ├─Monday.txt
│ ├─ditty.txt
│ ├─peddle/
│ ├─delta/
│ └─irredentism/
└─Perseus/
├─hothouse.txt
├─clock.txt
├─covetous/
└─Ekstrom/
```
Parameters
----------
depth : int, optional
Maximum depth to create folders and files. The default is `3`.
folders : int or collection of integers, optional
Parameter for setting the number of folders per directory.
The default is `range(1,4)`. If `int`, represents the number of folders
per directory. If collection of integers, a random value will be
chosen from the collection each time a directory is popualted.
files : int or collection of integers, optional, optional
Same as the `folders` parameter, but for files. The default
is `range(0,4)`.
stopchance : float between 0 and 1, optional
Chance that an added folder will not be populated. The default is `.5`.
seed : int or float, optional
Random seed. The default is `None`.
extensions : list-likie, optional
Collection of extensions to randomly select from for files. The
default is `['txt']`. Leading period can be included or omitted.
Returns
-------
top : seedir.fakedir.FakeDir
Fake directory.
'''
top = FakeDir(name)
new_ex = []
for x in extensions:
if x[0] != '.':
new_ex.append('.' + x)
else:
new_ex.append(x)
populate(top, depth, folders, files, seed=seed, stopchance=stopchance,
extensions=new_ex)
return top
def recursive_add_fakes(path, parent, depth=0, depthlimit=None,
itemlimit=None, first=None, sort=False,
sort_reverse=False, sort_key=None,
include_folders=None, exclude_folders=None,
include_files=None, exclude_files=None,
mask=None, regex=False):
'''
Recursive helper function for seedir.fakedir(), for creating a
fake folder tree from a real one.
Parameters
----------
path : str
System path of a folder.
parent : seedir.fakedir.FakeDir
Fake directory to add items to.
depth : int, optional
Tracker for depth of folders traversed. The default is 0.
depthlimit : int, optional
Limit on the depth of folders to traverse. The default is None.
itemlimit : int, optional
Limit on the number of items to include per directory.
The default is None.
first : 'folders' or 'files', optional
Sort to have folders or files appear first. The default is None.
sort : bool, optional
Apply a sort to items in each directory. The default is False.
sort_reverse : bool, optional
Reverse the sort. The default is False.
sort_key : function, optional
Key function for sorting item names. The default is None.
include_folders, exclude_folders, include_files, exclude_files : str, list-like, or None, optional
Folder / file names to include or exclude. The default is None.
mask : function, optional
Function for filtering items. Absolute paths of each individual item
are passed to the mask function. If True is returned, the
item is kept. The default is None.
regex : bool, optional
Interpret include/exclude folder/file arguments as regular
expressions. The default is False.
Returns
-------
None.
'''
RDS = RealDirStructure()
if depthlimit is not None and depth >= depthlimit:
return
depth +=1
listdir = listdir_fullpath(path)
if sort or first is not None:
listdir = RDS.sort_dir(listdir, first=first,
sort_reverse=sort_reverse, sort_key=sort_key)
if any(arg is not None for arg in [
include_folders,
exclude_folders,
include_files,
exclude_files,
mask]):
listdir = RDS.filter_items(listdir,
include_folders=include_folders,
exclude_folders=exclude_folders,
include_files=include_files,
exclude_files=exclude_files,
regex=regex,
mask=mask)
for i, f in enumerate(listdir):
name = os.path.basename(f)
if i == itemlimit:
break
if os.path.isdir(f):
new = FakeDir(name=name, parent=parent)
recursive_add_fakes(path=f, parent=new, depth=depth,
depthlimit=depthlimit,
itemlimit=itemlimit,
include_folders=include_folders,
exclude_folders=exclude_folders,
include_files=include_files,
exclude_files=exclude_files,
mask=mask, regex=regex)
else:
new = FakeFile(name=name, parent=parent)
def fakedir(path, depthlimit=None, itemlimit=None, first=None,
sort=False, sort_reverse=False, sort_key=None,
include_folders=None, exclude_folders=None, include_files=None,
exclude_files=None, mask=None, regex=True):
'''
Function for creating a `seedir.fakedir.FakeDir` (representation of a directory)
from a real system directory. Rather than immediately representing
a directory as a string (`seedir.realdir.seedir()`), this function can be used
to create an editable representation of the directory, or to join one or
more directories.
```
>>> import seedir as sd
>>> f = sd.fakedir('.', depthlimit=0)
>>> type(f)
<class 'seedir.fakedir.FakeDir'>
```
Parameters
----------
path : str
System path of a directory.
depthlimit : int, optional
Limit on the depth of directories to traverse. Folders at the depth
limit will be included, but their contents will not be.
The default is `None`.
itemlimit : int, optional
Limit on the number of items to include per directory.
The default is `None`, meaning all items will be added. The priority
of items is determined by `os.listdir()`, unless sorting arguments
are passed.
first : 'folders' or 'files', optional
Sort to show folders or files first. The default is `None`.
sort : bool, optional
Apply a (name) sort on each directory. The default is `False`.
sort_reverse : bool, optional
Reverse the sort. The default is `False`.
sort_key : function, optional
Key function for sorting the file names. The default is `None`.
include_folders, exclude_folders, include_files, exclude_files : str, list-like, or None, optional
Folder / file names to include or exclude. The default is `None`.
mask : function, optional
Function for filtering items. Absolute paths of each individual item
are passed to the mask function. If `True` is returned, the
item is kept. The default is `None`.
regex : bool, optional
Interpret include/exclude folder/file arguments as regular
expressions. The default is `False`.
Raises
------
FakedirError
path does not point to a directory.
Returns
-------
output : seedir.fakedir.FakeDir
Fake directory matching the path.
'''
if not os.path.isdir(path):
raise FakedirError('path must be a directory')
output = FakeDir(os.path.basename(path))
recursive_add_fakes(path, parent=output, depthlimit=depthlimit,
itemlimit=itemlimit,
first=first,
sort=sort,
sort_reverse=sort_reverse,
sort_key=sort_key,
include_folders=include_folders,
exclude_folders=exclude_folders,
include_files=include_files,
exclude_files=exclude_files,
mask=mask, regex=regex)
return output
def fakedir_fromstring(s, start_chars=None, name_chars=None,
header_regex=None, name_regex=None,
supername='FakeDir', parse_comments=True):
'''
Convert a string folder tree diagram into a `seedir.fakedir.FakeDir`.
This can be used to read in external representations of folder structures,
edit them, and recreate them in a new location.
This function has mostly been tested with examples from Stack Overflow
and other Python learning sites (as well as output from `seedir.realdir.seedir()`).
There are surely cases which will causes errors or unexpected results.
```
>>> import seedir as sd
>>> s = """doc/
... ├─_static/
... │ ├─embedded/
... │ │ ├─deep_file
... │ │ └─very/
... │ │ └─deep/
... │ │ └─folder/
... │ │ └─very_deep_file
... │ └─less_deep_file
... ├─about.rst
... ├─conf.py
... └─index.rst"""
>>> f = sd.fakedir_fromstring(s) # now can be edited or restyled
>>> f.seedir(style='spaces')
doc/
_static/
embedded/
deep_file
very/
deep/
folder/
very_deep_file
less_deep_file
about.rst
conf.py
index.rst
```
Parameters
----------
s : str
String representation a folder tree.
start_chars : str, optional
A string of characters which will be searched for as the start
of a folder or file name. The default is `None`, in which case the
characters are all letters, numbers, and punctuation marks except
for `/:?"*<>|` or `+=-`.
name_chars : str, optional
A string of characters which will be searched for as being part
of a file or folder name. The default is `None`, in which case the
characters are all letters, numbers, punctuation marks except
for `/:?"*<>|,` and spaces.
header_regex : str, optional
Regular expression to match the "header" of each line, i.e. the
structural characters of the folder diagram. The default is `None`.
If passed, the `start_chars` argument will be ignored. This and
`name_regex` are intended to provide functionality for parsing
specific or unusual cases.
name_regex : str, optional
Regular expression to match all the folder or file names.
The default is `None`.
If passed, the `start_chars` argument will be ignored.
supername : str, optional
Name to give the head directory if one cannot be found.
The default is `'FakeDir'`. This can happen when there is no clear
head directory in the string.
parse_comments : bool, optional
Try to parse and remove Python comments (following `#`).
The default is `True`.
Returns
-------
seedir.fakedir.FakeDir
Fake directory corresponding to the input string.
'''
slashes = ['/', '\\', os.sep]
joinedslashes = ''.join(slashes)
byline = s.split('\n')
keyboard_chars = (string.ascii_letters + string.digits
+ string.punctuation)
filtered = "".join([c for c in keyboard_chars if c not in '/:?"*<>|'])
if start_chars is None:
start_chars = "".join([c for c in filtered if c not in '+=-'])
if name_chars is None:
name_chars = filtered + ' ' + '-'
names = []
headers = []
depths = []
for line in byline:
if not line:
continue
if header_regex is None:
header = re.match('.*?(?=[{}])'.format(start_chars), line)
else:
header = re.match(header_regex, line)
if header is None:
continue
else:
header = header.group()
depth = len(header)
if name_regex is None:
name = re.match('[{}]*[/\\\\]*'.format(name_chars), line[depth:])
else:
name = re.match(name_regex, line)
if name is None:
continue
else:
name = name.group()
if '#' in name and parse_comments:
name = re.match('.*?(?=#)', name).group().strip()
if not name:
continue
headers.append(header)
names.append(name)
depths.append(depth)
fakeitems = []
superparent = None
min_depth = min(depths)
if len([d for d in depths if d == min_depth]) > 1:
superparent = FakeDir(supername)
min_depth_index1 = depths.index(min_depth)
if any(i > min_depth for i in depths[:min_depth_index1]):
superparent = FakeDir(supername)
for i, name in enumerate(names):
is_folder = False
if name.strip()[-1] in slashes:
is_folder = True
if i < len(names) - 1:
if depths[i + 1] > depths[i]:
is_folder = True
fmt_name = name.rstrip(joinedslashes)
if depths[i] == min_depth:
if is_folder:
fakeitems.append(FakeDir(fmt_name, parent=superparent))
else:
fakeitems.append(FakeFile(fmt_name, parent=superparent))
else:
shallower = [d for d in depths[:i] if d < depths[i]]
if shallower:
max_shallower = max([d for d in depths[:i] if d < depths[i]])
parent_index = max(idx for idx, val in enumerate(depths[:i])
if val == max_shallower)
parent = fakeitems[parent_index]
else:
parent = superparent
if is_folder:
fakeitems.append(FakeDir(fmt_name, parent=parent))
else:
fakeitems.append(FakeFile(fmt_name, parent=parent))
if superparent is not None:
return superparent
else:
idx = depths.index(min_depth)
return fakeitems[idx]
|