From e6c1b2d9ba3ccf884f50779c28f05731e1dadd11 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Sat, 18 Nov 2023 12:11:39 -0600 Subject: [PATCH] Add function for writing value to fannie config file --- .../office/scripts/update-fannie-config.php | 40 ++++++++++++++++ rattail_corepos/corepos/office/util.py | 46 +++++++++++++++++-- 2 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 rattail_corepos/corepos/office/scripts/update-fannie-config.php diff --git a/rattail_corepos/corepos/office/scripts/update-fannie-config.php b/rattail_corepos/corepos/office/scripts/update-fannie-config.php new file mode 100644 index 0000000..9b101e0 --- /dev/null +++ b/rattail_corepos/corepos/office/scripts/update-fannie-config.php @@ -0,0 +1,40 @@ + diff --git a/rattail_corepos/corepos/office/util.py b/rattail_corepos/corepos/office/util.py index c2c7426..2d70e34 100644 --- a/rattail_corepos/corepos/office/util.py +++ b/rattail_corepos/corepos/office/util.py @@ -31,7 +31,7 @@ import subprocess from rattail.files import resource_path -def get_fannie_config_value(config, name): +def get_fannie_config_value(config, name, fannie_config_path=None): """ Retrieve a config value from `fannie/config.php` @@ -40,6 +40,9 @@ def get_fannie_config_value(config, name): :param name: Name of the config value to be returned. This need not be prefixed with ``FANNIE_`` although it can be. + :param fannie_config_path: Optional path to fannie config file. + If not specified, it is will be determined via ``config``. + :returns: Config value as Python object, e.g. a string or dict. This is believed to work okay but since the Fannie config file represents data with PHP code, which is then converted to JSON @@ -48,20 +51,53 @@ def get_fannie_config_value(config, name): if not name.startswith('FANNIE_'): name = f'FANNIE_{name}' - is4c = config.require('corepos', 'srcdir') - path = os.path.join(is4c, 'fannie', 'config.php') + if not fannie_config_path: + is4c = config.require('corepos', 'srcdir') + fannie_config_path = os.path.join(is4c, 'fannie', 'config.php') script = resource_path('rattail_corepos.corepos.office:scripts/parse-fannie-config.php') try: output = subprocess.check_output(['php', script, - '--path', path, + '--path', fannie_config_path, '--setting', name]) except subprocess.CalledProcessError as error: - raise ValueError(f"failed to read value: {error.output.decode('utf_8')}") + output = error.output.decode('utf_8') + if "file does not contain setting" in output: + raise ValueError(output) + raise return json.loads(output.decode('utf_8')) +def set_fannie_config_value(config, name, value, fannie_config_path=None): + """ + Update a config value in `fannie/config.php` + + :param config: Rattail config object. + + :param name: Name of the config value to set. This need not be + prefixed with ``FANNIE_`` although it can be. + + :param value: Config value to set This must be provided as a + JSON-compatible string. + + :param fannie_config_path: Optional path to fannie config file. + If not specified, it is will be determined via ``config``. + """ + if not name.startswith('FANNIE_'): + name = f'FANNIE_{name}' + + if not fannie_config_path: + is4c = config.require('corepos', 'srcdir') + fannie_config_path = os.path.join(is4c, 'fannie', 'config.php') + script = resource_path('rattail_corepos.corepos.office:scripts/update-fannie-config.php') + + output = subprocess.check_output(['php', script, + '--path', fannie_config_path, + '--name', name, + '--value', json.dumps(value)]) + + def get_blueline_template(config): return get_fannie_config_value(config, 'BLUELINE_TEMPLATE')