113 lines
4.2 KiB
Python
113 lines
4.2 KiB
Python
"""Annotate properties
|
|
|
|
Created by:
|
|
EOLE (http://eole.orion.education.fr)
|
|
Copyright (C) 2005-2018
|
|
|
|
Forked by:
|
|
Cadoles (http://www.cadoles.com)
|
|
Copyright (C) 2019-2021
|
|
|
|
distribued with GPL-2 or later license
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program; if not, write to the Free Software
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
"""
|
|
from rougail.i18n import _
|
|
from rougail.error import DictConsistencyError
|
|
from rougail.annotator.variable import Walk
|
|
|
|
|
|
PROPERTIES = ('hidden', 'frozen', 'force_default_on_freeze',
|
|
'force_store_value', 'disabled', 'mandatory')
|
|
|
|
|
|
class Annotator(Walk):
|
|
"""Annotate properties
|
|
"""
|
|
level = 90
|
|
def __init__(self,
|
|
objectspace,
|
|
*args
|
|
) -> None:
|
|
self.objectspace = objectspace
|
|
if hasattr(self.objectspace.space, 'services'):
|
|
self.convert_services()
|
|
if hasattr(self.objectspace.space, 'variables'):
|
|
self.convert_family()
|
|
self.convert_variable()
|
|
|
|
def convert_property(self,
|
|
variable,
|
|
) -> None:
|
|
"""convert properties
|
|
"""
|
|
# hidden variable is also frozen
|
|
if isinstance(variable, self.objectspace.variable) and variable.hidden is True and \
|
|
variable.name != self.objectspace.rougailconfig['auto_freeze_variable']:
|
|
if not variable.auto_freeze and \
|
|
not hasattr(variable, 'provider'):
|
|
variable.frozen = True
|
|
if not variable.auto_save and \
|
|
not variable.auto_freeze and \
|
|
'force_default_on_freeze' not in vars(variable) and \
|
|
not hasattr(variable, 'provider'):
|
|
variable.force_default_on_freeze = True
|
|
if not hasattr(variable, 'properties'):
|
|
variable.properties = []
|
|
for prop in PROPERTIES:
|
|
if hasattr(variable, prop):
|
|
if getattr(variable, prop) is True:
|
|
# for subprop in CONVERT_PROPERTIES.get(prop, [prop]):
|
|
variable.properties.append(prop)
|
|
setattr(variable, prop, None)
|
|
if hasattr(variable, 'mode') and variable.mode:
|
|
variable.properties.append(variable.mode)
|
|
variable.mode = None
|
|
if 'force_store_value' in variable.properties and \
|
|
'force_default_on_freeze' in variable.properties: # pragma: no cover
|
|
# should not appened
|
|
msg = _('cannot have auto_freeze or auto_save with the hidden '
|
|
f'variable "{variable.name}"')
|
|
raise DictConsistencyError(msg, 50, variable.xmlfiles)
|
|
if not variable.properties:
|
|
del variable.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)
|
|
for family in service.family:
|
|
self.convert_property(family)
|
|
for variable in family.variable:
|
|
self.convert_property(variable)
|
|
|
|
def convert_family(self) -> None:
|
|
"""convert families
|
|
"""
|
|
for family in self.get_families():
|
|
self.convert_property(family)
|
|
|
|
def convert_variable(self) -> None:
|
|
"""convert variables
|
|
"""
|
|
for variable in self.get_variables():
|
|
self.convert_property(variable)
|