Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ukanga committed Jan 4, 2019
1 parent 4380bf5 commit b499446
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
54 changes: 32 additions & 22 deletions pyxform/survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ def _generate_static_instances(list_name, choice_list):
@staticmethod
def _get_dummy_instance():
"""Instance content required by ODK Validate for select inputs."""
return node("root", node("item", node("name", "_"), node("label", "_")))
return node("root",
node("item", node("name", "_"), node("label", "_")))

@staticmethod
def _generate_external_instances(element):
Expand Down Expand Up @@ -345,8 +346,8 @@ def _generate_instances(self):
Validation and business rules for output of instances:
- xml-external item name must be unique across the XForm and the form is
considered invalid if there is a duplicate name. This differs from
- xml-external item name must be unique across the XForm and the form
is considered invalid if there is a duplicate name. This differs from
other item types which allow duplicates if not in the same group.
- for all instance sources, if the same instance name is encountered,
the following rules are used to allow re-using instances but prevent
Expand Down Expand Up @@ -387,13 +388,14 @@ def _generate_instances(self):
if i.name in seen.keys() and seen[i.name].src != i.src:
# Instance id exists with different src URI -> error.
msg = "The same instance id will be generated for different " \
"external instance source URIs. Please check the form. " \
"Instance name: '{i}', Existing type: '{e}', " \
"external instance source URIs. Please check the form." \
" Instance name: '{i}', Existing type: '{e}', " \
"Existing URI: '{iu}', Duplicate type: '{d}', " \
"Duplicate URI: '{du}', Duplicate context: '{c}'.".format(
i=i.name, iu=seen[i.name].src, e=seen[i.name].type,
d=i.type, du=i.src, c=i.context
)
"Duplicate URI: '{du}', Duplicate context: '{c}'."\
.format(
i=i.name, iu=seen[i.name].src, e=seen[i.name].type,
d=i.type, du=i.src, c=i.context
)
raise PyXFormError(msg)
elif i.name in seen.keys() and seen[i.name].src == i.src:
# Instance id exists with same src URI -> ok, don't duplicate.
Expand All @@ -418,7 +420,8 @@ def xml_model(self):
model_children += list(self._generate_instances())
model_children += self.xml_bindings()

if self.submission_url or self.public_key or self.auto_send or self.auto_delete:
if (self.submission_url or self.public_key or self.auto_send or
self.auto_delete):
submission_attrs = dict()
if self.submission_url:
submission_attrs["action"] = self.submission_url
Expand Down Expand Up @@ -490,16 +493,20 @@ def _setup_choice_translations(name, choice_value, itext_id):
self._translations,
[media_type_or_language, itext_id, 'long'], value)

self._translations = defaultdict(dict) # pylint: disable=attribute-defined-outside-init
self._translations = defaultdict(dict) # pylint: disable=W0201
for element in self.iter_descendants():
for d in element.get_translations(self.default_language):
if 'guidance_hint' in d['path']:
hint_path = d['path'].replace('guidance_hint', 'hint')
self._translations[d['lang']][hint_path] = self._translations[d['lang']].get(hint_path, {})
self._translations[d['lang']][hint_path].update({"guidance": d['text']})
self._translations[d['lang']][hint_path] = \
self._translations[d['lang']].get(hint_path, {})
self._translations[d['lang']][hint_path].update(
{"guidance": d['text']})
else:
self._translations[d['lang']][d['path']] = self._translations[d['lang']].get(d['path'], {})
self._translations[d['lang']][d['path']].update({"long": d['text']})
self._translations[d['lang']][d['path']] = \
self._translations[d['lang']].get(d['path'], {})
self._translations[d['lang']][d['path']].update(
{"long": d['text']})

# This code sets up translations for choices in filtered selects.
for list_name, choice_list in self.choices.items():
Expand All @@ -508,7 +515,8 @@ def _setup_choice_translations(name, choice_value, itext_id):
itext_id = '-'.join(['static_instance', list_name,
str(idx)])
if isinstance(choice_value, dict):
_setup_choice_translations(name, choice_value, itext_id)
_setup_choice_translations(name, choice_value,
itext_id)
elif name == 'label':
self._add_to_nested_dict(
self._translations,
Expand Down Expand Up @@ -543,7 +551,7 @@ def _setup_media(self):
It matches the xform nesting order.
"""
if not self._translations:
self._translations = defaultdict(dict) # pylint: disable=attribute-defined-outside-init
self._translations = defaultdict(dict) # pylint: disable=W0201

for survey_element in self.iter_descendants():

Expand Down Expand Up @@ -630,12 +638,13 @@ def itext(self):
if media_type == "guidance":
itext_nodes.append(
node("value", value,
form='guidance',
toParseString=output_inserted)
form='guidance',
toParseString=output_inserted)
)
else:
itext_nodes.append(
node("value", value, toParseString=output_inserted)
itext_nodes.append(
node("value", value,
toParseString=output_inserted)
)
continue

Expand Down Expand Up @@ -743,10 +752,11 @@ def insert_xpaths(self, text, context, use_current=False):
"""
Replace all instances of ${var} with the xpath to var.
"""
bracketed_tag = r"\$\{(.*?)\}"
def _var_repl_function(matchobj):
return self._var_repl_function(matchobj, context, use_current)

bracketed_tag = r"\$\{(.*?)\}"

return re.sub(bracketed_tag, _var_repl_function, unicode(text))

def _var_repl_output_function(self, matchobj, context):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
'formencode',
'unittest2',
]

if sys.version_info < (3, 2):
REQUIRES.append('functools32==3.2.3.post2')


setup(
name='pyxform',
version='0.12.0',
Expand Down

0 comments on commit b499446

Please sign in to comment.