From d369c5f5c294d798adb32bc16b299467076cad0e Mon Sep 17 00:00:00 2001 From: idziubenko Date: Mon, 9 Aug 2021 17:50:00 +0300 Subject: [PATCH] fixed xfce xml format, removed u-strings --- pym/calculate/lib/cl_template.py | 1 - pym/calculate/lib/cl_xml.py | 9 ++++-- pym/calculate/lib/configparser.py | 4 +-- pym/calculate/lib/format/xml_gconf.py | 12 +++---- pym/calculate/lib/format/xml_gconf_tree.py | 6 ++-- pym/calculate/lib/format/xml_xfce.py | 37 ++++++++++------------ pym/calculate/lib/format/xml_xfcepanel.py | 2 +- 7 files changed, 36 insertions(+), 35 deletions(-) diff --git a/pym/calculate/lib/cl_template.py b/pym/calculate/lib/cl_template.py index e376046..deb987a 100644 --- a/pym/calculate/lib/cl_template.py +++ b/pym/calculate/lib/cl_template.py @@ -3795,7 +3795,6 @@ class Template(_file, _terms, _warning, xmlShare, _shareTemplate): # Название файла шаблона директории templDirNameFile = ".calculate_directory" _titleList = ("Modified", "Processed template files" + ":") - #TODO fix transl titleEnd = "For modify this file, create %(conf_path)s.clt template." protectPaths = [] allContents = {} diff --git a/pym/calculate/lib/cl_xml.py b/pym/calculate/lib/cl_xml.py index ae3f69e..b16e88d 100644 --- a/pym/calculate/lib/cl_xml.py +++ b/pym/calculate/lib/cl_xml.py @@ -23,8 +23,13 @@ from copy import deepcopy # ET._Element.append(*args, **kwargs) def display_xml(xml): - print(str(ET.tostring(xml, pretty_print=True), encoding="UTF-8")) + print(xml_to_str(xml)) +def xml_to_str(xml): + return str(ET.tostring(xml, pretty_print=True), encoding="UTF-8") + +def str_to_xml_doc(text): + return ET.XML(bytes(bytearray(text, encoding="UTF-8"))) #backwards compat class xpath(): @@ -221,7 +226,7 @@ class xmlDoc(): # self.root = self.doc.documentElement # self.body = xpath.Evaluate('child::body', self.root)[0] - self.doc = ET.XML(bytes(bytearray(docTxt, encoding='utf-8'))) + self.doc = str_to_xml_doc(docTxt) self.root = self.doc self.body = xpath.Evaluate('child::body', self.root)[0] # установка разделителя областей diff --git a/pym/calculate/lib/configparser.py b/pym/calculate/lib/configparser.py index 77030d5..3079ce7 100644 --- a/pym/calculate/lib/configparser.py +++ b/pym/calculate/lib/configparser.py @@ -39,7 +39,7 @@ RawConfigParser = configparser.RawConfigParser Error = configparser.Error class ConfigParserCaseSens(ConfigParser): - u"""ConfigParser with case sensitivity keys""" + """ConfigParser with case sensitivity keys""" def optionxform(self, optionstr): return optionstr @@ -76,7 +76,7 @@ class ConfigParserLocked(ConfigParser): class ConfigParserCaseSensLocked(ConfigParserLocked): - u"""ConfigParser with case sensitivity keys with locking""" + """ConfigParser with case sensitivity keys with locking""" def optionxform(self, optionstr): return optionstr diff --git a/pym/calculate/lib/format/xml_gconf.py b/pym/calculate/lib/format/xml_gconf.py index 1d9aa30..2dd5b03 100644 --- a/pym/calculate/lib/format/xml_gconf.py +++ b/pym/calculate/lib/format/xml_gconf.py @@ -106,10 +106,10 @@ class xml_gconf(xml_xfce): # if xmlNode.nodeType == xmlNode.ELEMENT_NODE: n = xmlNode tagName = n.tag - nAction = u'' - nType = u'' - nValue = u'' - nSchema = u'' + nAction = '' + nType = '' + nValue = '' + nSchema = '' attrName = '' if flagRootNode: if not tagName == "gconf": @@ -138,7 +138,7 @@ class xml_gconf(xml_xfce): _("Attribute \"type\" not found in the tag entry")) return False nName = n.get("name") - attrName = u"attribute::name='%s'" % nName + attrName = "attribute::name='%s'" % nName if flagType: flagValue = "value" in n.keys() nType = n.get("type") @@ -164,7 +164,7 @@ class xml_gconf(xml_xfce): findAttrStr = "" if attrName: findAttrStr = "[%s]" % attrName - findPath = u"child::%s%s" % (tagName, findAttrStr) + findPath = "child::%s%s" % (tagName, findAttrStr) # Рабочая нода if flagRootNode: workNode = xmlOldNode.getparent() diff --git a/pym/calculate/lib/format/xml_gconf_tree.py b/pym/calculate/lib/format/xml_gconf_tree.py index e0013f8..fcf8c32 100644 --- a/pym/calculate/lib/format/xml_gconf_tree.py +++ b/pym/calculate/lib/format/xml_gconf_tree.py @@ -37,7 +37,7 @@ class xml_gconf_tree(xml_gconf): n = xmlNode tagName = n.tag tagNameStr = tagName.encode("UTF-8") - nAction = u'' + nAction = '' attrName = '' if flagRootNode: if not tagName == "gconf": @@ -55,7 +55,7 @@ class xml_gconf_tree(xml_gconf): % {'tag': tagNameStr}) return False nName = n.get("name") - attrName = u"attribute::name='%s'" % nName + attrName = "attribute::name='%s'" % nName if "action" in n.keys(): nAction = n.get("action") if nAction not in ("join", "replace", "drop"): @@ -71,7 +71,7 @@ class xml_gconf_tree(xml_gconf): findAttrStr = "" if attrName: findAttrStr = "[%s]" % attrName - findPath = u"child::%s%s" % (tagName, findAttrStr) + findPath = "child::%s%s" % (tagName, findAttrStr) # Рабочая нода if flagRootNode: workNode = xmlOldNode.getparent() diff --git a/pym/calculate/lib/format/xml_xfce.py b/pym/calculate/lib/format/xml_xfce.py index 683f8b6..97b605c 100644 --- a/pym/calculate/lib/format/xml_xfce.py +++ b/pym/calculate/lib/format/xml_xfce.py @@ -15,9 +15,7 @@ # limitations under the License. import sys -from ..cl_xml import xpath -# import xml.dom.minidom -from lxml import etree +from ..cl_xml import xpath, xml_to_str, str_to_xml_doc from ..cl_template import TemplateFormat from ..cl_lang import setLocalTranslate from collections.abc import Iterable @@ -57,7 +55,7 @@ class xml_xfce(TemplateFormat): def setNameBodyNode(self, name): """Устанавливает название для корневой ноды документа""" - if not self.bodyNode: + if self.bodyNode is None: return False self.bodyNode.set("name", name) return True @@ -71,12 +69,12 @@ class xml_xfce(TemplateFormat): ''' try: - # self.doc = xml.dom.minidom.parseString(self.text) - self.doc = etree.XML(bytes(bytearray(self.text, encoding='utf-8'))) - except Exception: + self.doc = str_to_xml_doc(self.text) + except Exception as e: + print(e) self.setError(_("The template content is not XML")) return False - self.rootNode = self.doc + self.rootNode = self.doc.getroottree().getroot() self.bodyNode = self.rootNode return self.doc @@ -96,7 +94,6 @@ class xml_xfce(TemplateFormat): Также удаляет аттрибут action у всех нод """ childNodes = list(xmlNode) - # if xmlNode.nodeType == xmlNode.ELEMENT_NODE: if "action" in xmlNode.keys(): nAction = xmlNode.get("action") if nAction not in ("join", "replace", "drop"): @@ -131,18 +128,18 @@ class xml_xfce(TemplateFormat): nextOldNode = xmlOldNode # if xmlNode.nodeType == xmlNode.ELEMENT_NODE: n = xmlNode - nType = u'' - nValue = u'' - nAction = u'' + nType = '' + nValue = '' + nAction = '' attrName = '' attrType = '' path = n.tag if "name" in n.keys(): nName = n.get("name") - attrName = u"attribute::name='%s'" % nName + attrName = "attribute::name='%s'" % nName if "type" in n.keys(): nType = n.get("type") - attrType = u"attribute::type='%s'" % nType + attrType = "attribute::type='%s'" % nType if "value" in n.keys(): nValue = n.get("value") if "action" in n.keys(): @@ -161,9 +158,9 @@ class xml_xfce(TemplateFormat): findAttr = [x for x in strAttr if x] findAttrStr = '' if findAttr: - strAttr = u' and '.join(findAttr) + strAttr = ' and '.join(findAttr) findAttrStr = "[%s]" % strAttr - findPath = u"child::%s%s" % (path, findAttrStr) + findPath = "child::%s%s" % (path, findAttrStr) # Рабочая нода if flagRootNode: workNode = xmlOldNode.getparent() @@ -237,10 +234,10 @@ class xml_xfce(TemplateFormat): def joinDoc(self, doc): """Объединение документа шаблона и документа файла""" - if not self.doc: + if self.doc is None: self.setError(_("The text file is not XML")) return False - if not doc: + if doc is None: self.setError(_("The text file is not XML")) return False # Импортируем корневую ноду нового документа в текущий документ @@ -252,6 +249,6 @@ class xml_xfce(TemplateFormat): def getConfig(self): """Получение текстового файла из XML документа""" - data = self.doc.toprettyxml(encoding='UTF-8').split("\n") + data = xml_to_str(self.doc).split("\n") data = [x for x in data if x.strip()] - return "\n".join(data).replace("\t", " ").decode("UTF-8") + return "\n".join(data).replace("\t", " ") diff --git a/pym/calculate/lib/format/xml_xfcepanel.py b/pym/calculate/lib/format/xml_xfcepanel.py index 33572ff..e2d456f 100644 --- a/pym/calculate/lib/format/xml_xfcepanel.py +++ b/pym/calculate/lib/format/xml_xfcepanel.py @@ -92,7 +92,7 @@ class xml_xfcepanel(xml_xfce): findAttrStr = "" if attrName: findAttrStr = "[%s]" % attrName - findPath = u"child::%s%s" % (path, findAttrStr) + findPath = "child::%s%s" % (path, findAttrStr) # Рабочая нода if flagRootNode: workNode = xmlOldNode.getparent()