Skip to content

Commit

Permalink
Merge pull request #11 from tylerbrawl/docs
Browse files Browse the repository at this point in the history
 Added basic support for options accepting str values (v0.1.5).
  • Loading branch information
tylerbrawl authored Jan 1, 2020
2 parents f5d8017 + b238a39 commit 40ddf0a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/galaxyutils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__author__ = "Tyler Nichols"
__license__ = "MIT"
__version__ = "0.1.4"
__version__ = "0.1.5"
32 changes: 23 additions & 9 deletions src/galaxyutils/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class Option(object):
:param option_name: The name of the option which will be configured. This name must match the name provided within
the plugin's ``default_config.cfg`` file.
:param str_option: This defines whether or not the option uses ``str`` values. It allows any values to be used, so
long as it is an ``str`` value. If this parameter is not defined, then the default value of ``False`` is used.
:param allowed_values: This defines the list of possible values that the option can take. If this parameter is not
defined, then the default list of ``[True, False]`` is used.
:param default_value: This defines the default value of the option. This value is used if the specified option
Expand All @@ -25,12 +27,18 @@ class Option(object):
"""
option_name: str
str_option: Optional[bool] = False
allowed_values: Optional[List[Any]] = field(default_factory=lambda: [True, False])
default_value: Optional[Any] = False

def __setattr__(self, key, value):
if key == "default_value" and value not in self.allowed_values:
raise InvalidConfigOptionException
if key == "default_value":
if self.str_option or value in self.allowed_values:
super().__setattr__("default_value", value)
else:
log.debug(f"Name: {self.option_name} / str_option: {str(self.str_option)} / "
f"allowed_values: {str(self.allowed_values)} / default_value: {self.default_value}")
raise InvalidConfigOptionException
else:
super().__setattr__(key, value)

Expand Down Expand Up @@ -138,6 +146,7 @@ def _parse_config(config) -> Dict[str, Any]:
return_dict = {}
for op in CONFIG_OPTIONS:
options_dict[op.option_name] = {
'str_option': op.str_option,
'default': op.default_value,
'allowed': op.allowed_values
}
Expand All @@ -149,13 +158,18 @@ def _parse_config(config) -> Dict[str, Any]:
option[0] = option[0].strip() # Remove possible spaces before/after the option name.
option[1] = option[1].strip() # Remove possible spaces before/after the option value.
if option[0] in options_dict:
for o in options_dict[option[0]]['allowed']:
if str(option[1]).lower() == str(o).lower() and str(option[1]) != \
str(options_dict[option[0]]['default']):
return_dict[option[0]] = o
log.debug(f"GALAXY_CONFIG_OPTION: The option {option[0]} is now set to {str(o)} instead of "
f"{options_dict[option[0]]['default']}.")
break
if options_dict[option[0]]['str_option'] and option[1] != "None":
return_dict[option[0]] = option[1]
if option[1] != "":
log.debug(f"GALAXY_CONFIG_OPTION: The option {option[0]} is now set to {option[1]}.")
else:
for o in options_dict[option[0]]['allowed']:
if str(option[1]).lower() == str(o).lower() and str(option[1]) != \
str(options_dict[option[0]]['default']):
return_dict[option[0]] = o
log.debug(f"GALAXY_CONFIG_OPTION: The option {option[0]} is now set to {str(o)} instead of "
f"{options_dict[option[0]]['default']}.")
break
else:
log.debug(f"GALAXY_FAKE_CONFIG_OPTION: The option {option[0]} is not a defined option!")
config.close()
Expand Down

0 comments on commit 40ddf0a

Please sign in to comment.