Skip to content

Commit

Permalink
New feature: DefaultItems.
Browse files Browse the repository at this point in the history
  • Loading branch information
lhupfeldt committed May 31, 2020
1 parent c6687e5 commit d7a8cd7
Show file tree
Hide file tree
Showing 10 changed files with 1,621 additions and 65 deletions.
4 changes: 2 additions & 2 deletions multiconf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# pylint: disable=unused-import

from . import py_version_check
from .multiconf import McConfigRoot, AbstractConfigItem, ConfigItem, RepeatableConfigItem, ConfigBuilder
from .multiconf import McConfigRoot, AbstractConfigItem, ConfigItem, RepeatableConfigItem, DefaultItems, ConfigBuilder
from .decorators import mc_config
from .config_errors import ConfigException, ConfigDefinitionException, ConfigApiException, InvalidUsageException
from .config_errors import ConfigAttributeError, ConfigExcludedAttributeError, ConfigExcludedKeyError
Expand All @@ -13,7 +13,7 @@


__all__ = [
'mc_config', 'McConfigRoot', 'AbstractConfigItem', 'ConfigItem', 'RepeatableConfigItem', 'ConfigBuilder',
'mc_config', 'McConfigRoot', 'AbstractConfigItem', 'ConfigItem', 'RepeatableConfigItem', 'DefaultItems', 'ConfigBuilder',
'ConfigException', 'ConfigDefinitionException', 'ConfigApiException', 'InvalidUsageException',
'ConfigAttributeError', 'ConfigExcludedAttributeError',
'MC_REQUIRED', 'MC_TODO', 'McInvalidValue', 'McTodoHandling',
Expand Down
7 changes: 5 additions & 2 deletions multiconf/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
from .config_errors import ConfigException, ConfigDefinitionException, _line_msg, _error_msg, _warning_msg
from .repeatable import RepeatableDict
from .multiconf import McConfigRoot
from . import ConfigBuilder, RepeatableConfigItem
from . import ConfigBuilder, RepeatableConfigItem, DefaultItems
from .check_identifiers import check_valid_identifier, check_valid_identifiers


def _not_allowed_on_class(cls, decorator_name, not_cls):
if issubclass(cls, not_cls):
print(_line_msg(up_level=2), file=sys.stderr)
msg = "Decorator '@" + decorator_name + "' is not allowed on instance of " + ConfigBuilder.__name__ + "."
msg = "Decorator '@" + decorator_name + "' is not allowed on instance of " + not_cls.__name__ + "."
print(_error_msg(msg), file=sys.stderr)
raise ConfigDefinitionException(msg)

Expand Down Expand Up @@ -46,6 +46,7 @@ def named_as(insert_as_name):
"""Determine the name used to insert item in parent"""
def deco(cls):
_not_allowed_on_class(cls, named_as.__name__, ConfigBuilder)
_not_allowed_on_class(cls, named_as.__name__, DefaultItems)
check_valid_identifier(insert_as_name)
cls._mc_deco_named_as = insert_as_name
return cls
Expand All @@ -57,6 +58,7 @@ def nested_repeatables(*attr_names):
"""Specify which nested (child) items will be repeatable."""
def deco(cls):
_not_allowed_on_class(cls, nested_repeatables.__name__, ConfigBuilder)
_not_allowed_on_class(cls, nested_repeatables.__name__, DefaultItems)
cls._mc_deco_nested_repeatables = _add_super_list_deco_values(cls, attr_names, 'nested_repeatables')

# Make descriptor work, an instance of the descriptor class mut be assigened at the class level
Expand All @@ -71,6 +73,7 @@ def deco(cls):
def required(*attr_names):
"""Specify nested (child) items that must be defined."""
def deco(cls):
_not_allowed_on_class(cls, required.__name__, DefaultItems)
cls._mc_deco_required = _add_super_list_deco_values(cls, attr_names, 'required')
return cls

Expand Down
18 changes: 14 additions & 4 deletions multiconf/json_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
_property_method_value_hidden = '@property method value - call disabled'

_mc_filter_out_keys = ('env', 'env_factory', 'contained_in', 'root_conf', 'attributes', 'mc_config_result', 'num_invalid_property_usage', 'named_as')
_mc_hidden_if_not_true = ('mc_is_default_value_item',)
_mc_show_if_names_only = ('mc_is_default_value_item',)


def _class_tuple(obj, obj_info=""):
Expand Down Expand Up @@ -125,7 +127,12 @@ def _ref_item_str(self, objval):
if isinstance(objval, self.with_item_types):
return excl + objval.ref_type_info_for_json() + ", id: " + str(self.ref_repr(objval))

return excl + objval.ref_type_info_for_json() + " " + _mc_identification_msg_str(objval)
try:
ref_type_info = objval.ref_type_info_for_json()
except AttributeError:
ref_type_info = ''

return excl + ref_type_info + " " + _mc_identification_msg_str(objval)

def _ref_earlier_str(self, objval):
return "#ref" + self._ref_item_str(objval)
Expand All @@ -139,7 +146,7 @@ def _ref_self_str(self, objval):
def _ref_outside_str(self, objval):
# A reference to an item which is outside of the currently dumped hierarchy.
# Showing self.ref_repr(obj) does not help here as the object is not dumped, instead try to show some attributes which may identify the object
return "#outside-ref: " + _mc_identification_msg_str(objval)
return "#ref outside: " + _mc_identification_msg_str(objval)

def _ref_mc_item_str(self, objval):
if ref_id(objval) in self.seen:
Expand Down Expand Up @@ -247,9 +254,12 @@ def _handle_one_dir_entry_one_env(self, obj, key, _val, env, attributes_overridi
continue

if isinstance(real_attr, (property, self.multiconf_property_wrapper_type)):
if key in _mc_hidden_if_not_true and not getattr(obj, key):
return key, ()

calc_or_static = _calculated_value

if names_only:
if names_only and key not in _mc_show_if_names_only:
val = _property_method_value_hidden
break

Expand Down Expand Up @@ -303,7 +313,7 @@ def _handle_one_dir_entry_one_env(self, obj, key, _val, env, attributes_overridi
if overridden_property:
return key, [(overridden_property + calc_or_static + ' value was', val)] + property_inf
if self.compact:
return key, [('', str(val) + calc_or_static)] + property_inf
return key, [('', (str(val).lower() if isinstance(val, bool) else str(val)) + calc_or_static)] + property_inf
return key, [('', val), (calc_or_static, True)] + property_inf

if isinstance(val, (list, tuple)):
Expand Down
Loading

0 comments on commit d7a8cd7

Please sign in to comment.