Commit 3786d1bd authored by Janis Streib's avatar Janis Streib

FIX: nullable

parent 64505c4a
......@@ -28,6 +28,16 @@ def cli():
def python(strict):
keyword_replacements = {'class': 'cls'}
def gen_def(p, scope):
if p.__dict__[scope].data_default is not None:
return f'={quote_def(p, p.__dict__[scope])}'
if not p.__dict__[scope].is_required:
return '=None'
return ''
def replace_keywords(k):
for t, s in keyword_replacements.items():
k = k.replace(t, s)
......@@ -61,6 +71,15 @@ import click
class APIObject(object):
\tpass
class ExplicitNull(object):
\tpass
def __denullify_dict__(d):
d = {k:v for k,v in d.items() if v is not None}
for k,v in d.items():
if isinstance(v, ExplicitNull):
d[k] = None
return d
""")
class_bodies = {k: f"""
......@@ -75,7 +94,8 @@ class {v.system}_{v.name}(APIObject):
# generate fields
for k, v in loader.api_objects.items():
for p in v.attributes.values():
class_bodies[k] += f"""\t{replace_keywords(p.name)}: {translate_json_data_type(p.data_type['json_name'])}\n"""
class_bodies[
k] += f"""\t{replace_keywords(p.name)}: {translate_json_data_type(p.data_type['json_name'])}\n"""
# generate __post_init__
for k, v in loader.api_objects.items():
......@@ -97,23 +117,27 @@ class {v.system}_{v.name}(APIObject):
logging.warning(f"'{f.system}.{f.object_type}' not found in object index!")
continue
params = sorted([
f"{replace_keywords(p.name)}_new:{translate_json_data_type(p.data_type['json_name'])}{f'={quote_def(p, p.new)}' if p.new.data_default is not None else ''}"
f"{replace_keywords(p.name)}_new:{translate_json_data_type(p.data_type['json_name'])}{gen_def(p, 'new')}"
for p in f.parameters.values() if p.new is not None],
key=lambda item: 'Z' if '=' in item else 'A')
old_params = sorted([
f"{replace_keywords(p.name)}_old:{translate_json_data_type(p.data_type['json_name'])}{f'={quote_def(p, p.old)}' if p.old.data_default is not None else ''}"
f"{replace_keywords(p.name)}_old:{translate_json_data_type(p.data_type['json_name'])}{gen_def(p, 'old')}"
for p in f.parameters.values() if p.old is not None],
key=lambda item: 'Z' if '=' in item else 'A')
params.extend(old_params)
params = sorted(params,key=lambda item: 'Z' if '=' in item else 'A')
dict_new_params_identity = [f"'{replace_keywords(p.name)}':{replace_keywords(p.name)}_new" for p in f.parameters.values() if p.new is not None]
dict_old_params_identity = [f"'{replace_keywords(p.name)}':{replace_keywords(p.name)}_old" for p in f.parameters.values() if p.old is not None]
new_params_identity = [f"{replace_keywords(p.name)}_new={replace_keywords(p.name)}_new" for p in f.parameters.values() if p.new is not None]
old_params_identity = [f"{replace_keywords(p.name)}_old={replace_keywords(p.name)}_old" for p in f.parameters.values() if p.old is not None]
params = sorted(params, key=lambda item: 'Z' if '=' in item else 'A')
dict_new_params_identity = [f"'{replace_keywords(p.name)}':{replace_keywords(p.name)}_new" for p in
f.parameters.values() if p.new is not None]
dict_old_params_identity = [f"'{replace_keywords(p.name)}':{replace_keywords(p.name)}_old" for p in
f.parameters.values() if p.old is not None]
new_params_identity = [f"{replace_keywords(p.name)}_new={replace_keywords(p.name)}_new" for p in
f.parameters.values() if p.new is not None]
old_params_identity = [f"{replace_keywords(p.name)}_old={replace_keywords(p.name)}_old" for p in
f.parameters.values() if p.old is not None]
class_bodies[f'{f.system}.{f.object_type}'] += f"""
\t@staticmethod
\tdef {f.name}_ta({','.join(params)}) -> dict:
\t\treturn {{'name': '{f.system}.{f.object_type}.{f.name}', 'new': {{{','.join(dict_new_params_identity)}}}, 'old': {{{','.join(dict_old_params_identity)}}}}}
\t\treturn {{'name': '{f.system}.{f.object_type}.{f.name}', 'new': __denullify_dict__({{{','.join(dict_new_params_identity)}}}), 'old': __denullify_dict__({{{','.join(dict_old_params_identity)}}})}}
# TODO: Consider is_returning
\t@classmethod
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment