Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/thombashi/pathvalidate
A Python library to sanitize/validate a string such as filenames/file-paths/etc.
https://github.com/thombashi/pathvalidate
filepath lint python-library sanitization validation-library
Last synced: about 4 hours ago
JSON representation
A Python library to sanitize/validate a string such as filenames/file-paths/etc.
- Host: GitHub
- URL: https://github.com/thombashi/pathvalidate
- Owner: thombashi
- License: mit
- Created: 2016-03-24T12:11:39.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2024-08-24T17:33:02.000Z (4 months ago)
- Last Synced: 2024-10-29T15:48:32.132Z (about 1 month ago)
- Topics: filepath, lint, python-library, sanitization, validation-library
- Language: Python
- Homepage: https://pathvalidate.rtfd.io/
- Size: 801 KB
- Stars: 223
- Watchers: 7
- Forks: 13
- Open Issues: 7
-
Metadata Files:
- Readme: README.rst
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
- awesome-repositories - thombashi/pathvalidate - A Python library to sanitize/validate a string such as filenames/file-paths/etc. (Python)
README
.. contents:: **pathvalidate**
:backlinks: top
:depth: 2Summary
=========
`pathvalidate `__ is a Python library to sanitize/validate a string such as filenames/file-paths/etc.|PyPI pkg ver| |conda pkg ver| |Supported Python ver| |Supported Python impl| |CI status| |Test coverage| |CodeQL|
.. |PyPI pkg ver| image:: https://badge.fury.io/py/pathvalidate.svg
:target: https://badge.fury.io/py/pathvalidate
:alt: PyPI package version.. |conda pkg ver| image:: https://anaconda.org/conda-forge/pathvalidate/badges/version.svg
:target: https://anaconda.org/conda-forge/pathvalidate
:alt: conda package version.. |Supported Python ver| image:: https://img.shields.io/pypi/pyversions/pathvalidate.svg
:target: https://pypi.org/project/pathvalidate
:alt: Supported Python versions.. |Supported Python impl| image:: https://img.shields.io/pypi/implementation/pathvalidate.svg
:target: https://pypi.org/project/pathvalidate
:alt: Supported Python implementations.. |CI status| image:: https://github.com/thombashi/pathvalidate/actions/workflows/ci.yml/badge.svg
:target: https://github.com/thombashi/pathvalidate/actions/workflows/ci.yml
:alt: CI status of Linux/macOS/Windows.. |Test coverage| 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.. |CodeQL| 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: CodeQLFeatures
---------
- 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``: POSIX-compliant systems (Linux, macOS, etc.)
- ``universal``: platform independent
- Multibyte character supportCLI tool
---------
You can find this package's command line interface tool at the `pathvalidate-cli `__ repository.Examples
==========
Sanitize a filename
---------------------
:Sample Code:
.. code-block:: pythonfrom pathvalidate import sanitize_filename
fname = "fi:l*e/p\"a?t>h|.t {sanitize_filename(fname)}\n")
fname = "\0_a*b:ce%f/(g)h+i_0.txt"
print(f"{fname} -> {sanitize_filename(fname)}\n"):Output:
.. code-block::fi:l*e/p"a?t>h|.t filepath.txt
_a*b:ce%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:: pythonfrom pathvalidate import sanitize_filepath
fpath = "fi:l*e/p\"a?t>h|.t {sanitize_filepath(fpath)}\n")
fpath = "\0_a*b:ce%f/(g)h+i_0.txt"
print(f"{fpath} -> {sanitize_filepath(fpath)}\n"):Output:
.. code-block::fi:l*e/p"a?t>h|.t file/path.txt
_a*b:ce%f/(g)h+i_0.txt -> _abcde%f/(g)h+i_0.txt
Validate a filename
---------------------
:Sample Code:
.. code-block:: pythonimport sys
from pathvalidate import ValidationError, validate_filenametry:
validate_filename("fi:l*e/p\"a?t>h|.th|.th|.t 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:: pythonimport 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: abFor more information
----------------------
More examples can be found at
https://pathvalidate.rtfd.io/en/latest/pages/examples/index.htmlInstallation
============
Installation: pip
------------------------------
::pip install pathvalidate
Installation: conda
------------------------------
::conda install conda-forge::pathvalidate
Installation: apt
------------------------------
::sudo add-apt-repository ppa:thombashi/ppa
sudo apt update
sudo apt install python3-pathvalidateDependencies
============
Python 3.7+
no external dependencies.Documentation
===============
https://pathvalidate.rtfd.io/Sponsors
====================================
|chasbecker| |shiguredo| |b4tman| |Arturi0| |github|.. |chasbecker| image:: https://avatars.githubusercontent.com/u/44389260?s=48&u=6da7176e51ae2654bcfd22564772ef8a3bb22318&v=4
:target: https://github.com/chasbecker
:alt: ex-sponsor: Charles Becker (chasbecker)
.. |shiguredo| image:: https://avatars.githubusercontent.com/u/2549434?s=48&v=4
:target: https://github.com/shiguredo
:alt: ex-sponsor: 時雨堂 (shiguredo)
.. |b4tman| image:: https://avatars.githubusercontent.com/u/3658062?s=48&v=4
:target: https://github.com/b4tman
:alt: onetime: Dmitry Belyaev (b4tman)
.. |Arturi0| image:: https://avatars.githubusercontent.com/u/46711571?s=48&u=57687c0e02d5d6e8eeaf9177f7b7af4c9f275eb5&v=4
:target: https://github.com/Arturi0
:alt: onetime: Arturi0
.. |github| image:: https://avatars.githubusercontent.com/u/9919?s=48&v=4
:target: https://github.com/github
:alt: onetime: GitHub (github)`Become a sponsor `__