structurize 2.16.2__py3-none-any.whl → 2.16.5__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.
Files changed (53) hide show
  1. avrotize/__init__.py +63 -63
  2. avrotize/__main__.py +5 -5
  3. avrotize/_version.py +34 -34
  4. avrotize/asn1toavro.py +160 -160
  5. avrotize/avrotize.py +152 -152
  6. avrotize/avrotocpp.py +483 -483
  7. avrotize/avrotocsharp.py +992 -992
  8. avrotize/avrotocsv.py +121 -121
  9. avrotize/avrotodatapackage.py +173 -173
  10. avrotize/avrotodb.py +1383 -1383
  11. avrotize/avrotogo.py +476 -476
  12. avrotize/avrotographql.py +197 -197
  13. avrotize/avrotoiceberg.py +210 -210
  14. avrotize/avrotojava.py +1023 -1023
  15. avrotize/avrotojs.py +250 -250
  16. avrotize/avrotojsons.py +481 -481
  17. avrotize/avrotojstruct.py +345 -345
  18. avrotize/avrotokusto.py +363 -363
  19. avrotize/avrotomd.py +137 -137
  20. avrotize/avrotools.py +168 -168
  21. avrotize/avrotoparquet.py +208 -208
  22. avrotize/avrotoproto.py +358 -358
  23. avrotize/avrotopython.py +622 -622
  24. avrotize/avrotorust.py +435 -435
  25. avrotize/avrotots.py +598 -598
  26. avrotize/avrotoxsd.py +344 -344
  27. avrotize/commands.json +2493 -2433
  28. avrotize/common.py +828 -828
  29. avrotize/constants.py +4 -4
  30. avrotize/csvtoavro.py +131 -131
  31. avrotize/datapackagetoavro.py +76 -76
  32. avrotize/dependency_resolver.py +348 -348
  33. avrotize/jsonstoavro.py +1698 -1698
  34. avrotize/jsonstostructure.py +2642 -2642
  35. avrotize/jstructtoavro.py +878 -878
  36. avrotize/kstructtoavro.py +93 -93
  37. avrotize/kustotoavro.py +455 -455
  38. avrotize/parquettoavro.py +157 -157
  39. avrotize/proto2parser.py +497 -497
  40. avrotize/proto3parser.py +402 -402
  41. avrotize/prototoavro.py +382 -382
  42. avrotize/structuretocsharp.py +2005 -2005
  43. avrotize/structuretojsons.py +498 -498
  44. avrotize/structuretopython.py +772 -772
  45. avrotize/structuretots.py +653 -0
  46. avrotize/xsdtoavro.py +413 -413
  47. {structurize-2.16.2.dist-info → structurize-2.16.5.dist-info}/METADATA +848 -805
  48. structurize-2.16.5.dist-info/RECORD +52 -0
  49. {structurize-2.16.2.dist-info → structurize-2.16.5.dist-info}/licenses/LICENSE +200 -200
  50. structurize-2.16.2.dist-info/RECORD +0 -51
  51. {structurize-2.16.2.dist-info → structurize-2.16.5.dist-info}/WHEEL +0 -0
  52. {structurize-2.16.2.dist-info → structurize-2.16.5.dist-info}/entry_points.txt +0 -0
  53. {structurize-2.16.2.dist-info → structurize-2.16.5.dist-info}/top_level.txt +0 -0
