PathSchema

Finally an easy way to validate your folder structure Checkout this sick online demo!

  • root

					
Ko-fi donations Support Me on Ko-fi

How to use

Installation

PathSchema is published on Pypi and can be installed using Pip.

pip install pathschema

Using the Command Line

PathSchema comes with an easy to use command line tool to run a validation on a given folder.

python -m pathschema path/to/.pathschema path/to/folder

More options are available, make sure to take a look at the help files with the --help argument.

Schema Syntax

To get started, create a file named .pathschema. It is usually placed within the target folder you wish to validate

Basic Path Definition

Each line in the schema represents an acceptable path. By default, paths are treated as files but can be changed to be a folder by adding a slash (/) at the end of the line

Adding Comments

You can add comments to document your schema. Simply add a pound sign (#) at the beginning of the line.

Wildcard Characters

Wildcard characters are supported in the path name by default. For example you can use *.md to only accept markdown files.

Advanced Pattern Matching with Regex

In the cases where a simple wildcard isn't enough, regex can be used to achieve more advance pattern matching. To do so, enclose the name in double quotes like this: "[0-9]{4}".

Making Paths Required or Forbidden

Some path can be made required by using the plus sign (+) in front of it's name, example: + readme.md. Similarly, you can make a path forbidden by using the minus sign (-).

Ignoring Folder Contents

To ignore the content of a folder, use the ellipsis (...).

Copyright © 2023 | Apollo-Roboto

Powered by Pyscript and Tailwindcss

packages = ["matplotlib", "pandas", "pathschema==0.2.1"] from pathlib import Path from datetime import datetime import os import tempfile import shutil from pathschema import validate from pathschema.models import DirPathNode from pathschema.exceptions import SchemaError # elements schema_input_element = Element('schema-input') pathschema_output_element = Element('pathschema-output') temp_dir = Path(tempfile.gettempdir()) folder_to_validate = 'root' def validate_the_files_with_schema(): results = None pathschema_output_element.clear() show_command() try: # get the schema from the input element schema = schema_input_element.value # do the validation results = validate(temp_dir / folder_to_validate, schema) except SchemaError as e: # Error in the user schema pathschema_output_element.write(str(e), append=True) return except Exception as e: # Other exceptions pathschema_output_element.write('Unexpected error') raise e if(results.has_error()): show_validation_errors(results) else: show_validation_success() def show_command(): text = f'> python -m pathschema ./.pathschema {temp_dir / folder_to_validate}' pathschema_output_element.write(text, append=True) def show_validation_success(): pathschema_output_element.write('Success', append=True) def show_validation_errors(results): text = '' for path, errors in results.errors_by_path.items(): if(len(errors) > 0 ): text += 'FAIL ' else: text += ' OK ' text += str(path) + '\n' for error in errors: text += ' ' + error + '\n' for line in text.split('\n'): pathschema_output_element.write(line, append=True) def set_folder_to_validate(name): global folder_to_validate folder_to_validate = name def create_fake_file(path): full_path = temp_dir / path os.makedirs(full_path.parent, exist_ok=True) full_path.touch() def create_fake_dir(path): os.makedirs(temp_dir / path, exist_ok=True) def clear_paths(): shutil.rmtree(temp_dir)