A lightweight Python package to identify the top-level dataclass in files generated by xsdata, representing the root schema element.
Project description
xsdata-rootfinder
A Python package designed to analyze Python source files and identify root models. This package supports different model types, including dataclass, Pydantic, and attrs, making it easy to work with various schema formats.
Features
- Single File Analysis: Identify root models from a single Python source file.
- Batch Processing: Analyze multiple files or directories with support for recursion.
- Model Types: Supports dataclasses, Pydantic models, and attrs classes.
- Multiprocessing: Optional multiprocessing for faster batch analysis.
Installation
To install xsdata-rootfinder, run the following command:
pip install xsdata-rootfinder
How It Works
The xsdata-rootfinder package analyzes Python source files to identify root models. A root model is defined as a class that is present in a file but not referenced within it. This is specifically desinged for models generated by xsdata but theoretically could be used for any models.
Any number of files or paths can be passed as arguments to the root_finders function. One caveat is that all files passed must reflect the layout of the codebase they exist in. This is important so as to accurately match any local import to the correct python file.
Drawbacks
There are two edge cases that are not taken into account within the logic. Thus, there could be inaccurate results. These issues will be patched up in the next release.
- Importing from a
PYTHONPATHModule- Any file that imports models from a directory that is included in the
PYTHONPATHvariable.
- Any file that imports models from a directory that is included in the
- Import Forwarding Models
- Any file that imports models from files that are import forwarding (i.e. not importing them from where they are defined).
Functionality Overview
root_finder
Analyze a single Python source file to find root models.
def root_finder(source: CodeOrStrOrPath, xsd_models: XsdModels = "dataclass") -> Optional[List[RootModel]]:
...
- Arguments:
source: Python source to analyze (code string, file path, or path-like object).xsd_models: Specifies model type ('dataclass','pydantic', or'attrs').
- Returns: List of root models or
None.
Example Usage
from xsdata_rootfinder import root_finder
roots = root_finder("path/to/source.py", xsd_models="pydantic")
root_finders
Analyze a single directory or multiple files/directories to find root models. If a collection is passed, then each item in that collection must be a string or Path object representing an existing file or directory.
def root_finders(
sources: Union[StrOrPath, Collection[StrOrPath]],
xsd_models: XsdModels = "dataclass",
directory_walk: bool = False,
ignore_init_files: bool = True,
multiprocessing: Optional[MultiprocessingSettings] = None,
) -> Optional[List[RootModel]]:
...
- Arguments:
sources: One or more paths to analyze.xsd_models: Specifies model type ('dataclass','pydantic', or'attrs').directory_walk: Boolean indicating a recursive search of directories.ignore_init_files: Boolean indicating whether to ignore__init__.pyfiles.multiprocessing: Settings for parallel file processing.
- Returns: List of root models or
None.
Example Usage
from xsdata_rootfinder import root_finders
roots = root_finders(
["path/to/source1.py", "path/to/source2.py"],
xsd_models="attrs",
directory_walk=True,
multiprocessing=None
)
License
This project is licensed under the terms of the MIT License.
Workflow Details
GitHub Actions automate CI/CD workflows, including tests and package deployment.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file xsdata_rootfinder-0.1.0.tar.gz.
File metadata
- Download URL: xsdata_rootfinder-0.1.0.tar.gz
- Upload date:
- Size: 13.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.0.1 CPython/3.12.1 Windows/11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
95cf4be1a6afa0461f0a265c58bbd7cb419919c8d6b696b42ef9004e9f5dd807
|
|
| MD5 |
e9258c37645789d90dc14adf718ba97d
|
|
| BLAKE2b-256 |
8e31dad1cd3f3ef6b5c7a78c7218efa759936bad6992b0d59583b7dfd7a9d7bc
|
File details
Details for the file xsdata_rootfinder-0.1.0-py3-none-any.whl.
File metadata
- Download URL: xsdata_rootfinder-0.1.0-py3-none-any.whl
- Upload date:
- Size: 13.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.0.1 CPython/3.12.1 Windows/11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ef7d75f0b45cdb8ad3dd695e529c5a467a0ad37518cacd380fc3ed68ec903cf6
|
|
| MD5 |
68f793e3301436b1427232e66c5be1cd
|
|
| BLAKE2b-256 |
fef225d5ece82aac96af66f5e9ad1350d2642f06648b77f2388bc24abcb485e2
|