Python wrapper for Syster CLI - SysML v2 and KerML analysis
Project description
Systree
Python wrapper for the Syster CLI - SysML v2 and KerML analysis.
Installation
pip install systree
Or from source:
cd systree
pip install -e ".[dev]"
Installing the CLI
The CLI is automatically installed from crates.io on first use (requires Rust).
To pre-install or install manually:
# Pre-install via systree (installs to ~/.cache/systree/bin/)
python -c "from systree import download_cli; download_cli()"
# Or install globally
cargo install syster-cli
Or via make:
make install-cli
Python Usage
Basic Analysis
from systree import analyze
# Analyze a single file
result = analyze("model.sysml")
print(f"Files: {result.file_count}, Symbols: {result.symbol_count}")
print(f"Errors: {result.error_count}, Warnings: {result.warning_count}")
print(f"Diagnostics: {result.diagnostics}")
# Analyze without standard library (faster)
result = analyze("model.sysml", stdlib=False)
# With custom stdlib path
result = analyze("model.sysml", stdlib_path="/path/to/sysml.library")
Extract Symbols
from systree import get_symbols, export_ast
# Get typed symbol objects
file_symbols = get_symbols("model.sysml", stdlib=False)
for fs in file_symbols:
print(f"File: {fs.path}")
for sym in fs.symbols:
print(f" {sym.kind}: {sym.qualified_name} @ L{sym.start_line}:{sym.start_col}")
if sym.supertypes:
print(f" extends: {sym.supertypes}")
# Get raw AST JSON (for custom processing)
ast_data = export_ast("model.sysml", stdlib=False)
for file_data in ast_data.get("files", []):
print(f"File: {file_data['path']}")
for sym in file_data["symbols"]:
print(f" {sym['kind']}: {sym['qualified_name']}")
Export Formats
from systree import export_xmi, export_jsonld, export_kpar, export_yaml
# Export to XMI (returns XML string)
xmi = export_xmi("model.sysml")
# Export to JSON-LD (returns list of elements)
jsonld = export_jsonld("model.sysml")
# Export to KPAR (returns bytes - ZIP archive)
kpar_bytes = export_kpar("model.sysml")
with open("model.kpar", "wb") as f:
f.write(kpar_bytes)
# Export to YAML (returns YAML string)
yaml_str = export_yaml("model.sysml")
Self-Contained Exports
By default, exports contain only your model. Use self_contained=True to include
the standard library, creating a standalone file with no external dependencies:
# Normal export - just your model (small, but has external references)
xmi = export_xmi("model.sysml") # ~1 KB
# Self-contained - includes stdlib (large, but fully portable)
xmi_full = export_xmi("model.sysml", self_contained=True) # ~3 MB
# Works with all export formats
jsonld = export_jsonld("model.sysml", self_contained=True)
kpar = export_kpar("model.sysml", self_contained=True)
yaml_str = export_yaml("model.sysml", self_contained=True)
### Import Interchange Files
```python
from systree import import_file, import_symbols, import_export, decompile
# Import and validate XMI/KPAR/JSON-LD
result = import_file("model.xmi")
print(f"Imported {result.symbol_count} elements")
# Import and extract symbols
file_symbols = import_symbols("model.xmi")
for fs in file_symbols:
for sym in fs.symbols:
print(f" {sym.kind}: {sym.qualified_name}")
# Direct roundtrip: import -> export (preserves element IDs)
roundtrip_xmi = import_export("model.xmi", "xmi")
# Decompile back to SysML v2 text
sysml_source = decompile("model.xmi")
print(sysml_source)
Error Handling
from systree import analyze, CliNotFoundError, AnalysisError
try:
result = analyze("model.sysml")
except CliNotFoundError:
print("CLI auto-download failed. Install manually: cargo install syster-cli")
except AnalysisError as e:
print(f"Analysis failed: {e}")
API Reference
analyze(path, *, verbose=False, stdlib=True, stdlib_path=None) -> AnalysisResult
Analyze SysML/KerML files.
Returns: AnalysisResult with file_count, symbol_count, error_count, warning_count, diagnostics
get_symbols(path, *, stdlib=True, stdlib_path=None) -> list[FileSymbols]
Extract typed symbol objects from files.
Returns: List of FileSymbols, each containing path and symbols: list[Symbol]
export_ast(path, *, stdlib=True, stdlib_path=None) -> dict
Export raw AST (Abstract Syntax Tree) as JSON. For typed symbol objects, use get_symbols() instead.
Returns: Dict with files key containing list of file data with symbols.
export_xmi(path, *, stdlib=True, stdlib_path=None, self_contained=False) -> str
Export model to XMI XML format.
self_contained: Include standard library in export (default: False)
export_jsonld(path, *, stdlib=True, stdlib_path=None, self_contained=False) -> list | dict
Export model to JSON-LD format.
self_contained: Include standard library in export (default: False)
export_kpar(path, *, stdlib=True, stdlib_path=None, self_contained=False) -> bytes
Export model to KPAR (Kernel Package Archive) format. Returns ZIP bytes.
self_contained: Include standard library in export (default: False)
export_yaml(path, *, stdlib=True, stdlib_path=None, self_contained=False) -> str
Export model to YAML format.
self_contained: Include standard library in export (default: False)
import_file(path, *, stdlib=True, stdlib_path=None) -> AnalysisResult
Import and validate an interchange file (XMI, KPAR, or JSON-LD).
import_symbols(path, *, stdlib=True, stdlib_path=None) -> list[FileSymbols]
Import interchange file and extract typed symbol objects.
import_export(path, format="xmi", *, stdlib=True, stdlib_path=None) -> bytes
Import interchange file and re-export, preserving element IDs. Direct roundtrip without intermediate files.
decompile(path, *, stdlib=True, stdlib_path=None) -> str
Decompile interchange file back to SysML v2 source text.
Models
AnalysisResult
file_count: intsymbol_count: interror_count: intwarning_count: intdiagnostics: list[dict]
Symbol
name: str- Simple namequalified_name: str- Full path (e.g.,"Package::Class")kind: str- Symbol kind ("Package","PartDef", etc.)file: str | None- Source filestart_line: int | None- Start line (1-based)start_col: int | None- Start column (1-based)end_line: int | None- End lineend_col: int | None- End columnsupertypes: list[str]- Supertype names
FileSymbols
path: str- File pathsymbols: list[Symbol]- Symbols in file
Exceptions
SystreeError- Base exceptionCliNotFoundError- syster CLI not installedAnalysisError- Analysis failed
Development
make install-cli # Install syster CLI
make dev # Install with dev deps
make test # Run tests
License
MIT
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 systree-0.4.0a1.tar.gz.
File metadata
- Download URL: systree-0.4.0a1.tar.gz
- Upload date:
- Size: 21.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
361c15b98b7157f275cefe830389dba8993cc61bf520f81be288103bc4036619
|
|
| MD5 |
f8dccc92d251b8397e3d4aae9bf3cc43
|
|
| BLAKE2b-256 |
40e07d394d0d61032ff8eba2451bc40bf2e603526f7c9fa3abaf522d1abe2d32
|
File details
Details for the file systree-0.4.0a1-py3-none-any.whl.
File metadata
- Download URL: systree-0.4.0a1-py3-none-any.whl
- Upload date:
- Size: 9.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
89757bdecf827e894d847286bd2807a5065c21828fa1d0bc2fe4aa695a048231
|
|
| MD5 |
d43a623dfd093e023c14de090591a6a9
|
|
| BLAKE2b-256 |
7038767d6ed70d0b62cd3328c3ce1a836d1ff73046ab2a4a4d5c63dcb5ebead5
|