All Projects → thombashi → pathvalidate

thombashi / pathvalidate

Licence: MIT license
A Python library to sanitize/validate a string such as filenames/file-paths/etc.

Programming Languages

python
139335 projects - #7 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to pathvalidate

Parcel Plugin Typescript
🚨 Enhanced TypeScript support for Parcel
Stars: ✭ 176 (+26.62%)
Mutual labels:  lint
Markdownlint
A Node.js style checker and lint tool for Markdown/CommonMark files.
Stars: ✭ 2,828 (+1934.53%)
Mutual labels:  lint
Sql Lint
An SQL linter
Stars: ✭ 243 (+74.82%)
Mutual labels:  lint
Nbqa
Run any standard Python code quality tool on a Jupyter Notebook
Stars: ✭ 193 (+38.85%)
Mutual labels:  lint
Sql Language Server
SQL Language Server
Stars: ✭ 210 (+51.08%)
Mutual labels:  lint
D Scanner
Swiss-army knife for D source code
Stars: ✭ 221 (+58.99%)
Mutual labels:  lint
Openapi Cli
⚒️ OpenAPI 3 CLI toolbox with rich validation and bundling features.
Stars: ✭ 169 (+21.58%)
Mutual labels:  lint
landing-frontend
coala Landing Page - https://gitlab.com/coala/landing is needed as backend
Stars: ✭ 31 (-77.7%)
Mutual labels:  lint
Ember Template Lint
Linter for Ember or Handlebars templates
Stars: ✭ 214 (+53.96%)
Mutual labels:  lint
Secretlint
Pluggable linting tool to prevent committing credential.
Stars: ✭ 239 (+71.94%)
Mutual labels:  lint
Add And Commit
Add & commit files from a path directly from GitHub Actions
Stars: ✭ 198 (+42.45%)
Mutual labels:  lint
Whispers
Identify hardcoded secrets and dangerous behaviours
Stars: ✭ 66 (-52.52%)
Mutual labels:  lint
Fsharplint
Lint tool for F#
Stars: ✭ 224 (+61.15%)
Mutual labels:  lint
Woke
✊ Detect non-inclusive language in your source code.
Stars: ✭ 190 (+36.69%)
Mutual labels:  lint
Lint Rules
A set of very opinionated lint rules.
Stars: ✭ 252 (+81.29%)
Mutual labels:  lint
Ue4 Style Guide
An attempt to make Unreal Engine 4 projects more consistent
Stars: ✭ 2,656 (+1810.79%)
Mutual labels:  lint
Protoc Gen Lint
A plug-in for Google's Protocol Buffers (protobufs) compiler to lint .proto files for style violations.
Stars: ✭ 221 (+58.99%)
Mutual labels:  lint
vscode-lint
A VSCode configuration tool integrating Stylelint and Eslint
Stars: ✭ 60 (-56.83%)
Mutual labels:  lint
rpmlint
Tool for checking common errors in rpm packages
Stars: ✭ 112 (-19.42%)
Mutual labels:  lint
Lin
Lin is an Android Lint tool made simple
Stars: ✭ 235 (+69.06%)
Mutual labels:  lint

Summary

pathvalidate is a Python library to sanitize/validate a string such as filenames/file-paths/etc.

PyPI package version conda package version Supported Python versions Supported Python implementations Linux/macOS/Windows CI status Test coverage: coveralls CodeQL

Features

  • Sanitize/Validate a string as a:
    • file name
    • file path
  • file name/path argument validator/sanitizer for argparse and click
  • Multi platform support:
    • sanitize/validate file names/paths for a specific platform (Linux/Windows/macOS/Posix) or universal (platform independent)
  • Multibyte character support

Examples

Sanitize a filename

Sample Code:
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:
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:
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:
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:
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:
invalid char found: invalids=(':', '*', '/', '"', '?', '>', '|', '<'), value='fi:l*e/p"a?t>h|.t<xt', reason=INVALID_CHARACTER, target-platform=Windows

'COM1' is a reserved name, reason=RESERVED_NAME, target-platform=universal

Check a filename

Sample Code:
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:
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:
from argparse import ArgumentParser

from pathvalidate.argparse import validate_filename_arg, validate_filepath_arg

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

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

if options.filepath:
    print("filepath: {}".format(options.filepath))
Output:
$ ./examples/argparse_validate.py --filename eg
filename: eg
$ ./examples/argparse_validate.py --filepath e?g
usage: argparse_validate.py [-h] [--filepath FILEPATH] [--filename FILENAME]
argparse_validate.py: error: argument --filepath: invalid char found: invalids=('?'), value='e?g', reason=INVALID_CHARACTER, target-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:
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:
$ ./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:
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, filepath):
    if filename:
        click.echo("filename: {}".format(filename))
    if filepath:
        click.echo("filepath: {}".format(filepath))


if __name__ == "__main__":
    cli()
Output:
$ ./examples/click_validate.py --filename ab
filename: ab
$ ./examples/click_validate.py --filepath e?g
Usage: click_validate.py [OPTIONS]

Error: Invalid value for "--filepath": invalid char found: invalids=('?'), value='e?g', reason=INVALID_CHARACTER, target-platform=Windows

filename/filepath sanitizer for click

Sample Code:
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("filename: {}".format(filename))
    if filepath:
        click.echo("filepath: {}".format(filepath))


if __name__ == "__main__":
    cli()
Output:
$ ./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.6+ no external dependencies.

Documentation

https://pathvalidate.rtfd.io/

Sponsors

Charles Becker (chasbecker) onetime: GitHub (github) onetime: Arturi0 onetime: Dmitry Belyaev (b4tman)

Become a sponsor

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].