pathvalidate


Namepathvalidate JSON
Version 3.2.0 PyPI version JSON
download
home_pagehttps://github.com/thombashi/pathvalidate
Summarypathvalidate is a Python library to sanitize/validate a string such as filenames/file-paths/etc.
upload_time2023-09-17 03:03:27
maintainer
docs_urlNone
authorTsuyoshi Hombashi
requires_python>=3.7
licenseMIT License
keywords file path validation validator sanitization sanitizer
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            .. contents:: **pathvalidate**
   :backlinks: top
   :depth: 2

Summary
=========
`pathvalidate <https://github.com/thombashi/pathvalidate>`__ is a Python library to sanitize/validate a string such as filenames/file-paths/etc.

.. image:: https://badge.fury.io/py/pathvalidate.svg
    :target: https://badge.fury.io/py/pathvalidate
    :alt: PyPI package version

.. image:: https://anaconda.org/thombashi/pathvalidate/badges/version.svg
    :target: https://anaconda.org/thombashi/pathvalidate
    :alt: conda package version

.. image:: https://img.shields.io/pypi/pyversions/pathvalidate.svg
    :target: https://pypi.org/project/pathvalidate
    :alt: Supported Python versions

.. image:: https://img.shields.io/pypi/implementation/pathvalidate.svg
    :target: https://pypi.org/project/pathvalidate
    :alt: Supported Python implementations

.. image:: https://github.com/thombashi/pathvalidate/workflows/Tests/badge.svg
    :target: https://github.com/thombashi/pathvalidate/actions?query=workflow%3ATests
    :alt: Linux/macOS/Windows CI status

.. image:: https://coveralls.io/repos/github/thombashi/pathvalidate/badge.svg?branch=master
    :target: https://coveralls.io/github/thombashi/pathvalidate?branch=master
    :alt: Test coverage: coveralls

.. image:: https://github.com/thombashi/pathvalidate/actions/workflows/github-code-scanning/codeql/badge.svg
    :target: https://github.com/thombashi/pathvalidate/actions/workflows/github-code-scanning/codeql
    :alt: CodeQL

Features
---------
- Sanitize/Validate a string as a:
    - file name
    - file path
- Sanitize will do:
    - Remove invalid characters for a target platform
    - Replace reserved names for a target platform
    - Normalize
    - Remove unprintable characters
- Argument validator/sanitizer for ``argparse`` and ``click``
- Multi platform support:
    - ``Linux``
    - ``Windows``
    - ``macOS``
    - ``POSIX``
    - ``universal`` (platform independent)
- Multibyte character support

Examples
==========
Sanitize a filename
---------------------
:Sample Code:
    .. code-block:: python

        from pathvalidate import sanitize_filename

        fname = "fi:l*e/p\"a?t>h|.t<xt"
        print(f"{fname} -> {sanitize_filename(fname)}\n")

        fname = "\0_a*b:c<d>e%f/(g)h+i_0.txt"
        print(f"{fname} -> {sanitize_filename(fname)}\n")

:Output:
    .. code-block::

        fi:l*e/p"a?t>h|.t<xt -> filepath.txt

        _a*b:c<d>e%f/(g)h+i_0.txt -> _abcde%f(g)h+i_0.txt

The default target ``platform`` is ``universal``.
i.e. the sanitized file name is valid for any platform.

Sanitize a filepath
---------------------
:Sample Code:
    .. code-block:: python

        from pathvalidate import sanitize_filepath

        fpath = "fi:l*e/p\"a?t>h|.t<xt"
        print(f"{fpath} -> {sanitize_filepath(fpath)}\n")

        fpath = "\0_a*b:c<d>e%f/(g)h+i_0.txt"
        print(f"{fpath} -> {sanitize_filepath(fpath)}\n")

:Output:
    .. code-block::

        fi:l*e/p"a?t>h|.t<xt -> file/path.txt

        _a*b:c<d>e%f/(g)h+i_0.txt -> _abcde%f/(g)h+i_0.txt

