txdir


Nametxdir JSON
Version 2.0.2 PyPI version JSON
download
home_pagehttps://github.com/rpuntaie/txdir
SummaryCreating file tree from text tree and vice versa
upload_time2024-04-29 16:02:21
maintainerNone
docs_urlNone
authorRoland Puntaier
requires_python>=3.6
licenseMIT
keywords text tree directory file content template
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            =================================
txdir(1) Version 2.0.2 \| txdir
=================================

NAME
====

**txdir** — text tree from or to file tree

SYNOPSIS
========

**txdir** [<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.


DESCRIPTION
===========

- Construct a *file tree* from a text tree.
- Construct a *text tree* from 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 like `stpl <https://github.com/rpuntaie/stpl>`__
before being processed by ``txdir`` to produce the final file tree.
This usage is like `cookiecutter <https://github.com/cookiecutter/cookiecutter>`__,
only that it has the tree definition in one file.

INSTALLATION
============

To install for user only, do::

   pip install --user txdir

COMMAND USAGE
=============

Without arguments it expects input from ``stdin``::

    txdir

Exit this via ``CTRL+C``.
Use no input argument only in combination with piping, or when using `-c`.

Use on a directory tree where

- binary text files are only in dotted directories (e.g. .git) or
- binary files are ignored via ``.gitignore``

::

    txdir .

produces text output to ``stdout``, similar to ``tree``, but with content,
unless content is suppressed with ``-n``.

You can save the output in a file and edit it::

    txdir -l . > tmp.txt

The ``-l`` option makes the output flat.
You can drop the ``-l``, if you want ``tmp.txt`` contain 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 in ``tmp.txt`` on the current directory.

::

    txdir . again

produces the same tree below ``again``, almost like a ``cp -R . again``.
But internally a text tree of the file tree is created and then applied to the new location.

Note, that **text file content must not have an empty first line**.

EXAMPLES
--------

::

   cd ~/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 USAGE
=========

``txtdir`` is a python module.

Naming:

- ``view`` refers to a text tree view
- ``flat`` is a flat tree listing.
- ``tree`` is the actual file tree

Functions:

- ``set_ascii``, ``set_utf8``
- ``view_to_tree``
- ``tree_to_view``
- ``flat_to_tree``
- ``tree_to_flat``
- ``to_tree`` decides whether ``flat_to_tree`` or ``view_to_tree`` should be used
- ``main`` makes the command line functionality accessible to python

Class:

``TxDir`` can hold a file tree in memory. Its ``content`` represents

- *directory* if *list* of other ``TxDir`` instances
- *link* if *str* with path relative to the location as link target
- *file* if *tuple* of text file lines

``TxDir`` methods::

   __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 x


License
-------

MIT


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/rpuntaie/txdir",
    "name": "txdir",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": null,
    "keywords": "text tree directory file content template",
    "author": "Roland Puntaier",
    "author_email": "roland.puntaier@gmail.com",
    "download_url": null,
    "platform": null,
    "description": "=================================\ntxdir(1) Version 2.0.2 \\| txdir\n=================================\n\nNAME\n====\n\n**txdir** \u2014 text tree from or to file tree\n\nSYNOPSIS\n========\n\n**txdir** [<infile>\\|<indir>\\|-] [<outdir>\\|-] [<options>]\n\n\nOptions::\n\n    -h: help\n    -v: version\n    -l: flat listing\n    -f: exclude files\n    -d: include dot files/directories\n    -n: exclude file content (don't reapply such a tree as it will empty all files)\n    -m: maximum depth\n    -c: commands to create directories (from https://github.com/gcmt/mktree)\n\nFiles/dirs are ignored via .gitignore.\n\nCommand line help::\n\n\n    usage: txdir  [infile] [outdir] [-h] [-v] [-a] [-b] [-l] [-f] [-d] [-n] [-m M] [-c [C [C ...]]]\n\n    Files/dirs are ignored via .gitignore. If the directory contains unignored binary files, exclude\n    files with '-f' or ignoring content with '-n'. Text file content must not have an empty first\n    line.\n\n    positional arguments:\n      infile          If a file, it is expected to contain a text representation of a directory, flat\n                      or indented (none or - is stdin). If a directory, the text view is created with\n                      file content (unless -n).\n      outdir          None or - means printing to stdout. If the parameter is an existing file,\n                      nothing is done. If not a directory, the directory is created. The file tree is\n                      created in the directory.\n\n    optional arguments:\n      -h              Print help information.\n      -v              Print version information.\n      -a              Use ASCII instead of unicode when printint the indented view.\n      -b              Include content of binary files as base64 encoded.\n      -l              Create a flat listing instead of an indented view.\n      -f              Omit files. Only list directories.\n      -d              Include dot files/directories.\n      -n              Omit file content.\n      -m M            Maximum directory depth to scan.\n      -c [C [C ...]]  Directories described with a DSL (',' = end of token, '.' = up dir, '/' = down)\n                      `txdir - . -c 'a/b/d.c/d..a/u,v,x,g\\.x'` produces the same as `mkdir -p\n                      a/{b,c}/d a/u a/v a/x a/g.x` If not within ', use \\\\ to escape.\n\n\nDESCRIPTION\n===========\n\n- Construct a *file tree* from a text tree.\n- Construct a *text tree* from a file tree.\n\nThis allows to edit a whole file tree within one file first,\nwithout the necessity to switch files.\n\nThe text tree can also be templated\nand first run through a tool like `stpl <https://github.com/rpuntaie/stpl>`__\nbefore being processed by ``txdir`` to produce the final file tree.\nThis usage is like `cookiecutter <https://github.com/cookiecutter/cookiecutter>`__,\nonly that it has the tree definition in one file.\n\nINSTALLATION\n============\n\nTo install for user only, do::\n\n   pip install --user txdir\n\nCOMMAND USAGE\n=============\n\nWithout arguments it expects input from ``stdin``::\n\n    txdir\n\nExit this via ``CTRL+C``.\nUse no input argument only in combination with piping, or when using `-c`.\n\nUse on a directory tree where\n\n- binary text files are only in dotted directories (e.g. .git) or\n- binary files are ignored via ``.gitignore``\n\n::\n\n    txdir .\n\nproduces text output to ``stdout``, similar to ``tree``, but with content,\nunless content is suppressed with ``-n``.\n\nYou can save the output in a file and edit it::\n\n    txdir -l . > tmp.txt\n\nThe ``-l`` option makes the output flat.\nYou can drop the ``-l``, if you want ``tmp.txt`` contain an indented tree.\n\nNO directory is created, unless a root is provided as second argument::\n\n    txdir tmp.txt .\n\nThis applies to the (edited) text tree in ``tmp.txt`` on the current directory.\n\n::\n\n    txdir . again\n\nproduces the same tree below ``again``, almost like a ``cp -R . again``.\nBut internally a text tree of the file tree is created and then applied to the new location.\n\nNote, that **text file content must not have an empty first line**.\n\nEXAMPLES\n--------\n\n::\n\n   cd ~/tmp\n   txdir -c r/a/x,y,z\n      \u2514\u2500 r/\n         \u2514\u2500 a/\n            \u251c\u2500 x/\n            \u251c\u2500 y/\n            \u2514\u2500 z/\n   txdir - . -c r/a/x,y,z\n   cd r\n   tree\n      .\n      \u2514\u2500\u2500 a\n          \u251c\u2500\u2500 x\n          \u251c\u2500\u2500 y\n          \u2514\u2500\u2500 z\n   txdir .\n      \u2514\u2500 a/\n         \u251c\u2500 x/\n         \u251c\u2500 y/\n         \u2514\u2500 z/\n   txdir . > tmp.txt\n   #edit tmp.txt\n   cat tmp.txt\n      \u251c\u2500 a/\n      \u2502  \u251c\u2500 x/\n            \u251c\u2500 x.txt\n                 This is content in x.txt\n      \u2502  \u251c\u2500 y/\n            \u251c\u2500 y.txt\n                 This is content in y.txt\n   txdir tmp.txt .\n   txdir .\n      \u251c\u2500 a/\n      \u2502  \u251c\u2500 x/\n      \u2502  \u2502  \u2514\u2500 x.txt\n                  This is content in x.txt\n      \u2502  \u251c\u2500 y/\n      \u2502  \u2502  \u2514\u2500 y.txt\n                  This is content in y.txt\n      \u2502  \u2514\u2500 z/\n      \u2514\u2500 tmp.txt\n            \u251c\u2500 a/\n            \u2502  \u251c\u2500 x/\n                  \u251c\u2500 x.txt\n                       This is content in x.txt\n            \u2502  \u251c\u2500 y/\n                  \u251c\u2500 y.txt\n                       This is content in y.txt\n   #Note, that what is below tmp.txt is content of tmp.txt, not actual directories.\n   #`txdir . | txdir - .` does not create the same tree below ``tmp.txt``,\n   #because tmp.txt exists as file and not as directory.\n   txdir a b\n   txdir . > tmp.txt\n   #edit tmp.txt adding {{txt}} and removing the tmp.txt line (else tmp.txt is emptied when applying)\n   cat tmp.txt\n      \u251c\u2500 a/\n      \u2502  \u251c\u2500 x/\n      \u2502  \u2502  \u2514\u2500 x.txt\n      \u2502  \u2502        {{txt}} x.txt\n      \u2502  \u251c\u2500 y/\n      \u2502  \u2502  \u2514\u2500 y.txt\n      \u2502  \u2502        {{txt}} y.txt\n      \u2502  \u2514\u2500 z/\n      \u251c\u2500 b/\n      \u2502  \u251c\u2500 x/\n      \u2502  \u2502  \u2514\u2500 x.txt\n      \u2502  \u2502        {{txt}} x.txt\n      \u2502  \u251c\u2500 y/\n      \u2502  \u2502  \u2514\u2500 y.txt\n      \u2502  \u2502        {{txt}} y.txt\n      \u2502  \u2514\u2500 z/\n   stpl tmp.txt - 'txt=\"Greeting from\"' | txdir - .\n   rm tmp.txt\n   txdir . -l\n      a/x/x.txt\n         Greeting from x.txt\n      a/y/y.txt\n         Greeting from y.txt\n      a/z/\n      b/x/x.txt\n         Greeting from x.txt\n      b/y/y.txt\n         Greeting from y.txt\n      b/z/\n   txdir . -l | sed -e \"s/ \\(.\\)\\.txt/ \\1/g\" | txdir - .\n   txdir . -l\n      a/x/x.txt\n         Greeting from x\n      a/y/y.txt\n         Greeting from y\n      a/z/\n      b/x/x.txt\n         Greeting from x\n      b/y/y.txt\n         Greeting from y\n      b/z/\n\nAPI USAGE\n=========\n\n``txtdir`` is a python module.\n\nNaming:\n\n- ``view`` refers to a text tree view\n- ``flat`` is a flat tree listing.\n- ``tree`` is the actual file tree\n\nFunctions:\n\n- ``set_ascii``, ``set_utf8``\n- ``view_to_tree``\n- ``tree_to_view``\n- ``flat_to_tree``\n- ``tree_to_flat``\n- ``to_tree`` decides whether ``flat_to_tree`` or ``view_to_tree`` should be used\n- ``main`` makes the command line functionality accessible to python\n\nClass:\n\n``TxDir`` can hold a file tree in memory. Its ``content`` represents\n\n- *directory* if *list* of other ``TxDir`` instances\n- *link* if *str* with path relative to the location as link target\n- *file* if *tuple* of text file lines\n\n``TxDir`` methods::\n\n   __init__(self, name='', parent=None, content=None)\n   __iter__(self) #leaves only\n   __lt__(self,other) #by name\n   __str__(self)\n   __repr__(self)\n   __call__ = cd\n   __truediv__(self, other) #changes and returns root\n   root(self)\n   path(self)\n   mkdir = cd #with content=[]\n   cd(self,apath,content=None) #cd or make node if content!=None\n   isfile(self)\n   isdir(self)\n   islink(self)\n   view(self)\n   flat(self)\n   create(self)\n\n\nstatic::\n\n    fromcmds(descs)\n    fromview(viewstr)\n    fromflat(flatstr)\n    fromfs(root)\n\nEXAMPLES\n--------\n\n::\n\n   >>> import os\n   >>> from os.path import expanduser\n   >>> from shutil import rmtree\n   >>> import sys\n   >>> from txdir import *\n\n   >>> os.chdir(expanduser('~/tmp'))\n\n   >>> t = t.fromcmds(['r/a'])\n   >>> TxDir('x.txt',t('r/a'),('Text in x',))\n   >>> t.view()\n   \u2514\u2500 r/\n      \u2514\u2500 a/\n         \u2514\u2500 x.txt\n               Text in x\n   >>> t.flat()\n   r/a/x.txt\n      Text in x\n\n   >>> rmtree('r',ignore_errors=True)\n   >>> t.create()\n\n   >>> t = TxDir.fromfs('r')\n   >>> t.view()\n   \u2514\u2500 a/\n      \u2514\u2500 x.txt\n            Text in x\n\n   >>> rmtree('r',ignore_errors=True)\n   >>> r = TxDir.fromcmds(['r'])\n   >>> r = r('r')/t('a') #root is returned\n   >>> t('a') == r('r/a') #r and t are roots\n   True\n   >>> r.flat()\n   r/a/x.txt\n      Text in x\n\n\nLicense\n-------\n\nMIT\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Creating file tree from text tree and vice versa",
    "version": "2.0.2",
    "project_urls": {
        "Homepage": "https://github.com/rpuntaie/txdir"
    },
    "split_keywords": [
        "text",
        "tree",
        "directory",
        "file",
        "content",
        "template"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "946cd8cb9b17af2b68f72a88927bf8b993dccf9f97cbe63a195702eb3cb23d01",
                "md5": "3addd1d535a9f1a518608b6a932c6a25",
                "sha256": "7e3d37fac9b2a3c7fa5d32a51c082faf21c5dc2edd52c81eea77ca781545e2c0"
            },
            "downloads": -1,
            "filename": "txdir-2.0.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "3addd1d535a9f1a518608b6a932c6a25",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 16392,
            "upload_time": "2024-04-29T16:02:21",
            "upload_time_iso_8601": "2024-04-29T16:02:21.477210Z",
            "url": "https://files.pythonhosted.org/packages/94/6c/d8cb9b17af2b68f72a88927bf8b993dccf9f97cbe63a195702eb3cb23d01/txdir-2.0.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-29 16:02:21",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "rpuntaie",
    "github_project": "txdir",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "txdir"
}
        
Elapsed time: 0.23223s