structurize 3.0.2__py3-none-any.whl → 3.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
avrotize/commands.json CHANGED
@@ -997,6 +997,471 @@
997
997
  "suggested_output_file_path": "{input_file_name}.sql",
998
998
  "prompts": []
999
999
  },
1000
+ {
1001
+ "command": "sql2a",
1002
+ "description": "Convert SQL schema to Avrotize schema (connects to live database)",
1003
+ "group": "5_SQL",
1004
+ "function": {
1005
+ "name": "avrotize.sqltoavro.convert_sql_to_avro",
1006
+ "args": {
1007
+ "connection_string": "args.connection_string",
1008
+ "database": "args.database",
1009
+ "table_name": "args.table_name",
1010
+ "avro_namespace": "args.namespace",
1011
+ "avro_schema_file": "output_file_path",
1012
+ "dialect": "args.dialect",
1013
+ "emit_cloudevents": "args.emit_cloudevents",
1014
+ "emit_cloudevents_xregistry": "args.emit_xregistry",
1015
+ "sample_size": "args.sample_size",
1016
+ "infer_json_schema": "args.infer_json",
1017
+ "infer_xml_schema": "args.infer_xml",
1018
+ "username": "args.username",
1019
+ "password": "args.password"
1020
+ }
1021
+ },
1022
+ "extensions": [],
1023
+ "args": [
1024
+ {
1025
+ "name": "input",
1026
+ "type": "str",
1027
+ "nargs": "?",
1028
+ "help": "Not used (database connection is live)",
1029
+ "required": false
1030
+ },
1031
+ {
1032
+ "name": "--out",
1033
+ "type": "str",
1034
+ "help": "Path to the Avrotize schema file",
1035
+ "required": false
1036
+ },
1037
+ {
1038
+ "name": "--connection-string",
1039
+ "type": "str",
1040
+ "help": "Database connection string. Examples: postgresql://host:port/db (credentials via --username/--password), postgresql://user:pass@host:port/db?sslmode=require (PostgreSQL with SSL), mysql://user:pass@host/db?ssl=true (MySQL with SSL), mssql://@host/db (SQL Server with Windows Auth)",
1041
+ "required": true
1042
+ },
1043
+ {
1044
+ "name": "--username",
1045
+ "type": "str",
1046
+ "help": "Database username (overrides credentials in connection string, avoids credentials in command history)",
1047
+ "required": false
1048
+ },
1049
+ {
1050
+ "name": "--password",
1051
+ "type": "str",
1052
+ "help": "Database password (overrides credentials in connection string, avoids credentials in command history)",
1053
+ "required": false
1054
+ },
1055
+ {
1056
+ "name": "--database",
1057
+ "type": "str",
1058
+ "help": "Database name (if not in connection string)",
1059
+ "required": false
1060
+ },
1061
+ {
1062
+ "name": "--table-name",
1063
+ "type": "str",
1064
+ "help": "Specific table name (omit for all tables)",
1065
+ "required": false
1066
+ },
1067
+ {
1068
+ "name": "--namespace",
1069
+ "type": "str",
1070
+ "help": "Namespace for the Avrotize schema",
1071
+ "required": false
1072
+ },
1073
+ {
1074
+ "name": "--dialect",
1075
+ "type": "str",
1076
+ "help": "SQL dialect",
1077
+ "choices": [
1078
+ "postgres",
1079
+ "mysql",
1080
+ "sqlserver",
1081
+ "oracle",
1082
+ "sqlite"
1083
+ ],
1084
+ "default": "postgres",
1085
+ "required": false
1086
+ },
1087
+ {
1088
+ "name": "--emit-cloudevents",
1089
+ "type": "bool",
1090
+ "help": "Emit CloudEvents declarations for tables with type/source/data/id columns",
1091
+ "required": false
1092
+ },
1093
+ {
1094
+ "name": "--emit-xregistry",
1095
+ "type": "bool",
1096
+ "help": "Emit an xRegistry manifest instead of a single Avrotize schema",
1097
+ "required": false
1098
+ },
1099
+ {
1100
+ "name": "--sample-size",
1101
+ "type": "int",
1102
+ "help": "Number of rows to sample for JSON/XML inference",
1103
+ "default": 100,
1104
+ "required": false
1105
+ },
1106
+ {
1107
+ "name": "--infer-json",
1108
+ "type": "bool",
1109
+ "help": "Infer schema for JSON/JSONB columns",
1110
+ "default": true,
1111
+ "required": false
1112
+ },
1113
+ {
1114
+ "name": "--infer-xml",
1115
+ "type": "bool",
1116
+ "help": "Infer schema for XML columns",
1117
+ "default": true,
1118
+ "required": false
1119
+ }
1120
+ ],
1121
+ "suggested_output_file_path": "{database}.avsc",
1122
+ "prompts": [
1123
+ {
1124
+ "name": "--namespace",
1125
+ "message": "Enter the namespace for the Avro schema",
1126
+ "type": "str",
1127
+ "required": false
1128
+ },
1129
+ {
1130
+ "name": "--dialect",
1131
+ "message": "Select the SQL dialect",
1132
+ "choices": [
1133
+ "postgres",
1134
+ "mysql",
1135
+ "sqlserver",
1136
+ "oracle",
1137
+ "sqlite"
1138
+ ],
1139
+ "default": "postgres",
1140
+ "required": true
1141
+ }
1142
+ ]
1143
+ },
1144
+ {
1145
+ "command": "json2a",
1146
+ "description": "Infer Avro schema from JSON files",
1147
+ "group": "6_Inference",
1148
+ "skip_input_file_handling": true,
1149
+ "function": {
1150
+ "name": "avrotize.jsontoschema.convert_json_to_avro",
1151
+ "args": {
1152
+ "input_files": "args.input",
1153
+ "avro_schema_file": "output_file_path",
1154
+ "type_name": "args.type_name",
1155
+ "avro_namespace": "args.namespace",
1156
+ "sample_size": "args.sample_size"
1157
+ }
1158
+ },
1159
+ "extensions": [".json", ".jsonl", ".ndjson"],
1160
+ "args": [
1161
+ {
1162
+ "name": "input",
1163
+ "type": "str",
1164
+ "nargs": "+",
1165
+ "help": "JSON file(s) to analyze for schema inference",
1166
+ "required": true
1167
+ },
1168
+ {
1169
+ "name": "--out",
1170
+ "type": "str",
1171
+ "help": "Path to the output Avro schema file",
1172
+ "required": false
1173
+ },
1174
+ {
1175
+ "name": "--type-name",
1176
+ "type": "str",
1177
+ "help": "Name for the root type",
1178
+ "default": "Document",
1179
+ "required": false
1180
+ },
1181
+ {
1182
+ "name": "--namespace",
1183
+ "type": "str",
1184
+ "help": "Namespace for the Avro schema",
1185
+ "required": false
1186
+ },
1187
+ {
1188
+ "name": "--sample-size",
1189
+ "type": "int",
1190
+ "help": "Maximum number of records to sample (0 = all)",
1191
+ "default": 0,
1192
+ "required": false
1193
+ }
1194
+ ],
1195
+ "suggested_output_file_path": "{input_file_name}.avsc",
1196
+ "prompts": [
1197
+ {
1198
+ "name": "--type-name",
1199
+ "message": "Enter the name for the root type",
1200
+ "type": "str",
1201
+ "default": "Document",
1202
+ "required": false
1203
+ },
1204
+ {
1205
+ "name": "--namespace",
1206
+ "message": "Enter the namespace for the Avro schema",
1207
+ "type": "str",
1208
+ "required": false
1209
+ }
1210
+ ]
1211
+ },
1212
+ {
1213
+ "command": "json2s",
1214
+ "description": "Infer JSON Structure schema from JSON files",
1215
+ "group": "6_Inference",
1216
+ "skip_input_file_handling": true,
1217
+ "function": {
1218
+ "name": "avrotize.jsontoschema.convert_json_to_jstruct",
1219
+ "args": {
1220
+ "input_files": "args.input",
1221
+ "jstruct_schema_file": "output_file_path",
1222
+ "type_name": "args.type_name",
1223
+ "base_id": "args.base_id",
1224
+ "sample_size": "args.sample_size"
1225
+ }
1226
+ },
1227
+ "extensions": [".json", ".jsonl", ".ndjson"],
1228
+ "args": [
1229
+ {
1230
+ "name": "input",
1231
+ "type": "str",
1232
+ "nargs": "+",
1233
+ "help": "JSON file(s) to analyze for schema inference",
1234
+ "required": true
1235
+ },
1236
+ {
1237
+ "name": "--out",
1238
+ "type": "str",
1239
+ "help": "Path to the output JSON Structure schema file",
1240
+ "required": false
1241
+ },
1242
+ {
1243
+ "name": "--type-name",
1244
+ "type": "str",
1245
+ "help": "Name for the root type",
1246
+ "default": "Document",
1247
+ "required": false
1248
+ },
1249
+ {
1250
+ "name": "--base-id",
1251
+ "type": "str",
1252
+ "help": "Base URI for $id generation",
1253
+ "default": "https://example.com/",
1254
+ "required": false
1255
+ },
1256
+ {
1257
+ "name": "--sample-size",
1258
+ "type": "int",
1259
+ "help": "Maximum number of records to sample (0 = all)",
1260
+ "default": 0,
1261
+ "required": false
1262
+ }
1263
+ ],
1264
+ "suggested_output_file_path": "{input_file_name}.jstruct.json",
1265
+ "prompts": [
1266
+ {
1267
+ "name": "--type-name",
1268
+ "message": "Enter the name for the root type",
1269
+ "type": "str",
1270
+ "default": "Document",
1271
+ "required": false
1272
+ },
1273
+ {
1274
+ "name": "--base-id",
1275
+ "message": "Enter the base URI for $id generation",
1276
+ "type": "str",
1277
+ "default": "https://example.com/",
1278
+ "required": false
1279
+ }
1280
+ ]
1281
+ },
1282
+ {
1283
+ "command": "xml2a",
1284
+ "description": "Infer Avro schema from XML files",
1285
+ "group": "6_Inference",
1286
+ "skip_input_file_handling": true,
1287
+ "function": {
1288
+ "name": "avrotize.xmltoschema.convert_xml_to_avro",
1289
+ "args": {
1290
+ "input_files": "args.input",
1291
+ "avro_schema_file": "output_file_path",
1292
+ "type_name": "args.type_name",
1293
+ "avro_namespace": "args.namespace",
1294
+ "sample_size": "args.sample_size"
1295
+ }
1296
+ },
1297
+ "extensions": [".xml"],
1298
+ "args": [
1299
+ {
1300
+ "name": "input",
1301
+ "type": "str",
1302
+ "nargs": "+",
1303
+ "help": "XML file(s) to analyze for schema inference",
1304
+ "required": true
1305
+ },
1306
+ {
1307
+ "name": "--out",
1308
+ "type": "str",
1309
+ "help": "Path to the output Avro schema file",
1310
+ "required": false
1311
+ },
1312
+ {
1313
+ "name": "--type-name",
1314
+ "type": "str",
1315
+ "help": "Name for the root type",
1316
+ "default": "Document",
1317
+ "required": false
1318
+ },
1319
+ {
1320
+ "name": "--namespace",
1321
+ "type": "str",
1322
+ "help": "Namespace for the Avro schema",
1323
+ "required": false
1324
+ },
1325
+ {
1326
+ "name": "--sample-size",
1327
+ "type": "int",
1328
+ "help": "Maximum number of documents to sample (0 = all)",
1329
+ "default": 0,
1330
+ "required": false
1331
+ }
1332
+ ],
1333
+ "suggested_output_file_path": "{input_file_name}.avsc",
1334
+ "prompts": [
1335
+ {
1336
+ "name": "--type-name",
1337
+ "message": "Enter the name for the root type",
1338
+ "type": "str",
1339
+ "default": "Document",
1340
+ "required": false
1341
+ },
1342
+ {
1343
+ "name": "--namespace",
1344
+ "message": "Enter the namespace for the Avro schema",
1345
+ "type": "str",
1346
+ "required": false
1347
+ }
1348
+ ]
1349
+ },
1350
+ {
1351
+ "command": "xml2s",
1352
+ "description": "Infer JSON Structure schema from XML files",
1353
+ "group": "6_Inference",
1354
+ "skip_input_file_handling": true,
1355
+ "function": {
1356
+ "name": "avrotize.xmltoschema.convert_xml_to_jstruct",
1357
+ "args": {
1358
+ "input_files": "args.input",
1359
+ "jstruct_schema_file": "output_file_path",
1360
+ "type_name": "args.type_name",
1361
+ "base_id": "args.base_id",
1362
+ "sample_size": "args.sample_size"
1363
+ }
1364
+ },
1365
+ "extensions": [".xml"],
1366
+ "args": [
1367
+ {
1368
+ "name": "input",
1369
+ "type": "str",
1370
+ "nargs": "+",
1371
+ "help": "XML file(s) to analyze for schema inference",
1372
+ "required": true
1373
+ },
1374
+ {
1375
+ "name": "--out",
1376
+ "type": "str",
1377
+ "help": "Path to the output JSON Structure schema file",
1378
+ "required": false
1379
+ },
1380
+ {
1381
+ "name": "--type-name",
1382
+ "type": "str",
1383
+ "help": "Name for the root type",
1384
+ "default": "Document",
1385
+ "required": false
1386
+ },
1387
+ {
1388
+ "name": "--base-id",
1389
+ "type": "str",
1390
+ "help": "Base URI for $id generation",
1391
+ "default": "https://example.com/",
1392
+ "required": false
1393
+ },
1394
+ {
1395
+ "name": "--sample-size",
1396
+ "type": "int",
1397
+ "help": "Maximum number of documents to sample (0 = all)",
1398
+ "default": 0,
1399
+ "required": false
1400
+ }
1401
+ ],
1402
+ "suggested_output_file_path": "{input_file_name}.jstruct.json",
1403
+ "prompts": [
1404
+ {
1405
+ "name": "--type-name",
1406
+ "message": "Enter the name for the root type",
1407
+ "type": "str",
1408
+ "default": "Document",
1409
+ "required": false
1410
+ },
1411
+ {
1412
+ "name": "--base-id",
1413
+ "message": "Enter the base URI for $id generation",
1414
+ "type": "str",
1415
+ "default": "https://example.com/",
1416
+ "required": false
1417
+ }
1418
+ ]
1419
+ },
1420
+ {
1421
+ "command": "validate",
1422
+ "description": "Validate JSON instances against Avro or JSON Structure schemas",
1423
+ "group": "6_Inference",
1424
+ "skip_input_file_handling": true,
1425
+ "function": {
1426
+ "name": "avrotize.validate.validate",
1427
+ "args": {
1428
+ "input": "args.input",
1429
+ "schema": "args.schema",
1430
+ "schema_type": "args.schema_type",
1431
+ "quiet": "args.quiet"
1432
+ }
1433
+ },
1434
+ "extensions": [".json", ".jsonl"],
1435
+ "args": [
1436
+ {
1437
+ "name": "input",
1438
+ "type": "str",
1439
+ "nargs": "+",
1440
+ "help": "JSON file(s) to validate",
1441
+ "required": true
1442
+ },
1443
+ {
1444
+ "name": "--schema",
1445
+ "type": "str",
1446
+ "help": "Path to schema file (.avsc for Avro, .jstruct.json for JSON Structure)",
1447
+ "required": true
1448
+ },
1449
+ {
1450
+ "name": "--schema-type",
1451
+ "type": "str",
1452
+ "help": "Schema type: 'avro' or 'jstruct'. Auto-detected from file extension if omitted.",
1453
+ "required": false
1454
+ },
1455
+ {
1456
+ "name": "--quiet",
1457
+ "type": "bool",
1458
+ "help": "Suppress output. Exit code 0 if valid, 1 if invalid.",
1459
+ "default": false,
1460
+ "required": false
1461
+ }
1462
+ ],
1463
+ "prompts": []
1464
+ },
1000
1465
  {
1001
1466
  "command": "a2mongo",
1002
1467
  "description": "Convert Avrotize schema to MongoDB schema",
@@ -8,7 +8,7 @@
8
8
  "kafkajs": "^2.2.4"
9
9
  },
