Skip to main content

Add your description here

Project description

rdfcodegen

Generate typed code interfaces from RDF/Turtle ontologies (e.g., schema.org).

Currently supports TypeScript output. Additional language generators planned.

In this README :point_down:

Features

rdfcodegen parses RDF/Turtle ontology files (like schema.org) and generates language-specific code with full type definitions, documentation, and inheritance.

:rocket: What it produces:

  • Type-safe interfaces/classes extracted from RDFS/OWL class hierarchies.
  • JSDoc/Docstring comments pulled from rdfs:comment annotations.
  • Automatic dependency resolution — request one class and get all its parent types and field types transitively.
  • Primitive type mapping (e.g., schema:Textstring, schema:Numbernumber).

:computer: Supported output languages:

  • TypeScriptinterface definitions with optional properties (?) and array types.
  • Go (planned)
  • Python (planned)

Installation

pip install rdfcodegen

Requires Python 3.9+ and rdflib.

Usage

Command line

Generate TypeScript interfaces for all schema.org types:

rdfcodegen --ttl https://schema.org/version/latest/schemaorg-current-https.ttl \
           --namespace https://schema.org/ \
           --out schema.ts

Generate only specific classes (and everything they depend on):

rdfcodegen --class schema:Person --class schema:Organization --out schema.ts

Write to stdout (-):

rdfcodegen > schema.ts

CLI options:

Option Default Description
--ttl schema.org/version/latest/schemaorg-current-https.ttl URL or path to Turtle file
--namespace https://schema.org/ Ontology namespace URI
--out - Output file (- for stdout)
--class (all classes) Generate specific classes (repeatable)

Python API

from rdfcodegen import TypescriptGenerator

gen = TypescriptGenerator()

# Generate all types
ts_code = gen.generate(
    ttl_url="https://schema.org/version/latest/schemaorg-current-https.ttl",
    namespace_url="https://schema.org/",
)

# Generate a subset
ts_code = gen.generate(
    chosen=["schema:Person", "schema:Event"],
    ttl_url="https://schema.org/version/latest/schemaorg-current-https.ttl",
    namespace_url="https://schema.org/",
)

with open("schema.ts", "w") as f:
    f.write(ts_code)

Filtering specific classes

When you specify classes with --class (or the chosen parameter), the generator:

  1. Includes the requested classes.
  2. Recursively includes all parent types up the rdfs:subClassOf hierarchy.
  3. Recursively includes all field types referenced via schema:rangeIncludes.
  4. Skips primitive types (e.g., schema:Text, schema:Number) — these map directly to language primitives.

This ensures the generated output is always self-contained and compilable.

How it works

  1. Parse — The Turtle file is loaded into an RDF graph using rdflib.
  2. Discover classes — All subjects typed as rdfs:Class within the target namespace are collected.
  3. Traverse hierarchyrdfs:subClassOf relationships are followed to build the class hierarchy.
  4. Collect propertiesschema:domainIncludes and schema:rangeIncludes are used to determine which fields belong to which classes and what types they return.
  5. Extract documentationrdfs:comment values become JSDoc/docstring comments.
  6. Resolve dependencies — If filtering is requested, a dependency graph walk collects all transitive type references.
  7. Generate output — A language-specific generator (e.g., TypescriptGenerator) emits the final code.

TypeScript output example

From schema:Person, the generator produces:

// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT DIRECTLY.

/**
 * A person (alive, dead, undead, or fictional).
 */
export interface SchemaPerson extends SchemaThing {
  /**
   * An additional name for a Person, can be used for a middle name.
   */
  additionalName?: string[];
  /**
   * Physical address of the item.
   */
  address?: (SchemaPostalAddress | string)[];
  /**
   * Date of birth.
   */
  birthDate?: string[];
  // ... more fields
}

Roadmap

  • Typescript types generator
    • Helper function to generate JSON-LD from those structs
  • Go struct generator with JSON tags
  • Python dataclass/pydantic generator
  • Choose other languages

Contributing

If you find a bug :bug:, please open a bug report. If you have an idea for an improvement or new feature :rocket:, please open a feature request.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rdfcodegen-0.1.0.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rdfcodegen-0.1.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file rdfcodegen-0.1.0.tar.gz.

File metadata

  • Download URL: rdfcodegen-0.1.0.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for rdfcodegen-0.1.0.tar.gz
Algorithm Hash digest
SHA256 73be332b440e64905034d125487a9d90d9981d828f5639e964f1b64a06edaadf
MD5 f16cdeb7b3e351235fd76216b4a41698
BLAKE2b-256 302d89fca865597c864ab6c316ad50839d9c73e27ec7615b1ec44458d8a819d7

See more details on using hashes here.

File details

Details for the file rdfcodegen-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: rdfcodegen-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for rdfcodegen-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f7c3cad5ea3bdd939eac1762ff7cef98eacf9c94a5b34786bb004b3d30b495c9
MD5 4a67d05566c65cf6e5c38f5d2bac1c0c
BLAKE2b-256 25acced92397b57573ce4add6ae4d0b76e6e31422181785fed472db6cde01a22

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page