Skip to content

Commit b883619

Browse files
committed
Generate service endpoints based on the raml specs
This commit adds the logic to parse the commercetools api specifications to generate the service endpoint modules. Goal of this commit is to keep it backwards compatible and minimize the diff. It can be improved more in the future.
1 parent 70c7e06 commit b883619

18 files changed

+1603
-126
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ runserver:
2020
python -mcommercetools.testing.server
2121

2222
format:
23-
isort --recursive src tests
24-
black src/ tests/
23+
isort --recursive src tests codegen
24+
black src/ tests/ codegen/
2525

2626
release:
2727
pip install twine wheel

codegen/generate_abstract.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
class AbstractModuleGenerator:
77
def __init__(self):
8-
self._import_set: typing.Dict[str, typing.Set[typing.Tuple[str, str]]] = defaultdict(set)
8+
self._import_set: typing.Dict[
9+
str, typing.Set[typing.Tuple[str, str]]
10+
] = defaultdict(set)
911
self._import_nodes: typing.Dict[str, typing.List[ast.AST]] = defaultdict(list)
1012

1113
def add_import_statement(self, source, module_name, *subpackages):
@@ -16,7 +18,7 @@ def add_import_statement(self, source, module_name, *subpackages):
1618
node = ast.ImportFrom(
1719
module=module_name,
1820
names=[ast.alias(name=pkg, asname=None) for pkg in subpackages],
19-
level=0
21+
level=0,
2022
)
2123
else:
2224
node = ast.Import(names=[ast.alias(name=module_name, asname=None)], level=0)

codegen/generate_schema.py

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,9 @@ def build(self) -> typing.Optional[ast.ClassDef]:
264264
targets=[
265265
ast.Subscript(
266266
value=ast.Name(id="data"),
267-
slice=ast.Index(value=ast.Str(s=d_field.attribute_name, kind="str")),
267+
slice=ast.Index(
268+
value=ast.Str(s=d_field.attribute_name, kind="str")
269+
),
268270
)
269271
]
270272
),
@@ -277,7 +279,9 @@ def _create_schema_property(self, prop):
277279
if prop.many:
278280
if node.func.id == "marshmallow.fields.Nested":
279281
node.keywords.append(
280-
ast.keyword(arg="many", value=ast.NameConstant(value=True, kind=None))
282+
ast.keyword(
283+
arg="many", value=ast.NameConstant(value=True, kind=None)
284+
)
281285
)
282286
else:
283287
node = ast.Call(
@@ -288,7 +292,9 @@ def _create_schema_property(self, prop):
288292

289293
if prop.optional:
290294
node.keywords.append(
291-
ast.keyword(arg="missing", value=ast.NameConstant(value=None, kind=None))
295+
ast.keyword(
296+
arg="missing", value=ast.NameConstant(value=None, kind=None)
297+
)
292298
)
293299

294300
if prop.attribute_name != prop.name and not prop.name.startswith("/"):
@@ -331,7 +337,11 @@ def _get_property_field(self, prop):
331337
return ast.Call(
332338
func=ast.Name(id=FIELD_TYPES["object"]),
333339
args=[],
334-
keywords=[ast.keyword(arg="allow_none", value=ast.NameConstant(True, kind=None))],
340+
keywords=[
341+
ast.keyword(
342+
arg="allow_none", value=ast.NameConstant(True, kind=None)
343+
)
344+
],
335345
)
336346
elif prop.type.enum:
337347
self.generator.add_import_statement(
@@ -340,7 +350,9 @@ def _get_property_field(self, prop):
340350
return ast.Call(
341351
func=ast.Name(id="marshmallow_enum.EnumField"),
342352
args=[ast.Attribute(value=ast.Name(id="types"), attr=prop.type.name)],
343-
keywords=[ast.keyword(arg="by_value", value=ast.NameConstant(True, kind=None))],
353+
keywords=[
354+
ast.keyword(arg="by_value", value=ast.NameConstant(True, kind=None))
355+
],
344356
)
345357

346358
elif prop.type.discriminator:
@@ -355,7 +367,11 @@ def _get_property_field(self, prop):
355367
node = ast.Call(
356368
func=ast.Name(id=FIELD_TYPES[prop.type.name]),
357369
args=[],
358-
keywords=[ast.keyword(arg="allow_none", value=ast.NameConstant(True, kind=None))],
370+
keywords=[
371+
ast.keyword(
372+
arg="allow_none", value=ast.NameConstant(True, kind=None)
373+
)
374+
],
359375
)
360376
if prop.type.name == "array":
361377
assert prop.items, f"The array property {prop.name} has no items"
@@ -382,7 +398,11 @@ def _get_property_field(self, prop):
382398
return ast.Call(
383399
func=ast.Name(id=prop.type.name + "Field"),
384400
args=[],
385-
keywords=[ast.keyword(arg="allow_none", value=ast.NameConstant(True, kind=None))],
401+
keywords=[
402+
ast.keyword(
403+
arg="allow_none", value=ast.NameConstant(True, kind=None)
404+
)
405+
],
386406
)
387407

388408
elif prop.type.base and prop.type.base.name == "string":
@@ -412,7 +432,10 @@ def _create_discriminator_field(self, type_obj):
412432
ast.keyword(
413433
arg="discriminator_field",
414434
value=ast.Tuple(
415-
elts=[ast.Str(s=field.name, kind="str"), ast.Str(s=field.attribute_name, kind="str")]
435+
elts=[
436+
ast.Str(s=field.name, kind="str"),
437+
ast.Str(s=field.attribute_name, kind="str"),
438+
]
416439
),
417440
),
418441
ast.keyword(
@@ -447,7 +470,7 @@ def _create_nested_field(self, type_obj):
447470
arg="nested",
448471
value=ast.Str(
449472
s=f"commercetools.schemas.{type_obj.package_name}.{type_obj.name}Schema",
450-
kind="str"
473+
kind="str",
451474
),
452475
),
453476
ast.keyword(arg="unknown", value=ast.Name(id="marshmallow.EXCLUDE")),

0 commit comments

Comments
 (0)