The code of the NamespaceIniFiller and the CalculateIniParser is refactored.

packages
Иванов Денис 4 years ago
parent b1b383ea6a
commit f66b89f6b0

@ -92,7 +92,7 @@ class RegexFormat(BaseFormat):
def _patch_document(self, document_to_patch):
'''Метод, обходящий теги шаблона и использующий указанные в нем
регулярные выражения.'''
patch_iterator = self._parsed_patch.getiterator()
patch_iterator = self._parsed_patch.iter()
PATCH_DOCUMENT_TAGS = ('reg', 'text')
patch_element = next(patch_iterator, False)

@ -7,9 +7,9 @@ from enum import Enum
class CalculateIniParser:
'''Класс парсера calculate.ini файлов.'''
class Define(Enum):
Assign = 0
Append = 1
Remove = 2
assign = 0
append = 1
remove = 2
def __init__(self):
lbrack = Literal("[")
@ -64,43 +64,46 @@ class CalculateIniParser:
разбора.'''
error_line = line(location, string).strip()
if error_line:
self.parseError(error_line, lineno(location, string),
col(location, string))
self.parse_error(error_line, lineno(location, string),
col(location, string))
def parse(self, data):
for tokens, start, end in self.ini_section_parser.scanString(data):
if tokens.getName() == "error":
continue
section, defkeys = tokens
sectionList = section.asList()
if sectionList[-1] == []:
self.clearSection(sectionList[:-1])
section_list = section.asList()
if section_list[-1] == []:
self.clear_section(section_list[:-1])
continue
self.startSection(section.asList())
self.start_section(section.asList())
for defkey in defkeys:
if defkey.getName() == "error":
continue
mapOp = {"=": self.Define.Assign,
"+=": self.Define.Append,
"-=": self.Define.Remove}
self.defineKey(section.asList(),
defkey[0], defkey[2],
mapOp[defkey[1]])
def startSection(self, section):
mapOp = {"=": self.Define.assign,
"+=": self.Define.append,
"-=": self.Define.remove}
self.define_key(section.asList(),
defkey[0], defkey[2],
mapOp[defkey[1]])
def start_section(self, section):
"""Начало секции"""
pass
def clearSection(self, section):
def clear_section(self, section):
'''Метод для очистки секций.'''
pass
def defineKey(self, section, defkey, defval, deftype):
def define_key(self, section, defkey, defval, deftype):
'''Метод для определения ключа.
['section','block'], 'varname', 'varval', Define.Assign'''
['section','block'], 'varname', 'varval', Define.assign'''
pass
def parseError(self, line, lineno, col):
def parse_error(self, line, lineno, col):
'''Метод для обработки ошибок, обнаруженных в ini-файле.
line - пример строки;
lineno - номер строки;

@ -375,9 +375,7 @@ class Namespace(BaseClass):
class HashVariable(Namespace):
"""
Переменная представляет собой словарь
"""
"""Классс переменных представляющих собой словарь."""
BASE_CLASS = "HashVariable"
class HashValue(Variable):

@ -24,14 +24,14 @@ class NamespaceIniFiller(CalculateIniParser):
self.errors = []
self.parse(data)
def startSection(self, sections):
def start_section(self, sections):
self.curns = self.ns
for section in sections:
if section not in self.curns.childs:
self.curns.addNamespace(Namespace(section))
self.curns = self.curns[section]
def clearSection(self, sections):
def clear_section(self, sections):
curns = self.ns
for section in sections:
if section not in curns.childs:
@ -39,47 +39,47 @@ class NamespaceIniFiller(CalculateIniParser):
curns = curns[section]
curns.clearChilds()
def changeValue(self, key, value):
def change_value(self, key, value):
self.curns[key].setValue(value)
def defineVariable(self, key, value):
def define_variable(self, key, value):
self.curns.addStringVariable(key, value)
def appendValue(self, key, value):
def append_value(self, key, value):
l = self.curns[key].getValue().split(",")
vlist = value.split(",")
for v in vlist:
if v not in l:
l.append(v)
self.changeValue(key, ",".join(l))
self.change_value(key, ",".join(l))
def removeValue(self, key, value):
def remove_value(self, key, value):
l = self.curns[key].getValue().split(",")
vlist = value.split(",")
for v in vlist:
if v in l:
l.remove(v)
self.changeValue(key, ",".join(l))
self.change_value(key, ",".join(l))
def defineKey(self, section, key, value, optype):
def define_key(self, section, key, value, optype):
Define = CalculateIniParser.Define
if optype == Define.Assign:
if optype == Define.assign:
if key not in self.curns:
self.defineVariable(key, value)
self.define_variable(key, value)
else:
self.changeValue(key, value)
elif optype == Define.Append:
self.change_value(key, value)
elif optype == Define.append:
if key not in self.curns:
self.defineVariable(key, value)
self.define_variable(key, value)
else:
self.appendValue(key, value)
elif optype == Define.Remove:
self.append_value(key, value)
elif optype == Define.remove:
if key not in self.curns:
self.defineVariable(key, value)
self.define_variable(key, value)
else:
self.removeValue(key, value)
self.remove_value(key, value)
def parseError(self, line, lineno, col):
def parse_error(self, line, lineno, col):
self.error(lineno, _("Syntax error: %s") % line)
@ -97,7 +97,7 @@ class NamespaceIniFillerStrict(NamespaceIniFiller):
ns.addNamespace(Namespace(newns))
super().fill(ns, data)
def startSection(self, sections):
def start_section(self, sections):
self.curns = self.ns
self.canCreate = sections[0] in self.availableSection
for section in sections:
@ -108,7 +108,7 @@ class NamespaceIniFillerStrict(NamespaceIniFiller):
self.curns = None
self.curns = self.curns[section]
#def clearSection(self, sections):
#def clear_section(self, sections):
# curns = self.ns
# self.canCreate = sections[0] in self.availableSection
# for section in sections:
@ -117,14 +117,14 @@ class NamespaceIniFillerStrict(NamespaceIniFiller):
# curns = curns[section]
# curns.clearChilds()
def defineVariable(self, key, value):
def define_variable(self, key, value):
if not self.canCreate:
pass
var = Variable(key)
var.addProperty(IniCreated(value))
self.curns.addVariable(var)
def changeValue(self, key, value):
def change_value(self, key, value):
var = self.curns[key]
var.setValue(value)
@ -145,8 +145,8 @@ class VariableLoader:
Объект используемый для загрузки переменных из python модуля
"""
reUpper = re.compile("(.)([A-Z])")
def _getVarlikeAttrs(self, obj):
def _get_varlike_attrs(self, obj):
"""
Получить список аттрибутов похожих на переменные
"""
@ -165,14 +165,14 @@ class VariableLoader:
self.fill(newns, fullfn, "%s.%s"%(package,fn))
elif fn.endswith(".py"):
module = self._load_module_source(package, fn, fullfn)
for varname, cls in self._getVarlikeAttrs(module):
for varname, cls in self._get_varlike_attrs(module):
if Variable.isImplementation(cls):
ns.addVariable(cls(varname))
elif HashVariable.isImplementation(cls) or \
TableVariable.isImplementation(cls) or \
Namespace.isImplementation(cls):
_newns = ns.addNamespace(cls(varname,ns))
for _varname, _cls in self._getVarlikeAttrs(cls):
for _varname, _cls in self._get_varlike_attrs(cls):
if Variable.isImplementation(_cls):
_newns.addVariable(_cls(_varname))
@ -205,7 +205,7 @@ class ProfileFiller:
Заполнитель значений переменных из файлов calculate.ini в профилях
"""
basename = "calculate.ini"
def getRepositoryMap(self, ns):
def get_repository_map(self, ns):
return {
x.name.getValue(): x.path.getValue()
for x in ns.os.gentoo.repositories
@ -213,6 +213,6 @@ class ProfileFiller:
def fill(self, ns, profile_path):
nif = NamespaceIniFillerStrict()
pw = ProfileWalker(self.basename, self.getRepositoryMap(ns))
pw = ProfileWalker(self.basename, self.get_repository_map(ns))
for fn in pw.find(profile_path):
nif.fill(ns, readFile(fn))

@ -14,9 +14,9 @@ class TestCalculateIni:
def test_section_values(self, mocker):
cip = CalculateIniParser()
spy_section = mocker.spy(cip, 'startSection')
spy_def_key = mocker.spy(cip, 'defineKey')
spy_error = mocker.spy(cip, 'parseError')
spy_section = mocker.spy(cip, 'start_section')
spy_def_key = mocker.spy(cip, 'define_key')
spy_error = mocker.spy(cip, 'parse_error')
cip.parse("[section]\n"
"varval1 = value1\n")
@ -24,14 +24,14 @@ class TestCalculateIni:
spy_error.assert_not_called()
spy_section.assert_has_calls([call(['section'])])
spy_def_key.assert_has_calls([
call(['section'], 'varval1', 'value1', Define.Assign),
call(['section'], 'varval1', 'value1', Define.assign),
])
def test_simple_calculate_ini_with_comments(self, mocker):
cip = CalculateIniParser()
spy_section = mocker.spy(cip, 'startSection')
spy_def_key = mocker.spy(cip, 'defineKey')
spy_error = mocker.spy(cip, 'parseError')
spy_section = mocker.spy(cip, 'start_section')
spy_def_key = mocker.spy(cip, 'define_key')
spy_error = mocker.spy(cip, 'parse_error')
cip.parse("[section]\n"
"varval1 = value1\n"
@ -41,16 +41,16 @@ class TestCalculateIni:
spy_error.assert_not_called()
spy_section.assert_has_calls([call(['section'])])
spy_def_key.assert_has_calls([
call(['section'], 'varval1', 'value1', Define.Assign),
call(['section'], 'varval2', 'value2', Define.Append),
call(['section'], 'varval3', 'value3', Define.Remove),
call(['section'], 'varval1', 'value1', Define.assign),
call(['section'], 'varval2', 'value2', Define.append),
call(['section'], 'varval3', 'value3', Define.remove),
])
def test_some_complex_section_calculate_ini(self, mocker):
cip = CalculateIniParser()
spy_section = mocker.spy(cip, 'startSection')
spy_def_key = mocker.spy(cip, 'defineKey')
spy_error = mocker.spy(cip, 'parseError')
spy_section = mocker.spy(cip, 'start_section')
spy_def_key = mocker.spy(cip, 'define_key')
spy_error = mocker.spy(cip, 'parse_error')
cip.parse("[section][sub]\n"
"varval1 = value1\n"
@ -66,17 +66,17 @@ class TestCalculateIni:
spy_section.assert_has_calls([call(['section', 'sub2'])])
spy_section.assert_has_calls([call(['section2'])])
spy_def_key.assert_has_calls([
call(['section', 'sub'], 'varval1', 'value1', Define.Assign),
call(['section', 'sub'], 'varval2', 'value2', Define.Assign),
call(['section', 'sub2'], 'varval1', 'value1', Define.Assign),
call(['section2'], 'varval1', 'value1', Define.Assign),
call(['section', 'sub'], 'varval1', 'value1', Define.assign),
call(['section', 'sub'], 'varval2', 'value2', Define.assign),
call(['section', 'sub2'], 'varval1', 'value1', Define.assign),
call(['section2'], 'varval1', 'value1', Define.assign),
])
def test_error(self, mocker):
cip = CalculateIniParser()
spy_section = mocker.spy(cip, 'startSection')
spy_def_key = mocker.spy(cip, 'defineKey')
spy_error = mocker.spy(cip, 'parseError')
spy_section = mocker.spy(cip, 'start_section')
spy_def_key = mocker.spy(cip, 'define_key')
spy_error = mocker.spy(cip, 'parse_error')
cip.parse("[section\n"
"varval1 = value1\n"
@ -96,8 +96,8 @@ class TestCalculateIni:
# проверяем, что значение из криво определённой section2 попало
# в section.sub2
spy_def_key.assert_has_calls([
call(['section', 'sub2'], 'varval1', 'value1', Define.Assign),
call(['section', 'sub2'], 'varval4', 'value4', Define.Assign),
call(['section', 'sub2'], 'varval1', 'value1', Define.assign),
call(['section', 'sub2'], 'varval4', 'value4', Define.assign),
])
# проверяем, все нераспознанные строки попали в ошибки
# криво объявленная первая секция
@ -116,10 +116,10 @@ class TestCalculateIni:
def test_clear_section(self, mocker):
cip = CalculateIniParser()
spy_section = mocker.spy(cip, 'startSection')
spy_def_key = mocker.spy(cip, 'defineKey')
spy_error = mocker.spy(cip, 'parseError')
spy_clear_sec = mocker.spy(cip, 'clearSection')
spy_section = mocker.spy(cip, 'start_section')
spy_def_key = mocker.spy(cip, 'define_key')
spy_error = mocker.spy(cip, 'parse_error')
spy_clear_sec = mocker.spy(cip, 'clear_section')
cip.parse("[section][test][]\n")

@ -730,7 +730,7 @@ class TestNamespace:
vl.fill(ns, "tests/vars/variables", "testvars")
class ProfileFillerTest(ProfileFiller):
def getRepositoryMap(self, ns):
def get_repository_map(self, ns):
curdir = os.getcwd()
return {'distros':
os.path.join(curdir,

Loading…
Cancel
Save