Skip to content

Commit 3448a9b

Browse files
committed
add a console client
1 parent 7fe1b13 commit 3448a9b

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

README.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,43 @@ Requires Python 3.6 or later.
1818
pip install csvwlib
1919
```
2020

21+
## Usage from console
2122

22-
## Usage
23+
`csvwlib` can be used directly from the console via the `csvwlib` command-line tool (installed automatically with the package).
24+
25+
### Basic Usage
26+
27+
```bash
28+
csvwlib [options]
29+
```
30+
31+
Convert a CSV file (with optional metadata) into RDF (Turtle):
32+
33+
```bash
34+
csvwlib -c https://w3c.github.io/csvw/tests/test001.csv -f ttl -o output.ttl
35+
```
36+
37+
Convert using **metadata only** (metadata references the csv):
38+
39+
```bash
40+
csvwlib -m https://w3c.github.io/csvw/tests/test001.json -o table.json
41+
```
42+
43+
44+
### Options
45+
46+
| Option | Description |
47+
| ----------------- | -------------------------------------------------------------------------- |
48+
| `-c, --csv` | CSV file path or URL |
49+
| `-m, --metadata` | Path or URL to CSVW metadata file |
50+
| `-f, --format` | RDF serialization format (e.g. `ttl`, `xml`, `json-ld`); default: `json-ld`|
51+
| `-o, --output` | Output file path |
52+
| `--mode` | Conversion mode (`standard` or `minimal`); default: `minimal` |
53+
| `-h, --help` | Show usage help |
54+
55+
56+
57+
## Usage as python library
2358

2459
The library exposes one class - `CSVWConverter` which has methods `to_json()` and `to_rdf()`
2560

@@ -53,6 +88,8 @@ From the `rdflib` docs:
5388
If you don't specify the format, you will receive a `rdflib.Graph` object.
5489

5590

91+
92+
5693
## Examples
5794

5895
Example data+metadata files can be found at

csvwlib/csvwcli.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env python3
2+
"""
3+
a command line client for csvwlib
4+
"""
5+
6+
import argparse
7+
from csvwlib import CSVWConverter
8+
import rdflib
9+
10+
def convert(csv_path=None, metadata_path=None, mode="standard", format="json-ld"):
11+
# Run csvwlib conversion
12+
graph = CSVWConverter.to_rdf(csv_path, metadata_path, mode=mode)
13+
# Ensure result is an rdflib.Graph
14+
if not isinstance(graph, rdflib.Graph):
15+
raise RuntimeError("csvwlib did not return an rdflib.Graph")
16+
return graph.serialize(format=format)
17+
18+
def main():
19+
parser = argparse.ArgumentParser(description="Convert CSV + metadata to JSON-LD with csvwlib")
20+
parser.add_argument("--csv", "-c", help="Path or URL to CSV file (optional if metadata references it)")
21+
parser.add_argument("--meta", "-m", help="Path or URL to CSVW metadata JSON")
22+
parser.add_argument("--out", "-o", help="Output JSON-LD file (default: print to stdout)")
23+
parser.add_argument("--mode", default="minimal", choices=["standard", "minimal"],
24+
help="CSVW conversion mode")
25+
parser.add_argument("--format", "-f", default="json-ld", help="A rdf format to serialize",
26+
choices=["json-ld","xml","n3","nt","ttl","trig","nquads"])
27+
args = parser.parse_args()
28+
29+
myrdf = convert(args.csv, args.meta, mode=args.mode, format=args.format)
30+
31+
if args.out:
32+
with open(args.out, "w", encoding="utf-8") as f:
33+
f.write(myrdf)
34+
print(f"✔ Wrote rdf to {args.out}")
35+
else:
36+
print(myrdf)
37+
38+
39+
if __name__ == "__main__":
40+
main()

setup.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@
4747
"language-tags >= 0.4.3"
4848
],
4949
zip_safe=False,
50-
50+
entry_points={
51+
"console_scripts": [
52+
"csvwlib=csvwlib.csvwcli:main"
53+
],
54+
},
5155
keywords = ", ".join(KEYWORDS),
5256
classifiers = [
5357
"Programming Language :: Python :: 3",

0 commit comments

Comments
 (0)