feat: choice if invalid value or unknown variable in user data is a fatal error or not

This commit is contained in:
egarette@silique.fr 2025-11-05 21:46:04 +01:00
parent 21f1526891
commit 2ab5aee3cf

View file

@ -49,16 +49,22 @@ class UserDatas:
self, self,
user_datas: List[dict], user_datas: List[dict],
*, *,
user_datas_type: str = "user_datas", invalid_user_datas_error: bool = False,
unknown_user_datas_error: bool = False,
): ):
self.values = {} self.values = {}
self.errors = [] self.errors = []
self.warnings = [] self.warnings = []
self.show_secrets = False if invalid_user_datas_error:
if user_datas_type == "user_datas": self.invalids = self.errors
self._populate_values(user_datas)
else: else:
self.values = user_datas self.invalids = self.warnings
if unknown_user_datas_error:
self.unknowns = self.errors
else:
self.unknowns = self.warnings
self.show_secrets = False
self._populate_values(user_datas)
self._auto_configure_dynamics() self._auto_configure_dynamics()
self._populate_config() self._populate_config()
self.properties_to_string = get_properties_to_string() self.properties_to_string = get_properties_to_string()
@ -126,7 +132,7 @@ class UserDatas:
lists = config.list(uncalculated=True) lists = config.list(uncalculated=True)
except PropertiesOptionError: except PropertiesOptionError:
lists = [] lists = []
for tconfig in []: for tconfig in lists:
if not tconfig.isdynamic(only_self=True): if not tconfig.isdynamic(only_self=True):
# it's not a dynamic variable # it's not a dynamic variable
continue continue
@ -139,7 +145,7 @@ class UserDatas:
msg = _( msg = _(
'cannot load variable path "{0}", the identifier "{1}" is not valid in {2}' 'cannot load variable path "{0}", the identifier "{1}" is not valid in {2}'
).format(path, identifier, data["source"]) ).format(path, identifier, data["source"])
self.warnings.append(msg) self.invalids.append(msg)
continue continue
if identifier is None: if identifier is None:
# it's a dynamic variable but doesn't match the current name # it's a dynamic variable but doesn't match the current name
@ -298,7 +304,7 @@ class UserDatas:
try: try:
if option.isoptiondescription(): if option.isoptiondescription():
if value: if value:
self.warnings.append( self.invalids.append(
_( _(
'cannot set the value "{0}" to the family {1}, it will be ignored when loading from {2}' 'cannot set the value "{0}" to the family {1}, it will be ignored when loading from {2}'
).format( ).format(
@ -308,21 +314,25 @@ class UserDatas:
) )
) )
continue continue
except PropertiesOptionError as err:
self.unknowns.append(
_("{0} loaded from {1}").format(err, options["source"])
)
except AttributeOptionError as err: except AttributeOptionError as err:
if err.code == "option-not-found": if err.code == "option-not-found":
self.warnings.append( self.unknowns.append(
_( _(
'variable or family "{0}" does not exist, it will be ignored when loading from {1}' 'variable or family "{0}" does not exist, it will be ignored when loading from {1}'
).format(err.path, options["source"]) ).format(err.path, options["source"])
) )
elif err.code == "option-dynamic": elif err.code == "option-dynamic":
self.warnings.append( self.invalids.append(
_( _(
'"{0}" is the name of a dynamic family, it will be ignored when loading from {1}' '"{0}" is the name of a dynamic family, it will be ignored when loading from {1}'
).format(option.description(with_quote=True), options["source"]) ).format(option.description(with_quote=True), options["source"])
) )
else: else:
self.warnings.append( self.invalids.append(
_("{0} loaded from {1}").format(err, options["source"]) _("{0} loaded from {1}").format(err, options["source"])
) )
continue continue
@ -350,7 +360,7 @@ class UserDatas:
display_name = option.description(with_quote=True) display_name = option.description(with_quote=True)
if index is not None: if index is not None:
if path == err_path: if path == err_path:
self.warnings.append( self.unknowns.append(
_( _(
'variable {0} at index "{1}" is {2}, it will be ignored when loading from {3}' 'variable {0} at index "{1}" is {2}, it will be ignored when loading from {3}'
).format( ).format(
@ -361,7 +371,7 @@ class UserDatas:
) )
) )
else: else:
self.warnings.append( self.unknowns.append(
_( _(
'family {0} is {1}, {2} at index "{3}" will be ignored when loading from {4}' 'family {0} is {1}, {2} at index "{3}" will be ignored when loading from {4}'
).format( ).format(
@ -374,7 +384,7 @@ class UserDatas:
) )
else: else:
if path == err_path: if path == err_path:
self.warnings.append( self.unknowns.append(
_( _(
"variable {0} is {1}, it will be ignored when loading from {2}" "variable {0} is {1}, it will be ignored when loading from {2}"
).format( ).format(
@ -382,7 +392,7 @@ class UserDatas:
) )
) )
else: else:
self.warnings.append( self.unknowns.append(
_( _(
"family {0} is {1}, {2} will be ignored when loading from {3}" "family {0} is {1}, {2} will be ignored when loading from {3}"
).format( ).format(
@ -393,16 +403,16 @@ class UserDatas:
) )
) )
else: else:
self.warnings.append( self.unknowns.append(
_("{0} in {1}").format(err, options["source"]) _("{0} in {1}").format(err, options["source"])
) )
except LeadershipError as err: except LeadershipError as err:
self.warnings.append(_("{0} in {1}").format(err, options["source"])) self.unknowns.append(_("{0} in {1}").format(err, options["source"]))
except ValueError as err: except ValueError as err:
err.prefix = "" err.prefix = ""
if index is not None: if index is not None:
type_ = option.type(translation=True) type_ = option.type(translation=True)
self.warnings.append( self.invalids.append(
_( _(
'the value "{0}" is an invalid {1} for {2} at index "{3}", {4}, it will be ignored when loading from {5}' 'the value "{0}" is an invalid {1} for {2} at index "{3}", {4}, it will be ignored when loading from {5}'
).format( ).format(
@ -416,7 +426,7 @@ class UserDatas:
) )
else: else:
type_ = option.type(translation=True) type_ = option.type(translation=True)
self.warnings.append( self.invalids.append(
_( _(
'the value "{0}" is an invalid {1} for {2}, {3}, it will be ignored when loading from {4}' 'the value "{0}" is an invalid {1} for {2}, {3}, it will be ignored when loading from {4}'
).format( ).format(