2020-04-30 03:01:37 +02:00
|
|
|
#!/usr/bin/env python2
|
|
|
|
# -*- coding: utf-8 -*-
|
2020-05-01 09:45:58 +02:00
|
|
|
# __coconut_hash__ = 0x970fdbe8
|
2020-04-30 03:01:37 +02:00
|
|
|
|
|
|
|
# Compiled with Coconut version 1.4.3 [Ernest Scribbler]
|
|
|
|
|
|
|
|
""" XML helpers and macros """
|
|
|
|
|
|
|
|
# Coconut Header: -------------------------------------------------------------
|
|
|
|
|
|
|
|
from __future__ import print_function, absolute_import, unicode_literals, division
|
|
|
|
import sys as _coconut_sys, os.path as _coconut_os_path
|
|
|
|
_coconut_file_path = _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_path:
|
|
|
|
del _coconut_sys.modules[b"__coconut__"]
|
|
|
|
_coconut_sys.path.insert(0, _coconut_file_path)
|
|
|
|
from __coconut__ import *
|
|
|
|
from __coconut__ import _coconut, _coconut_MatchError, _coconut_igetitem, _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_back_pipe, _coconut_star_pipe, _coconut_back_star_pipe, _coconut_dubstar_pipe, _coconut_back_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_mark_as_match
|
|
|
|
|
|
|
|
|
|
|
|
# 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/>.
|
|
|
|
|
2020-04-30 03:01:37 +02: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-04-30 03:01:37 +02:00
|
|
|
from typing import Dict
|
|
|
|
from typing import List
|
|
|
|
from typing import Union
|
2020-04-30 18:32:47 +02:00
|
|
|
from typing import Text
|
2020-04-30 03:01:37 +02:00
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
|
2020-04-30 10:58:36 +02:00
|
|
|
# TODO: fix MyPy / typing
|
2020-04-30 03:01:37 +02:00
|
|
|
|
2020-04-30 18:32:47 +02:00
|
|
|
class XMLDictElement(_coconut.typing.NamedTuple("XMLDictElement", [("tag", 'Text'), ("attrs", 'XMLAttrs'), ("children", 'List[Any]')]), _coconut.object):
|
2020-04-30 03:01:37 +02:00
|
|
|
__slots__ = ()
|
|
|
|
__ne__ = _coconut.object.__ne__
|
|
|
|
def __eq__(self, other):
|
|
|
|
return self.__class__ is other.__class__ and _coconut.tuple.__eq__(self, other)
|
|
|
|
def __hash__(self):
|
|
|
|
return _coconut.tuple.__hash__(self) ^ hash(self.__class__)
|
|
|
|
|
2020-04-28 08:46:30 +02:00
|
|
|
|
2020-04-30 18:32:47 +02:00
|
|
|
XMLAttrs = Dict[Text, Text]
|
|
|
|
XMLChild = Union[XMLDictElement, Text, List]
|
2020-04-28 08:46:30 +02:00
|
|
|
|
2020-04-30 03:01:37 +02:00
|
|
|
|
|
|
|
def xmlroot(tree # type: Dict[str, Any]
|
|
|
|
):
|
|
|
|
# type: (...) -> ET.Element
|
2020-04-28 08:46:30 +02:00
|
|
|
""" Special process for root XML Node """
|
2020-04-30 03:01:37 +02:00
|
|
|
rootel = (ET.Element)(*(tree['tag'], tree['attrs']))
|
|
|
|
if 'children' in tree:
|
|
|
|
(xmlchild)(*(rootel, tree['children']))
|
2020-04-28 08:46:30 +02:00
|
|
|
return rootel
|
|
|
|
|
2020-04-30 03:01:37 +02:00
|
|
|
def xmlchild(parent, # type: ET.Element
|
2020-04-30 10:58:36 +02:00
|
|
|
children # type: XMLDictElement
|
2020-04-30 03:01:37 +02:00
|
|
|
):
|
|
|
|
# type: (...) -> None
|
2020-04-28 08:46:30 +02:00
|
|
|
""" Handling of children (ie non root) XML Nodes with/o text and
|
|
|
|
subchildren (recursive) """
|
2020-04-30 03:01:37 +02:00
|
|
|
_coconut_match_to = children
|
2020-04-30 10:58:36 +02:00
|
|
|
_coconut_case_check_0 = False
|
2020-04-30 18:32:47 +02:00
|
|
|
if _coconut.isinstance(_coconut_match_to, Text):
|
2020-04-30 10:58:36 +02:00
|
|
|
_coconut_case_check_0 = True
|
|
|
|
if _coconut_case_check_0:
|
2020-04-30 03:01:37 +02:00
|
|
|
parent.text = children
|
2020-04-30 10:58:36 +02:00
|
|
|
if not _coconut_case_check_0:
|
|
|
|
if _coconut.isinstance(_coconut_match_to, XMLDictElement):
|
|
|
|
_coconut_case_check_0 = True
|
|
|
|
if _coconut_case_check_0:
|
|
|
|
attrs = dict(((unicode(k)), (unicode(v))) for [k, v] in children.attrs.items())
|
|
|
|
new_parent = (ET.SubElement)(*(parent, children.tag, attrs))
|
|
|
|
subchildren = children.children
|
|
|
|
if subchildren:
|
|
|
|
(xmlchild)(*(new_parent, subchildren))
|
|
|
|
if not _coconut_case_check_0:
|
2020-04-30 18:32:47 +02:00
|
|
|
if _coconut.isinstance(_coconut_match_to, List):
|
2020-04-30 10:58:36 +02:00
|
|
|
_coconut_case_check_0 = True
|
|
|
|
if _coconut_case_check_0:
|
|
|
|
(consume)((map)(*(((_coconut.functools.partial(_coconut.functools.partial, xmlchild))(parent)), children)))
|
|
|
|
if not _coconut_case_check_0:
|
|
|
|
raise TypeError('Invalid arguments for xmlchild')
|
2020-04-28 08:46:30 +02:00
|
|
|
|
2020-04-30 18:32:47 +02:00
|
|
|
def xmln(tag='', # type: Text
|
2020-04-30 10:58:36 +02:00
|
|
|
attrs={}, # type: XMLAttrs
|
2020-04-30 18:32:47 +02:00
|
|
|
children=[] # type: Union[Text, List]
|
2020-04-30 03:01:37 +02:00
|
|
|
):
|
2020-04-30 10:58:36 +02:00
|
|
|
# type: (...) -> XMLDictElement
|
2020-04-30 18:32:47 +02:00
|
|
|
""" XMLDictElement building from dict object, with defaults """
|
2020-04-30 11:09:28 +02:00
|
|
|
xmldictel = (_coconut.functools.partial(_coconut.functools.partial, XMLDictElement))(*(tag, attrs))
|
2020-04-30 03:01:37 +02:00
|
|
|
_coconut_match_to = children
|
2020-04-30 10:58:36 +02:00
|
|
|
_coconut_case_check_1 = False
|
2020-04-30 18:32:47 +02:00
|
|
|
if _coconut.isinstance(_coconut_match_to, Text):
|
2020-04-30 03:01:37 +02:00
|
|
|
c = _coconut_match_to
|
2020-04-30 10:58:36 +02:00
|
|
|
_coconut_case_check_1 = True
|
|
|
|
if _coconut_case_check_1:
|
2020-04-30 11:09:28 +02:00
|
|
|
return (xmldictel)([c])
|
2020-04-30 10:58:36 +02:00
|
|
|
if not _coconut_case_check_1:
|
2020-04-30 03:01:37 +02:00
|
|
|
if _coconut.isinstance(_coconut_match_to, list):
|
|
|
|
c = _coconut_match_to
|
2020-04-30 10:58:36 +02:00
|
|
|
_coconut_case_check_1 = True
|
|
|
|
if _coconut_case_check_1:
|
2020-04-30 11:09:28 +02:00
|
|
|
return (xmldictel)(c)
|
2020-04-30 10:58:36 +02:00
|
|
|
if not _coconut_case_check_1:
|
|
|
|
raise TypeError('Invalid arguments for xmln')
|
2020-04-28 08:46:30 +02:00
|
|
|
|
2020-04-30 18:32:47 +02:00
|
|
|
|
2020-04-28 08:46:30 +02:00
|
|
|
def xml_write(filepath, tree):
|
|
|
|
""" Write XML file according to filename and given tree """
|
2020-04-30 18:32:47 +02:00
|
|
|
if (filepath.endswith)('.py'): # if .pyc, no need to generate XML
|
|
|
|
output_xml = ((minidom.parseString)((ET.tostring)(tree))).toprettyxml(indent=' ')
|
|
|
|
output_path = (path.dirname)((path.abspath)(filepath))
|
|
|
|
fpath = (('/'.join)([output_path, (path.basename)(filepath)])).replace('.py', '_views.xml')
|
2020-04-29 11:02:18 +02:00
|
|
|
with open(fpath, 'w') as output_file:
|
|
|
|
output_file.write(output_xml)
|