Skip to content

Commit 6f0f2d6

Browse files
committed
Update import paths to prepare for async
Use relative imports to prepare for generating additional services
1 parent ed8da4e commit 6f0f2d6

File tree

4 files changed

+43
-45
lines changed

4 files changed

+43
-45
lines changed

codegen/generate_services.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@ def get_module_nodes(self):
4040
for module in modules:
4141
self.add_import_statement(module, "typing")
4242

43+
self.add_import_statement(module, ".", "abstract")
44+
self.add_import_statement(module, ".", "traits")
4345
self.add_import_statement(module, "commercetools", "schemas")
4446
self.add_import_statement(module, "commercetools", "types")
45-
self.add_import_statement(module, "commercetools.services", "abstract")
46-
self.add_import_statement(module, "commercetools.services", "traits")
47+
self.add_import_statement(module, "commercetools.helpers", "RemoveEmptyValuesMixin")
4748
self.add_import_statement(module, "commercetools.typing", "OptionalListStr")
4849

4950
all_nodes = (
@@ -508,7 +509,7 @@ def make_serialize_query_params(
508509

509510
bases = [
510511
ast.Name(id="marshmallow.Schema"),
511-
ast.Name(id="abstract.RemoveEmptyValuesMixin"),
512+
ast.Name(id="RemoveEmptyValuesMixin"),
512513
]
513514

514515
schema_node = ast.ClassDef(
@@ -577,14 +578,14 @@ def _generate_init_file(self, modules):
577578
service_name = service.context_name + "Service"
578579
info = modules[module_name]
579580

580-
key = "commercetools.services.%s" % info["name"]
581+
key = ".%s" % info["name"]
581582
submodules[key] = {
582583
"class_name": service.context_name + "Service",
583584
"var_name": snakeit(service.context_name),
584585
}
585586

586587
# Add manual generated files (TODO)
587-
submodules["commercetools.services.project"] = {
588+
submodules[".project"] = {
588589
"class_name": "ProjectService",
589590
"var_name": "project",
590591
}
@@ -668,8 +669,8 @@ def _generate_trait_file(self):
668669
level=0,
669670
),
670671
ast.ImportFrom(
671-
module="commercetools.services",
672-
names=[ast.alias(name="abstract", asname=None)],
672+
module="commercetools.helpers",
673+
names=[ast.alias(name="RemoveEmptyValuesMixin", asname=None)],
673674
level=0,
674675
),
675676
ast.ImportFrom(
@@ -696,7 +697,7 @@ def _create_trait_schema(trait: TraitInfo) -> ast.ClassDef:
696697
name=schema_name,
697698
bases=[
698699
ast.Name(id="marshmallow.Schema"),
699-
ast.Name(id="abstract.RemoveEmptyValuesMixin"),
700+
ast.Name(id="RemoveEmptyValuesMixin"),
700701
],
701702
keywords=[],
702703
decorator_list=[],

src/commercetools/helpers.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import typing
22

3-
from marshmallow import class_registry, fields, missing
3+
from marshmallow import class_registry, fields, missing, post_dump
44
from marshmallow.exceptions import StringNotCollectionError, ValidationError
55
from marshmallow.fields import Field
66
from marshmallow.utils import RAISE, is_collection
@@ -32,6 +32,27 @@ def _serialize(self, nested_obj, attr, obj):
3232
return result
3333

3434

35+
36+
class RemoveEmptyValuesMixin:
37+
@post_dump
38+
def remove_empty_values(self, data, **kwargs):
39+
"""Remove the key, value pairs for which the value is None.
40+
41+
This doesn't work if allow_none is set. And in the future we might also
42+
want to remove values which are already the default to minimise the
43+
params.
44+
45+
"""
46+
result = {}
47+
for k, v in data.items():
48+
if isinstance(v, list):
49+
result[k] = [x for x in v if x is not None]
50+
elif v is not None:
51+
result[k] = v
52+
return result
53+
54+
55+
3556
class RegexField(Field):
3657
def _serialize(self, nested_obj, attr, obj):
3758
result = {}
Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import typing
22

3-
from marshmallow import Schema, fields, post_dump
4-
5-
from commercetools.helpers import OptionalList
6-
73
if typing.TYPE_CHECKING:
84
from commercetools import Client
95

@@ -17,33 +13,3 @@ def _serialize_params(self, params, schema):
1713
if schema not in self._schemas:
1814
self._schemas[schema] = schema()
1915
return self._schemas[schema].dump(params)
20-
21-
22-
class RemoveEmptyValuesMixin:
23-
@post_dump
24-
def remove_empty_values(self, data, **kwargs):
25-
"""Remove the key, value pairs for which the value is None.
26-
27-
This doesn't work if allow_none is set. And in the future we might also
28-
want to remove values which are already the default to minimise the
29-
params.
30-
31-
"""
32-
result = {}
33-
for k, v in data.items():
34-
if v is not None:
35-
result[k] = v
36-
return result
37-
38-
39-
class AbstractDeleteSchema(Schema, RemoveEmptyValuesMixin):
40-
version = fields.Integer()
41-
expand = OptionalList(fields.String())
42-
43-
44-
class AbstractQuerySchema(Schema, RemoveEmptyValuesMixin):
45-
where = OptionalList(fields.String())
46-
sort = OptionalList(fields.String())
47-
expand = OptionalList(fields.String())
48-
limit = fields.Int()
49-
offset = fields.Int()

src/commercetools/testing/abstract.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
import uuid
55

66
import marshmallow
7+
from marshmallow import fields
78
from requests_mock.request import _RequestObjectProxy
89

910
from commercetools import CommercetoolsError, schemas, types
11+
from commercetools.helpers import OptionalList
1012
from commercetools.schemas import BaseResourceSchema
11-
from commercetools.services import abstract
13+
from commercetools.services import traits
1214
from commercetools.testing import utils
1315
from commercetools.testing.predicates import PredicateFilter
1416
from commercetools.testing.utils import create_commercetools_response
@@ -164,13 +166,21 @@ def _matcher(self, request, skip_port_check=False):
164166
return create_commercetools_response(request, status_code=404)
165167

166168

169+
class GenericSchema(traits.QuerySchema, traits.SortableSchema, traits.PagingSchema):
170+
where = OptionalList(fields.String())
171+
sort = OptionalList(fields.String())
172+
expand = OptionalList(fields.String())
173+
limit = fields.Int()
174+
offset = fields.Int()
175+
176+
167177
class ServiceBackend(BaseBackend):
168178
hostnames = ["api.sphere.io", "localhost"]
169179
model_class: typing.Any = None
170180
_schema_draft: typing.Optional[marshmallow.Schema] = None
171181
_schema_update: typing.Optional[marshmallow.Schema] = None
172182
_schema_query_response: typing.Optional[marshmallow.Schema] = None
173-
_schema_query_params: marshmallow.Schema = abstract.AbstractQuerySchema
183+
_schema_query_params: marshmallow.Schema = GenericSchema
174184

175185
_verify_version: bool = True
176186

0 commit comments

Comments
 (0)