Validate a filename
---------------------
:Sample Code:
    .. code-block:: python

        import sys
        from pathvalidate import ValidationError, validate_filename

        try:
            validate_filename("fi:l*e/p\"a?t>h|.t<xt")
        except ValidationError as e:
            print(f"{e}\n", file=sys.stderr)

        try:
            validate_filename("COM1")
        except ValidationError as e:
            print(f"{e}\n", file=sys.stderr)

:Output:
    .. code-block::

        [PV1100] invalid characters found: platform=universal, description=invalids=('/'), value='fi:l*e/p"a?t>h|.t<xt'

        [PV1002] found a reserved name by a platform: 'COM1' is a reserved name, platform=universal, reusable_name=False

Check a filename
------------------
:Sample Code:
    .. code-block:: python

        from pathvalidate import is_valid_filename, sanitize_filename

        fname = "fi:l*e/p\"a?t>h|.t<xt"
        print(f"is_valid_filename('{fname}') return {is_valid_filename(fname)}\n")

        sanitized_fname = sanitize_filename(fname)
        print(f"is_valid_filename('{sanitized_fname}') return {is_valid_filename(sanitized_fname)}\n")

:Output:
    .. code-block::

        is_valid_filename('fi:l*e/p"a?t>h|.t<xt') return False

        is_valid_filename('filepath.txt') return True

filename/filepath validator for ``argparse``
----------------------------------------------
:Sample Code:
    .. code-block:: python

        from argparse import ArgumentParser

        from pathvalidate.argparse import validate_filename_arg, validate_filepath_arg

        parser = ArgumentParser()
        parser.add_argument("--filename", type=validate_filename_arg)
        parser.add_argument("--filepath", type=validate_filepath_arg)
        options = parser.parse_args()

        if options.filename:
            print(f"filename: {options.filename}")

        if options.filepath:
            print(f"filepath: {options.filepath}")

:Output:
    .. code-block::

        $ ./examples/argparse_validate.py --filename eg
        filename: eg
        $ ./examples/argparse_validate.py --filename e?g
        usage: argparse_validate.py [-h] [--filename FILENAME] [--filepath FILEPATH]
        argparse_validate.py: error: argument --filename: [PV1100] invalid characters found: invalids=(':'), value='e:g', platform=Windows

.. note::
    ``validate_filepath_arg`` consider ``platform`` as of ``"auto"`` if the input is an absolute file path.

filename/filepath sanitizer for ``argparse``
----------------------------------------------
:Sample Code:
    .. code-block:: python

        from argparse import ArgumentParser

        from pathvalidate.argparse import sanitize_filename_arg, sanitize_filepath_arg


        parser = ArgumentParser()
        parser.add_argument("--filename", type=sanitize_filename_arg)
        parser.add_argument("--filepath", type=sanitize_filepath_arg)
        options = parser.parse_args()

        if options.filename:
            print("filename: {}".format(options.filename))

        if options.filepath:
            print("filepath: {}".format(options.filepath))

:Output:
    .. code-block::

        $ ./examples/argparse_sanitize.py --filename e/g
        filename: eg

.. note::
    ``sanitize_filepath_arg`` is set platform as ``"auto"``.

filename/filepath validator for ``click``
-------------------------------------------
:Sample Code:
    .. code-block:: python

        import click

        from pathvalidate.click import validate_filename_arg, validate_filepath_arg


        @click.command()
        @click.option("--filename", callback=validate_filename_arg)
        @click.option("--filepath", callback=validate_filepath_arg)
        def cli(filename: str, filepath: str) -> None:
            if filename:
                click.echo(f"filename: {filename}")
            if filepath:
                click.echo(f"filepath: {filepath}")


        if __name__ == "__main__":
            cli()

:Output:
    .. code-block::

        $ ./examples/click_validate.py --filename ab
        filename: ab
        $ ./examples/click_validate.py --filepath e?g
        Usage: click_validate.py [OPTIONS]
        Try 'click_validate.py --help' for help.

        Error: Invalid value for '--filename': [PV1100] invalid characters found: invalids=('?'), value='e?g', platform=Windows

