Package: txdir
NAMEtxdir— text tree from or to file treeSYNOPSIStxdir[<infile>|<indir>|-] [<outdir>|-] [<options>]Options:-h: help
-v: version
-l: flat listing
-f: exclude files
-d: include dot files/directories
-n: exclude file content (don't reapply such a tree as it will empty all files)
-m: maximum depth
-c: commands to create directories (from https://github.com/gcmt/mktree)Files/dirs are ignored via .gitignore.Command line help:usage: txdir  [infile] [outdir] [-h] [-v] [-a] [-b] [-l] [-f] [-d] [-n] [-m M] [-c [C [C ...]]]

Files/dirs are ignored via .gitignore. If the directory contains unignored binary files, exclude
files with '-f' or ignoring content with '-n'. Text file content must not have an empty first
line.

positional arguments:
  infile          If a file, it is expected to contain a text representation of a directory, flat
                  or indented (none or - is stdin). If a directory, the text view is created with
                  file content (unless -n).
  outdir          None or - means printing to stdout. If the parameter is an existing file,
                  nothing is done. If not a directory, the directory is created. The file tree is
                  created in the directory.

optional arguments:
  -h              Print help information.
  -v              Print version information.
  -a              Use ASCII instead of unicode when printint the indented view.
  -b              Include content of binary files as base64 encoded.
  -l              Create a flat listing instead of an indented view.
  -f              Omit files. Only list directories.
  -d              Include dot files/directories.
  -n              Omit file content.
  -m M            Maximum directory depth to scan.
  -c [C [C ...]]  Directories described with a DSL (',' = end of token, '.' = up dir, '/' = down)
                  `txdir - . -c 'a/b/d.c/d..a/u,v,x,g\.x'` produces the same as `mkdir -p
                  a/{b,c}/d a/u a/v a/x a/g.x` If not within ', use \\ to escape.DESCRIPTIONConstruct afile treefrom a text tree.Construct atext treefrom a file tree.This allows to edit a whole file tree within one file first,
without the necessity to switch files.The text tree can also be templated
and first run through a tool likestplbefore being processed bytxdirto produce the final file tree.
This usage is likecookiecutter,
only that it has the tree definition in one file.INSTALLATIONTo install for user only, do:pip install --user txdirCOMMAND USAGEWithout arguments it expects input fromstdin:txdirExit this viaCTRL+C.
Use no input argument only in combination with piping, or when using-c.Use on a directory tree wherebinary text files are only in dotted directories (e.g. .git) orbinary files are ignored via.gitignoretxdir .produces text output tostdout, similar totree, but with content,
unless content is suppressed with-n.You can save the output in a file and edit it:txdir -l . > tmp.txtThe-loption makes the output flat.
You can drop the-l, if you wanttmp.txtcontain an indented tree.NO directory is created, unless a root is provided as second argument:txdir tmp.txt .This applies to the (edited) text tree intmp.txton the current directory.txdir . againproduces the same tree belowagain, almost like acp-R. again.
But internally a text tree of the file tree is created and then applied to the new location.Note, thattext file content must not have an empty first line.EXAMPLEScd ~/tmp
txdir -c r/a/x,y,z
   └─ r/
      └─ a/
         ├─ x/
         ├─ y/
         └─ z/
txdir - . -c r/a/x,y,z
cd r
tree
   .
   └── a
       ├── x
       ├── y
       └── z
txdir .
   └─ a/
      ├─ x/
      ├─ y/
      └─ z/
txdir . > tmp.txt
#edit tmp.txt
cat tmp.txt
   ├─ a/
   │  ├─ x/
         ├─ x.txt
              This is content in x.txt
   │  ├─ y/
         ├─ y.txt
              This is content in y.txt
txdir tmp.txt .
txdir .
   ├─ a/
   │  ├─ x/
   │  │  └─ x.txt
               This is content in x.txt
   │  ├─ y/
   │  │  └─ y.txt
               This is content in y.txt
   │  └─ z/
   └─ tmp.txt
         ├─ a/
         │  ├─ x/
               ├─ x.txt
                    This is content in x.txt
         │  ├─ y/
               ├─ y.txt
                    This is content in y.txt
#Note, that what is below tmp.txt is content of tmp.txt, not actual directories.
#`txdir . | txdir - .` does not create the same tree below ``tmp.txt``,
#because tmp.txt exists as file and not as directory.
txdir a b
txdir . > tmp.txt
#edit tmp.txt adding {{txt}} and removing the tmp.txt line (else tmp.txt is emptied when applying)
cat tmp.txt
   ├─ a/
   │  ├─ x/
   │  │  └─ x.txt
   │  │        {{txt}} x.txt
   │  ├─ y/
   │  │  └─ y.txt
   │  │        {{txt}} y.txt
   │  └─ z/
   ├─ b/
   │  ├─ x/
   │  │  └─ x.txt
   │  │        {{txt}} x.txt
   │  ├─ y/
   │  │  └─ y.txt
   │  │        {{txt}} y.txt
   │  └─ z/
stpl tmp.txt - 'txt="Greeting from"' | txdir - .
rm tmp.txt
txdir . -l
   a/x/x.txt
      Greeting from x.txt
   a/y/y.txt
      Greeting from y.txt
   a/z/
   b/x/x.txt
      Greeting from x.txt
   b/y/y.txt
      Greeting from y.txt
   b/z/
txdir . -l | sed -e "s/ \(.\)\.txt/ \1/g" | txdir - .
txdir . -l
   a/x/x.txt
      Greeting from x
   a/y/y.txt
      Greeting from y
   a/z/
   b/x/x.txt
      Greeting from x
   b/y/y.txt
      Greeting from y
   b/z/API USAGEtxtdiris a python module.Naming:viewrefers to a text tree viewflatis a flat tree listing.treeis the actual file treeFunctions:set_ascii,set_utf8view_to_treetree_to_viewflat_to_treetree_to_flatto_treedecides whetherflat_to_treeorview_to_treeshould be usedmainmakes the command line functionality accessible to pythonClass:TxDircan hold a file tree in memory. Itscontentrepresentsdirectoryiflistof otherTxDirinstanceslinkifstrwith path relative to the location as link targetfileiftupleof text file linesTxDirmethods:__init__(self, name='', parent=None, content=None)
__iter__(self) #leaves only
__lt__(self,other) #by name
__str__(self)
__repr__(self)
__call__ = cd
__truediv__(self, other) #changes and returns root
root(self)
path(self)
mkdir = cd #with content=[]
cd(self,apath,content=None) #cd or make node if content!=None
isfile(self)
isdir(self)
islink(self)
view(self)
flat(self)
create(self)static:fromcmds(descs)
fromview(viewstr)
fromflat(flatstr)
fromfs(root)EXAMPLES>>> import os
>>> from os.path import expanduser
>>> from shutil import rmtree
>>> import sys
>>> from txdir import *

>>> os.chdir(expanduser('~/tmp'))

>>> t = t.fromcmds(['r/a'])
>>> TxDir('x.txt',t('r/a'),('Text in x',))
>>> t.view()
└─ r/
   └─ a/
      └─ x.txt
            Text in x
>>> t.flat()
r/a/x.txt
   Text in x

>>> rmtree('r',ignore_errors=True)
>>> t.create()

>>> t = TxDir.fromfs('r')
>>> t.view()
└─ a/
   └─ x.txt
         Text in x

>>> rmtree('r',ignore_errors=True)
>>> r = TxDir.fromcmds(['r'])
>>> r = r('r')/t('a') #root is returned
>>> t('a') == r('r/a') #r and t are roots
True
>>> r.flat()
r/a/x.txt
   Text in xLicenseMIT