change python client generator for hydra

This commit is contained in:
TuxCoder 2022-02-19 10:22:21 +01:00
parent c15be406ea
commit 4e9fd55093
128 changed files with 16275 additions and 11 deletions

View file

@ -0,0 +1,3 @@
{% macro transform_header(property, source, destination) %}
{{ destination }} = "true" if {{ source }} else "false"
{% endmacro %}

View file

@ -0,0 +1,35 @@
{% macro construct_function(property, source) %}
isoparse({{ source }}).date()
{% endmacro %}
{% from "property_templates/property_macros.py.jinja" import construct_template %}
{% macro construct(property, source, initial_value=None) %}
{{ construct_template(construct_function, property, source, initial_value=initial_value) }}
{% endmacro %}
{% macro check_type_for_construct(property, source) %}isinstance({{ source }}, str){% endmacro %}
{% macro transform(property, source, destination, declare_type=True, multipart=False) %}
{% set transformed = source + ".isoformat()" %}
{% if multipart %}{# Multipart data must be bytes, not str #}
{% set transformed = transformed + ".encode()" %}
{% endif %}
{% if property.required %}
{{ destination }} = {{ transformed }} {% if property.nullable %}if {{ source }} else None {%endif%}
{% else %}
{% if declare_type %}
{% set type_annotation = property.get_type_string(json=True) %}
{% if multipart %}{% set type_annotation = type_annotation | replace("str", "bytes") %}{% endif %}
{{ destination }}: {{ type_annotation }} = UNSET
{% else %}
{{ destination }} = UNSET
{% endif %}
if not isinstance({{ source }}, Unset):
{% if property.nullable %}
{{ destination }} = {{ transformed }} if {{ source }} else None
{% else %}
{{ destination }} = {{ transformed }}
{% endif %}
{% endif %}
{% endmacro %}

View file

@ -0,0 +1,39 @@
{% macro construct_function(property, source) %}
isoparse({{ source }})
{% endmacro %}
{% from "property_templates/property_macros.py.jinja" import construct_template %}
{% macro construct(property, source, initial_value=None) %}
{{ construct_template(construct_function, property, source, initial_value=initial_value) }}
{% endmacro %}
{% macro check_type_for_construct(property, source) %}isinstance({{ source }}, str){% endmacro %}
{% macro transform(property, source, destination, declare_type=True, multipart=False) %}
{% set transformed = source + ".isoformat()" %}
{% if multipart %}{# Multipart data must be bytes, not str #}
{% set transformed = transformed + ".encode()" %}
{% endif %}
{% if property.required %}
{% if property.nullable %}
{{ destination }} = {{ transformed }} if {{ source }} else None
{% else %}
{{ destination }} = {{ transformed }}
{% endif %}
{% else %}
{% if declare_type %}
{% set type_annotation = property.get_type_string(json=True) %}
{% if multipart %}{% set type_annotation = type_annotation | replace("str", "bytes") %}{% endif %}
{{ destination }}: {{ type_annotation }} = UNSET
{% else %}
{{ destination }} = UNSET
{% endif %}
if not isinstance({{ source }}, Unset):
{% if property.nullable %}
{{ destination }} = {{ transformed }} if {{ source }} else None
{% else %}
{{ destination }} = {{ transformed }}
{% endif %}
{% endif %}
{% endmacro %}

View file

@ -0,0 +1,35 @@
{% macro construct_function(property, source) %}
{{ property.class_info.name }}({{ source }})
{% endmacro %}
{% from "property_templates/property_macros.py.jinja" import construct_template %}
{% macro construct(property, source, initial_value=None) %}
{{ construct_template(construct_function, property, source, initial_value=initial_value) }}
{% endmacro %}
{% macro check_type_for_construct(property, source) %}isinstance({{ source }}, {{ property.value_type.__name__ }}){% endmacro %}
{% macro transform(property, source, destination, declare_type=True, multipart=False) %}
{% set transformed = source + ".value" %}
{% set type_string = property.get_type_string(json=True) %}
{% if multipart %}
{% set transformed = "(None, str(" + transformed + ").encode(), \"text/plain\")" %}
{% set type_string = "Union[Unset, Tuple[None, bytes, str]]" %}
{% endif %}
{% if property.required %}
{% if property.nullable %}
{{ destination }} = {{ transformed }} if {{ source }} else None
{% else %}
{{ destination }} = {{ transformed }}
{% endif %}
{% else %}
{{ destination }}{% if declare_type %}: {{ type_string }}{% endif %} = UNSET
if not isinstance({{ source }}, Unset):
{% if property.nullable %}
{{ destination }} = {{ transformed }} if {{ source }} else None
{% else %}
{{ destination }} = {{ transformed }}
{% endif %}
{% endif %}
{% endmacro %}

View file

@ -0,0 +1,31 @@
{% macro construct_function(property, source) %}
File(
payload = BytesIO({{ source }})
)
{% endmacro %}
{% from "property_templates/property_macros.py.jinja" import construct_template %}
{% macro construct(property, source, initial_value=None) %}
{{ construct_template(construct_function, property, source, initial_value=initial_value) }}
{% endmacro %}
{% macro check_type_for_construct(property, source) %}isinstance({{ source }}, bytes){% endmacro %}
{% macro transform(property, source, destination, declare_type=True, multipart=False) %}
{% if property.required %}
{% if property.nullable %}
{{ destination }} = {{ source }}.to_tuple() if {{ source }} else None
{% else %}
{{ destination }} = {{ source }}.to_tuple()
{% endif %}
{% else %}
{{ destination }}{% if declare_type %}: {{ property.get_type_string(json=True) }}{% endif %} = UNSET
if not isinstance({{ source }}, Unset):
{% if property.nullable %}
{{ destination }} = {{ source }}.to_tuple() if {{ source }} else None
{% else %}
{{ destination }} = {{ source }}.to_tuple()
{% endif %}
{% endif %}
{% endmacro %}

View file

@ -0,0 +1,3 @@
{% macro transform_header(property, source, destination) %}
{{ destination }} = str({{ source }})
{% endmacro %}

View file

@ -0,0 +1,18 @@
{% macro guarded_statement(property, source, statement) %}
{# If the property can be UNSET or None, this macro returns the provided statement guarded by an if which will check
for those invalid values. Otherwise, it returns the statement unmodified. #}
{% if property.required and not property.nullable %}
{{ statement }}
{% else %}
{% if property.nullable and not property.required %}
if not isinstance({{ source }}, Unset) and {{ source }} is not None:
{{ statement }}
{% elif property.nullable %}
if {{ source }} is not None:
{{ statement }}
{% else %}
if not isinstance({{ source }}, Unset):
{{ statement }}
{% endif %}
{% endif %}
{% endmacro %}

View file

@ -0,0 +1,3 @@
{% macro transform_header(property, source, destination) %}
{{ destination }} = str({{ source }})
{% endmacro %}

View file

@ -0,0 +1,77 @@
{% macro construct(property, source, initial_value="[]") %}
{% set inner_property = property.inner_property %}
{% import "property_templates/" + inner_property.template as inner_template %}
{% if inner_template.construct %}
{% set inner_source = inner_property.python_name + "_data" %}
{{ property.python_name }} = {{ initial_value }}
_{{ property.python_name }} = {{ source }}
{% if property.required and not property.nullable %}
for {{ inner_source }} in (_{{ property.python_name }}):
{% else %}
for {{ inner_source }} in (_{{ property.python_name }} or []):
{% endif %}
{{ inner_template.construct(inner_property, inner_source) | indent(4) }}
{{ property.python_name }}.append({{ inner_property.python_name }})
{% else %}
{{ property.python_name }} = cast({{ property.get_type_string(no_optional=True) }}, {{ source }})
{% endif %}
{% endmacro %}
{% macro _transform(property, source, destination, multipart, transform_method) %}
{% set inner_property = property.inner_property %}
{% if multipart %}
{% set multipart_destination = destination %}
{% set destination = "_temp_" + destination %}
{% endif %}
{% import "property_templates/" + inner_property.template as inner_template %}
{% if inner_template.transform %}
{% set inner_source = inner_property.python_name + "_data" %}
{{ destination }} = []
for {{ inner_source }} in {{ source }}:
{{ inner_template.transform(inner_property, inner_source, inner_property.python_name, transform_method) | indent(4) }}
{{ destination }}.append({{ inner_property.python_name }})
{% else %}
{{ destination }} = {{ source }}
{% endif %}
{% if multipart %}
{{ multipart_destination }} = (None, json.dumps({{ destination }}).encode(), 'application/json')
{% endif %}
{% endmacro %}
{% macro check_type_for_construct(property, source) %}isinstance({{ source }}, list){% endmacro %}
{% macro transform(property, source, destination, declare_type=True, multipart=False, transform_method="to_dict") %}
{% set inner_property = property.inner_property %}
{% if multipart %}
{% set type_string = "Union[Unset, Tuple[None, bytes, str]]" %}
{% else %}
{% set type_string = property.get_type_string(json=True) %}
{% endif %}
{% if property.required %}
{% if property.nullable %}
if {{ source }} is None:
{{ destination }} = None
else:
{{ _transform(property, source, destination, multipart, transform_method) | indent(4) }}
{% else %}
{{ _transform(property, source, destination, multipart, transform_method) }}
{% endif %}
{% else %}
{{ destination }}{% if declare_type %}: {{ type_string }}{% endif %} = UNSET
if not isinstance({{ source }}, Unset):
{% if property.nullable %}
if {{ source }} is None:
{{ destination }} = None
else:
{{ _transform(property, source, destination, multipart, transform_method) | indent(8)}}
{% else %}
{{ _transform(property, source, destination, multipart, transform_method) | indent(4)}}
{% endif %}
{% endif %}
{% endmacro %}
{% macro transform_multipart(property, source, destination) %}
{{ transform(property, source, destination, transform_method="to_multipart") }}
{% endmacro %}

View file

@ -0,0 +1,40 @@
{% macro construct_function(property, source) %}
{{ property.class_info.name }}.from_dict({{ source }})
{% endmacro %}
{% from "property_templates/property_macros.py.jinja" import construct_template %}
{% macro construct(property, source, initial_value=None) %}
{{ construct_template(construct_function, property, source, initial_value=initial_value) }}
{% endmacro %}
{% macro check_type_for_construct(property, source) %}isinstance({{ source }}, dict){% endmacro %}
{% macro transform(property, source, destination, declare_type=True, multipart=False, transform_method="to_dict") %}
{% set transformed = source + "." + transform_method + "()" %}
{% if multipart %}
{% set transformed = "(None, json.dumps(" + transformed + ").encode(), 'application/json')" %}
{% set type_string = "Union[Unset, Tuple[None, bytes, str]]" %}
{% else %}
{% set type_string = property.get_type_string(json=True) %}
{% endif %}
{% if property.required %}
{% if property.nullable %}
{{ destination }} = {{ transformed }} if {{ source }} else None
{% else %}
{{ destination }} = {{ transformed }}
{% endif %}
{% else %}
{{ destination }}{% if declare_type %}: {{ type_string }}{% endif %} = UNSET
if not isinstance({{ source }}, Unset):
{% if property.nullable %}
{{ destination }} = {{ transformed }} if {{ source }} else None
{% else %}
{{ destination }} = {{ transformed }}
{% endif %}
{% endif %}
{% endmacro %}
{% macro transform_multipart(property, source, destination) %}
{{ transform(property, source, destination, transform_method="to_multipart") }}
{% endmacro %}

View file

@ -0,0 +1,20 @@
{% macro construct_template(construct_function, property, source, initial_value=None) %}
{% if property.required and not property.nullable %}
{{ property.python_name }} = {{ construct_function(property, source) }}
{% else %}{# Must be nullable OR non-required #}
_{{ property.python_name }} = {{ source }}
{{ property.python_name }}: {{ property.get_type_string() }}
{% if property.nullable %}
if _{{ property.python_name }} is None:
{{ property.python_name }} = {% if initial_value != None %}{{ initial_value }}{% else %}None{% endif %}
{% endif %}
{% if not property.required %}
{% if property.nullable %}elif{% else %}if{% endif %} isinstance(_{{ property.python_name }}, Unset):
{{ property.python_name }} = {% if initial_value != None %}{{ initial_value }}{% else %}UNSET{% endif %}
{% endif %}
else:
{{ property.python_name }} = {{ construct_function(property, "_" + property.python_name) }}
{% endif %}
{% endmacro %}

View file

@ -0,0 +1,85 @@
{% macro construct(property, source, initial_value=None) %}
def _parse_{{ property.python_name }}(data: object) -> {{ property.get_type_string() }}:
{% if "None" in property.get_type_strings_in_union(json=True) %}
if data is None:
return data
{% endif %}
{% if "Unset" in property.get_type_strings_in_union(json=True) %}
if isinstance(data, Unset):
return data
{% endif %}
{% set ns = namespace(contains_unmodified_properties = false) %}
{% for inner_property in property.inner_properties %}
{% import "property_templates/" + inner_property.template as inner_template %}
{% if not inner_template.construct %}
{% set ns.contains_unmodified_properties = true %}
{% continue %}
{% endif %}
{% if inner_template.check_type_for_construct and (not loop.last or ns.contains_unmodified_properties) %}
try:
if not {{ inner_template.check_type_for_construct(inner_property, "data") }}:
raise TypeError()
{{ inner_template.construct(inner_property, "data", initial_value="UNSET") | indent(8) }}
return {{ inner_property.python_name }}
except: # noqa: E722
pass
{% else %}{# Don't do try/except for the last one nor any properties with no type checking #}
{% if inner_template.check_type_for_construct %}
if not {{ inner_template.check_type_for_construct(inner_property, "data") }}:
raise TypeError()
{% endif %}
{{ inner_template.construct(inner_property, "data", initial_value="UNSET") | indent(4) }}
return {{ inner_property.python_name }}
{% endif %}
{% endfor %}
{% if ns.contains_unmodified_properties %}
return cast({{ property.get_type_string() }}, data)
{% endif %}
{{ property.python_name }} = _parse_{{ property.python_name }}({{ source }})
{% endmacro %}
{% macro transform(property, source, destination, declare_type=True, multipart=False) %}
{% if not property.required or property.nullable %}
{{ destination }}{% if declare_type %}: {{ property.get_type_string(json=True) }}{% endif %}
{% if not property.required %}
if isinstance({{ source }}, Unset):
{{ destination }} = UNSET
{% endif %}
{% endif %}
{% if property.nullable %}
{% if property.required %}
if {{ source }} is None:
{% else %}{# There's an if UNSET statement before this #}
elif {{ source }} is None:
{% endif %}
{{ destination }} = None
{% endif %}
{% set ns = namespace(contains_properties_without_transform = false, contains_modified_properties = not property.required) %}
{% for inner_property in property.inner_properties %}
{% import "property_templates/" + inner_property.template as inner_template %}
{% if not inner_template.transform %}
{% set ns.contains_properties_without_transform = true %}
{% continue %}
{% else %}
{% set ns.contains_modified_properties = true %}
{% endif %}
{% if loop.first and property.required and not property.nullable %}{# No if UNSET or if None statement before this #}
if isinstance({{ source }}, {{ inner_property.get_instance_type_string() }}):
{% elif not loop.last or ns.contains_properties_without_transform %}
elif isinstance({{ source }}, {{ inner_property.get_instance_type_string() }}):
{% else %}
else:
{% endif %}
{{ inner_template.transform(inner_property, source, destination, declare_type=False, multipart=multipart) | indent(4) }}
{% endfor %}
{% if ns.contains_properties_without_transform and ns.contains_modified_properties %}
else:
{{ destination }} = {{ source }}
{% elif ns.contains_properties_without_transform %}
{{ destination }} = {{ source }}
{% endif %}
{% endmacro %}