|
|
|
@ -7,14 +7,14 @@ from jinja2 import Environment, FileSystemLoader
|
|
|
|
|
from calculate.variables.datavars import NamespaceNode, VariableNode,\
|
|
|
|
|
ListType, IntegerType,\
|
|
|
|
|
FloatType, IniType, TableType,\
|
|
|
|
|
Namespace, VariableError, HashType,\
|
|
|
|
|
Namespace, HashType,\
|
|
|
|
|
VariableNotFoundError
|
|
|
|
|
from calculate.utils.gentoo import ProfileWalker
|
|
|
|
|
from calculate.utils.files import read_file, FilesError
|
|
|
|
|
from calculate.utils.tools import Singleton
|
|
|
|
|
from calculate.utils.io_module import IOModule
|
|
|
|
|
from pyparsing import Literal, Word, ZeroOrMore, Group, Optional, restOfLine,\
|
|
|
|
|
empty, printables, OneOrMore, lineno, line, col, SkipTo,\
|
|
|
|
|
empty, printables, OneOrMore, lineno, line, SkipTo,\
|
|
|
|
|
LineEnd, Combine, nums
|
|
|
|
|
from enum import Enum
|
|
|
|
|
from contextlib import contextmanager
|
|
|
|
@ -36,8 +36,6 @@ class CalculateIniParser(metaclass=Singleton):
|
|
|
|
|
'''Класс парсера calculate.ini файлов.'''
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self._errors = []
|
|
|
|
|
|
|
|
|
|
self.operations = {"=": Define.assign,
|
|
|
|
|
"+=": Define.append,
|
|
|
|
|
"-=": Define.remove}
|
|
|
|
@ -73,8 +71,12 @@ class CalculateIniParser(metaclass=Singleton):
|
|
|
|
|
+ (row_index | clear_section | ~lbrack)
|
|
|
|
|
+ LineEnd().suppress())
|
|
|
|
|
|
|
|
|
|
def add_lineno(string, location, tokens):
|
|
|
|
|
tokens.append(lineno(location, string))
|
|
|
|
|
|
|
|
|
|
section_start = (namespace_start('namespace') |
|
|
|
|
|
table_start('table'))
|
|
|
|
|
section_start.setParseAction(add_lineno)
|
|
|
|
|
|
|
|
|
|
# Если содержимое ini-файла не предваряется заголовком секции,
|
|
|
|
|
# значит эта строка ошибочна.
|
|
|
|
@ -86,63 +88,62 @@ class CalculateIniParser(metaclass=Singleton):
|
|
|
|
|
+ value_operation + empty
|
|
|
|
|
+ restOfLine + LineEnd().suppress())
|
|
|
|
|
|
|
|
|
|
def strip_key_value(tokens):
|
|
|
|
|
def process_key_value(string, location, tokens):
|
|
|
|
|
tokens[0] = tokens[0].strip()
|
|
|
|
|
tokens[1] = tokens[1].strip()
|
|
|
|
|
tokens.append(lineno(location, string))
|
|
|
|
|
|
|
|
|
|
key_value.setParseAction(strip_key_value)
|
|
|
|
|
key_value.setParseAction(process_key_value)
|
|
|
|
|
|
|
|
|
|
self.ini_section_parser = (section_start
|
|
|
|
|
+ Group(ZeroOrMore(
|
|
|
|
|
Group(key_value | unexpected)))
|
|
|
|
|
Group(key_value
|
|
|
|
|
| unexpected)))
|
|
|
|
|
| unexpected)
|
|
|
|
|
self.ini_section_parser.ignore(comment)
|
|
|
|
|
|
|
|
|
|
def parse(self, data: str):
|
|
|
|
|
for tokens, start, end in self.ini_section_parser.scanString(data):
|
|
|
|
|
if tokens.getName() == "error":
|
|
|
|
|
if tokens[1].strip():
|
|
|
|
|
yield({'error': (tokens[0], tokens[1])})
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
section, defkeys = tokens
|
|
|
|
|
section, section_lineno, defkeys = tokens
|
|
|
|
|
|
|
|
|
|
if section.getName() == 'namespace':
|
|
|
|
|
section_list = section.asList()
|
|
|
|
|
if section_list[-1] == []:
|
|
|
|
|
yield {'clear_section': (section_list[:-1], )}
|
|
|
|
|
yield {'clear_section': (section_list[:-1],
|
|
|
|
|
section_lineno)}
|
|
|
|
|
else:
|
|
|
|
|
yield {'start_section': (section_list, )}
|
|
|
|
|
yield {'start_section': (section_list, section_lineno)}
|
|
|
|
|
|
|
|
|
|
for defkey in defkeys:
|
|
|
|
|
if defkey.getName() == "error":
|
|
|
|
|
if defkey[1].strip():
|
|
|
|
|
yield({'error': (defkey[0], defkey[1])})
|
|
|
|
|
continue
|
|
|
|
|
yield {'define_key': (defkey[0], defkey[2],
|
|
|
|
|
self.operations[defkey[1]])}
|
|
|
|
|
self.operations[defkey[1]],
|
|
|
|
|
defkey[3])}
|
|
|
|
|
else:
|
|
|
|
|
table_list = section.asList()
|
|
|
|
|
if table_list[-1] == []:
|
|
|
|
|
yield {'clear_table': (table_list[:-1], )}
|
|
|
|
|
else:
|
|
|
|
|
table_values = {}
|
|
|
|
|
for defkey in defkeys:
|
|
|
|
|
if defkey.getName() == "error":
|
|
|
|
|
continue
|
|
|
|
|
table_values.update({defkey[0]: defkey[2]})
|
|
|
|
|
yield {'start_table': (table_list, table_values)}
|
|
|
|
|
table_values = {}
|
|
|
|
|
for defkey in defkeys:
|
|
|
|
|
if defkey.getName() == "error":
|
|
|
|
|
if defkey[1].strip():
|
|
|
|
|
yield({'error': (defkey[0], defkey[1])})
|
|
|
|
|
continue
|
|
|
|
|
table_values.update({defkey[0]: defkey[2]})
|
|
|
|
|
yield {'start_table': (table_list, table_values,
|
|
|
|
|
section_lineno)}
|
|
|
|
|
|
|
|
|
|
def _unexpected_token(self, string, location, tokens):
|
|
|
|
|
'''Метод вызываемый парсером, если обнаружена некорректная строка,
|
|
|
|
|
предназначен для получения некорректной строки и ее дальнейшего
|
|
|
|
|
разбора.'''
|
|
|
|
|
error_line = line(location, string).strip()
|
|
|
|
|
if error_line:
|
|
|
|
|
self._errors.append((error_line, lineno(location, string),
|
|
|
|
|
col(location, string)))
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def errors(self):
|
|
|
|
|
errors = self._errors
|
|
|
|
|
self._errors = []
|
|
|
|
|
return errors
|
|
|
|
|
return [lineno(location, string), line(location, string)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NamespaceIniFiller:
|
|
|
|
@ -150,25 +151,21 @@ class NamespaceIniFiller:
|
|
|
|
|
из calculate.ini файла.'''
|
|
|
|
|
available_sections = {'custom'}
|
|
|
|
|
|
|
|
|
|
def __init__(self, restrict_creation=True, ouput=None):
|
|
|
|
|
def __init__(self, restrict_creation=True):
|
|
|
|
|
self.ini_parser = CalculateIniParser()
|
|
|
|
|
self._errors = []
|
|
|
|
|
|
|
|
|
|
# Флаги, определяющие возможность создания новых переменных и новых
|
|
|
|
|
# пространств имен в данном пространстве имен.
|
|
|
|
|
self.restricted = restrict_creation
|
|
|
|
|
self.modify_only = False
|
|
|
|
|
|
|
|
|
|
self.output = ouput
|
|
|
|
|
|
|
|
|
|
def error(self, lineno, error_message):
|
|
|
|
|
self.errors.append(lineno, error_message)
|
|
|
|
|
|
|
|
|
|
def fill(self, namespace: NamespaceNode, ini_file_text: str) -> None:
|
|
|
|
|
'''Метод для разбора calculate.ini файла и добавления всех его
|
|
|
|
|
переменных в указанное пространство имен.'''
|
|
|
|
|
self.namespace = namespace
|
|
|
|
|
self.current_namespace = self.namespace
|
|
|
|
|
self.errors = []
|
|
|
|
|
self._errors = []
|
|
|
|
|
|
|
|
|
|
for parsed_line in self.ini_parser.parse(ini_file_text):
|
|
|
|
|
self._line_processor(**parsed_line)
|
|
|
|
@ -176,7 +173,6 @@ class NamespaceIniFiller:
|
|
|
|
|
def _line_processor(self, start_section=None,
|
|
|
|
|
clear_section=None,
|
|
|
|
|
start_table=None,
|
|
|
|
|
clear_table=None,
|
|
|
|
|
define_key=None,
|
|
|
|
|
error=None, **kwargs):
|
|
|
|
|
'''Метод вызывающий обработку токенов, выдаваемых парсером в
|
|
|
|
@ -187,15 +183,12 @@ class NamespaceIniFiller:
|
|
|
|
|
self.clear_section(*clear_section)
|
|
|
|
|
elif start_table is not None:
|
|
|
|
|
self.start_table(*start_table)
|
|
|
|
|
elif clear_table is not None:
|
|
|
|
|
self.clear_table(*clear_table)
|
|
|
|
|
elif define_key is not None:
|
|
|
|
|
self.define_key(*define_key)
|
|
|
|
|
elif error is not None:
|
|
|
|
|
self._set_error(error[0], 'SyntaxError', error[1])
|
|
|
|
|
|
|
|
|
|
for error in self.ini_parser.errors:
|
|
|
|
|
self.set_error(*error)
|
|
|
|
|
|
|
|
|
|
def start_section(self, sections: str) -> None:
|
|
|
|
|
def start_section(self, sections: str, lineno) -> None:
|
|
|
|
|
'''Метод для получения доступа и создания пространств имен.'''
|
|
|
|
|
if self.restricted:
|
|
|
|
|
self.modify_only = sections[0] not in self.available_sections
|
|
|
|
@ -204,9 +197,12 @@ class NamespaceIniFiller:
|
|
|
|
|
for section in sections:
|
|
|
|
|
if isinstance(self.current_namespace, Datavars):
|
|
|
|
|
if section not in self.current_namespace:
|
|
|
|
|
# TODO Поменять на простой вывод.
|
|
|
|
|
raise VariableNotFoundError("variables package '{}' is not"
|
|
|
|
|
" found".format(section))
|
|
|
|
|
# TODO Поменять на логгирование.
|
|
|
|
|
self._set_error(lineno, 'VariableError',
|
|
|
|
|
"variables package '{}' is not found.".
|
|
|
|
|
format(section))
|
|
|
|
|
self.current_namespace = None
|
|
|
|
|
return
|
|
|
|
|
elif isinstance(self.current_namespace, NamespaceNode):
|
|
|
|
|
if section not in self.current_namespace.namespaces:
|
|
|
|
|
if (section in self.current_namespace.variables and
|
|
|
|
@ -216,26 +212,38 @@ class NamespaceIniFiller:
|
|
|
|
|
self.current_namespace = self.current_namespace.\
|
|
|
|
|
variables[section]
|
|
|
|
|
return
|
|
|
|
|
elif not self.modify_only:
|
|
|
|
|
self.current_namespace.add_namespace(
|
|
|
|
|
NamespaceNode(section))
|
|
|
|
|
else:
|
|
|
|
|
if not self.modify_only:
|
|
|
|
|
self.current_namespace.add_namespace(
|
|
|
|
|
NamespaceNode(section))
|
|
|
|
|
else:
|
|
|
|
|
self.current_namespace = None
|
|
|
|
|
return
|
|
|
|
|
# TODO Поменять на логгирование.
|
|
|
|
|
self._set_error(lineno, 'VariableError',
|
|
|
|
|
"can not create namespace '{}.{}' in"
|
|
|
|
|
" not 'custom' namespace.".format(
|
|
|
|
|
self.current_namespace.get_fullname(),
|
|
|
|
|
section))
|
|
|
|
|
self.current_namespace = None
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
self.current_namespace = self.current_namespace.namespaces[section]
|
|
|
|
|
|
|
|
|
|
def clear_section(self, sections: list) -> None:
|
|
|
|
|
def clear_section(self, sections: list, lineno) -> None:
|
|
|
|
|
'''Метод для очистки пространства имен.'''
|
|
|
|
|
if self.restricted:
|
|
|
|
|
self.modify_only = sections[0] not in self.available_sections
|
|
|
|
|
current_namespace = self.namespace
|
|
|
|
|
|
|
|
|
|
for section in sections:
|
|
|
|
|
if (isinstance(current_namespace, Datavars) and
|
|
|
|
|
section in current_namespace):
|
|
|
|
|
current_namespace = current_namespace[section]
|
|
|
|
|
if isinstance(current_namespace, Datavars):
|
|
|
|
|
if section in current_namespace:
|
|
|
|
|
current_namespace = current_namespace[section]
|
|
|
|
|
else:
|
|
|
|
|
self._set_error(lineno, 'VariableError',
|
|
|
|
|
"can not clear"" namespace '{}'. Variables"
|
|
|
|
|
" package '{}' is not found.".format(
|
|
|
|
|
".".join(sections),
|
|
|
|
|
section))
|
|
|
|
|
return
|
|
|
|
|
elif isinstance(current_namespace, NamespaceNode):
|
|
|
|
|
if section in current_namespace.namespaces:
|
|
|
|
|
current_namespace = current_namespace[section]
|
|
|
|
@ -248,11 +256,20 @@ class NamespaceIniFiller:
|
|
|
|
|
table_variable.source = table_to_clear
|
|
|
|
|
return
|
|
|
|
|
else:
|
|
|
|
|
self._set_error(lineno, 'VariableError',
|
|
|
|
|
"can not clear namespace '{}'. Namespace"
|
|
|
|
|
" is not found.".format(
|
|
|
|
|
".".join(sections)))
|
|
|
|
|
return
|
|
|
|
|
if not self.modify_only:
|
|
|
|
|
current_namespace.clear()
|
|
|
|
|
else:
|
|
|
|
|
self._set_error(lineno, 'VariableError',
|
|
|
|
|
"can not clear namespace '{}' from not 'custom'"
|
|
|
|
|
" namespace.".format(current_namespace.
|
|
|
|
|
get_fullname()))
|
|
|
|
|
|
|
|
|
|
def start_table(self, sections: str, row) -> None:
|
|
|
|
|
def start_table(self, sections: str, row, lineno) -> None:
|
|
|
|
|
'''Метод для создания и модификации таблиц.'''
|
|
|
|
|
if self.restricted:
|
|
|
|
|
self.modify_only = sections[0] not in self.available_sections
|
|
|
|
@ -265,15 +282,27 @@ class NamespaceIniFiller:
|
|
|
|
|
self.current_namespace.add_namespace(
|
|
|
|
|
NamespaceNode(section))
|
|
|
|
|
else:
|
|
|
|
|
self._set_error(lineno, 'VariableError',
|
|
|
|
|
"can not create table '{}.{}', namespace"
|
|
|
|
|
" '{}' is not found.".format(
|
|
|
|
|
".".join(sections),
|
|
|
|
|
table_name, section))
|
|
|
|
|
self.current_namespace = None
|
|
|
|
|
return
|
|
|
|
|
self.current_namespace = self.current_namespace.namespaces[section]
|
|
|
|
|
|
|
|
|
|
if table_name not in self.current_namespace.variables:
|
|
|
|
|
if not self.modify_only:
|
|
|
|
|
table_variable = VariableNode(table_name,
|
|
|
|
|
self.current_namespace,
|
|
|
|
|
variable_type=TableType,
|
|
|
|
|
source=[row])
|
|
|
|
|
else:
|
|
|
|
|
self._set_error(lineno, 'VariableError',
|
|
|
|
|
"can not create table '{}.{}' in not 'custom'"
|
|
|
|
|
" namespace.".format(self.current_namespace.
|
|
|
|
|
get_fullname(),
|
|
|
|
|
table_name))
|
|
|
|
|
else:
|
|
|
|
|
table_variable = self.current_namespace.variables[table_name]
|
|
|
|
|
table = table_variable.get_value()
|
|
|
|
@ -283,28 +312,28 @@ class NamespaceIniFiller:
|
|
|
|
|
table.add_row(row)
|
|
|
|
|
table_variable.source = table
|
|
|
|
|
|
|
|
|
|
def define_key(self, key: str, value: str, optype) -> None:
|
|
|
|
|
def define_key(self, key: str, value: str, optype, lineno) -> None:
|
|
|
|
|
'''Метод для создания и модификации переменных.'''
|
|
|
|
|
if self.current_namespace is None:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
if (isinstance(self.current_namespace, VariableNode) and
|
|
|
|
|
self.current_namespace.variable_type is HashType):
|
|
|
|
|
self.update_hash(key, value, optype)
|
|
|
|
|
self.update_hash(key, value, optype, lineno)
|
|
|
|
|
else:
|
|
|
|
|
if optype == Define.assign:
|
|
|
|
|
if key not in self.current_namespace:
|
|
|
|
|
self.define_variable(key, value)
|
|
|
|
|
self.define_variable(key, value, lineno)
|
|
|
|
|
else:
|
|
|
|
|
self.change_value(key, value)
|
|
|
|
|
elif optype == Define.append:
|
|
|
|
|
if key not in self.current_namespace:
|
|
|
|
|
self.define_variable(key, value)
|
|
|
|
|
self.define_variable(key, value, lineno)
|
|
|
|
|
else:
|
|
|
|
|
self.append_value(key, value)
|
|
|
|
|
elif optype == Define.remove:
|
|
|
|
|
if key not in self.current_namespace:
|
|
|
|
|
self.define_variable(key, value)
|
|
|
|
|
self.define_variable(key, value, lineno)
|
|
|
|
|
else:
|
|
|
|
|
self.remove_value(key, value)
|
|
|
|
|
|
|
|
|
@ -313,15 +342,15 @@ class NamespaceIniFiller:
|
|
|
|
|
variable = self.current_namespace[key]
|
|
|
|
|
variable.source = value
|
|
|
|
|
|
|
|
|
|
def define_variable(self, key: str, value: str) -> None:
|
|
|
|
|
def define_variable(self, key: str, value: str, lineno) -> None:
|
|
|
|
|
'''Метод для создания переменных в calculate.ini файле.'''
|
|
|
|
|
if not self.modify_only:
|
|
|
|
|
VariableNode(key, self.current_namespace, variable_type=IniType,
|
|
|
|
|
source=value)
|
|
|
|
|
else:
|
|
|
|
|
# TODO Какая-то обработка ошибки.
|
|
|
|
|
self.output.set_error("Can not create variable '{}.{}' in not"
|
|
|
|
|
" 'custom' namespace".format(
|
|
|
|
|
self._set_error(lineno, 'VariableError',
|
|
|
|
|
"can not create variable '{}.{}' in not 'custom'"
|
|
|
|
|
" namespace.".format(
|
|
|
|
|
self.current_namespace.get_fullname(),
|
|
|
|
|
key))
|
|
|
|
|
|
|
|
|
@ -382,25 +411,31 @@ class NamespaceIniFiller:
|
|
|
|
|
|
|
|
|
|
variable.source = variable_value
|
|
|
|
|
|
|
|
|
|
def update_hash(self, key: str, value: str, optype):
|
|
|
|
|
def update_hash(self, key: str, value: str, optype, lineno):
|
|
|
|
|
'''Метод для изменения переменных хэшей через calculate.ini.'''
|
|
|
|
|
hash_to_update = self.current_namespace.get_value().get_hash()
|
|
|
|
|
if key not in hash_to_update:
|
|
|
|
|
# Если ключ отсутствует в хэше, то проверяем, является ли он
|
|
|
|
|
# фиксированным.
|
|
|
|
|
if self.current_namespace.fixed:
|
|
|
|
|
raise VariableError("key '{}' is unavailable for fixed"
|
|
|
|
|
" hash, available keys: '{}'".
|
|
|
|
|
format(key,
|
|
|
|
|
', '.join(self.current_namespace.
|
|
|
|
|
get_value()._fields)))
|
|
|
|
|
self._set_error(lineno, 'VariableError',
|
|
|
|
|
"key '{}' is unavailable for fixed hash '{}',"
|
|
|
|
|
" available keys: '{}'.".format(
|
|
|
|
|
key,
|
|
|
|
|
self.current_namespace.get_fullname(),
|
|
|
|
|
', '.join(self.current_namespace.
|
|
|
|
|
get_value()._fields)))
|
|
|
|
|
return
|
|
|
|
|
else:
|
|
|
|
|
hash_to_update.update({key: value})
|
|
|
|
|
|
|
|
|
|
elif optype == Define.assign:
|
|
|
|
|
hash_to_update.update({key: value})
|
|
|
|
|
|
|
|
|
|
elif optype == Define.append:
|
|
|
|
|
current_value = hash_to_update[key]
|
|
|
|
|
hash_to_update[key] = current_value + value
|
|
|
|
|
|
|
|
|
|
elif optype == Define.remove:
|
|
|
|
|
current_value = hash_to_update[key]
|
|
|
|
|
if (isinstance(current_value, int) or
|
|
|
|
@ -414,11 +449,18 @@ class NamespaceIniFiller:
|
|
|
|
|
if value_to_remove in current_value:
|
|
|
|
|
current_value.remove(value_to_remove)
|
|
|
|
|
hash_to_update[key] = ','.join(current_value)
|
|
|
|
|
|
|
|
|
|
self.current_namespace.source = hash_to_update
|
|
|
|
|
|
|
|
|
|
def set_error(self, line, lineno, col):
|
|
|
|
|
def _set_error(self, lineno, error_type, line):
|
|
|
|
|
'''Метод для добавления ошибки в лог.'''
|
|
|
|
|
self.error(lineno, "Syntax error: {}".format(line))
|
|
|
|
|
self._errors.append("{}:{}: {}".format(error_type, lineno, line))
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def errors(self):
|
|
|
|
|
errors = self._errors
|
|
|
|
|
self._errors = []
|
|
|
|
|
return errors
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class VariableLoader:
|
|
|
|
@ -437,8 +479,10 @@ class VariableLoader:
|
|
|
|
|
def load_variables_package(self, package_name: str) -> None:
|
|
|
|
|
'''Метод для загрузки пакетов с переменными.'''
|
|
|
|
|
directory_path = os.path.join(self.variables_path, package_name)
|
|
|
|
|
|
|
|
|
|
package = '{}.{}'.format(self.variables_package, package_name)
|
|
|
|
|
package_namespace = NamespaceNode(package_name)
|
|
|
|
|
|
|
|
|
|
self.datavars.root.add_namespace(package_namespace)
|
|
|
|
|
self._fill_from_package(package_namespace, directory_path, package)
|
|
|
|
|
|
|
|
|
@ -471,7 +515,7 @@ class VariableLoader:
|
|
|
|
|
" variables.")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
self._fill_from_ini(profile_path)
|
|
|
|
|
self._fill_from_profile_ini(profile_path)
|
|
|
|
|
|
|
|
|
|
def load_user_variables(self):
|
|
|
|
|
'''Метод для загрузки переменных из calculate.ini указанных в
|
|
|
|
@ -485,8 +529,15 @@ class VariableLoader:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
for ini_file in env_order:
|
|
|
|
|
self.output.set_info("Loading variables from file: '{}'".format(
|
|
|
|
|
ini_file))
|
|
|
|
|
if ini_file in env_path:
|
|
|
|
|
self.fill_from_custom_ini(env_path[ini_file].value)
|
|
|
|
|
self.output.set_success("Variables from '{}' are loaded".
|
|
|
|
|
format(ini_file))
|
|
|
|
|
else:
|
|
|
|
|
self.output.set_warning("File '{}' is not found. Variables are"
|
|
|
|
|
" not loaded".format(ini_file))
|
|
|
|
|
|
|
|
|
|
def _fill_from_package(self, current_namespace: NamespaceNode,
|
|
|
|
|
directory_path: str, package: str) -> None:
|
|
|
|
@ -506,6 +557,7 @@ class VariableLoader:
|
|
|
|
|
Namespace.set_current_namespace(current_namespace)
|
|
|
|
|
# with self.test(file_name, current_namespace):
|
|
|
|
|
# importlib.import_module('{}.{}'.format(package, file_name))
|
|
|
|
|
|
|
|
|
|
spec = importlib.util.spec_from_file_location(
|
|
|
|
|
'{}.{}'.format(package, file_name),
|
|
|
|
|
file_node.path)
|
|
|
|
@ -520,7 +572,7 @@ class VariableLoader:
|
|
|
|
|
'{}.{}'.format(package,
|
|
|
|
|
directory_node.name))
|
|
|
|
|
|
|
|
|
|
def _fill_from_ini(self, profile_path):
|
|
|
|
|
def _fill_from_profile_ini(self, profile_path):
|
|
|
|
|
'''Метод для зaполнения переменных из ini-файла.'''
|
|
|
|
|
profile_walker = ProfileWalker(self.ini_basename,
|
|
|
|
|
self.repository_map)
|
|
|
|
@ -540,10 +592,23 @@ class VariableLoader:
|
|
|
|
|
|
|
|
|
|
def fill_from_custom_ini(self, file_path: str):
|
|
|
|
|
'''Метод для заполнения переменных из конкретного указанного файла.'''
|
|
|
|
|
self.output.set_info('Loading variables from file: {}'.format(
|
|
|
|
|
file_path))
|
|
|
|
|
if os.path.exists(file_path):
|
|
|
|
|
ini_file_text = read_file(file_path)
|
|
|
|
|
self.ini_filler.fill(self.datavars, ini_file_text)
|
|
|
|
|
|
|
|
|
|
parsing_errors = self.ini_filler.errors
|
|
|
|
|
if parsing_errors:
|
|
|
|
|
for error in parsing_errors:
|
|
|
|
|
self.output.set_error(error)
|
|
|
|
|
self.output.set_warning('Some variables was not loaded.')
|
|
|
|
|
else:
|
|
|
|
|
self.output.set_success('All variables are loaded.')
|
|
|
|
|
else:
|
|
|
|
|
self.output.set_error("Variables are not loaded. File '{}' does"
|
|
|
|
|
" not exist.".format(file_path))
|
|
|
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
|
def test(self, file_name, namespace):
|
|
|
|
|
'''Контекстный менеджер для тестирования.'''
|
|
|
|
@ -723,3 +788,5 @@ class Datavars:
|
|
|
|
|
dict_to_save = self.variables_to_save[target]
|
|
|
|
|
target_path = target_paths[target].value
|
|
|
|
|
saver.save_to_ini(target_path, dict_to_save)
|
|
|
|
|
self.output.set_info("Variables for '{}' is saved in the"
|
|
|
|
|
" file: {}".format(target, target_path))
|
|
|
|
|