Skip to content

Commit

Permalink
Refactor generic get_xml_value_function moved to core
Browse files Browse the repository at this point in the history
  • Loading branch information
triole committed Feb 27, 2018
1 parent c46cbc2 commit 943d635
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
23 changes: 22 additions & 1 deletion rdmo/core/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,27 @@
log = logging.getLogger(__name__)


def get_value_from_xml_node(xml_node, element, what_to_get=None):
r = None
try:
if what_to_get == 'attrib':
r = str(xml_node.find(element).attrib)
elif what_to_get == 'tag':
r = str(xml_node.find(element).tag)
else:
r = str(xml_node.find(element).text)
except Exception as e:
log.debug('Unable to extract "' + element + '" from "' + str(xml_node) + '". ' + str(e))
pass
else:
try:
r = r.encode('utf-8', 'ignore')
except Exception as e:
log.debug('Unable to decode string to utf-8: ' + str(e))
pass
return r


def generate_tempfile_name():
t = int(round(time.time() * 1000))
r = randint(10000, 99999)
Expand Down Expand Up @@ -33,6 +54,6 @@ def validate_xml(tempfilename, root_tag):
else:
root = tree.getroot()
if root.tag != root_tag:
log.info('Validation failed. Xml\'s root node is "' + root_tag + '" and not "' + root.tag + '".')
log.info('Validation failed. Xml\'s root node is "' + root.tag + '" and not "' + root_tag + '".')
exit_code = 1
return exit_code, tree
24 changes: 2 additions & 22 deletions rdmo/projects/imports.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging

from rdmo.core.utils import get_ns_tag, get_ns_map
from rdmo.core.imports import get_value_from_xml_node
from rdmo.core.utils import get_ns_map, get_ns_tag
from rdmo.domain.models import Attribute
from rdmo.options.models import Option
from rdmo.questions.utils import Catalog
Expand All @@ -11,27 +12,6 @@
log = logging.getLogger(__name__)


def get_value_from_xml_node(xml_node, element, what_to_get=None):
r = None
try:
if what_to_get == 'attrib':
r = str(xml_node.find(element).attrib)
elif what_to_get == 'tag':
r = str(xml_node.find(element).tag)
else:
r = str(xml_node.find(element).text)
except Exception as e:
log.debug('Unable to extract "' + element + '" from "' + str(xml_node) + '". ' + str(e))
pass
else:
try:
r = r.encode('utf-8', 'ignore')
except Exception as e:
log.debug('Unable to decode string to utf-8: ' + str(e))
pass
return r


def import_project(project_node, user):
nsmap = get_ns_map(project_node.getroot())
project_title = get_value_from_xml_node(project_node, 'title')
Expand Down

0 comments on commit 943d635

Please sign in to comment.