avrotize/avrotographql.py CHANGED
@@ -1,197 +1,197 @@
1
- # coding: utf-8
2
- """
3
- Module to convert Avro schema to a GraphQL schema.
4
- """
5
-
6
- import json
7
- import os
8
- from typing import Dict, List
9
-
10
- from avrotize.common import get_longest_namespace_prefix
11
-
12
- JsonNode = Dict[str, 'JsonNode'] | List['JsonNode'] | str | bool | int | None
13
-
14
- class AvroToGraphQLConverter:
15
- """
16
- Class to convert Avro schema to GraphQL schema.
17
- """
18
-
19
- def __init__(self, avro_schema_path, graphql_schema_path):
20
- """
21
- Initialize the converter with file paths.
22
-
23
- :param avro_schema_path: Path to the Avro schema file.
24
- :param graphql_schema_path: Path to save the GraphQL schema file.
25
- """
26
- self.avro_schema_path = avro_schema_path
27
- self.graphql_schema_path = graphql_schema_path
28
- self.records = {}
29
- self.enums = {}
30
- self.fixeds = {}
31
- self.longest_namespace_prefix = ""
32
-
33
- def convert(self: 'AvroToGraphQLConverter'):
34
- """
35
- Convert Avro schema to GraphQL schema and save to file.
36
- """
37
- with open(self.avro_schema_path, 'r', encoding='utf-8') as file:
38
- avro_schemas: JsonNode = json.load(file)
39
-
40
- self.longest_namespace_prefix = get_longest_namespace_prefix(avro_schemas)
41
-
42
- if isinstance(avro_schemas, dict):
43
- self.extract_named_types(avro_schemas)
44
- elif isinstance(avro_schemas, list):
45
- for schema in avro_schemas:
46
- if isinstance(schema, dict):
47
- self.extract_named_types(schema)
48
- else:
49
- raise ValueError("Expected a single Avro schema as a JSON object, or a list of schema records")
50
-
51
- graphql_content = self.generate_graphql()
52
-
53
- with open(self.graphql_schema_path, "w", encoding="utf-8") as file:
54
- file.write(graphql_content)
55
-
56
- def qualified_name(self, schema: Dict[str, JsonNode]) -> str:
57
- """
58
- Get the full name of a record type.
59
- """
60
- name = str(schema['name'])
61
- namespace = str(schema.get('namespace', ''))
62
- if namespace.startswith(self.longest_namespace_prefix):
63
- namespace = namespace[len(self.longest_namespace_prefix):].strip('.')
64
- return f"{namespace}_{name}" if namespace else name
65
-
66
- def extract_named_types(self, schema: Dict[str, JsonNode]):
67
- """
68
- Extract all named types (records, enums, fixed) from the schema.
69
- """
70
- if isinstance(schema, dict):
71
- if schema['type'] == 'record':
72
- self.records[self.qualified_name(schema)] = schema
73
- elif schema['type'] == 'enum':
74
- self.enums[self.qualified_name(schema)] = schema
75
- elif schema['type'] == 'fixed':
76
- self.fixeds[self.qualified_name(schema)] = schema
77
- if 'fields' in schema and isinstance(schema['fields'], list):
78
- for field in schema['fields']:
79
- if isinstance(field, dict) and 'type' in field and isinstance(field['type'], dict):
80
- self.extract_named_types(field['type'])
81
- if 'items' in schema and isinstance(schema['items'], dict):
82
- self.extract_named_types(schema['items'])
83
- if 'values' in schema and isinstance(schema['values'], dict):
84
- self.extract_named_types(schema['values'])
85
- elif isinstance(schema, list):
86
- for sub_schema in schema:
87
- self.extract_named_types(sub_schema)
88
-
89
- def generate_graphql(self):
90
- """
91
- Generate GraphQL content from the extracted types.
92
-
93
- :return: GraphQL content as a string.
94
- """
95
- graphql = []
96
-
97
- for record in self.records.values():
98
- graphql.append(self.generate_graphql_record(record))
99
-
100
- for enum in self.enums.values():
101
- graphql.append(self.generate_graphql_enum(enum))
102
-
103
- for fixed in self.fixeds.values():
104
- graphql.append(self.generate_graphql_fixed(fixed))
105
-
106
- return "\n".join(graphql)
107
-
108
- def generate_graphql_record(self, record):
109
- """
110
- Generate GraphQL content for a record.
111
-
112
- :param record: Record schema as a dictionary.
113
- :return: GraphQL content as a string.
114
- """
115
- fields = []
116
- for field in record['fields']:
117
- field_type = self.get_graphql_type(field['type'])
118
- if 'null' not in field['type'] and field.get('default') is not None:
119
- field_type = f"{field_type}!"
120
- fields.append(f" {field['name']}: {field_type}")
121
- return f"type {record['name']} {{\n" + "\n".join(fields) + "\n}"
122
-
123
- def generate_graphql_enum(self, enum):
124
- """
125
- Generate GraphQL content for an enum.
126
-
127
- :param enum: Enum schema as a dictionary.
128
- :return: GraphQL content as a string.
129
- """
130
- symbols = [f" {symbol}" for symbol in enum['symbols']]
131
- return f"enum {enum['name']} {{\n" + "\n".join(symbols) + "\n}"
132
-
133
- def generate_graphql_fixed(self, fixed):
134
- """
135
- Generate GraphQL content for a fixed type.
136
-
137
- :param fixed: Fixed schema as a dictionary.
138
- :return: GraphQL content as a string.
139
- """
140
- return f"scalar {fixed['name']}"
141
-
142
- def get_graphql_type(self, avro_type):
143
- """
144
- Get GraphQL type as a string.
145
-
146
- :param avro_type: Avro type as a string or dictionary.
147
- :return: GraphQL type as a string.
148
- """
149
- if isinstance(avro_type, list):
150
- non_null_type = next(t for t in avro_type if t != "null")
151
- return self.get_graphql_type(non_null_type)
152
- if isinstance(avro_type, dict):
153
- if avro_type['type'] == 'array':
154
- return f"[{self.get_graphql_type(avro_type['items'])}]"
155
- if avro_type['type'] == 'map':
156
- return "JSON"
157
- if avro_type['type'] == "record" and self.qualified_name(avro_type) in self.records:
158
- return self.qualified_name(avro_type)
159
- if avro_type['type'] == "enum" and self.qualified_name(avro_type) in self.enums:
160
- return self.qualified_name(avro_type)
161
- if avro_type['type'] == "fixed" and self.qualified_name(avro_type) in self.fixeds:
162
- return self.qualified_name(avro_type)
163
- return self.get_graphql_primitive_type(avro_type['type'])
164
- return self.get_graphql_primitive_type(avro_type)
165
-
166
- def get_graphql_primitive_type(self, avro_type):
167
- """
168
- Map Avro primitive types to GraphQL types.
169
-
170
- :param avro_type: Avro type as a string.
171
- :return: GraphQL type as a string.
172
- """
173
-
174
- type_mapping = {
175
- "string": "String",
176
- "bytes": "String",
177
- "int": "Int",
178
- "long": "Int",
179
- "float": "Float",
180
- "double": "Float",
181
- "boolean": "Boolean",
182
- "null": "String",
183
- "date": "Date",
184
- "timestamp-millis": "DateTime"
185
- }
186
- return type_mapping.get(avro_type, 'String')
187
-
188
-
189
- def convert_avro_to_graphql(avro_schema_path, graphql_schema_path):
190
- """
191
- Convert an Avro schema file to a GraphQL schema file.
192
-
193
- :param avro_schema_path: Path to the Avro schema file.
194
- :param graphql_schema_path: Path to save the GraphQL schema file.
195
- """
196
- converter = AvroToGraphQLConverter(avro_schema_path, graphql_schema_path)
197
- converter.convert()
1
+ # coding: utf-8
2
+ """
3
+ Module to convert Avro schema to a GraphQL schema.
4
+ """
5
+
6
+ import json
7
+ import os
8
+ from typing import Dict, List
9
+
10
+ from avrotize.common import get_longest_namespace_prefix
11
+
12
+ JsonNode = Dict[str, 'JsonNode'] | List['JsonNode'] | str | bool | int | None
13
+
14
+ class AvroToGraphQLConverter:
15
+ """
16
+ Class to convert Avro schema to GraphQL schema.
17
+ """
18
+
19
+ def __init__(self, avro_schema_path, graphql_schema_path):
20
+ """
21
+ Initialize the converter with file paths.
22
+
23
+ :param avro_schema_path: Path to the Avro schema file.
24
+ :param graphql_schema_path: Path to save the GraphQL schema file.
25
+ """
26
+ self.avro_schema_path = avro_schema_path
27
+ self.graphql_schema_path = graphql_schema_path
28
+ self.records = {}
29
+ self.enums = {}
30
+ self.fixeds = {}
31
+ self.longest_namespace_prefix = ""
32
+
33
+ def convert(self: 'AvroToGraphQLConverter'):
34
+ """
35
+ Convert Avro schema to GraphQL schema and save to file.
36
+ """
37
+ with open(self.avro_schema_path, 'r', encoding='utf-8') as file:
38
+ avro_schemas: JsonNode = json.load(file)
39
+
40
+ self.longest_namespace_prefix = get_longest_namespace_prefix(avro_schemas)
41
+
42
+ if isinstance(avro_schemas, dict):
43
+ self.extract_named_types(avro_schemas)
44
+ elif isinstance(avro_schemas, list):
45
+ for schema in avro_schemas:
46
+ if isinstance(schema, dict):
47
+ self.extract_named_types(schema)
48
+ else:
49
+ raise ValueError("Expected a single Avro schema as a JSON object, or a list of schema records")
50
+
51
+ graphql_content = self.generate_graphql()
52
+
53
+ with open(self.graphql_schema_path, "w", encoding="utf-8") as file:
54
+ file.write(graphql_content)
55
+
56
+ def qualified_name(self, schema: Dict[str, JsonNode]) -> str:
57
+ """
58
+ Get the full name of a record type.
59
+ """
60
+ name = str(schema['name'])
61
+ namespace = str(schema.get('namespace', ''))
62
+ if namespace.startswith(self.longest_namespace_prefix):
63
+ namespace = namespace[len(self.longest_namespace_prefix):].strip('.')
64
+ return f"{namespace}_{name}" if namespace else name
65
+
66
+ def extract_named_types(self, schema: Dict[str, JsonNode]):
67
+ """
68
+ Extract all named types (records, enums, fixed) from the schema.
69
+ """
70
+ if isinstance(schema, dict):
71
+ if schema['type'] == 'record':
72
+ self.records[self.qualified_name(schema)] = schema
73
+ elif schema['type'] == 'enum':
74
+ self.enums[self.qualified_name(schema)] = schema
75
+ elif schema['type'] == 'fixed':
76
+ self.fixeds[self.qualified_name(schema)] = schema
77
+ if 'fields' in schema and isinstance(schema['fields'], list):
78
+ for field in schema['fields']:
79
+ if isinstance(field, dict) and 'type' in field and isinstance(field['type'], dict):
80
+ self.extract_named_types(field['type'])
81
+ if 'items' in schema and isinstance(schema['items'], dict):
82
+ self.extract_named_types(schema['items'])
83
+ if 'values' in schema and isinstance(schema['values'], dict):
84
+ self.extract_named_types(schema['values'])
85
+ elif isinstance(schema, list):
86
+ for sub_schema in schema:
87
+ self.extract_named_types(sub_schema)
88
+
89
+ def generate_graphql(self):
90
+ """
91
+ Generate GraphQL content from the extracted types.
92
+
93
+ :return: GraphQL content as a string.
94
+ """
95
+ graphql = []
96
+
97
+ for record in self.records.values():
98
+ graphql.append(self.generate_graphql_record(record))
99
+
100
+ for enum in self.enums.values():
101
+ graphql.append(self.generate_graphql_enum(enum))
102
+
103
+ for fixed in self.fixeds.values():
104
+ graphql.append(self.generate_graphql_fixed(fixed))
105
+
106
+ return "\n".join(graphql)
107
+
108
+ def generate_graphql_record(self, record):
109
+ """
110
+ Generate GraphQL content for a record.
111
+
112
+ :param record: Record schema as a dictionary.
113
+ :return: GraphQL content as a string.
114
+ """
115
+ fields = []
116
+ for field in record['fields']:
117
+ field_type = self.get_graphql_type(field['type'])
118
+ if 'null' not in field['type'] and field.get('default') is not None:
119
+ field_type = f"{field_type}!"
120
+ fields.append(f" {field['name']}: {field_type}")
121
+ return f"type {record['name']} {{\n" + "\n".join(fields) + "\n}"
122
+
123
+ def generate_graphql_enum(self, enum):
124
+ """
125
+ Generate GraphQL content for an enum.
126
+
127
+ :param enum: Enum schema as a dictionary.
128
+ :return: GraphQL content as a string.
129
+ """
130
+ symbols = [f" {symbol}" for symbol in enum['symbols']]
131
+ return f"enum {enum['name']} {{\n" + "\n".join(symbols) + "\n}"
132
+
133
+ def generate_graphql_fixed(self, fixed):
134
+ """
135
+ Generate GraphQL content for a fixed type.
136
+
137
+ :param fixed: Fixed schema as a dictionary.
138
+ :return: GraphQL content as a string.
139
+ """
140
+ return f"scalar {fixed['name']}"
141
+
142
+ def get_graphql_type(self, avro_type):
143
+ """
144
+ Get GraphQL type as a string.
145
+
146
+ :param avro_type: Avro type as a string or dictionary.
147
+ :return: GraphQL type as a string.
148
+ """
149
+ if isinstance(avro_type, list):
150
+ non_null_type = next(t for t in avro_type if t != "null")
151
+ return self.get_graphql_type(non_null_type)
152
+ if isinstance(avro_type, dict):
153
+ if avro_type['type'] == 'array':
154
+ return f"[{self.get_graphql_type(avro_type['items'])}]"
155
+ if avro_type['type'] == 'map':
156
+ return "JSON"
157
+ if avro_type['type'] == "record" and self.qualified_name(avro_type) in self.records:
158
+ return self.qualified_name(avro_type)
159
+ if avro_type['type'] == "enum" and self.qualified_name(avro_type) in self.enums:
160
+ return self.qualified_name(avro_type)
161
+ if avro_type['type'] == "fixed" and self.qualified_name(avro_type) in self.fixeds:
162
+ return self.qualified_name(avro_type)
163
+ return self.get_graphql_primitive_type(avro_type['type'])
164
+ return self.get_graphql_primitive_type(avro_type)
165
+
166
+ def get_graphql_primitive_type(self, avro_type):
167
+ """
168
+ Map Avro primitive types to GraphQL types.
169
+
170
+ :param avro_type: Avro type as a string.
171
+ :return: GraphQL type as a string.
172
+ """
173
+
174
+ type_mapping = {
175
+ "string": "String",
176
+ "bytes": "String",
177
+ "int": "Int",
178
+ "long": "Int",
179
+ "float": "Float",
180
+ "double": "Float",
181
+ "boolean": "Boolean",
182
+ "null": "String",
183
+ "date": "Date",
184
+ "timestamp-millis": "DateTime"
185
+ }
186
+ return type_mapping.get(avro_type, 'String')
187
+
188
+
189
+ def convert_avro_to_graphql(avro_schema_path, graphql_schema_path):
190
+ """
191
+ Convert an Avro schema file to a GraphQL schema file.
192
+
193
+ :param avro_schema_path: Path to the Avro schema file.
194
+ :param graphql_schema_path: Path to save the GraphQL schema file.
195
+ """
196
+ converter = AvroToGraphQLConverter(avro_schema_path, graphql_schema_path)
197
+ converter.convert()