fixed xfce xml format, removed u-strings

master
idziubenko 3 years ago
parent bba9451c68
commit d369c5f5c2

@ -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 = {}

@ -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]
# установка разделителя областей

@ -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

@ -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()

@ -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()

@ -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):
<channel version="1.0">
</channel>'''
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", " ")

@ -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()

Loading…
Cancel
Save