Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def dict_to_xml(data, tagname, toplevel=True, nsmap=None):
if USING_LXML and ":" in tagname and not toplevel:
tagname = (
"{"
+ nsmap.get(tagname.split(":", 1)[0], "")
+ "}"
+ tagname.split(":", 1)[1]
)
try:
if USING_LXML:
el = ET.Element(tagname, nsmap=nsmap)
else:
el = ET.Element(tagname)
except ValueError as e:
warn(str(e), DataErrorWarning)
return
if USING_LXML:
data = sort_attributes(data)
for k, v in data.items():
if type(v) == list:
for item in v:
child_to_xml(el, k, item, nsmap=nsmap)
else:
child_to_xml(el, k, v, toplevel=toplevel, nsmap=nsmap)
return el
elif type_string in ("array", "array_array", "string_array", "number_array"):
value = str(value)
if type_string == "number_array":
try:
if "," in value:
return [
[Decimal(y) for y in x.split(",")] for x in value.split(";")
]
else:
return [Decimal(x) for x in value.split(";")]
except (TypeError, ValueError, InvalidOperation):
warn(
'Non-numeric value "{}" found in number array column, returning as string array instead.'.format(
value
),
DataErrorWarning,
)
if "," in value:
return [x.split(",") for x in value.split(";")]
else:
return value.split(";")
elif type_string == "string":
if type(value) == datetime.datetime:
return timezone.localize(value).isoformat()
return str(value)
elif type_string == "date":
if type(value) == datetime.datetime:
return value.date().isoformat()
return str(value)
elif type_string == "":
if type(value) == datetime.datetime:
return timezone.localize(value).isoformat()
)
)
list_index = int(next_path_item)
current_type = "array"
if current_type == "array":
list_as_dict = current_path.get(path_item)
if list_as_dict is None:
list_as_dict = ListAsDict()
current_path[path_item] = list_as_dict
elif type(list_as_dict) is not ListAsDict:
warn(
"Column {} has been ignored, because it treats {} as an array, but another column does not.".format(
path, path_till_now
),
DataErrorWarning,
)
break
new_path = list_as_dict.get(list_index)
if new_path is None:
new_path = OrderedDict()
list_as_dict[list_index] = new_path
current_path = new_path
if not xml or num < len(path_list) - 2:
# In xml "arrays" can have text values, if they're the final element
# This corresponds to a tag with text, but also possibly attributes
continue
## Object
if current_type == "object" or (not current_type and next_path_item):
new_path = current_path.get(path_item)
if new_path is None:
sheet_header = list(sheet)
worksheet = self.workbook.create_sheet()
worksheet.title = self.sheet_prefix + sheet_name
worksheet.append(sheet_header)
for sheet_line in sheet.lines:
line = []
for header in sheet_header:
value = sheet_line.get(header)
if isinstance(value, str):
new_value = ILLEGAL_CHARACTERS_RE.sub("", value)
if new_value != value:
warn(
"Character(s) in '{}' are not allowed in a spreadsheet cell. Those character(s) will be removed".format(
value
),
DataErrorWarning,
)
value = new_value
line.append(value)
worksheet.append(line)