Skip to content

Commit

Permalink
Merge pull request #12 from Friends-of-Monika/config-id
Browse files Browse the repository at this point in the history
Implement config IDs and fetching by ID
  • Loading branch information
dreamscached authored Oct 19, 2022
2 parents aabba35 + 9db0968 commit 9944820
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 15 deletions.
35 changes: 33 additions & 2 deletions mod/config.rpy
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,19 @@ init 90 python in _fom_presence_config:
import configparser


# Errors
# Errors and warnings

_ERROR_CONFIG_LOADING = error.Error(
log_message_report="Could not load presence config from file {0}: {1}.",
ui_message_report="Could not load some presence configs, see log/submod_log.log."
)

_WARNING_CONFIG_CLASH = error.Error(
log_message_report="Config from file {0} has conflicting name with some other config: {1}.",
ui_message_report="There were some warnings during loading some of the presence configs, see log/submod_log.log.",
warning=True
)


# Supplier and related functions and constructors

Expand Down Expand Up @@ -300,6 +306,7 @@ init 90 python in _fom_presence_config:
compile(condition, "<string>", "eval")
self.condition = condition

self.id = parser.get_value("Presence", "ID", str, None)
self.priority = parser.get_value("Presence", "Priority", int, 0)
self.dynamic = parser.get_value("Presence", "Dynamic", _parse_bool, True)

Expand Down Expand Up @@ -380,6 +387,7 @@ init 90 python in _fom_presence_config:

_config_dir = os.path.join(mod.basedir, "config")
_configs = list()
_config_id_map = dict()

def reload_configs():
"""
Expand All @@ -391,6 +399,7 @@ init 90 python in _fom_presence_config:
"""

del _configs[:]
_config_id_map.clear()

for _dir, _, files in os.walk(_config_dir):
for _file in files:
Expand All @@ -407,6 +416,10 @@ init 90 python in _fom_presence_config:
eval(config.condition, dict(), store.__dict__)

_configs.append((_file, config))
if config.id is not None:
if config.id in _config_id_map:
_WARNING_CONFIG_CLASH.report(_file[len(_config_dir) + 1:], config.id)
_config_id_map[config.id] = config
except Exception as e:
_ERROR_CONFIG_LOADING.report(_file[len(_config_dir) + 1:], e)

Expand Down Expand Up @@ -434,4 +447,22 @@ init 90 python in _fom_presence_config:
return None

active.sort(key=lambda it: it.priority, reverse=True)
return active[0]
return active[0]

def get_config(_id):
"""
Fetches config by its ID (defined as ID= parameter in Presence section).
IN:
_id -> str:
ID of the config to fetch reference to.
OUT:
Config:
If config with such ID exists and was successfully loaded.
None:
If config with such ID does not exist.
"""

return _config_id_map.get(_id)
42 changes: 29 additions & 13 deletions mod/error.rpy
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ init -90 python in _fom_presence_error:
import contextlib


_OUTPUT_UI = 0
_OUTPUT_LOG = 1


class Context(object):
"""
Context is a simple error context that contains logger to report errors
Expand All @@ -25,7 +29,12 @@ init -90 python in _fom_presence_error:
locations on demand.
"""

def __init__(self, logger, sample=True):
def __init__(
self,
logger,
sample_ui=True,
sample_log=False
):
"""
Constructs a new Context instance with the provided logger and with
sampling preference.
Expand All @@ -40,7 +49,8 @@ init -90 python in _fom_presence_error:
"""

self._logger = logger
self._sample = sample
self._sample_ui = sample_ui
self._sample_log = sample_log

class Error(object):
"""
Expand All @@ -53,7 +63,8 @@ init -90 python in _fom_presence_error:
log_message_report=None,
log_message_resolve=None,
ui_message_report=None,
ui_message_resolve=None
ui_message_resolve=None,
warning=False
):
"""
Creates new Error instance with the provided messages (optional.)
Expand All @@ -77,6 +88,7 @@ init -90 python in _fom_presence_error:
self._ui_report=ui_message_report
self._ui_resolve=ui_message_resolve
self._count = 0
self._warning = warning

def report(self, *args):
"""
Expand All @@ -91,8 +103,8 @@ init -90 python in _fom_presence_error:

self._count += 1
self._invoke_msg_func_triples([
(current_context._logger.error, self._log_report, args),
(renpy.notify, self._ui_report, args)
(current_context._logger.warning if self._warning else current_context._logger.error, self._log_report, args, _OUTPUT_LOG),
(renpy.notify, self._ui_report, args, _OUTPUT_UI)
])

def resolve(self, *args):
Expand All @@ -109,8 +121,8 @@ init -90 python in _fom_presence_error:
if self._count > 0:
self._count = 0
self._invoke_msg_func_triples([
(current_context._logger.info, self._log_resolve, args),
(renpy.notify, self._ui_resolve, args)
(current_context._logger.info, self._log_resolve, args, _OUTPUT_LOG),
(renpy.notify, self._ui_resolve, args, _OUTPUT_UI)
])

def _invoke_msg_func_triples(self, input):
Expand All @@ -124,13 +136,14 @@ init -90 python in _fom_presence_error:
[0]: function to pass formatted message to
[1]: message template to format with args
[2]: args to pass to [1] message
[3]: output type of this message
"""

for function, message, args in input:
for function, message, args, _type in input:
if message is not None:
self._invoke_with_err_str(function, message, *args)
self._invoke_with_err_str(function, message, _type, *args)

def _invoke_with_err_str(self, function, message, *args):
def _invoke_with_err_str(self, function, message, _type, *args):
"""
Invokes provided function with formatted message template if
sampling is disabled or if occurrences counter is zero.
Expand All @@ -146,15 +159,18 @@ init -90 python in _fom_presence_error:
Arguments to pass to formatting function.
"""

if self._count == 0 or not current_context._sample:
if self._count == 0 or not (
current_context._sample_log and _type == _OUTPUT_LOG or
current_context._sample_ui and _type == _OUTPUT_UI
):
if args is not None:
message = message.format(*args)
function(message)


MAIN_CONTEXT = Context(logging.DEFAULT)
OPTIONS_CONTEXT = Context(logging.DEFAULT, sample=False)
EXTENSIONS_CONTEXT = Context(logging.DEFAULT, sample=False)
OPTIONS_CONTEXT = Context(logging.DEFAULT, sample_ui=False)
EXTENSIONS_CONTEXT = Context(logging.DEFAULT)

current_context = MAIN_CONTEXT

Expand Down

0 comments on commit 9944820

Please sign in to comment.