filename/filepath sanitizer for ``click``
-------------------------------------------
:Sample Code:
    .. code-block:: python

        import click

        from pathvalidate.click import sanitize_filename_arg, sanitize_filepath_arg


        @click.command()
        @click.option("--filename", callback=sanitize_filename_arg)
        @click.option("--filepath", callback=sanitize_filepath_arg)
        def cli(filename, filepath):
            if filename:
                click.echo(f"filename: {filename}")
            if filepath:
                click.echo(f"filepath: {filepath}")


        if __name__ == "__main__":
            cli()

:Output:
    .. code-block::

        $ ./examples/click_sanitize.py --filename a/b
        filename: ab

For more information
----------------------
More examples can be found at 
https://pathvalidate.rtfd.io/en/latest/pages/examples/index.html

Installation
============
Installation: pip
------------------------------
::

    pip install pathvalidate

Installation: conda
------------------------------
::

    conda install -c thombashi pathvalidate

Installation: apt
------------------------------
::

    sudo add-apt-repository ppa:thombashi/ppa
    sudo apt update
    sudo apt install python3-pathvalidate


Dependencies
============
Python 3.7+
no external dependencies.

Documentation
===============
https://pathvalidate.rtfd.io/

Sponsors
====================================
.. image:: https://avatars.githubusercontent.com/u/44389260?s=48&u=6da7176e51ae2654bcfd22564772ef8a3bb22318&v=4
   :target: https://github.com/chasbecker
   :alt: Charles Becker (chasbecker)
.. image:: https://avatars.githubusercontent.com/u/9919?s=48&v=4
   :target: https://github.com/github
   :alt: onetime: GitHub (github)
.. image:: https://avatars.githubusercontent.com/u/46711571?s=48&u=57687c0e02d5d6e8eeaf9177f7b7af4c9f275eb5&v=4
   :target: https://github.com/Arturi0
   :alt: onetime: Arturi0
.. image:: https://avatars.githubusercontent.com/u/3658062?s=48&v=4
   :target: https://github.com/b4tman
   :alt: onetime: Dmitry Belyaev (b4tman)

