|
|
|
@ -533,8 +533,11 @@ class DependenceSource:
|
|
|
|
|
if l_var != r_var:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
if not self._compare_depend_functions(self.depend_function,
|
|
|
|
|
other.depend_function):
|
|
|
|
|
if self.depend_function == other.depend_function:
|
|
|
|
|
return True
|
|
|
|
|
if (self.depend_function is None or other.depend_function is None
|
|
|
|
|
or not self._compare_depend_functions(self.depend_function,
|
|
|
|
|
other.depend_function)):
|
|
|
|
|
return False
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
@ -677,6 +680,9 @@ class VariableNode:
|
|
|
|
|
# Если источники не совпадают или текущее значение переменной было
|
|
|
|
|
# установлено пользователем, то инвалидируем переменную и меняем
|
|
|
|
|
# источник.
|
|
|
|
|
if self.name == "machine":
|
|
|
|
|
print('self.source =', self.source)
|
|
|
|
|
print('source =', source)
|
|
|
|
|
if self._source != source or self.set_by_user:
|
|
|
|
|
self.set_by_user = False
|
|
|
|
|
self._invalidate()
|
|
|
|
@ -760,57 +766,57 @@ class NamespaceNode:
|
|
|
|
|
'''Класс ноды соответствующей пространству имен в дереве переменных.'''
|
|
|
|
|
def __init__(self, name='', parent=None):
|
|
|
|
|
self._name = name
|
|
|
|
|
self.variables = dict()
|
|
|
|
|
self.namespaces = dict()
|
|
|
|
|
self.parent = parent
|
|
|
|
|
self._variables = dict()
|
|
|
|
|
self._namespaces = dict()
|
|
|
|
|
self._parent = parent
|
|
|
|
|
|
|
|
|
|
def add_variable(self, variable: VariableNode) -> None:
|
|
|
|
|
'''Метод для добавления переменной в пространство имен.'''
|
|
|
|
|
if variable.name in self.namespaces:
|
|
|
|
|
if variable.name in self._namespaces:
|
|
|
|
|
raise VariableError("namespace with the name '{}' is already in"
|
|
|
|
|
" the namespace '{}'".format(
|
|
|
|
|
variable.name,
|
|
|
|
|
self.get_fullname()))
|
|
|
|
|
self.variables.update({variable.name: variable})
|
|
|
|
|
self._variables.update({variable.name: variable})
|
|
|
|
|
variable.namespace = self
|
|
|
|
|
|
|
|
|
|
def add_namespace(self, namespace) -> None:
|
|
|
|
|
'''Метод для добавления пространства имен в пространство имен.'''
|
|
|
|
|
if namespace._name in self.variables:
|
|
|
|
|
if namespace._name in self._variables:
|
|
|
|
|
raise VariableError("variable with the name '{}' is already in"
|
|
|
|
|
" the namespace '{}'".format(
|
|
|
|
|
namespace._name,
|
|
|
|
|
self.get_fullname()))
|
|
|
|
|
self.namespaces.update({namespace._name: namespace})
|
|
|
|
|
namespace.parent = self
|
|
|
|
|
self._namespaces.update({namespace._name: namespace})
|
|
|
|
|
namespace._parent = self
|
|
|
|
|
|
|
|
|
|
def clear(self):
|
|
|
|
|
'''Метод для очистки пространства имен. Очищает и пространства имен
|
|
|
|
|
и переменные. Предназначен только для использования в calculate.ini.'''
|
|
|
|
|
for namespace_name in self.namespaces.keys():
|
|
|
|
|
self.namespaces[namespace_name].clear()
|
|
|
|
|
self.variables.clear()
|
|
|
|
|
self.namespaces.clear()
|
|
|
|
|
for namespace_name in self._namespaces.keys():
|
|
|
|
|
self._namespaces[namespace_name].clear()
|
|
|
|
|
self._variables.clear()
|
|
|
|
|
self._namespaces.clear()
|
|
|
|
|
|
|
|
|
|
def get_fullname(self) -> str:
|
|
|
|
|
'''Метод для получения полного имени пространства имен.'''
|
|
|
|
|
if self.parent is not None and self.parent._name != '<root>':
|
|
|
|
|
return '{}.{}'.format(self.parent.get_fullname(), self._name)
|
|
|
|
|
if self._parent is not None and self._parent._name != '<root>':
|
|
|
|
|
return '{}.{}'.format(self._parent.get_fullname(), self._name)
|
|
|
|
|
else:
|
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
|
|
def get_package_name(self) -> str:
|
|
|
|
|
if self.parent._name == '<root>':
|
|
|
|
|
if self._parent._name == '<root>':
|
|
|
|
|
return self._name
|
|
|
|
|
else:
|
|
|
|
|
return self.parent.get_package_name()
|
|
|
|
|
return self._parent.get_package_name()
|
|
|
|
|
|
|
|
|
|
def __getattr__(self, name: str):
|
|
|
|
|
'''Метод возвращает ноду пространства имен или значение переменной.'''
|
|
|
|
|
if name in self.namespaces:
|
|
|
|
|
return self.namespaces[name]
|
|
|
|
|
elif name in self.variables:
|
|
|
|
|
variable = self.variables[name]
|
|
|
|
|
if name in self._namespaces:
|
|
|
|
|
return self._namespaces[name]
|
|
|
|
|
elif name in self._variables:
|
|
|
|
|
variable = self._variables[name]
|
|
|
|
|
if variable.variable_type is TableType:
|
|
|
|
|
return variable.get_value().get_table()
|
|
|
|
|
return variable.get_value()
|
|
|
|
@ -824,10 +830,10 @@ class NamespaceNode:
|
|
|
|
|
|
|
|
|
|
def __getitem__(self, name: str) -> None:
|
|
|
|
|
'''Метод возвращает ноду пространства имен или ноду переменной.'''
|
|
|
|
|
if name in self.namespaces:
|
|
|
|
|
return self.namespaces[name]
|
|
|
|
|
elif name in self.variables:
|
|
|
|
|
return self.variables[name]
|
|
|
|
|
if name in self._namespaces:
|
|
|
|
|
return self._namespaces[name]
|
|
|
|
|
elif name in self._variables:
|
|
|
|
|
return self._variables[name]
|
|
|
|
|
else:
|
|
|
|
|
if self.get_package_name() == "custom":
|
|
|
|
|
return None
|
|
|
|
@ -837,7 +843,7 @@ class NamespaceNode:
|
|
|
|
|
namespace_name=self._name))
|
|
|
|
|
|
|
|
|
|
def __contains__(self, name):
|
|
|
|
|
return name in self.namespaces or name in self.variables
|
|
|
|
|
return name in self._namespaces or name in self._variables
|
|
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
|
return '<Namespace: {}>'.format(self.get_fullname())
|
|
|
|
@ -880,7 +886,7 @@ class DependenceAPI(metaclass=Singleton):
|
|
|
|
|
namespace = current_namespace
|
|
|
|
|
for index in range(1, len(name_parts)):
|
|
|
|
|
if not name_parts[index]:
|
|
|
|
|
namespace = namespace.parent
|
|
|
|
|
namespace = namespace._parent
|
|
|
|
|
else:
|
|
|
|
|
name_parts = name_parts[index:]
|
|
|
|
|
break
|
|
|
|
@ -945,11 +951,17 @@ class VariableAPI(metaclass=Singleton):
|
|
|
|
|
def __call__(self, name: str, source=None, type=VariableType,
|
|
|
|
|
readonly=False, fixed=False, force=False):
|
|
|
|
|
'''Метод для создания переменных внутри with Namespace('name').'''
|
|
|
|
|
if name not in self.current_namespace.variables:
|
|
|
|
|
if name == "machine":
|
|
|
|
|
print("creating var...")
|
|
|
|
|
|
|
|
|
|
if name not in self.current_namespace._variables:
|
|
|
|
|
variable = VariableNode(name, self.current_namespace)
|
|
|
|
|
else:
|
|
|
|
|
variable = self.current_namespace[name]
|
|
|
|
|
|
|
|
|
|
if name == "machine":
|
|
|
|
|
print('using dependence')
|
|
|
|
|
|
|
|
|
|
if isinstance(source, DependenceSource):
|
|
|
|
|
try:
|
|
|
|
|
source.check()
|
|
|
|
@ -1021,11 +1033,11 @@ class NamespaceAPI(metaclass=Singleton):
|
|
|
|
|
@contextmanager
|
|
|
|
|
def __call__(self, namespace_name):
|
|
|
|
|
'''Метод для создания пространств имен с помощью with.'''
|
|
|
|
|
if namespace_name not in self.current_namespace.namespaces:
|
|
|
|
|
if namespace_name not in self.current_namespace._namespaces:
|
|
|
|
|
namespace = NamespaceNode(namespace_name,
|
|
|
|
|
parent=self.current_namespace)
|
|
|
|
|
else:
|
|
|
|
|
namespace = self.current_namespace.namespaces[namespace_name]
|
|
|
|
|
namespace = self.current_namespace._namespaces[namespace_name]
|
|
|
|
|
|
|
|
|
|
self.current_namespace.add_namespace(namespace)
|
|
|
|
|
self.current_namespace = namespace
|
|
|
|
@ -1039,7 +1051,7 @@ class NamespaceAPI(metaclass=Singleton):
|
|
|
|
|
try:
|
|
|
|
|
yield self
|
|
|
|
|
finally:
|
|
|
|
|
self.current_namespace = self.current_namespace.parent
|
|
|
|
|
self.current_namespace = self.current_namespace._parent
|
|
|
|
|
self._variables_fabric.current_namespace = self.current_namespace
|
|
|
|
|
self._dependence_fabric.current_namespace = self.current_namespace
|
|
|
|
|
|
|
|
|
|