从文本树创建文件树,反之亦然

txdir的Python项目详细描述


姓名

txdir-文本树from or to file tree

概要

txdir[<;infile>;|<;indir>;|-][<;outdir>;|-][<;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)

通过.gitignore忽略文件/目录。在

命令行帮助:

^{pr2}$

说明

  • 从文本树构造一个文件树。在
  • 从文件树构造一个text树。在

这允许首先在一个文件中编辑整个文件树, 不需要交换文件。在

文本树也可以模板化 首先运行一个工具,比如stpl 在由txdir处理以生成最终文件树之前。 此用法类似于cookiecutter, 只有它在一个文件中有树定义。在

安装

要仅为用户安装,请执行以下操作:

pip install --user txdir

命令用法

如果没有参数,它需要来自stdin的输入:

txdir

通过CTRL+C退出。 无输入参数只能与管道结合使用,或者在使用-c时使用。在

在目录树上使用

  • 二进制文本文件仅位于点目录(例如.git)或
  • 通过.gitignore忽略二进制文件
txdir .

生成到stdout的文本输出,类似于tree,但是包含内容, 除非使用-n抑制内容。在

可以将输出保存在文件中并进行编辑:

txdir -l . > tmp.txt

-l选项使输出平坦。 如果希望tmp.txt包含缩进树,可以删除-l。在

除非根作为第二个参数提供,否则不会创建目录:

txdir tmp.txt .

这适用于当前目录中tmp.txt中的(已编辑)文本树。在

txdir . again

again下面生成相同的树,几乎类似于cp -R . again。 但是在内部创建了文件树的文本树,然后将其应用到新位置。在

请注意,文本文件内容的第一行不能为空。在

示例

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使用

txtdir是一个python模块。在

命名:

  • view引用文本树视图
  • flat是一个平面树列表。在
  • tree是实际的文件树

功能:

  • set_asciiset_utf8
  • view_to_tree
  • tree_to_view
  • flat_to_tree
  • tree_to_flat
  • to_tree决定应该使用flat_to_tree还是{tt20}$
  • main使命令行功能可由python访问

班级:

TxDir可以在内存中保存文件树。它的content表示

  • directory如果list其他TxDir实例
  • link如果str,那么相对于该位置的路径作为链接目标
  • file如果tuple文本文件行

TxDir方法:

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

静态:

fromcmds(descs)
fromview(viewstr)
fromflat(flatstr)
fromfs(root)

示例

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

许可证

麻省理工学院

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何通过标记名检索多个标记中的元素以进行selenium自动化测试   java iText如何查找最后一行被拆分到下一页   java如何在hudson中的特定jdk上运行Findbugs和PMD?   如何确保java程序与java Environment 6兼容?   对形状进行分组,这样我就可以通过鼠标点击和java处理循环浏览它们   使用生成器映射对象时,java定义无效   maven Java:Struts2和IntelliJ供初学者使用   java子类不继承父类字段   java Android Grid View在Android版本kitkat上崩溃   java Hibernate从缓存返回错误的列表,即使预期的列表与缓存的列表不同   java SendGrid:模板和替换标记   用于普通生产者| Kafka流的java自定义分区器   安卓理解Java内部类中的作用域   无法从Android Studio中的非静态方法调用java非静态方法   比较两个XML响应的JavaXMLUnit   java使用keytool列出密钥   不使用Java客户端库将视频上传到YouTube数据API v3   java My While循环即使在满足条件时也不会结束   自动在外部存储字符串数据,以便以后在Java中使用