[![build status](https://github.com/pre-commit/identify/actions/workflows/main.yml/badge.svg)](https://github.com/pre-commit/identify/actions/workflows/main.yml)
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/pre-commit/identify/main.svg)](https://results.pre-commit.ci/latest/github/pre-commit/identify/main)
identify
========
File identification library for Python.
Given a file (or some information about a file), return a set of standardized
tags identifying what the file is.
## Installation
```bash
pip install identify
```
## Usage
### With a file on disk
If you have an actual file on disk, you can get the most information possible
(a superset of all other methods):
```python
>>> from identify import identify
>>> 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'}
```
When using a file on disk, the checks performed are:
* File type (file, symlink, directory, socket)
* Mode (is it executable?)
* File name (mostly based on extension)
* If executable, the shebang is read and the interpreter interpreted
### If you only have the filename
```python
>>> identify.tags_from_filename('file.py')
{'text', 'python'}
```
### If you only have the interpreter
```python
>>> identify.tags_from_interpreter('python3.5')
{'python', 'python3'}
>>> identify.tags_from_interpreter('bash')
{'shell', 'bash'}
>>> identify.tags_from_interpreter('some-unrecognized-thing')
set()
```
### As a 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
```
```console
$ identify-cli setup.py; echo $?
["file", "non-executable", "python", "text"]
0
$ identify-cli 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
```
### Identifying LICENSE files
`identify` also has an api for determining what type of license is contained
in a file. This routine is roughly based on the approaches used by
[licensee] (the ruby gem that github uses to figure out the license for a
repo).
The approach that `identify` uses is as follows:
1. Strip the copyright line
2. Normalize all whitespace
3. Return any exact matches
4. Return the closest by edit distance (where edit distance < 5%)
To use the api, install via `pip install identify[license]`
```pycon
>>> from identify import identify
>>> identify.license_id('LICENSE')
'MIT'
```
The return value of the `license_id` function is an [SPDX] id. Currently
licenses are sourced from [choosealicense.com].
[licensee]: https://github.com/benbalter/licensee
[SPDX]: https://spdx.org/licenses/
[choosealicense.com]: https://github.com/github/choosealicense.com
## How it works
A call to `tags_from_path` does this:
1. What is the type: file, symlink, directory? If it's not file, stop here.
2. Is it executable? Add the appropriate tag.
3. Do we recognize the file extension? If so, add the appropriate tags, stop
here. These tags would include binary/text.
4. Peek at the first X bytes of the file. Use these to determine whether it is
binary or text, add the appropriate tag.
5. If identified as text above, try to read and interpret the shebang, and add
appropriate tags.
By design, this means we don't need to partially read files where we recognize
the file extension.
Raw data
{
"_id": null,
"home_page": "https://github.com/pre-commit/identify",
"name": "identify",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": null,
"author": "Chris Kuehl",
"author_email": "ckuehl@ocf.berkeley.edu",
"download_url": "https://files.pythonhosted.org/packages/02/79/7a520fc5011e02ca3f3285b5f6820eaf80443eb73e3733f73c02fb42ba0b/identify-2.6.2.tar.gz",
"platform": null,
"description": "[![build status](https://github.com/pre-commit/identify/actions/workflows/main.yml/badge.svg)](https://github.com/pre-commit/identify/actions/workflows/main.yml)\n[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/pre-commit/identify/main.svg)](https://results.pre-commit.ci/latest/github/pre-commit/identify/main)\n\nidentify\n========\n\nFile identification library for Python.\n\nGiven a file (or some information about a file), return a set of standardized\ntags identifying what the file is.\n\n## Installation\n\n```bash\npip install identify\n```\n\n## Usage\n### With a file on disk\n\nIf you have an actual file on disk, you can get the most information possible\n(a superset of all other methods):\n\n```python\n>>> from identify import identify\n>>> identify.tags_from_path('/path/to/file.py')\n{'file', 'text', 'python', 'non-executable'}\n>>> identify.tags_from_path('/path/to/file-with-shebang')\n{'file', 'text', 'shell', 'bash', 'executable'}\n>>> identify.tags_from_path('/bin/bash')\n{'file', 'binary', 'executable'}\n>>> identify.tags_from_path('/path/to/directory')\n{'directory'}\n>>> identify.tags_from_path('/path/to/symlink')\n{'symlink'}\n```\n\nWhen using a file on disk, the checks performed are:\n\n* File type (file, symlink, directory, socket)\n* Mode (is it executable?)\n* File name (mostly based on extension)\n* If executable, the shebang is read and the interpreter interpreted\n\n\n### If you only have the filename\n\n```python\n>>> identify.tags_from_filename('file.py')\n{'text', 'python'}\n```\n\n\n### If you only have the interpreter\n\n```python\n>>> identify.tags_from_interpreter('python3.5')\n{'python', 'python3'}\n>>> identify.tags_from_interpreter('bash')\n{'shell', 'bash'}\n>>> identify.tags_from_interpreter('some-unrecognized-thing')\nset()\n```\n\n### As a cli\n\n```\n$ identify-cli --help\nusage: identify-cli [-h] [--filename-only] path\n\npositional arguments:\n path\n\noptional arguments:\n -h, --help show this help message and exit\n --filename-only\n```\n\n```console\n$ identify-cli setup.py; echo $?\n[\"file\", \"non-executable\", \"python\", \"text\"]\n0\n$ identify-cli setup.py --filename-only; echo $?\n[\"python\", \"text\"]\n0\n$ identify-cli wat.wat; echo $?\nwat.wat does not exist.\n1\n$ identify-cli wat.wat --filename-only; echo $?\n1\n```\n\n### Identifying LICENSE files\n\n`identify` also has an api for determining what type of license is contained\nin a file. This routine is roughly based on the approaches used by\n[licensee] (the ruby gem that github uses to figure out the license for a\nrepo).\n\nThe approach that `identify` uses is as follows:\n\n1. Strip the copyright line\n2. Normalize all whitespace\n3. Return any exact matches\n4. Return the closest by edit distance (where edit distance < 5%)\n\nTo use the api, install via `pip install identify[license]`\n\n```pycon\n>>> from identify import identify\n>>> identify.license_id('LICENSE')\n'MIT'\n```\n\nThe return value of the `license_id` function is an [SPDX] id. Currently\nlicenses are sourced from [choosealicense.com].\n\n[licensee]: https://github.com/benbalter/licensee\n[SPDX]: https://spdx.org/licenses/\n[choosealicense.com]: https://github.com/github/choosealicense.com\n\n## How it works\n\nA call to `tags_from_path` does this:\n\n1. What is the type: file, symlink, directory? If it's not file, stop here.\n2. Is it executable? Add the appropriate tag.\n3. Do we recognize the file extension? If so, add the appropriate tags, stop\n here. These tags would include binary/text.\n4. Peek at the first X bytes of the file. Use these to determine whether it is\n binary or text, add the appropriate tag.\n5. If identified as text above, try to read and interpret the shebang, and add\n appropriate tags.\n\nBy design, this means we don't need to partially read files where we recognize\nthe file extension.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "File identification library for Python",
"version": "2.6.2",
"project_urls": {
"Homepage": "https://github.com/pre-commit/identify"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "e086c4395700f3c5475424fb5c41e20c16be28d10c904aee4d005ba3217fc8e7",
"md5": "a7bb7e45e2bb94a272973ca252fc59cc",
"sha256": "c097384259f49e372f4ea00a19719d95ae27dd5ff0fd77ad630aa891306b82f3"
},
"downloads": -1,
"filename": "identify-2.6.2-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "a7bb7e45e2bb94a272973ca252fc59cc",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": ">=3.9",
"size": 98982,
"upload_time": "2024-11-09T18:11:35",
"upload_time_iso_8601": "2024-11-09T18:11:35.861431Z",
"url": "https://files.pythonhosted.org/packages/e0/86/c4395700f3c5475424fb5c41e20c16be28d10c904aee4d005ba3217fc8e7/identify-2.6.2-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "02797a520fc5011e02ca3f3285b5f6820eaf80443eb73e3733f73c02fb42ba0b",
"md5": "4545b8b924638a14c05999a4fc6407b9",
"sha256": "fab5c716c24d7a789775228823797296a2994b075fb6080ac83a102772a98cbd"
},
"downloads": -1,
"filename": "identify-2.6.2.tar.gz",
"has_sig": false,
"md5_digest": "4545b8b924638a14c05999a4fc6407b9",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 99113,
"upload_time": "2024-11-09T18:11:37",
"upload_time_iso_8601": "2024-11-09T18:11:37.697246Z",
"url": "https://files.pythonhosted.org/packages/02/79/7a520fc5011e02ca3f3285b5f6820eaf80443eb73e3733f73c02fb42ba0b/identify-2.6.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-09 18:11:37",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "pre-commit",
"github_project": "identify",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "identify"
}