Refactor switches in templates: step 1

This commit is contained in:
Vincent Jacques
2012-05-31 21:53:27 +01:00
parent 0cc1743b3c
commit ca390879f0
37 changed files with 126 additions and 214 deletions
@@ -0,0 +1,4 @@
{
key : {% if attribute.type.name != class.name %}{{ attribute.type.name }}.{% endif %}{{ attribute.type.name }}( self._requester, element, completed = False )
for key, element in attributes[ "{{ attribute.name }}" ].iteritems()
}
@@ -0,0 +1,4 @@
[
{% if attribute.type.name != class.name %}{{ attribute.type.name }}.{% endif %}{{ attribute.type.name }}( self._requester, element, completed = False )
for element in attributes[ "{{ attribute.name }}" ]
]
@@ -0,0 +1 @@
{% with template_name="GithubObject.AttributeValue.complex."|add:attribute.type.cardinality|add:".py" %}None if attributes[ "{{ attribute.name }}" ] is None else {% include template_name %}{% endwith %}
@@ -0,0 +1 @@
{% if attribute.type.name != class.name %}{{ attribute.type.name }}.{% endif %}{{ attribute.type.name }}( self._requester, attributes[ "{{ attribute.name }}" ], completed = False )
@@ -0,0 +1 @@
attributes[ "{{ attribute.name }}" ]
@@ -0,0 +1 @@
str( {{ value|join:"." }} )
@@ -0,0 +1 @@
str( self.{{ value|join:"." }} )
@@ -0,0 +1 @@
"{{ value }}"
@@ -0,0 +1 @@
str( {{ value|join:"." }}._identity )
@@ -0,0 +1 @@
self._parentUrl( {% include "GithubObject.Concatenation.py" with concatenation=value only %} )
@@ -1 +1 @@
{% for part in concatenation %}{% if forloop.counter0 > 0 %} + {% endif %}{% if part.type == "constant" %}"{{ part.value }}"{% endif %}{% if part.type == "argument" %}str( {{ part.value|join:"." }} ){% endif %}{% if part.type == "attribute" %}str( self.{{ part.value|join:"." }} ){% endif %}{% if part.type == "urlquote" %}urllib.quote( {% with template_name="GithubObject.Concatenation.py" %}{% include template_name with concatenation=part.value only %}{% endwith %} ){% endif %}{% if part.type == "parentUrl" %}self._parentUrl( {% with template_name="GithubObject.Concatenation.py" %}{% include template_name with concatenation=part.value only %}{% endwith %} ){% endif %}{% if part.type == "identity" %}str( {{ part.value|join:"." }}._identity ){% endif %}{% endfor %}
{% for part in concatenation %}{% if forloop.counter0 > 0 %} + {% endif %}{% with part_template_name="GithubObject.Concatenation."|add:part.type|add:".py" %}{% include part_template_name with value=part.value only %}{% endwith %}{% endfor %}
@@ -0,0 +1 @@
urllib.quote( {% include "GithubObject.Concatenation.py" with concatenation=value only %} )
@@ -1,81 +1,30 @@
{% if class.identity %}
@property
def _identity( self ):
return {% include "GithubObject.Concatenation.py" with concatenation=class.identity only %}
{% endif %}
{% if class.identity %}
@property
def _identity( self ):
return {% include "GithubObject.Concatenation.py" with concatenation=class.identity only %}
{% endif %}
def _initAttributes( self ):
{% for attribute in class.attributes|dictsort:"name" %}
self._{{ attribute.name }} = GithubObject.NotSet
{% endfor %}
def _useAttributes( self, attributes ):
{% for attribute in class.attributes|dictsort:"name" %}
def _initAttributes( self ):
{% for attribute in class.attributes|dictsort:"name" %}
self._{{ attribute.name }} = GithubObject.NotSet
{% endfor %}
def _useAttributes( self, attributes ):
{% for attribute in class.attributes|dictsort:"name" %}
if "{{ attribute.name }}" in attributes: # pragma no branch
{% if attribute.type.cardinality == "scalar" %}
{% if attribute.type.simple %}
{% if attribute.type.name == "string" %}
assert attributes[ "{{ attribute.name }}" ] is None or isinstance( attributes[ "{{ attribute.name }}" ], ( str, unicode ) ), attributes[ "{{ attribute.name }}" ]
{% endif %}
{% if attribute.type.name == "integer" %}
assert attributes[ "{{ attribute.name }}" ] is None or isinstance( attributes[ "{{ attribute.name }}" ], int ), attributes[ "{{ attribute.name }}" ]
{% endif %}
{% if attribute.type.name == "bool" %}
assert attributes[ "{{ attribute.name }}" ] is None or isinstance( attributes[ "{{ attribute.name }}" ], bool ), attributes[ "{{ attribute.name }}" ]
{% endif %}
{% if attribute.type.name == "dict" %}
assert attributes[ "{{ attribute.name }}" ] is None or isinstance( attributes[ "{{ attribute.name }}" ], dict ), attributes[ "{{ attribute.name }}" ]
{% endif %}
{% else %}
assert attributes[ "{{ attribute.name }}" ] is None or isinstance( attributes[ "{{ attribute.name }}" ], dict ), attributes[ "{{ attribute.name }}" ]
{% endif %}
{% endif %}
{% if attribute.type.cardinality == "list" %}
{% if attribute.type.simple %}
{% if attribute.type.name == "string" %}
assert all( isinstance( element, ( str, unicode ) ) for element in attributes[ "{{ attribute.name }}" ] ), attributes[ "{{ attribute.name }}" ]
{% endif %}
{% if attribute.type.name == "integer" %}
assert all( isinstance( element, int ) for element in attributes[ "{{ attribute.name }}" ] ), attributes[ "{{ attribute.name }}" ]
{% endif %}
{% if attribute.type.name == "bool" %}
assert all( isinstance( element, bool ) for element in attributes[ "{{ attribute.name }}" ] ), attributes[ "{{ attribute.name }}" ]
{% endif %}
{% else %}
assert all( isinstance( element, dict ) for element in attributes[ "{{ attribute.name }}" ] ), attributes[ "{{ attribute.name }}" ]
{% endif %}
{% endif %}
{% if attribute.type.cardinality == "scalar" %}
{% if attribute.type.simple %}
self._{{ attribute.name }} = attributes[ "{{ attribute.name }}" ]
{% else %}
self._{{ attribute.name }} = None if attributes[ "{{ attribute.name }}" ] is None else {% if attribute.type.name != class.name %}{{ attribute.type.name }}.{% endif %}{{ attribute.type.name }}( self._requester, attributes[ "{{ attribute.name }}" ], completed = False )
{% endif %}
{% endif %}
{% if attribute.type.cardinality == "list" %}
{% if attribute.type.simple %}
self._{{ attribute.name }} = attributes[ "{{ attribute.name }}" ]
{% else %}
self._{{ attribute.name }} = [
{% if attribute.type.name != class.name %}{{ attribute.type.name }}.{% endif %}{{ attribute.type.name }}( self._requester, element, completed = False )
for element in attributes[ "{{ attribute.name }}" ]
]
{% endif %}
{% endif %}
{% if attribute.type.cardinality == "dict" %}
{% if attribute.type.simple %}
self._{{ attribute.name }} = attributes[ "{{ attribute.name }}" ]
{% else %}
self._{{ attribute.name }} = {
key : {% if attribute.type.name != class.name %}{{ attribute.type.name }}.{% endif %}{{ attribute.type.name }}( self._requester, element, completed = False )
for key, element in attributes[ "{{ attribute.name }}" ].iteritems()
}
{% endif %}
{% endif %}
{% endfor %}
{% if attribute.type.name != "@todo" %}
{% with simple_or_complex=attribute.type.simple|yesno:"simple,complex_as_dict" %}
if "{{ attribute.name }}" in attributes: # pragma no branch
{% with template_name="GithubObject.IsInstance."|add:attribute.type.cardinality|add:"."|add:simple_or_complex|add:".py" %}
assert attributes[ "{{ attribute.name }}" ] is None or {% include template_name with variable="attributes[ \""|add:attribute.name|add:"\" ]"|safe type=attribute.type only %}, attributes[ "{{ attribute.name }}" ]
{% endwith %}
{% endwith %}
{% endif %}
{% with simple_or_complex=attribute.type.simple|yesno:"simple,complex" %}
{% with template_name="GithubObject.AttributeValue."|add:simple_or_complex|add:".py" %}
self._{{ attribute.name }} = {% include template_name %}
{% endwith %}
{% endwith %}
{% endfor %}
@@ -0,0 +1 @@
{% include "GithubObject.IsInstance.py" with variable=variable type="bool" only %}
@@ -0,0 +1 @@
all( {% include "GithubObject.IsInstance.py" with variable="element" type="dict" only %} for element in {{ variable }}.itervalues() )
@@ -0,0 +1 @@
{% include "GithubObject.IsInstance.py" with variable=variable type="dict" only %}
@@ -0,0 +1 @@
{% include "GithubObject.IsInstance.py" with variable=variable type="int" only %}
@@ -0,0 +1 @@
all( {% include "GithubObject.IsInstance.py" with variable="element" type=type.name|add:"."|add:type.name only %} for element in {{ variable }} )
@@ -0,0 +1 @@
all( {% include "GithubObject.IsInstance.py" with variable="element" type="dict" only %} for element in {{ variable }} )
@@ -0,0 +1 @@
all( {% with template_name="GithubObject.IsInstance."|add:type.name|add:".py" %}{% include template_name with variable="element" only %}{% endwith %} for element in {{ variable }} )
@@ -0,0 +1 @@
isinstance( {{ variable }}, {{ type }} )
@@ -0,0 +1 @@
{% include "GithubObject.IsInstance.py" with variable=variable type=type.name|add:"."|add:type.name only %}
@@ -0,0 +1 @@
{% include "GithubObject.IsInstance.py" with variable=variable type="dict" only %}
@@ -0,0 +1 @@
{% with template_name="GithubObject.IsInstance."|add:type.name|add:".py" %}{% include template_name with variable=variable only %}{% endwith %}
@@ -0,0 +1 @@
{% include "GithubObject.IsInstance.py" with variable=variable type="( str, unicode )" only %}
@@ -1,94 +1,27 @@
{% for parameter in method.mandatoryParameters %}
{% if parameter.type.cardinality == "scalar" %}
{% if parameter.type.simple %}
{% if parameter.type.name == "string" %}
assert isinstance( {{ parameter.name }}, ( str, unicode ) ), {{ parameter.name }}
{% endif %}
{% if parameter.type.name == "integer" %}
assert isinstance( {{ parameter.name }}, int ), {{ parameter.name }}
{% endif %}
{% if parameter.type.name == "bool" %}
assert isinstance( {{ parameter.name }}, bool ), {{ parameter.name }}
{% endif %}
{% if parameter.type.name == "dict" %}
assert isinstance( {{ parameter.name }}, dict ), {{ parameter.name }}
{% endif %}
{% else %}
assert isinstance( {{ parameter.name }}, {{ parameter.type.name }}.{{ parameter.type.name }} ), {{ parameter.name }}
{% endif %}
{% else %}
{% if parameter.type.simple %}
{% if parameter.type.name == "string" %}
assert all( isinstance( element, ( str, unicode ) ) for element in {{ parameter.name }} ), {{ parameter.name }}
{% endif %}
{% if parameter.type.name == "integer" %}
assert all( isinstance( element, int ) for element in {{ parameter.name }} ), {{ parameter.name }}
{% endif %}
{% if parameter.type.name == "bool" %}
assert all( isinstance( element, bool ) for element in {{ parameter.name }} ), {{ parameter.name }}
{% endif %}
{% else %}
assert all( isinstance( element, {{ parameter.type.name }}.{{ parameter.type.name }} ) for element in {{ parameter.name }} ), {{ parameter.name }}
{% endif %}
{% endif %}
{% endfor %}
{% for parameter in method.optionalParameters %}
{% if parameter.type.cardinality == "scalar" %}
{% if parameter.type.simple %}
{% if parameter.type.name == "string" %}
assert {{ parameter.name }} is GithubObject.NotSet or isinstance( {{ parameter.name }}, ( str, unicode ) ), {{ parameter.name }}
{% endif %}
{% if parameter.type.name == "integer" %}
assert {{ parameter.name }} is GithubObject.NotSet or isinstance( {{ parameter.name }}, int ), {{ parameter.name }}
{% endif %}
{% if parameter.type.name == "bool" %}
assert {{ parameter.name }} is GithubObject.NotSet or isinstance( {{ parameter.name }}, bool ), {{ parameter.name }}
{% endif %}
{% if parameter.type.name == "dict" %}
assert {{ parameter.name }} is GithubObject.NotSet or isinstance( {{ parameter.name }}, dict ), {{ parameter.name }}
{% endif %}
{% else %}
assert {{ parameter.name }} is GithubObject.NotSet or isinstance( {{ parameter.name }}, {{ parameter.type.name }}.{{ parameter.type.name }} ), {{ parameter.name }}
{% endif %}
{% else %}
{% if parameter.type.simple %}
{% if parameter.type.name == "string" %}
assert {{ parameter.name }} is GithubObject.NotSet or all( isinstance( element, ( str, unicode ) ) for element in {{ parameter.name }} ), {{ parameter.name }}
{% endif %}
{% if parameter.type.name == "integer" %}
assert {{ parameter.name }} is GithubObject.NotSet or all( isinstance( element, int ) for element in {{ parameter.name }} ), {{ parameter.name }}
{% endif %}
{% if parameter.type.name == "bool" %}
assert {{ parameter.name }} is GithubObject.NotSet or all( isinstance( element, bool ) for element in {{ parameter.name }} ), {{ parameter.name }}
{% endif %}
{% else %}
assert {{ parameter.name }} is GithubObject.NotSet or all( isinstance( element, {{ parameter.type.name }}.{{ parameter.type.name }} ) for element in {{ parameter.name }} ), {{ parameter.name }}
{% endif %}
{% endif %}
{% endfor %}
{% if method.variadicParameter %}
{% if method.variadicParameter.type.name != "@todo" %}
{% if method.variadicParameter.type.simple %}
{% if method.variadicParameter.type.name == "string" %}
assert all( isinstance( {{ method.variadicParameter.name }}, ( str, unicode ) ) for {{ method.variadicParameter.name }} in {{ method.variadicParameter.name }}s ), {{ method.variadicParameter.name }}s
{% endif %}
{% if parameter.type.name == "integer" %}
assert all( isinstance( {{ method.variadicParameter.name }}, int ) for {{ method.variadicParameter.name }} in {{ method.variadicParameter.name }}s ), {{ method.variadicParameter.name }}s
{% endif %}
{% if parameter.type.name == "bool" %}
assert all( isinstance( {{ method.variadicParameter.name }}, bool ) for {{ method.variadicParameter.name }} in {{ method.variadicParameter.name }}s ), {{ method.variadicParameter.name }}s
{% endif %}
{% else %}
assert all( isinstance( {{ method.variadicParameter.name }}, {{ method.variadicParameter.type.name }}.{{ method.variadicParameter.type.name }} ) for {{ method.variadicParameter.name }} in {{ method.variadicParameter.name }}s ), {{ method.variadicParameter.name }}s
{% endif %}
{% endif %}
{% endif %}
{% for parameter in method.mandatoryParameters %}
{% if parameter.type.name != "@todo" %}
{% with simple_or_complex=parameter.type.simple|yesno:"simple,complex" %}
{% with template_name="GithubObject.IsInstance."|add:parameter.type.cardinality|add:"."|add:simple_or_complex|add:".py" %}
assert {% include template_name with variable=parameter.name type=parameter.type only %}, {{ parameter.name }}
{% endwith %}
{% endwith %}
{% endif %}
{% endfor %}
{% for parameter in method.optionalParameters %}
{% if parameter.type.name != "@todo" %}
{% with simple_or_complex=parameter.type.simple|yesno:"simple,complex" %}
{% with template_name="GithubObject.IsInstance."|add:parameter.type.cardinality|add:"."|add:simple_or_complex|add:".py" %}
assert {{ parameter.name }} is GithubObject.NotSet or {% include template_name with variable=parameter.name type=parameter.type only %}, {{ parameter.name }}
{% endwith %}
{% endwith %}
{% endif %}
{% endfor %}
{% if method.variadicParameter %}
{% with simple_or_complex=method.variadicParameter.type.simple|yesno:"simple,complex" %}
{% with template_name="GithubObject.IsInstance.list."|add:simple_or_complex|add:".py" %}
assert {% include template_name with variable=method.variadicParameter.name|add:"s" type=method.variadicParameter.type only %}, {{ method.variadicParameter.name }}s
{% endwith %}
{% endwith %}
{% endif %}
@@ -1,8 +1,8 @@
{% for attribute in class.attributes|dictsort:"name" %}
@property
def {{ attribute.name }}( self ):
{% if class.isCompletable %}
self._completeIfNotSet( self._{{ attribute.name }} )
{% endif %}
return self._NoneIfNotSet( self._{{ attribute.name }} )
{% endfor %}
{% for attribute in class.attributes|dictsort:"name" %}
@property
def {{ attribute.name }}( self ):
{% if class.isCompletable %}
self._completeIfNotSet( self._{{ attribute.name }} )
{% endif %}
return self._NoneIfNotSet( self._{{ attribute.name }} )
{% endfor %}
@@ -1,6 +1,6 @@
{% for method in class.methods|dictsort:"name" %}
def {{ method.name|join:"_" }}( {% include "GithubObject.Parameters.py" with function=method only %} ):
{% include "GithubObject.MethodBody.CheckArguments.py" %}
{% include "GithubObject.MethodBody.DoRequest.py" %}
{% include "GithubObject.MethodBody.UseResult.py" %}
{% endfor %}
{% for method in class.methods|dictsort:"name" %}
def {{ method.name|join:"_" }}( {% include "GithubObject.Parameters.py" with function=method only %} ):
{% include "GithubObject.MethodBody.CheckArguments.py" %}
{% include "GithubObject.MethodBody.DoRequest.py" %}
{% include "GithubObject.MethodBody.UseResult.py" %}
{% endfor %}