You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
calculate-utils-3-lib/pym/calculate/lib/format/ldap.py

186 lines
7.4 KiB

9 years ago
# -*- coding: utf-8 -*-
# Copyright 2008-2016 Mir Calculate. http://www.calculate-linux.org
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import re
9 years ago
from calculate.lib.cl_template import blocText
from calculate.lib.cl_xml import xmlDoc
from calculate.lib.format.samba import samba
9 years ago
class ldap(samba):
"""Класс для обработки конфигурационного файла типа ldap
"""
_comment = "#"
configName = "ldap"
configVersion = "0.1"
# Регулярное выражение для заголовка области
reHeader = re.compile("^[\t ]*(access|syncrepl)[^\n]+\n?")
# Регулярное выражения для области
reArea = re.compile("([\t ]*(access|syncrepl)[^\n]+\
9 years ago
\n([\t ]+[^\n]+\n?)+)", re.M | re.S)
reComment = re.compile("\s*%s.*" % _comment)
# разделитель между переменной и значением переменной
reSeparator = re.compile("\s+")
# разделитель полей
sepFields = "\n"
# регулярное выражение для разделителя полей
reSepFields = re.compile(sepFields)
9 years ago
def prepare(self):
self.blocTextObj = blocText()
9 years ago
self._splitToFields = self.splitToFields
# Объект документ
self.docObj = self._textToXML()
# Создаем поля-массивы
self.docObj.postParserList()
# Создаем поля разделенные массивы
self.docObj.postParserListSeplist(self.docObj.body)
# XML документ
self.doc = self.docObj.doc
def join(self, ldapObj):
"""Объединяем конфигурации"""
if isinstance(ldapObj, ldap):
self.docObj.joinDoc(ldapObj.doc)
def setDataField(self, txtLines, endtxtLines):
"""Создаем список объектов с переменными"""
9 years ago
class fieldData:
def __init__(self):
self.name = False
self.value = False
self.comment = False
self.br = False
9 years ago
fields = []
field = fieldData()
z = 0
for k in txtLines:
textLine = k + endtxtLines[z]
z += 1
findComment = self.reComment.search(textLine)
if not textLine.strip():
field.br = textLine
fields.append(field)
field = fieldData()
elif findComment:
field.comment = textLine
fields.append(field)
field = fieldData()
else:
pars = textLine.strip()
nameValue = self.reSeparator.split(pars)
if len(nameValue) > 2:
valueList = nameValue[2:]
9 years ago
nameValue = [nameValue[0] + nameValue[1],
" ".join(valueList)]
if len(nameValue) == 2:
name = nameValue[0]
9 years ago
value = nameValue[1].replace(self.sepFields, "")
field.name = name.replace(" ", "").replace("\t", "")
field.value = value
field.br = textLine
fields.append(field)
field = fieldData()
return fields
def _textToXML(self):
"""Преобразует текст в XML"""
9 years ago
blTmp = self.blocTextObj.findArea(self.text, self.reHeader, self.reArea)
blocs = self.getFullAreas(blTmp)
headers = []
docObj = xmlDoc()
docObj.createDoc(self.configName, self.configVersion)
rootNode = docObj.getNodeBody()
# Если пустой текст то создаем пустой документ
if not blocs:
return docObj
for h in blocs[0]:
headers.append(h.rstrip())
bodys = blocs[1]
z = 0
for h in headers:
if not bodys[z]:
z += 1
continue
areaAction = False
if h:
if h[0] == "!":
header = self.removeSymbolTerm(h.strip())
headerQuote = self.removeSymbolTerm(h)
9 years ago
docObj.createCaption(header, [headerQuote, ""])
areaAction = "drop"
elif h[0] == "-":
header = self.removeSymbolTerm(h.strip())
headerQuote = self.removeSymbolTerm(h)
9 years ago
docObj.createCaption(header, [headerQuote, ""])
areaAction = "replace"
else:
9 years ago
docObj.createCaption(h.strip(), [h.rstrip(), ""])
else:
9 years ago
docObj.createCaption(h.strip(), [h.rstrip(), ""])
if "\n" in blocs[0][z]:
resHead = self.reComment.search(h)
if resHead:
9 years ago
docObj.createField('comment',
blocs[0][z][resHead.start():])
else:
docObj.createField('br')
fields = self._splitToFields(bodys[z])
for f in fields:
9 years ago
if (f.name is not False and
f.value is not False and f.br is not False):
# Обработка условий для samba
9 years ago
if f.name[0] == "!" or f.name[0] == "-" or \
f.name[0] == "+":
qns = self.removeSymbolTerm(f.br)
xmlField = docObj.createField("var",
9 years ago
[qns],
f.name[1:], [f.value])
if f.name[0] == "!":
# Удаляемое в дальнейшем поле
docObj.setActionField(xmlField, "drop")
elif f.name[0] == "+":
# Добавляем уникальное поле
xmlField.setAttribute("type", "seplist")
docObj.setActionField(xmlField, "join")
else:
9 years ago
docObj.createField("var", [f.br.replace("\n", "")],
f.name, [f.value])
docObj.createField('br')
9 years ago
elif f.comment is not False:
docObj.createField('comment', [f.comment])
9 years ago
elif f.br is not False:
docObj.createField('br', [f.br.replace("\n", "")])
if h.strip():
area = docObj.createArea()
if areaAction:
docObj.setActionArea(area, areaAction)
rootNode.appendChild(area)
else:
fieldsNodes = docObj.tmpFields.getFields()
for fieldNode in fieldsNodes:
rootNode.appendChild(fieldNode)
docObj.clearTmpFields()
z += 1
return docObj