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
|
# Getting started
The following examples will cover what you can do with seedir. We can start by importing the package with the `sd` alias:
```python
>>> import seedir as sd
```
## Displaying folder trees
The primary function of seedir is to create plain text diagrams of folders, for use in blogs, examples, Q&As, etc. The GitHub repo for seedir includes an example folder (`seedir/seedir/exampledir`) which will be used here.
To printout out the structure of this folder, you can use the primary `seedir.realdir.seedir()` function:
```python
>>> path = 'exampledir'
>>> sd.seedir(path)
exampledir/
├─jowly.pdf
├─monkish.txt
├─pedantic/
│ └─cataclysmic.txt
├─scrooge/
│ ├─light.pdf
│ ├─paycheck/
│ │ └─electrophoresis.txt
│ └─reliquary.pdf
└─Vogel.txt
```
By default, **the output is printed**. To return a string instead, use the `printout` argument:
```python
>>> s = sd.seedir(path, printout=False)
>>> print(type(s))
<class 'str'>
```
## Trimming folder trees
Sometimes a directory is too large, is private, or contains certain irrelevant files. To handle this, there are a few arguments which allow you to edit the items included in the output.
### Including & excluding folders & files
One way is to call out specific folders of files to include or exclude:
```python
>>> sd.seedir(path, include_folders=['scrooge','paycheck'], exclude_files='reliquary.pdf')
exampledir/
├─jowly.pdf
├─monkish.txt
├─scrooge/
│ ├─light.pdf
│ └─paycheck/
│ └─electrophoresis.txt
└─Vogel.txt
```
By passing `regex=True`, these "include" and "exclude" arguments also support regular expressions, :
```python
>>> sd.seedir(path, include_files='.*\.pdf$', regex=True) # all PDFs
exampledir/
├─jowly.pdf
├─pedantic/
└─scrooge/
├─light.pdf
├─paycheck/
└─reliquary.pdf
```
### Masking
You can also use the `mask` parameter to functionally filter out items in the folder. Pathnames are passed to the mask function, and items are kept if `True` is returned:
```python
>>> import os
>>> def foo(x): # omits folders with more than 2 items
... if os.path.isdir(x) and len(os.listdir(x)) > 2:
... return False
... return True
>>> sd.seedir(path, mask=foo)
exampledir/
├─jowly.pdf
├─monkish.txt
├─pedantic/
│ └─cataclysmic.txt
└─Vogel.txt
```
### Limiting the depth or number of items
You can also wholly limit the output by providing the `depthlimit` or `itemlimit` arguments. Respectively, these arguments limit the depth of folders to enter and the number of items to include per folder.
```python
>>> sd.seedir(path, depthlimit=1)
exampledir/
├─jowly.pdf
├─monkish.txt
├─pedantic/
├─scrooge/
└─Vogel.txt
>>> sd.seedir(path, itemlimit=3)
exampledir/
├─jowly.pdf
├─monkish.txt
└─pedantic/
└─cataclysmic.txt
```
**New in v0.5.0!** You can also pass a 2-tuple to `itemlimit` to have separate limits for folders and files, respectively:
```python
>>> sd.seedir(path, itemlimit=(None, 1))
exampledir/
├─scrooge/
│ ├─light.pdf
│ └─paycheck/
│ └─electrophoresis.txt
├─jowly.pdf
└─pedantic/
└─cataclysmic.txt
```
As `seedir.realdir.seedir()` uses recursion, these arguments can hedge the traversal of deep, complicated folders.
When limiting the tree, using the `beyond` argument can be helpful to show what is being cut. The special value `'content'` shows the number of folders and files:
```python
>>> sd.seedir(path, depthlimit=1, beyond='content')
exampledir/
├─jowly.pdf
├─monkish.txt
├─pedantic/
│ └─0 folder(s), 1 file(s)
├─scrooge/
│ └─1 folder(s), 2 file(s)
└─Vogel.txt
```
### Sorting
Especially when using the `itemlimit`, but also generally, you may want to sort the output to determine which items appear first. You can apply a general sort using `sort=True`:
```python
>>> sd.seedir(path, itemlimit=4, sort=True)
exampledir/
├─Vogel.txt
├─jowly.pdf
├─monkish.txt
└─pedantic/
└─cataclysmic.txt
```
There are additional reverse and key arguments (akin to `sorted()` or `list.sort()`) which allow you to customize the sorting:
```python
>>> sd.seedir(path, sort=True, sort_reverse=True, sort_key=lambda s : len(s))
exampledir/
├─monkish.txt
├─jowly.pdf
├─Vogel.txt
├─pedantic/
│ └─cataclysmic.txt
└─scrooge/
├─reliquary.pdf
├─light.pdf
└─paycheck/
└─electrophoresis.txt
```
The `first` argument allows you to select whether files or folders appear first:
```python
>>> sd.seedir(path, first='files')
exampledir/
├─Vogel.txt
├─jowly.pdf
├─monkish.txt
├─pedantic/
│ └─cataclysmic.txt
└─scrooge/
├─light.pdf
├─reliquary.pdf
└─paycheck/
└─electrophoresis.txt
```
## Styles 💅
`seedir.realdir.seedir()` has a few builtin styles for formatting the output of the folder tree:
```python
>>> sd.seedir(path, style='emoji')
📁 exampledir/
├─📄 jowly.pdf
├─📄 monkish.txt
├─📁 pedantic/
│ └─📄 cataclysmic.txt
├─📁 scrooge/
│ ├─📄 light.pdf
│ ├─📁 paycheck/
│ │ └─📄 electrophoresis.txt
│ └─📄 reliquary.pdf
└─📄 Vogel.txt
```
To make use of the 'emoji' style, install [emoji](https://pypi.org/project/emoji/) (`pip install emoji`) or use `pip install seedir[emoji]`.
For any builtin style, you can customize the indent size:
```python
>>> sd.seedir(path, style='spaces', indent=4)
exampledir/
jowly.pdf
monkish.txt
pedantic/
cataclysmic.txt
scrooge/
light.pdf
paycheck/
electrophoresis.txt
reliquary.pdf
Vogel.txt
>>> sd.seedir(path, style='plus', indent=6)
exampledir/
+-----jowly.pdf
+-----monkish.txt
+-----pedantic/
| +-----cataclysmic.txt
+-----scrooge/
| +-----light.pdf
| +-----paycheck/
| | +-----electrophoresis.txt
| +-----reliquary.pdf
+-----Vogel.txt
```
Each style is basically a collection of string "tokens" which are combined to form the header of each printed line (based on the depth and folder structure). You can see these tokens using `seedir.printing.get_styleargs()`:
```python
>>> sd.get_styleargs('emoji')
{'split': '├─', 'extend': '│ ', 'space': ' ', 'final': '└─', 'folderstart': '📁 ', 'filestart': '📄 ', 'folderend': '/', 'fileend': ''}
```
You can pass any of these tokens as `**kwargs` to explicitly customize styles with new symbols (note that passed tokens will not be affected by the `indent` parameter; it assumes you know how long you want them to be):
```python
>>> sd.seedir(path, space=' ', extend='||', split='-}', final='\\\\', folderstart=' ', filestart=' ', folderend='%')
exampledir%
-} jowly.pdf
-} monkish.txt
-} pedantic%
||\\ cataclysmic.txt
-} scrooge%
||-} light.pdf
||-} paycheck%
||||\\ electrophoresis.txt
||\\ reliquary.pdf
\\ Vogel.txt
```
There are also `uniform` and `anystart` arguments for customizing multiple tokens at once:
```python
>>> sd.seedir(path, uniform='----', anystart='>')
>exampledir/
---->jowly.pdf
---->monkish.txt
---->pedantic/
-------->cataclysmic.txt
---->scrooge/
-------->light.pdf
-------->paycheck/
------------>electrophoresis.txt
-------->reliquary.pdf
---->Vogel.txt
```
## Programmatic formatting
[Following a user-raised issue](https://github.com/earnestt1234/seedir/issues/4), seedir has added a `formatter` parameter for enabling some programmatic editing of the folder tree. This can be useful when you want to alter the style of the diagram based on things like the depth, item name, file extension, etc. The path of each item (relative to the root) is passed to `formatter`, which returns new settings to use for that item.
The following example edits the style tokens for items with particular names or file extensions:
```python
>>> import os
>>> def my_style(item):
...
... outdict = {}
...
... # get the extension
... ext = os.path.splitext(item)[1]
...
... if ext == '.txt':
... outdict['filestart'] = '✏️'
...
... if os.path.basename(item) == 'scrooge':
... outdict['folderstart'] = '👉'
...
... return outdict
>>> sd.seedir(path, formatter=my_style)
exampledir/
├─jowly.pdf
├─✏️monkish.txt
├─pedantic/
│ └─✏️cataclysmic.txt
├─👉scrooge/
│ ├─light.pdf
│ ├─paycheck/
│ │ └─✏️electrophoresis.txt
│ └─reliquary.pdf
└─✏️Vogel.txt
```
The `formatter` parameter can also dynamically set other options, such as the filtering arguments outlined above. The following example sets a mask that removes files, but only in the root directory:
```python
>>> import os
>>> def no_root_files(item):
... if os.path.basename(item) == 'exampledir':
... mask = lambda x: os.path.isdir(x)
... return {'mask': mask}
>>> sd.seedir(path, formatter=no_root_files)
exampledir/
├─pedantic/
│ └─cataclysmic.txt
└─scrooge/
├─light.pdf
├─paycheck/
│ └─electrophoresis.txt
└─reliquary.pdf
```
Note that by default, any changes set by the `formatter` are unset for sub-directories. You can turn on the `sticky_formatter` option to make changes persist:
```
>>> def mark(item):
... d = {}
... parent = os.path.basename(os.path.dirname(item))
... if parent == 'pedantic':
... d['folderstart'] = '✔️ '
... d['filestart'] = '✔️ '
... if parent == 'scrooge':
... d['folderstart'] = '❌ '
... d['filestart'] = '❌ '
...
... return d
# The function only makes changes to items in the 'pedantic' and 'scrooge'
# folders explicilty.
>>> sd.seedir(path, formatter=mark)
exampledir/
├─jowly.pdf
├─monkish.txt
├─pedantic/
│ └─✔️ cataclysmic.txt
├─scrooge/
│ ├─❌ light.pdf
│ ├─❌ paycheck/
│ │ └─electrophoresis.txt
│ └─❌ reliquary.pdf
└─Vogel.txt
# By passing `sticky_formatter`, changes are also applied to subdirectories
>>> sd.seedir(path, formatter=mark, sticky_formatter=True)
exampledir/
├─jowly.pdf
├─monkish.txt
├─pedantic/
│ └─✔️ cataclysmic.txt
├─scrooge/
│ ├─❌ light.pdf
│ ├─❌ paycheck/
│ │ └─❌ electrophoresis.txt
│ └─❌ reliquary.pdf
└─Vogel.txt
```
Some properties (e.g., the depth of the folder relative to the parent) are a little more difficult to determine with a system paths. However, switching to object-oriented FakeDirs (introduced in the following section) make accessing interfolder relations a little easier:
```python
>>> # now items passed are FakeDir / FakeFile, not system path
>>> def formatter(item):
... if item.depth > 1: # we can access the depth attribute
... return sd.get_styleargs('plus')
>>> f = sd.fakedir(path)
>>> f.seedir(formatter=formatter)
exampledir/
├─jowly.pdf
├─monkish.txt
├─pedantic/
| +-cataclysmic.txt
├─scrooge/
| +-light.pdf
| +-paycheck/
| | +-electrophoresis.txt
| +-reliquary.pdf
└─Vogel.txt
```
## Pathlib
As of version 0.4.0, `sd.realdir.seedir()` also accepts [pathlib](https://docs.python.org/3/library/pathlib.html) objects:
```python
>>> import pathlib
>>> p = pathlib.Path(path)
>>> p
PosixPath('exampledir')
>>> sd.seedir(p)
exampledir/
├─jowly.pdf
├─monkish.txt
├─pedantic/
│ └─cataclysmic.txt
├─scrooge/
│ ├─light.pdf
│ ├─paycheck/
│ │ └─electrophoresis.txt
│ └─reliquary.pdf
└─Vogel.txt
```
Most of the features demonstrated above apply as usual. The only major difference is that arguments accepting callables (`mask` & `fromatter`) will now be passed `pathlib.Path` objects, instead of strings:
```python
>>> mask = lambda x: x.is_dir() or x.suffix == '.txt'
>>> sd.seedir(p, mask=mask)
exampledir/
├─scrooge/
│ └─paycheck/
│ └─electrophoresis.txt
├─monkish.txt
├─pedantic/
│ └─cataclysmic.txt
└─Vogel.txt
```
## Fake directories
You can also create or edit directory examples by using "fake directories" in `seedir`. These are Python representations of folders and files, which can be manipulated in ways similar to real directories.
### Making from scratch
`seedir.fakedir.FakeDir` is a folder class; you can be used to initialize an example folder tree. `FakeDir` objects have a `seedir.fakedir.FakeDir.seedir()` method, which is fundamentally similar to `seedir.realdir.seedir()` for real system paths:
```python
>>> x = sd.FakeDir('myfakedir')
>>> x.seedir()
myfakedir/
```
To add items to `x`, you can create subitems, initialize new directories, or move existing ones:
```python
>>> x.create_file(['__init__.py', 'main.py', 'styles.txt'])
[FakeFile(myfakedir/__init__.py), FakeFile(myfakedir/main.py), FakeFile(myfakedir/styles.txt)]
>>> y = sd.FakeDir('resources', parent=x)
>>> 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/
├─resources/
│ └─images/
│ ├─a.png
│ ├─b.png
│ └─c.png
├─__init__.py
├─main.py
└─styles.txt
```
You can use path-like strings to index fake directories:
```python
>>> x['resources/images/a.png']
FakeFile(myfakedir/resources/images/a.png)
```
You can also use the `listdir` or `get_child_names` methods to get the children of a folder (or their names):
```python
>>> x['resources/images'].listdir()
[FakeFile(myfakedir/resources/images/a.png), FakeFile(myfakedir/resources/images/b.png), FakeFile(myfakedir/resources/images/c.png)]
>>> x['resources/images'].get_child_names()
['a.png', 'b.png', 'c.png']
```
You can use the `delete` method to remove items by name or by object:
```python
>>> x['resources/images'].delete([x['resources/images/a.png'], 'b.png'])
>>> x.seedir()
myfakedir/
├─__init__.py
├─main.py
├─styles.txt
└─resources/
└─images/
└─c.png
```
You can also move items within the tree:
```python
>>> x['styles.txt'].parent = x['resources']
>>> x.seedir()
myfakedir/
├─__init__.py
├─main.py
└─resources/
├─images/
│ └─c.png
└─styles.txt
```
### Turn existing directories into fakes
The `seedir.fakedir.fakedir()` function allows you to convert real directories into `seedir.fakedir.FakeDir` objects:
```python
>>> f = sd.fakedir(path) # using path from above
>>> f.seedir()
exampledir/
├─jowly.pdf
├─monkish.txt
├─pedantic/
│ └─cataclysmic.txt
├─scrooge/
│ ├─light.pdf
│ ├─paycheck/
│ │ └─electrophoresis.txt
│ └─reliquary.pdf
└─Vogel.txt
>>> type(f)
<class 'seedir.fakedir.FakeDir'>
```
Similar to `seedir.realdir.seedir()`, `seedir.fakedir.fakedir()` has options to limit the incoming folders and files:
```python
>>> f = sd.fakedir(path, exclude_folders='scrooge', exclude_files='Vogel.txt')
>>> f.seedir()
exampledir/
├─jowly.pdf
├─monkish.txt
└─pedantic/
└─cataclysmic.txt
```
Fake directories created from your system directories can be combined with other ones created from scratch:
```python
>>> f.parent = x
>>> x.seedir()
myfakedir/
├─__init__.py
├─main.py
├─resources/
│ ├─images/
│ │ └─c.png
│ └─styles.txt
└─exampledir/
├─jowly.pdf
├─monkish.txt
└─pedantic/
└─cataclysmic.txt
```
### Creating folder trees from text
The `seedir.fakedir.fakedir_fromstring()` method allows you to read in existing folder tree diagrams. Given this:
```python
somedir/
- folder1
- folder2
- folder3
- file1
- file2
- subfolder
- subfolder
- deepfile
- folder4
```
You can do:
```python
>>> s = '''somedir/
... - folder1
... - folder2
... - folder3
... - file1
... - file2
... - subfolder
... - subfolder
... - deepfile
... - folder4'''
>>> g = sd.fakedir_fromstring(s)
>>> g.seedir()
somedir/
├─folder1
├─folder2
├─folder3/
│ ├─file1
│ ├─file2
│ └─subfolder/
│ └─subfolder/
│ └─deepfile
└─folder4
```
This can be used to read in examples found online. [For instance](https://chrisyeh96.github.io/2017/08/08/definitive-guide-python-imports.html):
```python
>>> s = '''test/ # root folder
... packA/ # package packA
... subA/ # subpackage subA
... __init__.py
... sa1.py
... sa2.py
... __init__.py
... a1.py
... a2.py
... packB/ # package packB (implicit namespace package)
... b1.py
... b2.py
... math.py
... random.py
... other.py
... start.py'''
>>> h = sd.fakedir_fromstring(s, parse_comments=True) # handling the added comments
>>> h.seedir(style='emoji')
📁 test/
├─📁 packA/
│ ├─📁 subA/
│ │ ├─📄 __init__.py
│ │ ├─📄 sa1.py
│ │ └─📄 sa2.py
│ ├─📄 __init__.py
│ ├─📄 a1.py
│ └─📄 a2.py
├─📁 packB/
│ ├─📄 b1.py
│ └─📄 b2.py
├─📄 math.py
├─📄 random.py
├─📄 other.py
└─📄 start.py
```
This function has not been extensively tested, so expect failures/misreadings!
### Creating random directories
You can also use seedir to create randomly generated directories.
```python
>>> r = sd.randomdir(seed=4.21, extensions=['png', 'jpg', 'pdf'], name='sorandom')
>>> r.seedir()
sorandom/
├─plethora.pdf
├─randy.jpg
├─giddap.pdf
├─adage/
└─Gobi/
├─inventive.pdf
├─Noel.jpg
├─archaic/
│ └─bream.pdf
├─salve/
└─NY/
└─Bahrein.jpg
```
Additional parameters dictate the size of the tree created:
```python
>>> sd.randomdir(files=range(5), # making an unnecessarily large tree
... folders=[1,2],
... stopchance=.3,
... depth=5,
... extensions=['png', 'jpg', 'pdf'],
... seed=4.21).seedir()
MyFakeDir/
├─plethora.pdf
├─randy.jpg
├─giddap.pdf
├─adage.jpg
├─oint/
│ ├─inventive.pdf
│ ├─Noel.jpg
│ ├─archaic.pdf
│ ├─NY/
│ │ ├─neonatal.png
│ │ ├─pyknotic.png
│ │ ├─wife.png
│ │ ├─Mickelson/
│ │ │ └─Caruso/
│ │ │ ├─royal.pdf
│ │ │ ├─aurora.pdf
│ │ │ ├─stampede.pdf
│ │ │ ├─divan.pdf
│ │ │ └─Coffey/
│ │ │ ├─sad.pdf
│ │ │ ├─cob.pdf
│ │ │ ├─natural.jpg
│ │ │ ├─skinflint.pdf
│ │ │ └─exploration/
│ │ └─Westchester/
│ │ ├─metamorphism.jpg
│ │ ├─Pulitzer.jpg
│ │ ├─permissive.png
│ │ ├─wakerobin/
│ │ │ ├─talon.jpg
│ │ │ ├─peace.pdf
│ │ │ ├─drowse.jpg
│ │ │ ├─rutabaga/
│ │ │ └─silage/
│ │ │ ├─pert.png
│ │ │ ├─defensible.png
│ │ │ ├─lexicon.png
│ │ │ ├─eureka.pdf
│ │ │ └─impasse/
│ │ └─calve/
│ │ ├─superstitious.png
│ │ └─embroider/
│ │ ├─safekeeping.pdf
│ │ ├─brig.png
│ │ ├─systemic.pdf
│ │ ├─caddis.png
│ │ └─borosilicate/
│ └─Pentecost/
│ ├─visceral.pdf
│ ├─replica.png
│ ├─rockaway.jpg
│ ├─luxe.jpg
│ └─breath/
│ ├─contain.jpg
│ └─homophobia/
│ ├─crewman.jpg
│ └─restroom/
└─joss/
├─Technion.jpg
├─Mynheer.png
├─begonia/
└─Djakarta/
├─Lionel.png
├─Cantonese.pdf
├─drafty.jpg
├─half/
│ └─builtin/
│ ├─ermine.jpg
│ └─Cretan/
│ ├─happenstance.pdf
│ ├─insoluble.jpg
│ ├─Audrey/
│ └─octennial/
└─recipient/
├─splintery.jpg
├─bottom.png
├─stunk.png
├─Polyhymnia/
└─Holocene/
├─plaque.pdf
├─westerly.png
├─glassine.jpg
├─titanium.pdf
└─square/
```
### Turning fake directories into real ones
Finally, there is a `seedir.fakedir.FakeDir.realize()` method which can covert fake directories into real folders on your computer.
```
x = sd.randomdir()
x.realize(path='where/to/create/it/')
```
All files created will be empty.
## Extending seedir
It is also possible to use seedir to generate folder tree diagrams for other types of Python objects that have directory-like structures. This can be done by **subclassing the `seedir.folderstructure.FolderStructure` class**, which implements the main algorithm:
```python
>>> from seedir.folderstructure import FolderStructure
```
FolderStructure is an abstract class, which requires three arguments to be implemented:
1. `seedir.folderstructure.FolderStructure.getname()`: returns a string name for the object
2. ``seedir.folderstructure.FolderStructure.isdir()`: returns a boolean indicating whether the object passed is a folder or not.
3. `seedir.folderstructure.FolderStructure.listdir()`: when called on the folder-like object, returns a list of child objects
Once these methods are implemented, your custom FolderStructure can be called on inputs to create folder structures. The following example demonstrates creating a "folder" structure for factorizing numbers:
```python
>>> def factors(n):
... return [i for i in range(2, n) if n % i == 0]
# note that the isdir_func always returns true, treating all numbers as "folders"
>>> class FactorStructure(FolderStructure):
...
... def getname(self, item):
... return str(item)
...
... def isdir(self, item):
... return True
...
... def listdir(self, item):
... return factors(item)
>>> factorizer = FactorStructure()
>>> factorizer(36, folderend='')
36
├─2
├─3
├─4
│ └─2
├─6
│ ├─2
│ └─3
├─9
│ └─3
├─12
│ ├─2
│ ├─3
│ ├─4
│ │ └─2
│ └─6
│ ├─2
│ └─3
└─18
├─2
├─3
├─6
│ ├─2
│ └─3
└─9
└─3
```
|