10
10
  "devDependencies": {
11
- "@types/node": "^24.10.1",
11
+ "@types/node": "^25.0.3",
12
12
  "typescript": "^5.7.2",
13
13
  "jest": "^30.2.0",
14
14
  "@types/jest": "^30.0.0"
@@ -0,0 +1,151 @@
1
+ """Infers schema from JSON files and converts to Avro or JSON Structure format.
2
+
3
+ This module provides:
4
+ - json2a: Infer Avro schema from JSON files
5
+ - json2s: Infer JSON Structure schema from JSON files
6
+ """
7
+
8
+ import json
9
+ import os
10
+ from typing import Any, Dict, List
11
+
12
+ from avrotize.schema_inference import (
13
+ AvroSchemaInferrer,
14
+ JsonStructureSchemaInferrer,
15
+ JsonNode
16
+ )
17
+
18
+
19
+ def convert_json_to_avro(
20
+ input_files: List[str],
21
+ avro_schema_file: str,
22
+ type_name: str = 'Document',
23
+ avro_namespace: str = '',
24
+ sample_size: int = 0
25
+ ) -> None:
26
+ """Infers Avro schema from JSON files.
27
+
28
+ Reads JSON files, analyzes their structure, and generates an Avro schema
29
+ that can represent all the data. Multiple files are analyzed together to
30
+ produce a unified schema.
31
+
32
+ Args:
33
+ input_files: List of JSON file paths to analyze
34
+ avro_schema_file: Output path for the Avro schema
35
+ type_name: Name for the root type
36
+ avro_namespace: Namespace for generated Avro types
37
+ sample_size: Maximum number of records to sample (0 = all)
38
+ """
39
+ if not input_files:
40
+ raise ValueError("At least one input file is required")
41
+
42
+ values = _load_json_values(input_files, sample_size)
43
+
44
+ if not values:
45
+ raise ValueError("No valid JSON data found in input files")
46
+
47
+ inferrer = AvroSchemaInferrer(namespace=avro_namespace)
48
+ schema = inferrer.infer_from_json_values(type_name, values)
49
+
50
+ # Ensure output directory exists
51
+ output_dir = os.path.dirname(avro_schema_file)
52
+ if output_dir and not os.path.exists(output_dir):
53
+ os.makedirs(output_dir)
54
+
55
+ with open(avro_schema_file, 'w', encoding='utf-8') as f:
56
+ json.dump(schema, f, indent=2)
57
+
58
+
59
+ def convert_json_to_jstruct(
60
+ input_files: List[str],
61
+ jstruct_schema_file: str,
62
+ type_name: str = 'Document',
63
+ base_id: str = 'https://example.com/',
64
+ sample_size: int = 0
65
+ ) -> None:
66
+ """Infers JSON Structure schema from JSON files.
67
+
68
+ Reads JSON files, analyzes their structure, and generates a JSON Structure
69
+ schema that validates with the official JSON Structure SDK.
70
+
71
+ Args:
72
+ input_files: List of JSON file paths to analyze
73
+ jstruct_schema_file: Output path for the JSON Structure schema
74
+ type_name: Name for the root type
75
+ base_id: Base URI for $id generation
76
+ sample_size: Maximum number of records to sample (0 = all)
77
+ """
78
+ if not input_files:
79
+ raise ValueError("At least one input file is required")
80
+
81
+ values = _load_json_values(input_files, sample_size)
82
+
83
+ if not values:
84
+ raise ValueError("No valid JSON data found in input files")
85
+
86
+ inferrer = JsonStructureSchemaInferrer(base_id=base_id)
87
+ schema = inferrer.infer_from_json_values(type_name, values)
88
+
89
+ # Ensure output directory exists
90
+ output_dir = os.path.dirname(jstruct_schema_file)
91
+ if output_dir and not os.path.exists(output_dir):
92
+ os.makedirs(output_dir)
93
+
94
+ with open(jstruct_schema_file, 'w', encoding='utf-8') as f:
95
+ json.dump(schema, f, indent=2)
96
+
97
+
98
+ def _load_json_values(input_files: List[str], sample_size: int) -> List[Any]:
99
+ """Loads JSON values from files.
100
+
101
+ Handles both single JSON documents and JSON Lines (JSONL) files.
102
+ Arrays at the root level are flattened into individual values.
103
+
104
+ Args:
105
+ input_files: List of file paths
106
+ sample_size: Maximum values to load (0 = all)
107
+
108
+ Returns:
109
+ List of parsed JSON values
110
+ """
111
+ values: List[Any] = []
112
+
113
+ for file_path in input_files:
114
+ if sample_size > 0 and len(values) >= sample_size:
115
+ break
116
+
117
+ with open(file_path, 'r', encoding='utf-8') as f:
118
+ content = f.read().strip()
119
+
120
+ if not content:
121
+ continue
122
+
123
+ # Try parsing as a single JSON document first
124
+ try:
125
+ data = json.loads(content)
126
+ if isinstance(data, list):
127
+ # Root-level array: each element is a separate value
128
+ for item in data:
129
+ values.append(item)
130
+ if sample_size > 0 and len(values) >= sample_size:
131
+ break
132
+ else:
133
+ values.append(data)
134
+ continue
135
+ except json.JSONDecodeError:
136
+ pass
137
+
138
+ # Try parsing as JSON Lines (JSONL)
139
+ for line in content.split('\n'):
140
+ line = line.strip()
141
+ if not line:
142
+ continue
143
+ try:
144
+ data = json.loads(line)
145
+ values.append(data)
146
+ if sample_size > 0 and len(values) >= sample_size:
147
+ break
148
+ except json.JSONDecodeError:
149
+ pass
150
+
151
+ return values