-
Notifications
You must be signed in to change notification settings - Fork 5
/
cli.py
173 lines (147 loc) · 4.67 KB
/
cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Examples code for using `pynock`
"""
from icecream import ic # type: ignore
import cloudpathlib
import pyarrow.parquet as pq # type: ignore
import typer
from pynock import Partition
APP = typer.Typer()
@APP.command("load-parq")
def cli_load_parq (
*,
load_parq: str = typer.Option(..., "--file", "-f", help="input Parquet file"),
save_csv: str = typer.Option(None, "--save-csv", help="output as CSV"),
save_rdf: str = typer.Option(None, "--save-rdf", help="output as RDF"),
rdf_format: str = typer.Option("ttl", "--format", help="RDF format: ttl, rdf, jsonld, etc."),
encoding: str = typer.Option("utf-8", "--encoding", help="output encoding"),
dump: bool = typer.Option(False, "--dump", help="dump the data, only"),
sort: bool = typer.Option(False, "--sort", help="sort the output"),
debug: bool = False,
) -> None:
"""
Load a Parquet file into a graph partition, optionally converting and
saving to different formats.
"""
part: Partition = Partition(
part_id = 0,
)
parq_file: pq.ParquetFile = pq.ParquetFile(load_parq)
# in this case, only print what Parquet has parsed then quit
if dump:
part.dump_parquet(parq_file)
return
part.parse_rows(
part.iter_load_parquet(
parq_file,
debug = debug,
),
debug = debug,
)
if debug:
ic(part)
# next, handle the output options
if save_csv is not None:
part.save_file_csv(
cloudpathlib.AnyPath(save_csv),
encoding = encoding,
sort = sort,
debug = debug,
)
if save_rdf is not None:
part.save_file_rdf(
cloudpathlib.AnyPath(save_rdf),
rdf_format = rdf_format,
encoding = encoding,
sort = sort,
debug = debug,
)
@APP.command("load-csv")
def cli_load_csv (
*,
load_csv: str = typer.Option(..., "--file", "-f", help="input CSV file"),
save_parq: str = typer.Option(None, "--save-parq", help="output as Parquet"),
save_rdf: str = typer.Option(None, "--save-rdf", help="output as RDF"),
rdf_format: str = typer.Option("ttl", "--format", help="RDF format: ttl, rdf, jsonld, etc."),
encoding: str = typer.Option("utf-8", "--encoding", help="output encoding"),
sort: bool = typer.Option(False, "--sort", help="sort the output"),
debug: bool = False,
) -> None:
"""
Load a CSV file into a graph partition, optionally converting and
saving to different formats.
"""
part: Partition = Partition(
part_id = 0,
)
part.parse_rows(
part.iter_load_csv(
cloudpathlib.AnyPath(load_csv),
encoding = encoding,
debug = debug,
),
debug = debug,
)
if debug:
ic(part)
# next, handle the output options
if save_parq is not None:
part.save_file_parquet(
cloudpathlib.AnyPath(save_parq),
sort = sort,
debug = debug,
)
if save_rdf is not None:
part.save_file_rdf(
cloudpathlib.AnyPath(save_rdf),
rdf_format = rdf_format,
encoding = encoding,
sort = sort,
debug = debug,
)
@APP.command("load-rdf")
def cli_load_rdf (
*,
load_rdf: str = typer.Option(..., "--file", "-f", help="input RDF file"),
rdf_format: str = typer.Option("ttl", "--format", help="RDF format: ttl, rdf, jsonld, etc."),
save_parq: str = typer.Option(None, "--save-parq", help="output as Parquet"),
save_csv: str = typer.Option(None, "--save-csv", help="output as CSV"),
encoding: str = typer.Option("utf-8", "--encoding", help="output encoding"),
sort: bool = typer.Option(False, "--sort", help="sort the output"),
debug: bool = False,
) -> None:
"""
Load an RDF file into a graph partition, optionally converting and
saving to different formats.
"""
part: Partition = Partition(
part_id = 0,
)
part.parse_rows(
part.iter_load_rdf(
cloudpathlib.AnyPath(load_rdf),
rdf_format = rdf_format,
encoding = encoding,
debug = debug,
),
)
if debug:
ic(part)
# next, handle the output options
if save_parq is not None:
part.save_file_parquet(
cloudpathlib.AnyPath(save_parq),
sort = sort,
debug = debug,
)
if save_csv is not None:
part.save_file_csv(
cloudpathlib.AnyPath(save_csv),
encoding = encoding,
sort = sort,
debug = debug,
)
if __name__ == "__main__":
APP()