rougail/src/rougail/annotator/property.py

84 lines
3.3 KiB
Python

"""Annotate properties
"""
from ..i18n import _
from ..error import DictConsistencyError
PROPERTIES = ('hidden', 'frozen', 'auto_freeze', 'auto_save', 'force_default_on_freeze',
'force_store_value', 'disabled', 'mandatory')
CONVERT_PROPERTIES = {'auto_save': ['force_store_value'],
'auto_freeze': ['force_store_value', 'auto_freeze'],
}
class PropertyAnnotator:
"""Annotate properties
"""
def __init__(self, objectspace):
self.objectspace = objectspace
if hasattr(self.objectspace.space, 'services'):
self.convert_services()
if hasattr(self.objectspace.space, 'variables'):
self.convert_variables()
def convert_property(self,
variable,
) -> None:
"""convert properties
"""
properties = []
for prop in PROPERTIES:
if hasattr(variable, prop):
if getattr(variable, prop) is True:
for subprop in CONVERT_PROPERTIES.get(prop, [prop]):
properties.append(subprop)
setattr(variable, prop, None)
if hasattr(variable, 'mode') and variable.mode:
properties.append(variable.mode)
variable.mode = None
if 'force_store_value' in properties and 'force_default_on_freeze' in properties:
xmlfiles = self.objectspace.display_xmlfiles(variable.xmlfiles)
msg = _('cannot have auto_freeze or auto_store with the hidden '
f'variable "{variable.name}" in {xmlfiles}')
raise DictConsistencyError(msg, 50)
if properties:
variable.properties = frozenset(properties)
def convert_services(self) -> None:
"""convert services
"""
self.convert_property(self.objectspace.space.services)
for services in self.objectspace.space.services.service.values():
self.convert_property(services)
for service in vars(services).values():
if not isinstance(service, self.objectspace.family):
continue
self.convert_property(service)
if not hasattr(service, 'family'):
continue
self.convert_property(service)
for family in service.family:
self.convert_property(family)
if not hasattr(family, 'variable'):
continue
for variable in family.variable:
self.convert_property(variable)
def convert_variables(self) -> None:
"""convert variables
"""
for variables in self.objectspace.space.variables.values():
if not hasattr(variables, 'family'):
continue
for family in variables.family.values():
self.convert_property(family)
if not hasattr(family, 'variable'):
continue
for variable in family.variable.values():
if isinstance(variable, self.objectspace.leadership):
self.convert_property(variable)
for follower in variable.variable:
self.convert_property(follower)
else:
self.convert_property(variable)