2020-04-30 03:01:37 +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/>.
|
|
|
|
|
|
|
|
""" XML helpers and macros """
|
|
|
|
|
|
|
|
from os import path
|
|
|
|
import xml.etree.ElementTree as ET
|
|
|
|
from xml.dom import minidom
|
2020-04-30 18:32:47 +02:00
|
|
|
from typing import Dict, List, Union, Text, Any
|
2020-04-30 03:01:37 +02:00
|
|
|
|
|
|
|
|
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
|
|
|
data XMLDictElement(tag: Text, attrs: XMLAttrs, children: List[Any])
|
2020-04-30 03:01:37 +02:00
|
|
|
|
2020-04-30 18:32:47 +02:00
|
|
|
XMLAttrs = Dict[Text, Text]
|
|
|
|
XMLChild = Union[XMLDictElement, Text, List]
|
2020-04-30 03:01:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
def xmlroot(tree: Dict[str, Any]) -> ET.Element:
|
|
|
|
""" Special process for root XML Node """
|
|
|
|
rootel = (tree['tag'], tree['attrs']) |*> ET.Element
|
|
|
|
if 'children' in tree:
|
|
|
|
(rootel, tree['children']) |*> xmlchild
|
|
|
|
return rootel
|
|
|
|
|
2020-04-30 10:58:36 +02:00
|
|
|
def xmlchild(parent: ET.Element, children: XMLDictElement) -> None:
|
2020-04-30 03:01:37 +02:00
|
|
|
""" Handling of children (ie non root) XML Nodes with/o text and
|
|
|
|
subchildren (recursive) """
|
2020-04-30 10:58:36 +02:00
|
|
|
case children:
|
2020-04-30 18:32:47 +02:00
|
|
|
match _ is Text:
|
2020-04-30 10:58:36 +02:00
|
|
|
parent.text = children
|
|
|
|
match _ is XMLDictElement:
|
|
|
|
attrs = {unicode(k): unicode(v) for [k, v] in children.attrs.items()}
|
|
|
|
new_parent = (parent, children.tag, attrs) |*> ET.SubElement
|
|
|
|
subchildren = children.children
|
|
|
|
if subchildren:
|
|
|
|
(new_parent, subchildren) |*> xmlchild
|
2020-04-30 18:32:47 +02:00
|
|
|
match _ is List:
|
2020-04-30 10:58:36 +02:00
|
|
|
((xmlchild$ <| parent), children) |*> map |> consume
|
|
|
|
else:
|
|
|
|
raise TypeError('Invalid arguments for xmlchild')
|
2020-04-30 03:01:37 +02:00
|
|
|
|
2020-04-30 18:32:47 +02:00
|
|
|
def xmln(tag: Text = '',
|
2020-04-30 10:58:36 +02:00
|
|
|
attrs: XMLAttrs = {},
|
2020-04-30 18:32:47 +02:00
|
|
|
children: Union[Text, List] = []) -> XMLDictElement:
|
|
|
|
""" XMLDictElement building from dict object, with defaults """
|
2020-04-30 11:09:28 +02:00
|
|
|
xmldictel = XMLDictElement$ <*| (tag, attrs)
|
2020-04-30 10:58:36 +02:00
|
|
|
case children:
|
2020-04-30 18:32:47 +02:00
|
|
|
match c is Text:
|
2020-04-30 11:09:28 +02:00
|
|
|
return [c] |> xmldictel
|
2020-04-30 10:58:36 +02:00
|
|
|
match c is list:
|
2020-04-30 11:09:28 +02:00
|
|
|
return c |> xmldictel
|
2020-04-30 03:01:37 +02:00
|
|
|
else:
|
|
|
|
raise TypeError('Invalid arguments for xmln')
|
|
|
|
|
2020-04-30 18:32:47 +02:00
|
|
|
|
2020-04-30 03:01:37 +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 '.py' |> filepath.endswith: # if .pyc, no need to generate XML
|
|
|
|
output_xml = tree |> ET.tostring |> minidom.parseString |> .toprettyxml(indent=' ')
|
|
|
|
output_path = filepath |> path.abspath |> path.dirname
|
|
|
|
fpath = [output_path, filepath |> path.basename] |> '/'.join |> .replace('.py', '_views.xml')
|
2020-04-30 03:01:37 +02:00
|
|
|
with open(fpath, 'w') as output_file:
|
|
|
|
output_file.write(output_xml)
|