2022-11-17 16:05:36 +01:00
|
|
|
#!/usr/bin/env python2
|
|
|
|
# -*- coding: utf-8 -*-
|
2022-11-18 10:37:20 +01:00
|
|
|
# __coconut_hash__ = 0x37b96b63
|
2022-11-17 16:05:36 +01:00
|
|
|
|
|
|
|
# Compiled with Coconut version 2.1.1 [The Spanish Inquisition]
|
|
|
|
|
|
|
|
""" XML helpers and macros """
|
|
|
|
|
|
|
|
# Coconut Header: -------------------------------------------------------------
|
|
|
|
|
|
|
|
from __future__ import print_function, absolute_import, unicode_literals, division
|
|
|
|
import sys as _coconut_sys, os as _coconut_os
|
|
|
|
_coconut_file_dir = _coconut_os.path.dirname(_coconut_os.path.abspath(__file__))
|
|
|
|
_coconut_cached_module = _coconut_sys.modules.get(b"__coconut__")
|
|
|
|
if _coconut_cached_module is not None and _coconut_os.path.dirname(_coconut_cached_module.__file__) != _coconut_file_dir: # type: ignore
|
|
|
|
del _coconut_sys.modules[b"__coconut__"]
|
|
|
|
_coconut_sys.path.insert(0, _coconut_file_dir)
|
|
|
|
_coconut_module_name = _coconut_os.path.splitext(_coconut_os.path.basename(_coconut_file_dir))[0]
|
|
|
|
if _coconut_module_name and _coconut_module_name[0].isalpha() and all(c.isalpha() or c.isdigit() for c in _coconut_module_name) and "__init__.py" in _coconut_os.listdir(_coconut_file_dir):
|
|
|
|
_coconut_full_module_name = str(_coconut_module_name + ".__coconut__")
|
|
|
|
import __coconut__ as _coconut__coconut__
|
|
|
|
_coconut__coconut__.__name__ = _coconut_full_module_name
|
|
|
|
for _coconut_v in vars(_coconut__coconut__).values():
|
|
|
|
if getattr(_coconut_v, "__module__", None) == b"__coconut__":
|
|
|
|
try:
|
|
|
|
_coconut_v.__module__ = _coconut_full_module_name
|
|
|
|
except AttributeError:
|
|
|
|
_coconut_v_type = type(_coconut_v)
|
|
|
|
if getattr(_coconut_v_type, "__module__", None) == b"__coconut__":
|
|
|
|
_coconut_v_type.__module__ = _coconut_full_module_name
|
|
|
|
_coconut_sys.modules[_coconut_full_module_name] = _coconut__coconut__
|
|
|
|
from __coconut__ import *
|
2022-11-18 10:37:20 +01:00
|
|
|
from __coconut__ import _coconut_call_set_names, _coconut_handle_cls_kwargs, _coconut_handle_cls_stargs, _namedtuple_of, _coconut, _coconut_super, _coconut_MatchError, _coconut_iter_getitem, _coconut_base_compose, _coconut_forward_compose, _coconut_back_compose, _coconut_forward_star_compose, _coconut_back_star_compose, _coconut_forward_dubstar_compose, _coconut_back_dubstar_compose, _coconut_pipe, _coconut_star_pipe, _coconut_dubstar_pipe, _coconut_back_pipe, _coconut_back_star_pipe, _coconut_back_dubstar_pipe, _coconut_none_pipe, _coconut_none_star_pipe, _coconut_none_dubstar_pipe, _coconut_bool_and, _coconut_bool_or, _coconut_none_coalesce, _coconut_minus, _coconut_map, _coconut_partial, _coconut_get_function_match_error, _coconut_base_pattern_func, _coconut_addpattern, _coconut_sentinel, _coconut_assert, _coconut_raise, _coconut_mark_as_match, _coconut_reiterable, _coconut_self_match_types, _coconut_dict_merge, _coconut_exec, _coconut_comma_op, _coconut_multi_dim_arr, _coconut_mk_anon_namedtuple, _coconut_matmul
|
2022-11-17 16:05:36 +01:00
|
|
|
_coconut_sys.path.pop(0)
|
|
|
|
|
|
|
|
# Compiled Coconut: -----------------------------------------------------------
|
|
|
|
|
2020-04-28 08:46:30 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright 2019-2020 Fabien Bourgeois <fabien@yaltik.com>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2022-11-17 16:05:36 +01:00
|
|
|
|
2020-04-28 08:46:30 +02:00
|
|
|
|
|
|
|
from os import path
|
|
|
|
import xml.etree.ElementTree as ET
|
|
|
|
from xml.dom import minidom
|
2020-05-03 12:03:47 +02:00
|
|
|
from collections import namedtuple
|
|
|
|
from functools import partial
|
2020-04-28 08:46:30 +02:00
|
|
|
|
2020-05-03 12:03:47 +02:00
|
|
|
XMLDictElement = namedtuple('XMLDictElement', ['tag', 'attrs', 'children'])
|
2020-04-28 08:46:30 +02:00
|
|
|
|
2020-04-30 03:01:37 +02:00
|
|
|
|
2020-05-03 12:03:47 +02:00
|
|
|
def xmlroot(tree):
|
2020-04-28 08:46:30 +02:00
|
|
|
""" Special process for root XML Node """
|
2020-05-03 12:03:47 +02:00
|
|
|
rootel = ET.Element(tree['tag'], tree['attrs'])
|
2020-04-30 03:01:37 +02:00
|
|
|
if 'children' in tree:
|
2020-05-03 12:03:47 +02:00
|
|
|
xmlchild(rootel, tree['children'])
|
2020-04-28 08:46:30 +02:00
|
|
|
return rootel
|
|
|
|
|
2022-11-17 16:05:36 +01:00
|
|
|
|
2020-05-03 12:03:47 +02:00
|
|
|
def xmlchild(parent, children):
|
2020-04-28 08:46:30 +02:00
|
|
|
""" Handling of children (ie non root) XML Nodes with/o text and
|
|
|
|
subchildren (recursive) """
|
2022-11-18 10:37:20 +01:00
|
|
|
if isinstance(children, basestring):
|
2020-04-30 03:01:37 +02:00
|
|
|
parent.text = children
|
2020-05-03 12:03:47 +02:00
|
|
|
elif isinstance(children, XMLDictElement):
|
2022-11-17 16:05:36 +01:00
|
|
|
attrs = dict(((str(k)), (str(v))) for [k, v] in children.attrs.items())
|
2020-05-03 12:03:47 +02:00
|
|
|
new_parent = ET.SubElement(parent, children.tag, attrs)
|
|
|
|
subchildren = children.children
|
|
|
|
if subchildren:
|
|
|
|
xmlchild(new_parent, subchildren)
|
|
|
|
elif isinstance(children, list):
|
2022-11-17 16:05:36 +01:00
|
|
|
list(map(partial(xmlchild, parent), children))
|
2020-05-03 12:03:47 +02:00
|
|
|
else:
|
2020-04-30 10:58:36 +02:00
|
|
|
raise TypeError('Invalid arguments for xmlchild')
|
2020-04-28 08:46:30 +02:00
|
|
|
|
2022-11-17 16:05:36 +01:00
|
|
|
|
2020-05-03 12:03:47 +02:00
|
|
|
def xmln(tag='', attrs={}, children=[]):
|
2020-04-30 18:32:47 +02:00
|
|
|
""" XMLDictElement building from dict object, with defaults """
|
2020-05-03 12:03:47 +02:00
|
|
|
if isinstance(attrs, list):
|
2020-05-01 18:58:34 +02:00
|
|
|
children = attrs
|
|
|
|
attrs = {}
|
2020-05-03 12:03:47 +02:00
|
|
|
xmldictel = partial(XMLDictElement, tag, attrs)
|
2022-11-18 10:37:20 +01:00
|
|
|
if isinstance(children, basestring):
|
|
|
|
return xmldictel([children,])
|
2020-05-03 12:03:47 +02:00
|
|
|
if isinstance(children, list):
|
2022-11-18 10:37:20 +01:00
|
|
|
return xmldictel(children)
|
2020-05-03 12:03:47 +02:00
|
|
|
raise TypeError('Invalid arguments for xmln')
|
2020-04-28 08:46:30 +02:00
|
|
|
|
2020-04-30 18:32:47 +02:00
|
|
|
|
2022-11-17 16:05:36 +01:00
|
|
|
|
|
|
|
def xml_write(filepath, tree, pretty=True, suffix='_views'):
|
2020-04-28 08:46:30 +02:00
|
|
|
""" Write XML file according to filename and given tree """
|
2022-11-17 16:05:36 +01:00
|
|
|
if filepath.endswith('.py'): # if .pyc, no need to generate XML
|
2020-05-03 12:31:40 +02:00
|
|
|
output_xml = ET.tostring(tree)
|
|
|
|
if pretty:
|
|
|
|
output_xml = minidom.parseString(output_xml).toprettyxml(indent=' ')
|
2022-11-17 16:05:36 +01:00
|
|
|
output_path = path.abspath(filepath).split('/')
|
|
|
|
output_path[-1] = output_path[-1].replace('.py', '%s.xml' % suffix)
|
|
|
|
output_path = '/'.join(output_path)
|
2020-05-03 12:31:40 +02:00
|
|
|
with open(output_path, 'w') as output_file:
|
2020-04-29 11:02:18 +02:00
|
|
|
output_file.write(output_xml)
|