Schema-based class generation
The drm.schema_gen module generates Python entity classes from a YAML
schema. It is used by drm.rdf_schema internally and can also be called
directly with any YAML conforming to the GraphStore.schema_yaml() format.
Usage
Generate from a YAML string:
from drm.schema_gen import generate_classes
yaml_str = """
labels:
Document:
class_name: Document
base_class: Node
properties:
title: string
primary_key: ["id"]
doc: "A cultural document."
Page:
class_name: Page
base_class: WeakNode
properties: {}
primary_key: []
parent: Document
parent_relation: HAS_PAGE
doc: "A page within a document."
relationships: {}
weak_relations: {}
"""
py_source = generate_classes(yaml_str)
with open("drm/entities.py", "w") as f:
f.write(py_source)
Generate from an existing graph schema:
from drm import NetworkXGraph
from drm.schema_gen import generate_classes
g = NetworkXGraph()
# ... load data ...
yaml_str = g.schema_yaml("my_db")
py_source = generate_classes(yaml_str)
PK detection
The generator inspects the primary_key field in the YAML:
primary_key: [](empty) →pk: Optional[Dict[str, Any]] = Noneprimary_key: ["id"](non-empty) →pk: Dict[str, Any](mandatory)
This is essential for ontologies that do not define owl:hasKey: the backend
assigns an internal ID (neo4j_id) that becomes the effective primary key.
Generate Python entity classes from a YAML schema.
Parses the YAML output of GraphStore.schema_yaml() and generates
Python source code with class definitions for every label, relationship,
and weak relation found in the schema.
Example usage:
from drm.networkx_graph import NetworkXGraph
from drm.schema_gen import generate_classes, generate_file
g = NetworkXGraph(persistence_path="my_graph.pkl")
source = generate_classes(g.schema_yaml("my_db"))
# Or write directly to a file:
generate_file(g, "my_db", output_dir="entities/")
Output structure:
from drm.base import Node, WeakNode, Relation, WeakRelation
class Character(Node):
"""Auto-generated from schema."""
def __init__(self, pk, **kwargs):
super().__init__(pk=pk, main_label="Character", **kwargs)
self.house = kwargs.get("house", "")
self.name = kwargs.get("name", "")
class Section(WeakNode):
"""Auto-generated from schema."""
def __init__(self, parent, **kwargs):
super().__init__(parent=parent, main_label="Section",
parent_relation="HAS_SECTION", **kwargs)
self.title = kwargs.get("title", "")
class Knows(Relation):
"""Auto-generated from schema."""
def __init__(self, src, dst, **kwargs):
super().__init__(src=src, dst=dst, rel_type="KNOWS", **kwargs)
class HasSection(WeakRelation):
"""Auto-generated from schema."""
def __init__(self, src, dst, **kwargs):
super().__init__(src=src, dst=dst, rel_type="HAS_SECTION",
propagate=True, **kwargs)
- drm.schema_gen.generate_classes(yaml_source)[source]
Generate Python entity classes from a YAML schema string.
- drm.schema_gen.generate_file(graph, db_name, output_dir, filename=None)[source]
Generate Python entity classes and write them to a file.
- Parameters:
graph (NetworkXGraph) – A graph store instance (NetworkXGraph or Neo4jGraph).
db_name (str) – Database name used for schema introspection.
output_dir (str) – Directory where the .py file will be written.
filename (str | None) – Output filename (default:
entities_{db_name}.py).
- Returns:
The absolute path to the generated file.
- Return type: