cl_xml fixes for lxml

master
idziubenko 3 years ago
parent f42ef08067
commit 4b77311b48

@ -4405,7 +4405,6 @@ gettext -d cl_template "$*"
@post_unlock_packages
def applyTemplates(self, progress=True, rerun=True):
"""Применяет шаблоны к конфигурационным файлам"""
print("DEBUG APPLY TEMPL")
def createDictTemplates(path, prefix, dictTemplates):
"""Создает словарь {"директория":"кол-во шаблонов" ...}
@ -4470,7 +4469,6 @@ gettext -d cl_template "$*"
if not dirsTemplatesExists and not self.cltObj:
return self.createdDirs, self.filesApply
# check cl_name in first template dirs and files
skipTemplates = self.getTemplateDirs(dirsTemplatesExists)
# if not os.environ.get("EBUILD_PHASE","") and progress:
@ -4493,6 +4491,8 @@ gettext -d cl_template "$*"
self.cltObj.allTemplates = self.allTemplates
self.numberAllTemplates(self.allTemplates)
# Обрабатываем шаблоны
locationPath = dict(self.objVar.ZipVars('main.cl_template_path',
'main.cl_template_location'))
for dirTemplate in dirsTemplatesExists:
@ -4502,6 +4502,7 @@ gettext -d cl_template "$*"
if self.scanningTemplates(dirTemplate,
skipTemplates=skipTemplates) is False:
break
if self.cltObj:
self.objVar.Set('cl_pass_location', 'clt', True)
# Созданные директории
@ -4523,7 +4524,6 @@ gettext -d cl_template "$*"
self.cltObj.changedFiles = self.changedFiles
self.cltObj.autoUpdateFiles = self.autoUpdateFiles
self.cltObj.autoUpdateDirs = self.autoUpdateDirs
if self.cltFilter:
# Шаблоны + .clt которые будут применены
self.cltObj.filterApplyTemplates = {}
@ -4556,7 +4556,7 @@ gettext -d cl_template "$*"
self.cltObj.applyTemplatesClt()
finally:
self.objVar.defaultModule = old_mod
if ((self.objVar.Get('cl_merge_pkg') or
self.objVar.Get('cl_action') in (
"sync", "domain", "server_setup",
@ -4791,6 +4791,7 @@ gettext -d cl_template "$*"
prevAction = self.functObj.currentAction
try:
if stat.S_ISREG(statInfo):
if not self.processingFile(absPath, prefix, optDir):
ret = False
continue
@ -6054,7 +6055,6 @@ gettext -d cl_template "$*"
if not optFile:
optFile = {"path": os.path.split(nameFileConfig)[0]}
filesApply, objHeadNew = self.getApplyHeadTemplate(nameFileTemplate,
nameFileConfig,
templateFileType,
@ -6236,6 +6236,7 @@ gettext -d cl_template "$*"
else:
return None
# Создаем объект в случае параметра format в заголовке
if ((typeAppendTemplate == HParams.AppendParams.Replace or
typeAppendTemplate == HParams.AppendParams.Before or
typeAppendTemplate == HParams.AppendParams.After) and
@ -6379,6 +6380,12 @@ gettext -d cl_template "$*"
" " + nameFileTemplate)
return None
# создаем объект формата шаблона
# print("BEFORE")
# print("+++++++++++++++++++++++++++++")
# print(formatTemplate)
# print("--")
# print(self.textTemplate)
# print("----------------------------")
objTemplNew = self.formatFactory.createObject(
formatTemplate, self.textTemplate)
if not objTemplNew:
@ -6419,8 +6426,16 @@ gettext -d cl_template "$*"
objTxtCoder = utfBin()
self.textConfig = objTxtCoder.encode(self.textConfig)
# создаем объект формата шаблона для конфигурационного файла
# print("A")
# print("+++++++++++++++++++++++++++++")
# print(formatTemplate)
# print("--")
# print(self.textConfig)
# print("----------------------------")
objTemplOld = self.formatFactory.createObject(
formatTemplate, self.textConfig)
if not objTemplOld:
self.setError(_("Error in template %s") % nameFileConfig)
return None
@ -6432,8 +6447,8 @@ gettext -d cl_template "$*"
nameRootNode = nameFileConfig.rpartition("/")[2].split(".")[
0]
objTemplOld.setNameBodyNode(nameRootNode)
objTemplOld.join(objTemplNew)
if "xml_" in formatTemplate:
if objTemplOld.getError():
self.setError(_("Wrong template") + _(": ") + \
@ -6450,6 +6465,7 @@ gettext -d cl_template "$*"
self.textTemplate = objTxtCoder.decode(self.textTemplate)
self.textConfig = objTxtCoder.decode(self.textConfig)
self.saveConfFile()
if HParams.RunNow in objHeadNew.params:
if not self.executeTemplate(
self.textConfig, objHeadNew.params[HParams.RunNow]):

@ -22,7 +22,8 @@ from copy import deepcopy
# def appendChild(*args, **kwargs):
# ET._Element.append(*args, **kwargs)
def display(xml):
print(str(ET.tostring(xml, pretty_print=True), encoding="UTF-8"))
# ET._Element.appendChild = appendChild
@ -33,14 +34,41 @@ class xpath():
f = ET.XPath(xpath)
return f(xml)
#can't add methods to Cython lib. Gotta do this
#can't add methods to Cython lib.
#have to do this the ugly way
def firstChild(element):
# f = ET.XPath("//*[1]")
# found = f(element)
# print("DEBUG FIRST CHILD")
# display(element)
# display(found[0])
# print("DEBUG FIRST CHILD")
# display(element)
if(element.text):
# print("element has text")
# print(element.text)
return element
if(len(element) == 0):
# print("element empty")
# print(None)
return None
# print("element has a child")
# print(element[0])
return element[0]
# def firstChild(element):
# if(element.text):
# return element.text
# if(len(element) == 0):
# return None
# return element[0]
def insertBefore(elem, new_child, ref_child):
#don't actually need parent element, its here just so we can have structure similar to old elem.insertBefore(new_child, ref_child)
child_parent = new_child.getparent()
if child_parent is not None:
child_parent.remove(new_child)
if(ref_child is None):
elem.append(new_child)
ref_child.addprevious(new_child)
return new_child
@ -53,11 +81,15 @@ class xmlShare(object):
if not isinstance(attributes, dict):
attributes = {}
element = ET.SubElement(doc, tag, attributes)
# element = ET.SubElement(doc, tag, attributes)
element = ET.Element(tag, attributes)
# print(type(element))
# print(dir(element))
# raise Exception
# element = doc.createElement(tag)
# print(tag)
# print(text)
# print(attributes)
if text:
# txtNode = doc.createTextNode(_u(text))
# txtNode = doc.createTextNode(text)
@ -67,7 +99,6 @@ class xmlShare(object):
# attribute = doc.createAttribute(attr)
# attribute.text = attributes[attr]
# element.setAttributeNode(attribute)
# print("++++++")
# print(ET.tostring(element, pretty_print=True))
return element
@ -210,7 +241,7 @@ class xmlDoc(object):
docTxt = ('<?xml version="1.0" encoding="UTF-8"?><cxmlconf><head>'
'<ver>{version}</ver>'
'<format>{type_doc}</format>'
'</head><body> </body></cxmlconf>'.format(version=version,
'</head><body></body></cxmlconf>'.format(version=version,
type_doc=typeDoc))
# self.doc = minidom.parseString(docTxt)
# self.root = self.doc.documentElement
@ -225,7 +256,10 @@ class xmlDoc(object):
# print(ET.tostring(self.doc, pretty_print=True))
# print(ET.tostring(self.body, pretty_print=True))
# установка разделителя областей
self.sepAreas = self.createField("br", [], "", [], False, False)
# установка разделителя областей разделенных списков
# self.sepSplitFields = self.createField("br",[],"",[],False,False)
return self.doc
@ -292,7 +326,7 @@ class xmlDoc(object):
def setActionField(self, xmlField, actionTxt):
"""Устанавливает свойство action для XML поля"""
xmlActions = xpath.Evaluate('child::action', xmlField)
if firstChild(xmlActions and xmlActions[0]):
if xmlActions and firstChild(xmlActions[0]) is not None:
firstChild(xmlActions[0]).text = actionTxt
else:
nodeObj = xmlNode()
@ -303,7 +337,7 @@ class xmlDoc(object):
"""Устанавливает свойство action для XML области"""
xmlActions = xpath.Evaluate('child::caption/action', xmlArea)
xmlCaptions = xpath.Evaluate('child::caption', xmlArea)
if xmlActions and firstChild(xmlActions[0]):
if xmlActions and firstChild(xmlActions[0]) is not None:
firstChild(xmlActions[0]).text = actionTxt
else:
if xmlCaptions:
@ -476,7 +510,7 @@ class xmlDoc(object):
"""Удаляет комментарии в XML области"""
fieldNodes = xpath.Evaluate('descendant::field', xmlArea)
for fieldNode in fieldNodes:
if fieldNode.get("type"):
if fieldNode.get("type") is not None:
if fieldNode.get("type") == "comment" or \
fieldNode.get("type") == "br":
parentNode = fieldNode.getparent()
@ -491,9 +525,18 @@ class xmlDoc(object):
def joinBody(self, baseBody, newBody):
"""Объединяет две области Body"""
# print("baseBody")
# display(baseBody)
# print("newBody")
# display(newBody)
newFields = xpath.Evaluate('child::field', newBody)
# if(newFields):
# print("newFields")
# display(newFields[0])
xmlNewAreas = xpath.Evaluate('child::area', newBody)
for xmlNewArea in xmlNewAreas:
for i, xmlNewArea in enumerate(xmlNewAreas):
# print("xmlNewArea ", i)
# display(xmlNewArea)
self.joinArea(baseBody, xmlNewArea)
joinNewFields = xpath.Evaluate("child::field[child::action='join']",
newBody)
@ -676,7 +719,10 @@ class xmlDoc(object):
self.setActionArea(xmlNewArea, "append")
# Добавляем разделитель областей во вложенные области
areaNodes = xpath.Evaluate('descendant::area', xmlNewArea)
# print("DEBUG appendArea")
# print(len(areaNodes))
for areaNode in areaNodes:
# display(areaNode)
self.setActionArea(areaNode, "append")
parentNode = areaNode.getparent()
insertBefore(parentNode, deepcopy(self.sepAreas),
@ -687,6 +733,10 @@ class xmlDoc(object):
nodesNames = xpath.Evaluate('child::area/caption/name', baseNode)
nodesNewArea = xpath.Evaluate('child::caption/name', xmlNewArea)
# print("nodesNames")
# print(nodesNames)
# print('nodesNewArea')
# print(nodesNewArea)
if not nodesNames:
# Добавляем область
if nodesNewArea:
@ -697,17 +747,29 @@ class xmlDoc(object):
if not nodesNames or not nodesNewArea:
return False
nameArea = ""
if firstChild(nodesNewArea[0]):
if firstChild(nodesNewArea[0]) is not None:
# print("HELLO")
nameArea = firstChild(nodesNewArea[0]).text.strip()
# print('nameArea: ', nameArea)
# print(firstChild(nodesNewArea[0]))
flagFindArea = False
newAreaAction = None
baseNodes = []
for oName in nodesNames:
# print("DEBUG in for oName")
# print(oName)
# display(oName)
# print(firstChild(oName))
newAreaAction = self.getActionArea(xmlNewArea)
oArea = oName.getparent().getparent()
oNameTxt = ""
if firstChild(oName):
if firstChild(oName) is not None:
oNameTxt = firstChild(oName).text
# print("BBBBBBBBBB")
# print(oNameTxt)
if nameArea == oNameTxt:
flagFindArea = True
# При использовании удаления
@ -728,10 +790,13 @@ class xmlDoc(object):
oldAreaCaption = xpath.Evaluate('child::caption',
oldAreaNode)[0]
if newAreaCaption and oldAreaCaption:
xmlNewArea.replaceChild(oldAreaCaption, newAreaCaption)
#its was replace(old, new) in legacy code, even though
#the func takes (new, old). Mistake, or on purpose?
#xmlNewArea.replaceChild(oldAreaCaption, newAreaCaption)
xmlNewArea.replace(newAreaCaption, oldAreaCaption)
self.setActionArea(xmlNewArea, "replace")
baseNode.replaceChild(xmlNewArea,
oldAreaNode)
baseNode.replace(oldAreaNode, xmlNewArea)
continue
baseNodes.append(oName.getparent().getparent())
newFields = xpath.Evaluate('child::field', xmlNewArea)
@ -742,6 +807,7 @@ class xmlDoc(object):
self.addNewFielsOldArea(newFields, joinNewFields, oArea)
if not flagFindArea:
# print("FLAG")
# Добавляем область
if not (newAreaAction == "drop" or newAreaAction == "replace"):
appendArea(baseNode, xmlNewArea)
@ -754,18 +820,20 @@ class xmlDoc(object):
def joinDoc(self, xmlNewDoc):
"""Объединяет два документа"""
# newRootNode = xmlNewDoc.documentElement
newRootNode = xmlNewDoc.getroottree()
newBodyNode = xpath.Evaluate('child::body', newRootNode)[0]
# newImportBodyNode = self.doc.importNode(newBodyNode, True)
newImportBodyNode = deepcopy(newBodyNode)
# Перед объединение области с документом
# удаляем комментарии
self.removeComment(newImportBodyNode)
self.joinBody(self.body, newImportBodyNode)
# расставляем BR
self.insertBRtoBody(self.body)
# print("final:")
# display(self.doc)
# print("--------------------------------")
def getQuoteField(self, xmlField):
"""Выдает текст из поля"""
@ -775,7 +843,7 @@ class xmlDoc(object):
br = "\n"
if xmlQuotes:
field = xmlQuotes[0]
if firstChild(field):
if firstChild(field) is not None:
return firstChild(field).text + br
return "" + br
@ -799,7 +867,7 @@ class xmlDoc(object):
def getNameField(self, xmlField):
"""Выдает имя поля"""
xmlNameFields = xpath.Evaluate('child::name', xmlField)
if xmlNameFields and firstChild(xmlNameFields[0]):
if xmlNameFields and firstChild(xmlNameFields[0]) is not None:
return firstChild(xmlNameFields[0]).text
else:
return False
@ -819,7 +887,7 @@ class xmlDoc(object):
quotes = []
xmlQuotes = xpath.Evaluate('child::caption/quote', xmlArea)
for node in xmlQuotes:
if firstChild(node):
if firstChild(node) is not None:
quotes.append(firstChild(node).text)
if len(quotes) == 0:
quotes.append("")
@ -855,7 +923,7 @@ class xmlDoc(object):
def getActionField(self, xmlField):
"""Выдает свойство action XML поля"""
xmlActions = xpath.Evaluate('child::action', xmlField)
if xmlActions and firstChild(xmlActions[0]):
if xmlActions and firstChild(xmlActions[0]) is not None:
return firstChild(xmlActions[0]).text
else:
return False
@ -866,14 +934,14 @@ class xmlDoc(object):
xmlValues = xpath.Evaluate('child::value', xmlField)
if xmlValues:
for node in xmlValues:
if firstChild(node):
if firstChild(node) is not None:
vals.append(firstChild(node).text)
return vals
def getActionArea(self, xmlArea):
"""Выдает свойство action XML области"""
xmlActions = xpath.Evaluate('child::caption/action', xmlArea)
if xmlActions and firstChild(xmlActions[0]):
if xmlActions and firstChild(xmlActions[0]) is not None:
return firstChild(xmlActions[0]).text
else:
return False
@ -881,7 +949,7 @@ class xmlDoc(object):
def delActionNodeArea(self, xmlArea):
"""Удаляет свойство action XML области"""
xmlActions = xpath.Evaluate('child::caption/action', xmlArea)
if xmlActions and firstChild(xmlActions[0]):
if xmlActions and firstChild(xmlActions[0]) is not None:
parentNode = xmlActions[0].getparent()
parentNode.remove(xmlActions[0])
return True
@ -891,7 +959,7 @@ class xmlDoc(object):
def delActionNodeField(self, xmlField):
"""Удаляет свойство action XML поля"""
xmlActions = xpath.Evaluate('child::action', xmlField)
if xmlActions and firstChild(xmlActions[0]):
if xmlActions and firstChild(xmlActions[0]) is not None:
parentNode = xmlActions[0].getparent()
parentNode.remove(xmlActions[0])
return True
@ -931,10 +999,12 @@ class xmlDoc(object):
childNodes = self.getFieldsArea(xmlArea)
# нода BR
fieldXMLBr = self.createField("br", [], "", [], False, False)
# Предыдущая нода
lastNode = False
lastNode = None
lenChildNodes = len(childNodes)
for i in range(lenChildNodes):
# print(lastNode)
node = childNodes[i]
lastTmpNode = node
# Нода area
@ -942,8 +1012,8 @@ class xmlDoc(object):
if self.getActionArea(node) == "append" or \
self.getActionArea(node) == "join":
self.delActionNodeArea(node)
if lastNode and lastNode.get("type") == "br" or \
lastNode and lastNode.get("type") == "comment":
if lastNode is not None and lastNode.get("type") == "br" or \
lastNode is not None and lastNode.get("type") == "comment":
indNext = i + 1
if indNext == lenChildNodes:
xmlArea.append(deepcopy(fieldXMLBr))
@ -1013,7 +1083,7 @@ class xmlDoc(object):
if flagListXml:
nameNode = xpath.Evaluate('child::caption/name', xmlArea)[0]
fieldName = ""
if firstChild(nameNode):
if firstChild(nameNode) is not None:
fieldName = firstChild(nameNode).text
listArea = []
self.xmlToText([xmlArea], listArea)

@ -14,7 +14,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import
import re
from calculate.lib.cl_xml import xpath, insertBefore
from .generic import objShare
@ -41,6 +40,9 @@ class samba(objShare):
self._splitToFields = self.splitToFields
# Объект документ
self.docObj = self._textToXML()
print("DEBUG samba format")
from lxml import etree
print(str(etree.tostring(self.docObj.doc, pretty_print=True), encoding="UTF-8"))
# XML документ
self.doc = self.docObj.doc
@ -65,7 +67,7 @@ class samba(objShare):
if not (xmlFields and (
self.docObj.getTypeField(xmlFields[-1]) == "br" or
self.docObj.getTypeField(xmlFields[-1]) == "comment")):
if xmlArea.getnext():
if xmlArea.getnext() is not None:
parentNode = xmlArea.getparent()
nextNode = xmlArea.getnext()
insertBefore(parentNode, self.docObj.createField(
@ -108,8 +110,17 @@ class samba(objShare):
def join(self, sambaObj):
"""Объединяем конфигурации"""
if isinstance(sambaObj, samba):
print("DEBUG samba format join\n")
from lxml import etree
# print("before join (a):")
# print(str(etree.tostring(self.docObj.doc, pretty_print=True), encoding="UTF-8"))
# print("before join (b):")
# print(str(etree.tostring(sambaObj.docObj.doc, pretty_print=True), encoding="UTF-8"))
self.docObj.joinDoc(sambaObj.doc)
self.postXML()
# print("after join:")
# print(str(etree.tostring(self.docObj.doc, pretty_print=True), encoding="UTF-8"))
def setDataField(self, txtLines, endtxtLines):
"""Создаем список объектов с переменными"""
@ -223,7 +234,7 @@ class samba(objShare):
# Если пустой текст то создаем пустой документ
if not blocs:
return docObj
for h in blocs[0]:
listfinH = h.split("]")
finH = listfinH[0]
@ -238,11 +249,19 @@ class samba(objShare):
headers.append(finH.replace("[", "").replace("]", "").strip())
bodys = blocs[1]
# print("HEADER")
# print(headers)
# print(startHeaders)
# print(finHeaders)
# print("DEBUG before")
# from lxml import etree
# print(str(etree.tostring(docObj.doc, pretty_print=True), encoding="UTF-8"))
z = 0
for h in headers:
if not bodys[z]:
z += 1
continue
areaAction = False
if h:
if h[0] == "!":
@ -255,7 +274,6 @@ class samba(objShare):
docObj.createCaption(h, [startHeaders[z], ""])
else:
docObj.createCaption(h, [startHeaders[z], ""])
if "\n" in blocs[0][z]:
if self.reComment.search(finHeaders[z]):
docObj.createField('comment', [finHeaders[z]])
@ -272,6 +290,8 @@ class samba(objShare):
# Обработка условий для samba
if (f.name[0] == "!" or
f.name[0] == "-" or f.name[0] == "+"):
print("DEBUG !")
print(f.name[0])
qns = self.removeSymbolTerm(f.br)
xmlField = docObj.createField(
"var", [qns], f.name[1:], [f.value])
@ -296,5 +316,10 @@ class samba(objShare):
for fieldNode in fieldsNodes:
rootNode.append(fieldNode)
docObj.clearTmpFields()
# print(f"DEBUG for: \nheader: {h}\nbody: {bodys[z]}")
# from lxml import etree
# print(str(etree.tostring(docObj.doc, pretty_print=True), encoding="UTF-8"))
z += 1
return docObj

Loading…
Cancel
Save