python文件标识库
identif的Python项目详细描述
识别
python的文件标识库。
给定一个文件(或一个文件的一些信息),返回一组 标识文件的标记。
用法
磁盘上的文件
如果您在磁盘上有一个实际的文件,您可以获得尽可能多的信息 (所有其他方法的超集):
>>>identify.tags_from_path('/path/to/file.py'){'file','text','python','non-executable'}>>>identify.tags_from_path('/path/to/file-with-shebang'){'file','text','shell','bash','executable'}>>>identify.tags_from_path('/bin/bash'){'file','binary','executable'}>>>identify.tags_from_path('/path/to/directory'){'directory'}>>>identify.tags_from_path('/path/to/symlink'){'symlink'}
使用磁盘上的文件时,执行的检查是:
- 文件类型(文件、符号链接、目录)
- 模式(是否可执行?)
- 文件名(主要基于扩展名)
- 如果可执行,则读取shebang并解释解释程序
如果只有文件名
>>>identify.tags_from_filename('file.py'){'text','python'}
如果你只有翻译
>>>identify.tags_from_interpreter('python3.5'){'python','python3'}>>>identify.tags_from_interpreter('bash'){'shell','bash'}>>>identify.tags_from_interpreter('some-unrecognized-thing')set()
作为cli
$ identify-cli --help
usage: identify-cli [-h] [--filename-only] path
positional arguments:
path
optional arguments:
-h, --help show this help message and exit
--filename-only
$ identify-cli setup.py;echo$?["file", "non-executable", "python", "text"]0 identify setup.py --filename-only;echo$?["python", "text"]0 $ identify-cli wat.wat;echo$? wat.wat does not exist. 1 $ identify-cli wat.wat --filename-only;echo$?1
识别许可文件
identify
还有一个api用于确定包含哪种类型的许可证
在一个文件中。此例程大致基于
licensee(github用来计算
回购协议)。
identify
使用的方法如下:
- 删除版权行
- 规范化所有空白
- 返回任何精确匹配项
- 按编辑距离返回最近的值(其中编辑距离为5%)
要使用该api,请通过pip install identify[license]
>>> fromidentifyimportidentify>>> identify.license_id('LICENSE')'MIT'
license_id
函数的返回值是一个SPDXid。
许可证来自choosealicense.com。
工作原理
呼叫tags_from_path
可以做到这一点:
- 什么是类型:文件、符号链接、目录?如果不是文件,就停在这里。
- 它是可执行的吗?添加适当的标记。
- 我们能识别文件扩展名吗?如果是,添加适当的标记,停止 在这里。这些标记将包括二进制/文本。
- 查看文件的前x个字节。使用这些来确定它是否 二进制或文本,添加适当的标记。
- 如果识别为上述文本,请尝试阅读和解释shebang,并添加 适当的标签。
在设计上,这意味着我们不需要部分读取我们识别的文件 文件扩展名。