identify


Nameidentify JSON
Version 2.6.3 PyPI version JSON
download
home_pagehttps://github.com/pre-commit/identify
SummaryFile identification library for Python
upload_time2024-11-25 23:13:11
maintainerNone
docs_urlNone
authorChris Kuehl
requires_python>=3.9
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![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/1a/5f/05f0d167be94585d502b4adf8c7af31f1dc0b1c7e14f9938a88fdbbcf4a7/identify-2.6.3.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.3",
    "project_urls": {
        "Homepage": "https://github.com/pre-commit/identify"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c9f509644a3ad803fae9eca8efa17e1f2aef380c7f0b02f7ec4e8d446e51d64a",
                "md5": "96849b021bc04568450a066743f131de",
                "sha256": "9edba65473324c2ea9684b1f944fe3191db3345e50b6d04571d10ed164f8d7bd"
            },
            "downloads": -1,
            "filename": "identify-2.6.3-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "96849b021bc04568450a066743f131de",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": ">=3.9",
            "size": 99049,
            "upload_time": "2024-11-25T23:13:09",
            "upload_time_iso_8601": "2024-11-25T23:13:09.959463Z",
            "url": "https://files.pythonhosted.org/packages/c9/f5/09644a3ad803fae9eca8efa17e1f2aef380c7f0b02f7ec4e8d446e51d64a/identify-2.6.3-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1a5f05f0d167be94585d502b4adf8c7af31f1dc0b1c7e14f9938a88fdbbcf4a7",
                "md5": "1b137f57628ad636c3ff2da7ccc23900",
                "sha256": "62f5dae9b5fef52c84cc188514e9ea4f3f636b1d8799ab5ebc475471f9e47a02"
            },
            "downloads": -1,
            "filename": "identify-2.6.3.tar.gz",
            "has_sig": false,
            "md5_digest": "1b137f57628ad636c3ff2da7ccc23900",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 99179,
            "upload_time": "2024-11-25T23:13:11",
            "upload_time_iso_8601": "2024-11-25T23:13:11.816208Z",
            "url": "https://files.pythonhosted.org/packages/1a/5f/05f0d167be94585d502b4adf8c7af31f1dc0b1c7e14f9938a88fdbbcf4a7/identify-2.6.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-25 23:13:11",
    "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"
}
        
Elapsed time: 0.39692s