From 40d4455f4103e6352609ee43d9a1e254352df3ad Mon Sep 17 00:00:00 2001 From: Paul van Genuchten Date: Wed, 17 Sep 2025 17:28:35 +0200 Subject: [PATCH] add a console client --- README.md | 39 ++++++++++++++++++++++++++++++++++++++- csvwlib/csvwcli.py | 41 +++++++++++++++++++++++++++++++++++++++++ setup.py | 6 +++++- 3 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 csvwlib/csvwcli.py diff --git a/README.md b/README.md index 55b0439..019d15e 100644 --- a/README.md +++ b/README.md @@ -18,8 +18,43 @@ Requires Python 3.6 or later. pip install csvwlib ``` +## Usage from console -## Usage +`csvwlib` can be used directly from the console via the `csvwlib` command-line tool (installed automatically with the package). + +### Basic Usage + +```bash +csvwlib [options] +``` + +Convert a CSV file (with optional metadata) into RDF (Turtle): + +```bash +csvwlib -c https://w3c.github.io/csvw/tests/test001.csv -f ttl -o output.ttl +``` + +Convert using **metadata only** (metadata references the csv): + +```bash +csvwlib -m https://w3c.github.io/csvw/tests/test001.json -o table.json +``` + + +### Options + +| Option | Description | +| ----------------- | -------------------------------------------------------------------------- | +| `-c, --csv` | CSV file path or URL | +| `-m, --metadata` | Path or URL to CSVW metadata file | +| `-f, --format` | RDF serialization format (e.g. `ttl`, `xml`, `json-ld`); default: `json-ld`| +| `-o, --output` | Output file path | +| `--mode` | Conversion mode (`standard` or `minimal`); default: `minimal` | +| `-h, --help` | Show usage help | + + + +## Usage as python library The library exposes one class - `CSVWConverter` which has methods `to_json()` and `to_rdf()` @@ -53,6 +88,8 @@ From the `rdflib` docs: If you don't specify the format, you will receive a `rdflib.Graph` object. + + ## Examples Example data+metadata files can be found at diff --git a/csvwlib/csvwcli.py b/csvwlib/csvwcli.py new file mode 100644 index 0000000..67258f3 --- /dev/null +++ b/csvwlib/csvwcli.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 +""" +a command line client for csvwlib +""" + +import argparse +from csvwlib import CSVWConverter +import rdflib + +def convert(csv_path=None, metadata_path=None, mode="standard", format="json-ld"): + # Run csvwlib conversion + graph = CSVWConverter.to_rdf(csv_path, metadata_path, mode=mode) + # Ensure result is an rdflib.Graph + if not isinstance(graph, rdflib.Graph): + raise RuntimeError("csvwlib did not return an rdflib.Graph") + return graph.serialize(format=format) + +def main(): + parser = argparse.ArgumentParser(description="Convert CSV + metadata to JSON-LD with csvwlib") + parser.add_argument("--csv", "-c", help="URL to a CSV file (optional if metadata references it)") + parser.add_argument("--meta", "-m", help="URL to a CSVW metadata JSON") + parser.add_argument("--out", "-o", help="Output RDF file (default: print to stdout)") + parser.add_argument("--mode", default="minimal", choices=["standard", "minimal"], + help="CSVW conversion mode") + parser.add_argument("--format", "-f", default="json-ld", + help="A rdf format to serialize (default: json-ld)", + choices=["json-ld","xml","n3","nt","ttl","trig","nquads"]) + args = parser.parse_args() + + myrdf = convert(args.csv, args.meta, mode=args.mode, format=args.format) + + if args.out: + with open(args.out, "w", encoding="utf-8") as f: + f.write(myrdf) + print(f"✔ Wrote rdf to {args.out}") + else: + print(myrdf) + + +if __name__ == "__main__": + main() diff --git a/setup.py b/setup.py index 0867f64..8d20846 100644 --- a/setup.py +++ b/setup.py @@ -47,7 +47,11 @@ "language-tags >= 0.4.3" ], zip_safe=False, - + entry_points={ + "console_scripts": [ + "csvwlib=csvwlib.csvwcli:main" + ], + }, keywords = ", ".join(KEYWORDS), classifiers = [ "Programming Language :: Python :: 3",