-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataconv.py
More file actions
338 lines (276 loc) · 9.83 KB
/
dataconv.py
File metadata and controls
338 lines (276 loc) · 9.83 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/usr/bin/env python3
"""
dataconv — Universal data format converter.
Convert between JSON, YAML, TOML, CSV, and XML with one command.
Replaces jq + yq + csvkit + xml tools with a single Python script.
Usage:
py dataconv.py input.yaml --to json
py dataconv.py input.json --to csv
py dataconv.py input.toml --to yaml
py dataconv.py data.csv --to json
py dataconv.py config.json --to toml
py dataconv.py input.json --to xml
cat data.json | py dataconv.py --from json --to yaml # stdin/stdout
py dataconv.py input.json --to yaml -o output.yaml # output to file
py dataconv.py input.json --query "users[0].name" # jq-lite query
"""
import argparse
import csv
import io
import json
import sys
import xml.etree.ElementTree as ET
from pathlib import Path
try:
import yaml
HAS_YAML = True
except ImportError:
HAS_YAML = False
# Python 3.11+ has tomllib; for writing we need tomli_w or manual
try:
import tomllib
HAS_TOML_READ = True
except ImportError:
try:
import tomli as tomllib
HAS_TOML_READ = True
except ImportError:
HAS_TOML_READ = False
FORMATS = ["json", "yaml", "toml", "csv", "xml"]
# --- Readers ---
def read_json(text: str) -> any:
return json.loads(text)
def read_yaml(text: str) -> any:
if not HAS_YAML:
raise ImportError("pyyaml not installed. Run: pip install pyyaml")
return yaml.safe_load(text)
def read_toml(text: str) -> any:
if not HAS_TOML_READ:
raise ImportError("tomllib not available. Use Python 3.11+ or: pip install tomli")
return tomllib.loads(text)
def read_csv_data(text: str) -> list[dict]:
reader = csv.DictReader(io.StringIO(text))
return list(reader)
def read_xml(text: str) -> dict:
"""Parse XML to dict (simple recursive conversion)."""
root = ET.fromstring(text)
return {root.tag: _xml_element_to_dict(root)}
def _xml_element_to_dict(elem) -> dict | str:
result = {}
if elem.attrib:
result["@attributes"] = dict(elem.attrib)
children = list(elem)
if not children:
text = (elem.text or "").strip()
if result:
if text:
result["#text"] = text
return result
return text
for child in children:
child_data = _xml_element_to_dict(child)
if child.tag in result:
if not isinstance(result[child.tag], list):
result[child.tag] = [result[child.tag]]
result[child.tag].append(child_data)
else:
result[child.tag] = child_data
return result
# --- Writers ---
def write_json(data: any, indent: int = 2) -> str:
return json.dumps(data, indent=indent, ensure_ascii=False, default=str)
def write_yaml(data: any) -> str:
if not HAS_YAML:
raise ImportError("pyyaml not installed. Run: pip install pyyaml")
return yaml.dump(data, default_flow_style=False, allow_unicode=True, sort_keys=False)
def write_toml(data: dict) -> str:
"""Simple TOML writer (handles common cases)."""
lines = []
_write_toml_section(data, lines, [])
return "\n".join(lines) + "\n"
def _write_toml_section(data: dict, lines: list, path: list):
# Write simple key-values first, then tables
simple = {}
tables = {}
for k, v in data.items():
if isinstance(v, dict):
tables[k] = v
elif isinstance(v, list) and v and isinstance(v[0], dict):
tables[k] = v
else:
simple[k] = v
for k, v in simple.items():
lines.append(f"{k} = {_toml_value(v)}")
for k, v in tables.items():
if isinstance(v, list):
for item in v:
lines.append(f"\n[[{'.'.join(path + [k])}]]")
_write_toml_section(item, lines, path + [k])
else:
lines.append(f"\n[{'.'.join(path + [k])}]")
_write_toml_section(v, lines, path + [k])
def _toml_value(v) -> str:
if isinstance(v, str):
return f'"{v}"'
elif isinstance(v, bool):
return "true" if v else "false"
elif isinstance(v, (int, float)):
return str(v)
elif isinstance(v, list):
return "[" + ", ".join(_toml_value(i) for i in v) + "]"
else:
return f'"{v}"'
def write_csv(data: any) -> str:
"""Convert data to CSV. Expects list of dicts or list of lists."""
if not isinstance(data, list):
if isinstance(data, dict):
# Single dict: convert to list of one-row
data = [data]
else:
raise ValueError("CSV output requires a list of records")
if not data:
return ""
output = io.StringIO()
if isinstance(data[0], dict):
fieldnames = list(data[0].keys())
writer = csv.DictWriter(output, fieldnames=fieldnames)
writer.writeheader()
for row in data:
writer.writerow({k: str(v) for k, v in row.items()})
else:
writer = csv.writer(output)
for row in data:
writer.writerow(row)
return output.getvalue()
def write_xml(data: any, root_tag: str = "root") -> str:
"""Convert dict/list to XML string."""
if isinstance(data, dict):
if len(data) == 1:
root_tag = list(data.keys())[0]
data = data[root_tag]
root = _dict_to_xml_element(root_tag, data)
ET.indent(root, space=" ")
return ET.tostring(root, encoding="unicode", xml_declaration=True) + "\n"
def _dict_to_xml_element(tag: str, data) -> ET.Element:
elem = ET.Element(tag)
if isinstance(data, dict):
for k, v in data.items():
if k == "@attributes":
for ak, av in v.items():
elem.set(ak, str(av))
elif k == "#text":
elem.text = str(v)
elif isinstance(v, list):
for item in v:
child = _dict_to_xml_element(k, item)
elem.append(child)
else:
child = _dict_to_xml_element(k, v)
elem.append(child)
elif isinstance(data, list):
for item in data:
child = _dict_to_xml_element("item", item)
elem.append(child)
else:
elem.text = str(data)
return elem
# --- Query (jq-lite) ---
def query_data(data: any, query: str) -> any:
"""Simple dot-notation query: 'users[0].name' or 'config.database.host'"""
parts = []
for part in query.replace("]", "").split("."):
if "[" in part:
key, idx = part.split("[")
if key:
parts.append(key)
parts.append(int(idx))
else:
parts.append(part)
result = data
for part in parts:
if isinstance(part, int):
result = result[part]
elif isinstance(result, dict):
result = result[part]
else:
raise KeyError(f"Cannot access '{part}' on {type(result).__name__}")
return result
# --- Format detection ---
def detect_format(filepath: str) -> str | None:
ext = Path(filepath).suffix.lower()
mapping = {
".json": "json", ".yaml": "yaml", ".yml": "yaml",
".toml": "toml", ".csv": "csv", ".xml": "xml",
}
return mapping.get(ext)
# --- Main ---
def main():
parser = argparse.ArgumentParser(
description="dataconv -- universal data format converter (JSON/YAML/TOML/CSV/XML)"
)
parser.add_argument("input", nargs="?", help="Input file (or - for stdin)")
parser.add_argument("--to", "-t", choices=FORMATS, help="Output format")
parser.add_argument("--from", "-f", dest="from_fmt", choices=FORMATS, help="Input format (auto-detected from extension)")
parser.add_argument("--output", "-o", help="Output file (default: stdout)")
parser.add_argument("--query", "-q", help="Query expression (dot notation, e.g. 'users[0].name')")
parser.add_argument("--compact", "-c", action="store_true", help="Compact JSON output (no indentation)")
args = parser.parse_args()
# Read input
if args.input and args.input != "-":
filepath = Path(args.input)
if not filepath.exists():
print(f"Error: {filepath} not found", file=sys.stderr)
sys.exit(1)
text = filepath.read_text(encoding="utf-8")
in_fmt = args.from_fmt or detect_format(str(filepath))
else:
text = sys.stdin.read()
in_fmt = args.from_fmt
if not in_fmt:
print("Error: specify --from format when reading from stdin", file=sys.stderr)
sys.exit(1)
if not in_fmt:
print("Error: cannot detect input format. Use --from to specify.", file=sys.stderr)
sys.exit(1)
# Parse
readers = {"json": read_json, "yaml": read_yaml, "toml": read_toml, "csv": read_csv_data, "xml": read_xml}
try:
data = readers[in_fmt](text)
except Exception as e:
print(f"Error parsing {in_fmt}: {e}", file=sys.stderr)
sys.exit(1)
# Query
if args.query:
try:
data = query_data(data, args.query)
except (KeyError, IndexError, TypeError) as e:
print(f"Query error: {e}", file=sys.stderr)
sys.exit(1)
# Determine output format
out_fmt = args.to
if not out_fmt:
if args.output:
out_fmt = detect_format(args.output)
if not out_fmt:
out_fmt = "json" # default
# Write
writers = {
"json": lambda d: write_json(d, indent=0 if args.compact else 2),
"yaml": write_yaml,
"toml": write_toml,
"csv": write_csv,
"xml": write_xml,
}
try:
output = writers[out_fmt](data)
except Exception as e:
print(f"Error converting to {out_fmt}: {e}", file=sys.stderr)
sys.exit(1)
# Output
if args.output:
Path(args.output).write_text(output, encoding="utf-8")
print(f"Written to {args.output}", file=sys.stderr)
else:
print(output, end="")
if __name__ == "__main__":
main()