`Become a sponsor <https://github.com/sponsors/thombashi>`__


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/thombashi/pathvalidate",
    "name": "pathvalidate",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "file,path,validation,validator,sanitization,sanitizer",
    "author": "Tsuyoshi Hombashi",
    "author_email": "tsuyoshi.hombashi@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/27/93/e2b086c7aba96c70b5e7ef48251702fb95027638fb5a3e787d2efd65783e/pathvalidate-3.2.0.tar.gz",
    "platform": null,
    "description": ".. contents:: **pathvalidate**\n   :backlinks: top\n   :depth: 2\n\nSummary\n=========\n`pathvalidate <https://github.com/thombashi/pathvalidate>`__ is a Python library to sanitize/validate a string such as filenames/file-paths/etc.\n\n.. image:: https://badge.fury.io/py/pathvalidate.svg\n    :target: https://badge.fury.io/py/pathvalidate\n    :alt: PyPI package version\n\n.. image:: https://anaconda.org/thombashi/pathvalidate/badges/version.svg\n    :target: https://anaconda.org/thombashi/pathvalidate\n    :alt: conda package version\n\n.. image:: https://img.shields.io/pypi/pyversions/pathvalidate.svg\n    :target: https://pypi.org/project/pathvalidate\n    :alt: Supported Python versions\n\n.. image:: https://img.shields.io/pypi/implementation/pathvalidate.svg\n    :target: https://pypi.org/project/pathvalidate\n    :alt: Supported Python implementations\n\n.. image:: https://github.com/thombashi/pathvalidate/workflows/Tests/badge.svg\n    :target: https://github.com/thombashi/pathvalidate/actions?query=workflow%3ATests\n    :alt: Linux/macOS/Windows CI status\n\n.. image:: https://coveralls.io/repos/github/thombashi/pathvalidate/badge.svg?branch=master\n    :target: https://coveralls.io/github/thombashi/pathvalidate?branch=master\n    :alt: Test coverage: coveralls\n\n.. image:: https://github.com/thombashi/pathvalidate/actions/workflows/github-code-scanning/codeql/badge.svg\n    :target: https://github.com/thombashi/pathvalidate/actions/workflows/github-code-scanning/codeql\n    :alt: CodeQL\n\nFeatures\n---------\n- Sanitize/Validate a string as a:\n    - file name\n    - file path\n- Sanitize will do:\n    - Remove invalid characters for a target platform\n    - Replace reserved names for a target platform\n    - Normalize\n    - Remove unprintable characters\n- Argument validator/sanitizer for ``argparse`` and ``click``\n- Multi platform support:\n    - ``Linux``\n    - ``Windows``\n    - ``macOS``\n    - ``POSIX``\n    - ``universal`` (platform independent)\n- Multibyte character support\n\nExamples\n==========\nSanitize a filename\n---------------------\n:Sample Code:\n    .. code-block:: python\n\n        from pathvalidate import sanitize_filename\n\n        fname = \"fi:l*e/p\\\"a?t>h|.t<xt\"\n        print(f\"{fname} -> {sanitize_filename(fname)}\\n\")\n\n        fname = \"\\0_a*b:c<d>e%f/(g)h+i_0.txt\"\n        print(f\"{fname} -> {sanitize_filename(fname)}\\n\")\n\n:Output:\n    .. code-block::\n\n        fi:l*e/p\"a?t>h|.t<xt -> filepath.txt\n\n        _a*b:c<d>e%f/(g)h+i_0.txt -> _abcde%f(g)h+i_0.txt\n\nThe default target ``platform`` is ``universal``.\ni.e. the sanitized file name is valid for any platform.\n\nSanitize a filepath\n---------------------\n:Sample Code:\n    .. code-block:: python\n\n        from pathvalidate import sanitize_filepath\n\n        fpath = \"fi:l*e/p\\\"a?t>h|.t<xt\"\n        print(f\"{fpath} -> {sanitize_filepath(fpath)}\\n\")\n\n        fpath = \"\\0_a*b:c<d>e%f/(g)h+i_0.txt\"\n        print(f\"{fpath} -> {sanitize_filepath(fpath)}\\n\")\n\n:Output:\n    .. code-block::\n\n        fi:l*e/p\"a?t>h|.t<xt -> file/path.txt\n\n        _a*b:c<d>e%f/(g)h+i_0.txt -> _abcde%f/(g)h+i_0.txt\n\nValidate a filename\n---------------------\n:Sample Code:\n    .. code-block:: python\n\n        import sys\n        from pathvalidate import ValidationError, validate_filename\n\n        try:\n            validate_filename(\"fi:l*e/p\\\"a?t>h|.t<xt\")\n        except ValidationError as e:\n            print(f\"{e}\\n\", file=sys.stderr)\n\n        try:\n            validate_filename(\"COM1\")\n        except ValidationError as e:\n            print(f\"{e}\\n\", file=sys.stderr)\n\n:Output:\n    .. code-block::\n\n        [PV1100] invalid characters found: platform=universal, description=invalids=('/'), value='fi:l*e/p\"a?t>h|.t<xt'\n\n        [PV1002] found a reserved name by a platform: 'COM1' is a reserved name, platform=universal, reusable_name=False\n\nCheck a filename\n------------------\n:Sample Code:\n    .. code-block:: python\n\n        from pathvalidate import is_valid_filename, sanitize_filename\n\n        fname = \"fi:l*e/p\\\"a?t>h|.t<xt\"\n        print(f\"is_valid_filename('{fname}') return {is_valid_filename(fname)}\\n\")\n\n        sanitized_fname = sanitize_filename(fname)\n        print(f\"is_valid_filename('{sanitized_fname}') return {is_valid_filename(sanitized_fname)}\\n\")\n\n:Output:\n    .. code-block::\n\n        is_valid_filename('fi:l*e/p\"a?t>h|.t<xt') return False\n\n        is_valid_filename('filepath.txt') return True\n\nfilename/filepath validator for ``argparse``\n----------------------------------------------\n:Sample Code:\n    .. code-block:: python\n\n        from argparse import ArgumentParser\n\n        from pathvalidate.argparse import validate_filename_arg, validate_filepath_arg\n\n        parser = ArgumentParser()\n        parser.add_argument(\"--filename\", type=validate_filename_arg)\n        parser.add_argument(\"--filepath\", type=validate_filepath_arg)\n        options = parser.parse_args()\n\n        if options.filename:\n            print(f\"filename: {options.filename}\")\n\n        if options.filepath:\n            print(f\"filepath: {options.filepath}\")\n\n:Output:\n    .. code-block::\n\n        $ ./examples/argparse_validate.py --filename eg\n        filename: eg\n        $ ./examples/argparse_validate.py --filename e?g\n        usage: argparse_validate.py [-h] [--filename FILENAME] [--filepath FILEPATH]\n        argparse_validate.py: error: argument --filename: [PV1100] invalid characters found: invalids=(':'), value='e:g', platform=Windows\n\n.. note::\n    ``validate_filepath_arg`` consider ``platform`` as of ``\"auto\"`` if the input is an absolute file path.\n\nfilename/filepath sanitizer for ``argparse``\n----------------------------------------------\n:Sample Code:\n    .. code-block:: python\n\n        from argparse import ArgumentParser\n\n        from pathvalidate.argparse import sanitize_filename_arg, sanitize_filepath_arg\n\n\n        parser = ArgumentParser()\n        parser.add_argument(\"--filename\", type=sanitize_filename_arg)\n        parser.add_argument(\"--filepath\", type=sanitize_filepath_arg)\n        options = parser.parse_args()\n\n        if options.filename:\n            print(\"filename: {}\".format(options.filename))\n\n        if options.filepath:\n            print(\"filepath: {}\".format(options.filepath))\n\n:Output:\n    .. code-block::\n\n        $ ./examples/argparse_sanitize.py --filename e/g\n        filename: eg\n\n.. note::\n    ``sanitize_filepath_arg`` is set platform as ``\"auto\"``.\n\nfilename/filepath validator for ``click``\n-------------------------------------------\n:Sample Code:\n    .. code-block:: python\n\n        import click\n\n        from pathvalidate.click import validate_filename_arg, validate_filepath_arg\n\n\n        @click.command()\n        @click.option(\"--filename\", callback=validate_filename_arg)\n        @click.option(\"--filepath\", callback=validate_filepath_arg)\n        def cli(filename: str, filepath: str) -> None:\n            if filename:\n                click.echo(f\"filename: {filename}\")\n            if filepath:\n                click.echo(f\"filepath: {filepath}\")\n\n\n        if __name__ == \"__main__\":\n            cli()\n\n:Output:\n    .. code-block::\n\n        $ ./examples/click_validate.py --filename ab\n        filename: ab\n        $ ./examples/click_validate.py --filepath e?g\n        Usage: click_validate.py [OPTIONS]\n        Try 'click_validate.py --help' for help.\n\n        Error: Invalid value for '--filename': [PV1100] invalid characters found: invalids=('?'), value='e?g', platform=Windows\n\nfilename/filepath sanitizer for ``click``\n-------------------------------------------\n:Sample Code:\n    .. code-block:: python\n\n        import click\n\n        from pathvalidate.click import sanitize_filename_arg, sanitize_filepath_arg\n\n\n        @click.command()\n        @click.option(\"--filename\", callback=sanitize_filename_arg)\n        @click.option(\"--filepath\", callback=sanitize_filepath_arg)\n        def cli(filename, filepath):\n            if filename:\n                click.echo(f\"filename: {filename}\")\n            if filepath:\n                click.echo(f\"filepath: {filepath}\")\n\n\n        if __name__ == \"__main__\":\n            cli()\n\n:Output:\n    .. code-block::\n\n        $ ./examples/click_sanitize.py --filename a/b\n        filename: ab\n\nFor more information\n----------------------\nMore examples can be found at \nhttps://pathvalidate.rtfd.io/en/latest/pages/examples/index.html\n\nInstallation\n============\nInstallation: pip\n------------------------------\n::\n\n    pip install pathvalidate\n\nInstallation: conda\n------------------------------\n::\n\n    conda install -c thombashi pathvalidate\n\nInstallation: apt\n------------------------------\n::\n\n    sudo add-apt-repository ppa:thombashi/ppa\n    sudo apt update\n    sudo apt install python3-pathvalidate\n\n\nDependencies\n============\nPython 3.7+\nno external dependencies.\n\nDocumentation\n===============\nhttps://pathvalidate.rtfd.io/\n\nSponsors\n====================================\n.. image:: https://avatars.githubusercontent.com/u/44389260?s=48&u=6da7176e51ae2654bcfd22564772ef8a3bb22318&v=4\n   :target: https://github.com/chasbecker\n   :alt: Charles Becker (chasbecker)\n.. image:: https://avatars.githubusercontent.com/u/9919?s=48&v=4\n   :target: https://github.com/github\n   :alt: onetime: GitHub (github)\n.. image:: https://avatars.githubusercontent.com/u/46711571?s=48&u=57687c0e02d5d6e8eeaf9177f7b7af4c9f275eb5&v=4\n   :target: https://github.com/Arturi0\n   :alt: onetime: Arturi0\n.. image:: https://avatars.githubusercontent.com/u/3658062?s=48&v=4\n   :target: https://github.com/b4tman\n   :alt: onetime: Dmitry Belyaev (b4tman)\n\n`Become a sponsor <https://github.com/sponsors/thombashi>`__\n\n",
    "bugtrack_url": null,
    "license": "MIT License",
    "summary": "pathvalidate is a Python library to sanitize/validate a string such as filenames/file-paths/etc.",
    "version": "3.2.0",
    "project_urls": {
        "Changlog": "https://github.com/thombashi/pathvalidate/releases",
        "Documentation": "https://pathvalidate.rtfd.io/",
        "Homepage": "https://github.com/thombashi/pathvalidate",
        "Source": "https://github.com/thombashi/pathvalidate",
        "Tracker": "https://github.com/thombashi/pathvalidate/issues"
    },
    "split_keywords": [
        "file",
        "path",
        "validation",
        "validator",
        "sanitization",
        "sanitizer"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0cab673cce13ab635fd755d206b18c0a371ef6e28ddbe25fadba9ae6c59f22a5",
                "md5": "ff3e47bfb2c77330cd19571526c72ac7",
                "sha256": "cc593caa6299b22b37f228148257997e2fa850eea2daf7e4cc9205cef6908dee"
            },
            "downloads": -1,
            "filename": "pathvalidate-3.2.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "ff3e47bfb2c77330cd19571526c72ac7",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 23286,
            "upload_time": "2023-09-17T03:03:24",
            "upload_time_iso_8601": "2023-09-17T03:03:24.923036Z",
            "url": "https://files.pythonhosted.org/packages/0c/ab/673cce13ab635fd755d206b18c0a371ef6e28ddbe25fadba9ae6c59f22a5/pathvalidate-3.2.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2793e2b086c7aba96c70b5e7ef48251702fb95027638fb5a3e787d2efd65783e",
                "md5": "2a76cbe4e08f58087f418d150a1461b5",
                "sha256": "5e8378cf6712bff67fbe7a8307d99fa8c1a0cb28aa477056f8fc374f0dff24ad"
            },
            "downloads": -1,
            "filename": "pathvalidate-3.2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "2a76cbe4e08f58087f418d150a1461b5",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 31246,
            "upload_time": "2023-09-17T03:03:27",
            "upload_time_iso_8601": "2023-09-17T03:03:27.086442Z",
            "url": "https://files.pythonhosted.org/packages/27/93/e2b086c7aba96c70b5e7ef48251702fb95027638fb5a3e787d2efd65783e/pathvalidate-3.2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-17 03:03:27",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "thombashi",
    "github_project": "pathvalidate",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "pathvalidate"
}
        
Elapsed time: 0.12483s