preparing pyXML to lxml migration

master
idziubenko 3 years ago
parent 0ff7ce47bc
commit f42ef08067

@ -103,8 +103,6 @@ class Application(object):
self.name = name
self.config = config
self.classes = classes
print("SPYNE DEBUG")
print(__name__)
if self.name is None:
self.name = self.__class__.__name__.split('.')[-1]

@ -338,7 +338,7 @@ class _shareTemplate(object):
groupsNames = map(lambda x: x.gr_name,
filter(lambda x: userName in x.gr_mem,
grp.getgrall()))
groupsNames = [groupName] + groupsNames
groupsNames = [groupName] + list(groupsNames)
return uid, gid, homeDir, groupsNames
return uid, gid, homeDir
@ -497,8 +497,8 @@ class _terms(_error, _shareTermsFunction, _shareTemplate):
elemB.append(lB)
# dereferencing suffix in suffixes list
ssA = map(lambda x: (self._suffixDict.get(x[0], 0), x[1]), ssA)
ssB = map(lambda x: (self._suffixDict.get(x[0], 0), x[1]), ssB)
ssA = list(map(lambda x: (self._suffixDict.get(x[0], 0), x[1]), ssA))
ssB = list(map(lambda x: (self._suffixDict.get(x[0], 0), x[1]), ssB))
for suffix, sufVer in reversed(ssA):
if ssB:
sB, svB = ssB.pop()
@ -1551,7 +1551,7 @@ class _file(_error):
try:
self.F_CONF.truncate(0)
self.F_CONF.seek(0)
self.F_CONF.write(self.textConfig)
self.F_CONF.write(str(self.textConfig, encoding="UTF-8"))
except IOError:
self.setError(_("unable to open the file:")
+ self.nameFileConfig)
@ -3801,7 +3801,7 @@ class Template(_file, _terms, _warning, xmlShare, _shareTemplate):
protectPaths = ["/etc"] + list(filter(lambda x: x.strip(),
os.environ["CONFIG_PROTECT"].split(
" ")))
protectPaths = map(lambda x: os.path.normpath(x), protectPaths)
protectPaths = list(map(lambda x: os.path.normpath(x), protectPaths))
@classmethod
def removeComment(cls, text):
@ -3828,6 +3828,7 @@ class Template(_file, _terms, _warning, xmlShare, _shareTemplate):
printERROR=lambda x: x, askConfirm=lambda x: x,
userProfile=False, dispatchConf=None,
critical=False):
_file.__init__(self)
# совместимость с вызовами из модулей предыдущих версий
self.translator = RegexpLocalization("cl_templates3")
if userProfile and objVar:
@ -4315,8 +4316,8 @@ gettext -d cl_template "$*"
skipTemplates = []
debug = False
for dirsTemplate in dirsTemplates:
filesAndDirs = map(lambda x: os.path.join(dirsTemplate, x),
listDirectory(dirsTemplate))
filesAndDirs = list(map(lambda x: os.path.join(dirsTemplate, x),
listDirectory(dirsTemplate)))
for dirFile in filesAndDirs:
if os.path.isdir(dirFile):
flagDir = True
@ -4404,7 +4405,7 @@ gettext -d cl_template "$*"
@post_unlock_packages
def applyTemplates(self, progress=True, rerun=True):
"""Применяет шаблоны к конфигурационным файлам"""
print("DEBUG APPLY TEMPL")
def createDictTemplates(path, prefix, dictTemplates):
"""Создает словарь {"директория":"кол-во шаблонов" ...}
@ -4464,9 +4465,12 @@ gettext -d cl_template "$*"
scanObj.processingFile = lambda x, y: createDictTemplates(x, y,
self.dictTemplates)
# Считаем количество шаблонов
dirsTemplatesExists = filter(lambda x: os.path.exists(x), dirsTemplates)
dirsTemplatesExists = list(filter(lambda x: os.path.exists(x), dirsTemplates))
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:
@ -4541,10 +4545,10 @@ gettext -d cl_template "$*"
filename = PkgContents.reCfg.sub("/", filename)
if not filename in self.cltObj.filterApplyTemplates:
self.cltObj.filterApplyTemplates[filename] = []
pkgs = filter(
pkgs = list(filter(
lambda x: x not in
self.cltObj.filterApplyTemplates[filename],
map(lambda x: x[0], pkgs))
list(map(lambda x: x[0], pkgs))))
self.cltObj.filterApplyTemplates[filename].extend(pkgs)
old_mod = self.objVar.defaultModule
try:
@ -4622,8 +4626,8 @@ gettext -d cl_template "$*"
if self.objVar.Get('cl_verbose_set') == 'on' and \
self.filesApply:
self.verboseOutput(
filter(lambda x: not x.endswith('/ini.env'),
self.filesApply))
list(filter(lambda x: not x.endswith('/ini.env'),
self.filesApply)))
self.objVar.Set('cl_merge_pkg_pass', [], force=True)
self.objVar.Set('cl_merge_pkg_new', [], force=True)
self.objVar.Set('cl_merge_pkg', [], force=True)
@ -4733,8 +4737,8 @@ gettext -d cl_template "$*"
pkgContents.writeContents()
except IOError:
self.printWARNING(_("Failed to modify %s contents") % pkg)
self.filesApply = map(lambda x: autoUpdateDict.get(x, x),
self.filesApply)
self.filesApply = list(map(lambda x: autoUpdateDict.get(x, x),
self.filesApply))
if filter(lambda x: "._cfg" in x, self.filesApply):
if self.objVar.Get('cl_ebuild_phase') != '':
self.printWARNING(_("Some config files need updating. "
@ -5111,8 +5115,8 @@ gettext -d cl_template "$*"
headerLine = self.getHeaderText(textTemplate)
if headerLine:
envparam = "%s=" % HParams.Environ
moduleParam = filter(lambda x: x.startswith(envparam),
headerLine.split())
moduleParam = list(filter(lambda x: x.startswith(envparam),
headerLine.split()))
if moduleParam:
self.objVar.defaultModule = moduleParam[0].partition('=')[2]
try:
@ -6004,7 +6008,8 @@ gettext -d cl_template "$*"
with open(os.path.abspath(fileName), 'r') as FD:
data = FD.read(1) + FD.read()
try:
data.decode("UTF-8")
if(not isinstance(data, str)):
data.decode("UTF-8")
except UnicodeDecodeError:
return False
return True
@ -6037,8 +6042,8 @@ gettext -d cl_template "$*"
headerLine = self.getHeaderText(self.textTemplate)
if headerLine:
envparam = "%s=" % HParams.Environ
moduleParam = filter(lambda x: x.startswith(envparam),
headerLine.split())
moduleParam = list(filter(lambda x: x.startswith(envparam),
headerLine.split()))
if moduleParam:
self.objVar.defaultModule = moduleParam[0].partition('=')[2]
try:
@ -6473,7 +6478,7 @@ gettext -d cl_template "$*"
:param body:
:return:
"""
if comment.startswith("<!--") and body.startswith('<?xml'):
if comment.startswith(b"<!--") and body.startswith(b'<?xml'):
header, body = body.partition("\n")[::2]
body = header + "\n" + comment + body
else:
@ -6665,7 +6670,7 @@ class templateClt(scanDirectoryClt, Template):
return dictTemplates
if not dirsTemplates:
dirsTemplates = self.objVar.Get("cl_template_clt_path")
dirsTemplates = list(self.objVar.Get("cl_template_clt_path"))
dirsTemplates.sort()
scanObj = scanDirectoryClt(objVar=self.objVar)
scanObj.processingFile = lambda x, y: createDictTemplates(x, y,
@ -6686,6 +6691,7 @@ class templateClt(scanDirectoryClt, Template):
dirsTemplates = self.objVar.Get("cl_template_clt_path")
else:
dirsTemplates = cltPath
dirsTemplates = list(dirsTemplates)
dirsTemplates.sort()
if self.checkNumberTemplate:
# Созданные директории

File diff suppressed because it is too large Load Diff

@ -14,37 +14,61 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import xml
from xml.etree import ElementTree as ET
import xml.dom.minidom as minidom
from calculate.lib.utils.text import _u
try:
if hasattr(xml, "use_pyxml"):
xml.use_pyxml()
from xml import xpath
except ImportError:
xpath = None
# import lxml
from lxml import etree as ET
from copy import deepcopy
ET_VERSION = ET.VERSION
# def appendChild(*args, **kwargs):
# ET._Element.append(*args, **kwargs)
# ET._Element.appendChild = appendChild
#backwards compat
class xpath():
@staticmethod
def Evaluate(xpath, xml):
f = ET.XPath(xpath)
return f(xml)
#can't add methods to Cython lib. Gotta do this
def firstChild(element):
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)
ref_child.addprevious(new_child)
return new_child
class xmlShare(object):
"""Общий класс для объектов XML, наследуем
"""
def _createElement(self, doc, tagName, text="", attributes=None):
def _createElement(self, doc, tag, text="", attributes=None):
"""Создание нового XML элемента"""
if not isinstance(attributes, dict):
attributes = {}
element = doc.createElement(tagName)
element = ET.SubElement(doc, tag, attributes)
# print(type(element))
# print(dir(element))
# raise Exception
# element = doc.createElement(tag)
if text:
txtNode = doc.createTextNode(_u(text))
element.appendChild(txtNode)
for attr in attributes:
attribute = doc.createAttribute(attr)
attribute.nodeValue = attributes[attr]
element.setAttributeNode(attribute)
# txtNode = doc.createTextNode(_u(text))
# txtNode = doc.createTextNode(text)
# element.append(txtNode)
element.text = text
# for attr in attributes:
# attribute = doc.createAttribute(attr)
# attribute.text = attributes[attr]
# element.setAttributeNode(attribute)
# print("++++++")
# print(ET.tostring(element, pretty_print=True))
return element
@ -55,9 +79,9 @@ class xmlNode(xmlShare):
def __init__(self):
self.node = False
def createNode(self, doc, tagName, text=""):
def createNode(self, doc, tag, text=""):
"""Создает XML элемент без аттрибутов"""
self.node = self._createElement(doc, tagName, text)
self.node = self._createElement(doc, tag, text)
return self.node
def getNode(self):
@ -78,13 +102,13 @@ class xmlCaption(object):
tmpNode = xmlNode()
self.caption = tmpNode.createNode(doc, "caption")
nameNode = tmpNode.createNode(doc, "name", name)
self.caption.appendChild(nameNode)
self.caption.append(nameNode)
if action:
actNode = tmpNode.createNode(doc, "action", action)
self.caption.appendChild(actNode)
self.caption.append(actNode)
for q in quotes:
quoteNode = tmpNode.createNode(doc, "quote", q)
self.caption.appendChild(quoteNode)
self.caption.append(quoteNode)
return self.caption
def getCaption(self):
@ -107,16 +131,16 @@ class xmlField(xmlShare):
self.field = self._createElement(doc, "field", "", {"type": typeField})
if name:
nameNode = self._createElement(doc, "name", name)
self.field.appendChild(nameNode)
self.field.append(nameNode)
for v in values:
valueNode = self._createElement(doc, "value", v)
self.field.appendChild(valueNode)
self.field.append(valueNode)
if action:
actNode = self._createElement(doc, "action", action)
self.field.appendChild(actNode)
self.field.append(actNode)
for q in quotes:
quoteNode = self._createElement(doc, "quote", q)
self.field.appendChild(quoteNode)
self.field.append(quoteNode)
return self.field
@ -151,12 +175,12 @@ class xmlArea(object):
"""Создание XML области"""
tmpNode = xmlNode()
self.area = tmpNode.createNode(doc, "area")
if xmlCaption and xmlCaption.getCaption():
self.area.appendChild(xmlCaption.getCaption())
if xmlCaption and xmlCaption.getCaption() is not None:
self.area.append(xmlCaption.getCaption())
if xmlFields:
fields = xmlFields.getFields()
for field in fields:
self.area.appendChild(field)
self.area.append(field)
return self.area
@ -186,11 +210,20 @@ 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
# self.doc = minidom.parseString(docTxt)
# 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 = ET.XML(bytes(docTxt, encoding='utf8'))
# print(ET.tostring(self.doc, pretty_print=True))
self.root = self.doc
# print(ET.tostring(self.root, pretty_print=True))
self.body = xpath.Evaluate('child::body', self.root)[0]
# print(ET.tostring(self.doc, pretty_print=True))
# print(ET.tostring(self.body, pretty_print=True))
# установка разделителя областей
self.sepAreas = self.createField("br", [], "", [], False, False)
# установка разделителя областей разделенных списков
@ -259,24 +292,24 @@ class xmlDoc(object):
def setActionField(self, xmlField, actionTxt):
"""Устанавливает свойство action для XML поля"""
xmlActions = xpath.Evaluate('child::action', xmlField)
if xmlActions and xmlActions[0].firstChild:
xmlActions[0].firstChild.nodeValue = actionTxt
if firstChild(xmlActions and xmlActions[0]):
firstChild(xmlActions[0]).text = actionTxt
else:
nodeObj = xmlNode()
newNode = nodeObj.createNode(self.doc, "action", actionTxt)
xmlField.appendChild(newNode)
xmlField.append(newNode)
def setActionArea(self, xmlArea, actionTxt):
"""Устанавливает свойство action для XML области"""
xmlActions = xpath.Evaluate('child::caption/action', xmlArea)
xmlCaptions = xpath.Evaluate('child::caption', xmlArea)
if xmlActions and xmlActions[0].firstChild:
xmlActions[0].firstChild.nodeValue = actionTxt
if xmlActions and firstChild(xmlActions[0]):
firstChild(xmlActions[0]).text = actionTxt
else:
if xmlCaptions:
nodeObj = xmlNode()
newNode = nodeObj.createNode(self.doc, "action", actionTxt)
xmlCaptions[0].appendChild(newNode)
xmlCaptions[0].append(newNode)
def joinField(self, xmlArea, xmlNewField):
"""Объединяет XML ноду область и XML ноду поле"""
@ -290,7 +323,7 @@ class xmlDoc(object):
if not fieldsOldComp and typeNewField != "seplist":
if self.getActionField(xmlNewField) != "drop":
self.setActionField(xmlNewField, "append")
xmlArea.appendChild(xmlNewField)
xmlArea.append(xmlNewField)
return True
newFieldsAction = self.getActionField(xmlNewField)
newValues = self.getFieldValues(xmlNewField)
@ -298,15 +331,15 @@ class xmlDoc(object):
for nodeFieldOld in fieldsOldComp:
if newFieldsAction == "drop":
if nodeFieldOld.nextSibling and \
if nodeFieldOld.getnext() and \
self.getTypeField(
nodeFieldOld.nextSibling) == "br":
xmlArea.removeChild(nodeFieldOld.nextSibling)
elif nodeFieldOld.previousSibling and \
nodeFieldOld.getnext()) == "br":
xmlArea.remove(nodeFieldOld.getnext())
elif nodeFieldOld.getprevious() and \
self.getTypeField(
nodeFieldOld.previousSibling) == "br":
xmlArea.removeChild(nodeFieldOld.previousSibling)
xmlArea.removeChild(nodeFieldOld)
nodeFieldOld.getprevious()) == "br":
xmlArea.remove(nodeFieldOld.getprevious())
xmlArea.remove(nodeFieldOld)
continue
oldValues = self.getFieldValues(nodeFieldOld)
# Сравнение значений переменной шаблона и файла
@ -335,8 +368,8 @@ class xmlDoc(object):
flagCompareSeplist = True
break
if not flagCompareSeplist:
nextNode = xmlOldField.nextSibling
newInsNode = nodeSeplist.cloneNode(True)
nextNode = xmlOldField.getnext()
newInsNode = deepcopy(nodeSeplist)
self.setActionField(newInsNode, "append")
if nextNode:
@ -348,7 +381,7 @@ class xmlDoc(object):
False,
"append"))
else:
newInsNode = nodeSeplist.cloneNode(True)
newInsNode = deepcopy(nodeSeplist)
if self.getActionField(newInsNode) == "join":
self.setActionField(newInsNode, "append")
if xmlOldField:
@ -364,8 +397,8 @@ class xmlDoc(object):
# nodeSeplist.cloneNode(True),
# xmlOldField)
parentNode = nodeSeplist.parentNode
parentNode.removeChild(nodeSeplist)
parentNode = nodeSeplist.getparent()
parentNode.remove(nodeSeplist)
insNodesRepl = []
for newNode, nxtNode, app in insSplLst:
@ -384,26 +417,25 @@ class xmlDoc(object):
for newNode, nxtNode, app in insNodesRepl:
if app == "insert":
xmlArea.insertBefore(newNode, nxtNode)
insertBefore(xmlArea, newNode, nxtNode)
elif app == "append":
xmlArea.appendChild(newNode)
xmlArea.append(newNode)
if xmlOldField:
parentNode = xmlOldField.parentNode
parentNode = xmlOldField.getparent()
if parentNode and newFieldsAction != "join":
parentNode.removeChild(xmlOldField)
parentNode.remove(xmlOldField)
for newNode, nxtNode, app in appSplLst:
if app == "insert":
xmlArea.insertBefore(newNode, nxtNode)
insertBefore(xmlArea, newNode, nxtNode)
elif app == "append":
xmlArea.appendChild(newNode)
xmlArea.append(newNode)
if not flagCompare and typeNewField != "seplist":
# Устанавливаем action=replace
self.setActionField(xmlNewField, "replace")
# Если параметры поля не сходятся заменяем поле
xmlArea.replaceChild(xmlNewField.cloneNode(True),
fieldsOldComp[-1])
xmlArea.replace(fieldsOldComp[-1], deepcopy(xmlNewField))
if newFieldsAction == "join":
fieldsOldRemove = []
@ -415,11 +447,11 @@ class xmlDoc(object):
if actionOldNode == "insert" or actionOldNode == "append":
pass
else:
if nodeFieldOld.nextSibling and \
if nodeFieldOld.getnext() and \
self.getTypeField(
nodeFieldOld.nextSibling) == "br":
xmlArea.removeChild(nodeFieldOld.nextSibling)
xmlArea.removeChild(nodeFieldOld)
nodeFieldOld.getnext()) == "br":
xmlArea.remove(nodeFieldOld.getnext())
xmlArea.remove(nodeFieldOld)
return True
def getSepListToField(self, xmlField):
@ -431,7 +463,7 @@ class xmlDoc(object):
nameField = self.getNameField(xmlField)
if not nameField:
return []
parentNode = xmlField.parentNode
parentNode = xmlField.getparent()
if parentNode:
fieldsVal = xpath.Evaluate(
"child::field[attribute::type='seplist'][child::name='%s'] " \
@ -444,11 +476,11 @@ class xmlDoc(object):
"""Удаляет комментарии в XML области"""
fieldNodes = xpath.Evaluate('descendant::field', xmlArea)
for fieldNode in fieldNodes:
if fieldNode.hasAttribute("type"):
if fieldNode.getAttribute("type") == "comment" or \
fieldNode.getAttribute("type") == "br":
parentNode = fieldNode.parentNode
parentNode.removeChild(fieldNode)
if fieldNode.get("type"):
if fieldNode.get("type") == "comment" or \
fieldNode.get("type") == "br":
parentNode = fieldNode.getparent()
parentNode.remove(fieldNode)
else:
if self.getActionField(fieldNode) == "drop":
pass
@ -534,9 +566,9 @@ class xmlDoc(object):
removeNodes = removeNodes[:-2]
for removeNode in removeNodes:
xmlOldArea.removeChild(removeNode)
xmlOldArea.remove(removeNode)
for node in sepListField:
node.setAttribute("type", "seplist")
node.set("type", "seplist")
if not (self.getActionField(node) == "join" or
self.getActionField(node) == "drop"):
self.setActionField(node, "insert")
@ -544,7 +576,7 @@ class xmlDoc(object):
for node in notSepListField:
if self.getTypeField(node) == "seplist":
self.setActionField(node, "append")
xmlOldArea.appendChild(node)
xmlOldArea.append(node)
else:
self.joinField(xmlOldArea, node)
@ -554,10 +586,10 @@ class xmlDoc(object):
return False
areaNodes = xpath.Evaluate('descendant::area', xmlArea)
for areaNode in areaNodes:
prevNode = areaNode.previousSibling
prevNode = areaNode.getprevious()
if prevNode:
parentNode = areaNode.parentNode
parentNode.insertBefore(self.sepAreas.cloneNode(True),
parentNode = areaNode.getparent()
insertBefore(parentNode, deepcopy(self.sepAreas),
areaNode)
return True
@ -575,14 +607,14 @@ class xmlDoc(object):
dictVar = {}
for namesAreaCompare in namesAreaCompareAll:
fields = xpath.Evaluate("child::field/child::name",
namesAreaCompare.parentNode)
namesAreaCompare.getparent())
for fieldName in fields:
nodeField = fieldName.parentNode
nodeField = fieldName.getparent()
fieldValue = xpath.Evaluate("child::value", nodeField)
name = fieldName.firstChild.nodeValue
name = firstChild(fieldName).text
value = ""
if fieldValue and fieldValue[0].firstChild:
value = fieldValue[0].firstChild.nodeValue
if fieldValue and firstChild(fieldValue[0]):
value = firstChild(fieldValue[0]).text
dictVar[name] = value
if not allVars:
break
@ -600,7 +632,7 @@ class xmlDoc(object):
for areaComp in namesAreaComare:
fieldsVal = xpath.Evaluate(
"child::field[child::name='%s'] "
% nameField, areaComp.parentNode)
% nameField, areaComp.getparent())
if fieldsVal:
break
if not fieldsVal:
@ -609,8 +641,8 @@ class xmlDoc(object):
fieldsVal[0])
if not fieldValue:
return False
if fieldValue[0].firstChild:
return fieldValue[0].firstChild.nodeValue
if firstChild(fieldValue[0]):
return firstChild(fieldValue[0]).text
else:
return ""
@ -623,7 +655,7 @@ class xmlDoc(object):
namesAreaComare = xpath.Evaluate(
"child::area/child::caption[child::name='%s']" % nameArea,
xmlArea)
return map(lambda x: x.parentNode, namesAreaComare)
return map(lambda x: x.getparent(), namesAreaComare)
def joinArea(self, baseNode, xmlNewArea):
"""Объединяет область c областью Body (xmlNewArea c baseNode)"""
@ -632,26 +664,26 @@ class xmlDoc(object):
fieldsRemove = xpath.Evaluate(
"descendant::field[child::action='drop']", xmlNewArea)
for rmNode in fieldsRemove:
parentNode = rmNode.parentNode
parentNode.removeChild(rmNode)
parentNode = rmNode.getparent()
parentNode.remove(rmNode)
captionAreasRemove = xpath.Evaluate(
"descendant::area/child::caption[child::action='drop']",
xmlNewArea)
for rmNodeCapt in captionAreasRemove:
rmNode = rmNodeCapt.parentNode
parentNode = rmNode.parentNode
parentNode.removeChild(rmNode)
rmNode = rmNodeCapt.getparent()
parentNode = rmNode.getparent()
parentNode.remove(rmNode)
self.setActionArea(xmlNewArea, "append")
# Добавляем разделитель областей во вложенные области
areaNodes = xpath.Evaluate('descendant::area', xmlNewArea)
for areaNode in areaNodes:
self.setActionArea(areaNode, "append")
parentNode = areaNode.parentNode
parentNode.insertBefore(self.sepAreas.cloneNode(True),
parentNode = areaNode.getparent()
insertBefore(parentNode, deepcopy(self.sepAreas),
areaNode)
baseNode.appendChild(xmlNewArea)
baseNode.append(xmlNewArea)
# Добавляем разделитель областей
baseNode.insertBefore(self.sepAreas.cloneNode(True), xmlNewArea)
insertBefore(baseNode, deepcopy(self.sepAreas), xmlNewArea)
nodesNames = xpath.Evaluate('child::area/caption/name', baseNode)
nodesNewArea = xpath.Evaluate('child::caption/name', xmlNewArea)
@ -665,32 +697,32 @@ class xmlDoc(object):
if not nodesNames or not nodesNewArea:
return False
nameArea = ""
if nodesNewArea[0].firstChild:
nameArea = nodesNewArea[0].firstChild.nodeValue.strip()
if firstChild(nodesNewArea[0]):
nameArea = firstChild(nodesNewArea[0]).text.strip()
flagFindArea = False
newAreaAction = None
baseNodes = []
for oName in nodesNames:
newAreaAction = self.getActionArea(xmlNewArea)
oArea = oName.parentNode.parentNode
oArea = oName.getparent().getparent()
oNameTxt = ""
if oName.firstChild:
oNameTxt = oName.firstChild.nodeValue
if firstChild(oName):
oNameTxt = firstChild(oName).text
if nameArea == oNameTxt:
flagFindArea = True
# При использовании удаления
if newAreaAction == "drop":
prevNode = oName.parentNode.parentNode.previousSibling
prevNode = oName.getparent().getparent().getprevious()
removePrevNodes = []
while prevNode and self.getTypeField(prevNode) == "br":
removePrevNodes.append(prevNode)
prevNode = prevNode.previousSibling
prevNode = prevNode.getprevious()
for removeNode in removePrevNodes:
baseNode.removeChild(removeNode)
baseNode.removeChild(oName.parentNode.parentNode)
baseNode.remove(removeNode)
baseNode.remove(oName.getparent().getparent())
continue
elif newAreaAction == "replace":
oldAreaNode = oName.parentNode.parentNode
oldAreaNode = oName.getparent().getparent()
newAreaCaption = xpath.Evaluate('child::caption',
xmlNewArea)[0]
oldAreaCaption = xpath.Evaluate('child::caption',
@ -701,7 +733,7 @@ class xmlDoc(object):
baseNode.replaceChild(xmlNewArea,
oldAreaNode)
continue
baseNodes.append(oName.parentNode.parentNode)
baseNodes.append(oName.getparent().getparent())
newFields = xpath.Evaluate('child::field', xmlNewArea)
joinNewFields = xpath.Evaluate(
@ -722,9 +754,12 @@ class xmlDoc(object):
def joinDoc(self, xmlNewDoc):
"""Объединяет два документа"""
newRootNode = xmlNewDoc.documentElement
# newRootNode = xmlNewDoc.documentElement
newRootNode = xmlNewDoc.getroottree()
newBodyNode = xpath.Evaluate('child::body', newRootNode)[0]
newImportBodyNode = self.doc.importNode(newBodyNode, True)
# newImportBodyNode = self.doc.importNode(newBodyNode, True)
newImportBodyNode = deepcopy(newBodyNode)
# Перед объединение области с документом
# удаляем комментарии
self.removeComment(newImportBodyNode)
@ -736,45 +771,44 @@ class xmlDoc(object):
"""Выдает текст из поля"""
xmlQuotes = xpath.Evaluate('child::quote', xmlField)
br = ""
if (xmlField.hasAttribute("type") and
xmlField.getAttribute("type") == "br"):
if (xmlField.get("type") == "br"):
br = "\n"
if xmlQuotes:
field = xmlQuotes[0]
if field.firstChild:
return field.firstChild.nodeValue + br
if firstChild(field):
return firstChild(field).text + br
return "" + br
def getFieldsArea(self, xmlArea):
"""Выдает потомков XML области"""
xmlFields = []
childNodes = xmlArea.childNodes
childNodes = xmlArea.getchildren()
for node in childNodes:
if node.nodeType == node.ELEMENT_NODE:
if node.tagName == "area" or node.tagName == "field":
xmlFields.append(node)
# if node.nodeType == node.ELEMENT_NODE:
if node.tag == "area" or node.tag == "field":
xmlFields.append(node)
return xmlFields
def getTypeField(self, xmlField):
"""Выдает тип поля"""
if xmlField.hasAttribute("type"):
return xmlField.getAttribute("type")
if xmlField.get("type"):
return xmlField.get("type")
else:
return False
def getNameField(self, xmlField):
"""Выдает имя поля"""
xmlNameFields = xpath.Evaluate('child::name', xmlField)
if xmlNameFields and xmlNameFields[0].firstChild:
return xmlNameFields[0].firstChild.nodeValue
if xmlNameFields and firstChild(xmlNameFields[0]):
return firstChild(xmlNameFields[0]).text
else:
return False
def getNameArea(self, xmlArea):
"""Выдает имя области"""
xmlNameAreas = xpath.Evaluate('child::caption/name', xmlArea)
if xmlNameAreas and xmlNameAreas[0].firstChild:
return xmlNameAreas[0].firstChild.nodeValue
if xmlNameAreas and firstChild(xmlNameAreas[0]):
return firstChild(xmlNameAreas[0]).text
else:
return False
@ -785,8 +819,8 @@ class xmlDoc(object):
quotes = []
xmlQuotes = xpath.Evaluate('child::caption/quote', xmlArea)
for node in xmlQuotes:
if node.firstChild:
quotes.append(node.firstChild.nodeValue)
if firstChild(node):
quotes.append(firstChild(node).text)
if len(quotes) == 0:
quotes.append("")
quotes.append("")
@ -795,14 +829,14 @@ class xmlDoc(object):
return quotes
for i in xmlAreas:
if i.tagName == "area":
if i.tag == "area":
quotesI = getQuotesArea(i)
startAreaI = quotesI[0]
endAreaI = quotesI[1]
text.append(startAreaI)
xmlFieldsI = self.getFieldsArea(i)
for f in xmlFieldsI:
if f.tagName == "area":
if f.tag == "area":
quotesF = getQuotesArea(f)
startAreaF = quotesF[0]
endAreaF = quotesF[1]
@ -821,8 +855,8 @@ class xmlDoc(object):
def getActionField(self, xmlField):
"""Выдает свойство action XML поля"""
xmlActions = xpath.Evaluate('child::action', xmlField)
if xmlActions and xmlActions[0].firstChild:
return xmlActions[0].firstChild.nodeValue
if xmlActions and firstChild(xmlActions[0]):
return firstChild(xmlActions[0]).text
else:
return False
@ -832,24 +866,24 @@ class xmlDoc(object):
xmlValues = xpath.Evaluate('child::value', xmlField)
if xmlValues:
for node in xmlValues:
if node.firstChild:
vals.append(node.firstChild.nodeValue)
if firstChild(node):
vals.append(firstChild(node).text)
return vals
def getActionArea(self, xmlArea):
"""Выдает свойство action XML области"""
xmlActions = xpath.Evaluate('child::caption/action', xmlArea)
if xmlActions and xmlActions[0].firstChild:
return xmlActions[0].firstChild.nodeValue
if xmlActions and firstChild(xmlActions[0]):
return firstChild(xmlActions[0]).text
else:
return False
def delActionNodeArea(self, xmlArea):
"""Удаляет свойство action XML области"""
xmlActions = xpath.Evaluate('child::caption/action', xmlArea)
if xmlActions and xmlActions[0].firstChild:
parentNode = xmlActions[0].parentNode
parentNode.removeChild(xmlActions[0])
if xmlActions and firstChild(xmlActions[0]):
parentNode = xmlActions[0].getparent()
parentNode.remove(xmlActions[0])
return True
else:
return False
@ -857,9 +891,9 @@ class xmlDoc(object):
def delActionNodeField(self, xmlField):
"""Удаляет свойство action XML поля"""
xmlActions = xpath.Evaluate('child::action', xmlField)
if xmlActions and xmlActions[0].firstChild:
parentNode = xmlActions[0].parentNode
parentNode.removeChild(xmlActions[0])
if xmlActions and firstChild(xmlActions[0]):
parentNode = xmlActions[0].getparent()
parentNode.remove(xmlActions[0])
return True
else:
return False
@ -872,13 +906,13 @@ class xmlDoc(object):
# содержит списки нод полей с одинаковыми именами в одной области
fieldsSeplist = {}
for node in childNodes:
if node.tagName == "area":
if node.tag == "area":
self.postParserListSeplist(node)
else:
fieldName = False
xmlFieldNameNodes = xpath.Evaluate('child::name', node)
if xmlFieldNameNodes and xmlFieldNameNodes[0].firstChild:
fieldName = xmlFieldNameNodes[0].firstChild.nodeValue
if xmlFieldNameNodes and firstChild(xmlFieldNameNodes[0]):
fieldName = firstChild(xmlFieldNameNodes[0]).text
if fieldName:
if fieldName in fieldsSeplist:
fieldsSeplist[fieldName].append(node)
@ -888,7 +922,7 @@ class xmlDoc(object):
for listNodes in fieldsSeplist.values():
if len(listNodes) > 1:
for node in listNodes:
node.setAttribute("type", "seplist")
node.set("type", "seplist")
def insertBRtoBody(self, xmlArea):
"""Добавляет необходимые переводы строк
@ -904,26 +938,22 @@ class xmlDoc(object):
node = childNodes[i]
lastTmpNode = node
# Нода area
if node.tagName == "area":
if node.tag == "area":
if self.getActionArea(node) == "append" or \
self.getActionArea(node) == "join":
self.delActionNodeArea(node)
if lastNode and lastNode.hasAttribute("type") and \
lastNode.getAttribute("type") == "br" or \
lastNode and lastNode.hasAttribute(
"type") and \
lastNode.getAttribute(
"type") == "comment":
if lastNode and lastNode.get("type") == "br" or \
lastNode and lastNode.get("type") == "comment":
indNext = i + 1
if indNext == lenChildNodes:
xmlArea.appendChild(fieldXMLBr.cloneNode(True))
xmlArea.append(deepcopy(fieldXMLBr))
else:
nextNode = childNodes[indNext]
lastTmpNode = xmlArea.insertBefore(
fieldXMLBr.cloneNode(True),
lastTmpNode = insertBefore(xmlArea,
deepcopy(fieldXMLBr),
nextNode)
else:
xmlArea.insertBefore(fieldXMLBr.cloneNode(True),
insertBefore(xmlArea, deepcopy(fieldXMLBr),
node)
self.insertBRtoBody(node)
# Нода field
@ -931,23 +961,18 @@ class xmlDoc(object):
if self.getActionField(node) == "append" or \
self.getActionField(node) == "join":
self.delActionNodeField(node)
if lastNode and lastNode.hasAttribute("type") and \
lastNode.getAttribute("type") == "br" or \
lastNode and lastNode.hasAttribute(
"type") and \
lastNode.getAttribute(
"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.appendChild(fieldXMLBr.cloneNode(True))
xmlArea.append(deepcopy(fieldXMLBr))
else:
nextNode = childNodes[indNext]
lastTmpNode = xmlArea.insertBefore(
fieldXMLBr.cloneNode(True),
lastTmpNode = insertBefore(xmlArea,
deepcopy(fieldXMLBr),
nextNode)
else:
xmlArea.insertBefore(fieldXMLBr.cloneNode(True),
node)
insertBefore(xmlArea, deepcopy(fieldXMLBr), node)
lastNode = lastTmpNode
def postParserList(self):
@ -964,31 +989,32 @@ class xmlDoc(object):
for xmlField in xmlFields:
xmlNames = xpath.Evaluate('child::name', xmlField)
xmlVals = xpath.Evaluate('child::value', xmlField)
if xmlField.hasAttribute("type") and \
xmlField.getAttribute("type") == "br":
# if xmlField.hasAttribute("type") and \
# xmlField.getAttribute("type") == "br":
if xmlField.get("type") == "br":
lenBrArea += 1
continue
if not xmlNames and not xmlVals:
flagListXml = False
break
if xmlNames and xmlNames[0].firstChild and \
xmlNames[0].firstChild.nodeValue:
if xmlNames and firstChild(xmlNames[0]) and \
firstChild(xmlNames[0]).text:
flagListXml = False
break
if not (xmlVals and xmlVals[0].firstChild and
xmlVals[0].firstChild.nodeValue):
if not (xmlVals and firstChild(xmlVals[0]) and
firstChild(xmlVals[0]).text):
flagListXml = False
break
else:
fieldValues.append(xmlVals[0].firstChild.nodeValue)
fieldValues.append(firstChild(xmlVals[0]).text)
if lenXmlFields == lenBrArea:
flagListXml = False
if flagListXml:
nameNode = xpath.Evaluate('child::caption/name', xmlArea)[0]
fieldName = ""
if nameNode.firstChild:
fieldName = nameNode.firstChild.nodeValue
if firstChild(nameNode):
fieldName = firstChild(nameNode).text
listArea = []
self.xmlToText([xmlArea], listArea)
fieldQuote = "".join(listArea)
@ -1004,8 +1030,8 @@ class xmlDoc(object):
areaAction = self.getActionArea(xmlArea)
if areaAction:
self.setActionField(fieldXML, areaAction)
parentNode = xmlArea.parentNode
parentNode.insertBefore(fieldXML, xmlArea)
parentNode = xmlArea.getparent()
insertBefore(parentNode, fieldXML, xmlArea)
if fieldXMLBr:
parentNode.insertBefore(fieldXMLBr, xmlArea)
parentNode.removeChild(xmlArea)
insertBefore(parentNode, fieldXMLBr, xmlArea)
parentNode.remove(xmlArea)

@ -551,7 +551,7 @@ class Variable(VariableInterface):
"The value for variable '{varname}' may be {vartype} only")
if "string" in self.type:
value, valuecopy = tee(value, 2)
if any(x for x in valuecopy if not type(x) in (str, unicode)):
if any(x for x in valuecopy if not isinstance(x, str)):
raise VariableError(error.format(
varname=self.label or self.name,
vartype="string"))
@ -1041,9 +1041,10 @@ class SimpleDataVars(object):
def getList(delimeter=','):
def wrapper(val):
val = str(val)
if val == "":
return []
return map(fixEmpty, val.split(delimeter))
return list(map(fixEmpty, val.split(delimeter)))
return wrapper
@ -1160,14 +1161,14 @@ class SimpleDataVars(object):
if isinstance(selField, (tuple, list)):
count = len(selField) + woffset
mapFunc = lambda x: x[woffset:]
res = filter(filterFunc,
zipVars(*(where + selField)))
res = list(filter(filterFunc,
zipVars(*(where + selField))))
else:
count = 1 + woffset
fields = where + [selField]
mapFunc = lambda x: x[woffset]
res = filter(filterFunc,
zipVars(*fields))
res = list(filter(filterFunc,
zipVars(*fields)))
if sort:
if "/" in sort:
sort, sortkey = sort.split('/')
@ -1178,7 +1179,7 @@ class SimpleDataVars(object):
res = [[""] * count]
return mapFunc(res[0])
else:
return map(mapFunc, res[:limit])
return list(map(mapFunc, res[:limit]))
class DataVars(SimpleDataVars):
@ -1416,7 +1417,7 @@ class DataVars(SimpleDataVars):
continue
if onlySection:
iniSections = tuple(set(iniSections) &
{unicode(onlySection)})
{str(onlySection)})
for section in iniSections:
for key, value in config.items(section, raw=True):
key = _u8(key)
@ -1857,9 +1858,11 @@ class DataVars(SimpleDataVars):
"""
def compare_choice(part_s, *options):
lower_unicode = lambda x: _u(x).lower()
part_s = lower_unicode(part_s)
options = [lower_unicode(x) for x in options]
# lower_unicode = lambda x: _u(x).lower()
# part_s = lower_unicode(part_s)
part_s = part_s.lower()
# options = [lower_unicode(x) for x in options]
options = [x.lower() for x in options]
return any(x == part_s for x in options)
def get_val_from_choice(choicedata, val):
@ -1920,8 +1923,8 @@ class DataVars(SimpleDataVars):
continue
# get value of variable from info
val = getattr(info, var)
if type(val) == unicode:
val = _u8(val)
# if type(val) == unicode:
# val = _u8(val)
varSendFromClient = val is not None and var not in default
varGroupUntrusted = varObj.untrusted and var in groupVars
varAllUntrusted = varObj.untrusted and allvars

@ -83,13 +83,13 @@ class objShare(TemplateFormat):
def getConfig(self, joinChar=""):
"""Выдает конфигурационный файл"""
listConfigTxt = []
childNodes = self.docObj.getNodeBody().childNodes
childNodes = self.docObj.getNodeBody().getchildren()
for node in childNodes:
if node.nodeType == node.ELEMENT_NODE:
if node.tagName == "field":
listConfigTxt.append(self.docObj.getQuoteField(node))
elif node.tagName == "area":
self.docObj.xmlToText([node], listConfigTxt)
# if node.nodeType == node.ELEMENT_NODE:
if node.tag == "field":
listConfigTxt.append(self.docObj.getQuoteField(node))
elif node.tag == "area":
self.docObj.xmlToText([node], listConfigTxt)
return "%s\n" % joinChar.join(listConfigTxt).rstrip("\n ")
def splitToFields(self, txtBloc):

@ -16,7 +16,7 @@
from __future__ import absolute_import
import re
from calculate.lib.cl_xml import xpath
from calculate.lib.cl_xml import xpath, insertBefore
from .generic import objShare
from calculate.lib.cl_template import blocText
from calculate.lib.cl_xml import xmlDoc
@ -54,8 +54,8 @@ class samba(objShare):
if not xmlFields:
removeList.append(xmlArea)
for xmlArea in removeList:
parentNode = xmlArea.parentNode
parentNode.removeChild(xmlArea)
parentNode = xmlArea.getparent()
parentNode.remove(xmlArea)
# Для добавления перевода строки между областями если его нет
xmlAreas = xpath.Evaluate("child::area", self.docObj.body)
@ -65,20 +65,20 @@ class samba(objShare):
if not (xmlFields and (
self.docObj.getTypeField(xmlFields[-1]) == "br" or
self.docObj.getTypeField(xmlFields[-1]) == "comment")):
if xmlArea.nextSibling:
parentNode = xmlArea.parentNode
nextNode = xmlArea.nextSibling
parentNode.insertBefore(self.docObj.createField(
if xmlArea.getnext():
parentNode = xmlArea.getparent()
nextNode = xmlArea.getnext()
insertBefore(parentNode, self.docObj.createField(
"br", [], "", [], False, False), nextNode)
# Добавление переводов строк между полями
if xmlFields:
for node in xmlFields:
# Добавление перевода строк в если его нет между полями
if (self.docObj.getTypeField(node) == "var" and
node.previousSibling and
node.getprevious() is not None and
not (self.docObj.getTypeField(
node.previousSibling) in ("br", "comment"))):
xmlArea.insertBefore(self.docObj.createField(
node.getprevious()) in ("br", "comment"))):
insertBefore(xmlArea, self.docObj.createField(
"br", [], "", [], False, False), node)
# Удаление лишних переводов строк
@ -86,7 +86,7 @@ class samba(objShare):
lenBr = 0
removeBrNodes = []
for node in childNodes:
if (node.tagName == "field" and
if (node.tag == "field" and
self.docObj.getTypeField(node) == "br"):
lenBr += 1
if lenBr > 2:
@ -95,15 +95,15 @@ class samba(objShare):
lenBr = 0
# Удаление
for rmNode in removeBrNodes:
self.docObj.body.removeChild(rmNode)
self.docObj.body.remove(rmNode)
# Если после есть BR а за ним ничего нет, удаляем BR
if xmlArea:
if (xmlArea.nextSibling and
self.docObj.getTypeField(xmlArea.nextSibling) == "br"):
if not xmlArea.nextSibling.nextSibling:
parentNode = xmlArea.nextSibling.parentNode
parentNode.removeChild(xmlArea.nextSibling)
if xmlArea is not None:
if (xmlArea.getnext() is not None and
self.docObj.getTypeField(xmlArea.getnext()) == "br"):
if not xmlArea.getnext().getnext():
parentNode = xmlArea.getnext().getparent()
parentNode.remove(xmlArea.getnext())
def join(self, sambaObj):
"""Объединяем конфигурации"""
@ -290,11 +290,11 @@ class samba(objShare):
area = docObj.createArea()
if areaAction:
docObj.setActionArea(area, areaAction)
rootNode.appendChild(area)
rootNode.append(area)
else:
fieldsNodes = docObj.tmpFields.getFields()
for fieldNode in fieldsNodes:
rootNode.appendChild(fieldNode)
rootNode.append(fieldNode)
docObj.clearTmpFields()
z += 1
return docObj

@ -253,8 +253,8 @@ def getCfgFiles(protected_dirs=('/etc',), prefix='/'):
Get protected cfg files
"""
reCfg = re.compile(r"/\._cfg\d{4}_", re.S)
findParams = ["find"] + map(lambda x: pathJoin(prefix, x),
protected_dirs) + [
findParams = ["find"] + list(map(lambda x: pathJoin(prefix, x),
protected_dirs)) + [
"-name", "._cfg????_*", "!", "-name", ".*~", "!", "-iname",
".*.bak", "-printf", r"%T@ %p\n"]
mapCfg = {}

@ -256,9 +256,9 @@ class UdevAdm(UdevAdmNull):
value = path
udev_output = files.process(self.udevadm_cmd, "info", "--query",
"property",
type_query, value).read().split("\n")
type_query, value).read().split(b"\n")
return dict(x.partition("=")[0::2] for x in udev_output if "=" in x)
return dict(x.partition(b"=")[0::2] for x in udev_output if b"=" in x)
def info_export(self):
return files.process(self.udevadm_cmd, "info", "-e").read().strip()
@ -645,7 +645,7 @@ class DeviceFs(object):
self.fs = files.RealFs("/")
def pathjoin(self, *dns):
res = path.join("/", *(dn[1:] if dn.startswith("/") else dn
res = path.join(b"/", *(dn[1:] if dn.startswith(b"/") else dn
for dn in dns))
return res

@ -203,7 +203,7 @@ class process(StdoutableProcess):
"""Write to process stdin"""
self._open()
try:
self.pipe.stdin.write(data)
self.pipe.stdin.write(bytes(data, encoding="UTF-8"))
self.pipe.stdin.flush()
except IOError as e:
raise FilesError(str(e))
@ -271,8 +271,8 @@ class process(StdoutableProcess):
raise KeyboardInterrupt
finally:
if self.cacheresult is None:
self.cacheresult = "\n".join(self._cachedata)
self.cacheerr = "".join(_cacheerr)
self.cacheresult = (b'\n'.join(self._cachedata)).decode(encoding="UTF-8")
self.cacheerr = (b''.join(_cacheerr)).decode(encoding="UTF-8")
self.close()
def read(self):
@ -285,7 +285,7 @@ class process(StdoutableProcess):
else:
for line in self.readByLine():
pass
return str(self.cacheresult, "UTF-8")
return self.cacheresult
except KeyboardInterrupt:
self.kill()
raise KeyboardInterrupt
@ -294,7 +294,7 @@ class process(StdoutableProcess):
def readlines(self):
"""Read lines"""
return self.read().split('\n')
return self.read().decode(encoding="UTF-8").split('\n')
def __iter__(self):
"""Get iterator"""
@ -445,7 +445,7 @@ class UnixUtilityFile:
filter(lambda x: flags & x,
sorted(paramdict.keys())))
fileCmd = getProgPath('/usr/bin/file')
return [fileCmd, '-b'] + appendParam
return [fileCmd, '-b'] + list(appendParam)
def file(self, filename):
if path.exists(filename):
@ -556,7 +556,7 @@ class typeFile(object):
"""является ли файл бинарным"""
mime = self.getMType(filename)
if mime:
if mime.startswith("text"):
if mime.startswith(b'text'):
return False
else:
return True

@ -111,9 +111,9 @@ class VariableClTemplateCltPath(ReadonlyVariable):
"""
chroot = self.Get('cl_chroot_path')
if "CONFIG_PROTECT" in os.environ:
protect_paths = ["/etc"] + filter(
protect_paths = ["/etc"] + list(filter(
lambda x: x.strip(),
os.environ["CONFIG_PROTECT"].split(" "))
os.environ["CONFIG_PROTECT"].split(" ")))
else:
protect_paths = ["/etc", "/usr/share/X11/xkb", "var/lib/hsqldb",
"/usr/share/config"]

Loading…
Cancel
Save