Add function for writing value to fannie config file

This commit is contained in:
Lance Edgar 2023-11-18 12:11:39 -06:00
parent ebef0f6be1
commit e6c1b2d9ba
2 changed files with 81 additions and 5 deletions

View file

@ -0,0 +1,40 @@
<?php
$options = getopt('h', array(
'help',
'path:',
'name:',
'value:',
));
$help = isset($options['h']) or isset($options['help']);
$path = isset($options['path']) ? $options['path'] : null;
$name = isset($options['name']) ? $options['name'] : null;
$value = isset($options['value']) ? $options['value'] : null;
if ($help or !$path or !$name or !$value) {
$script = basename($argv[0]);
echo "Usage: $script --path FANNIE_CONFIG_PATH --name SETTING_NAME --value SETTING_VALUE\n";
exit($help ? 0 : 1);
}
if (!is_file(realpath($path))) {
echo "Config file does not exist: $path\n";
exit(2);
}
$path = realpath($path);
// convert value from JSON to PHP
$value = json_decode($value);
if (gettype($value) == 'string') {
$value = "'$value'";
} else {
echo gettype($value) . " data type not supported: " . print_r($value, true);
exit(2);
}
// invoke native CORE logic to update config file
require(dirname($path) . '/install/util.php');
confset($name, $value);
?>

View file

@ -31,7 +31,7 @@ import subprocess
from rattail.files import resource_path 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` 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 :param name: Name of the config value to be returned. This need
not be prefixed with ``FANNIE_`` although it can be. 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. :returns: Config value as Python object, e.g. a string or dict.
This is believed to work okay but since the Fannie config file This is believed to work okay but since the Fannie config file
represents data with PHP code, which is then converted to JSON 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_'): if not name.startswith('FANNIE_'):
name = f'FANNIE_{name}' name = f'FANNIE_{name}'
if not fannie_config_path:
is4c = config.require('corepos', 'srcdir') is4c = config.require('corepos', 'srcdir')
path = os.path.join(is4c, 'fannie', 'config.php') fannie_config_path = os.path.join(is4c, 'fannie', 'config.php')
script = resource_path('rattail_corepos.corepos.office:scripts/parse-fannie-config.php') script = resource_path('rattail_corepos.corepos.office:scripts/parse-fannie-config.php')
try: try:
output = subprocess.check_output(['php', script, output = subprocess.check_output(['php', script,
'--path', path, '--path', fannie_config_path,
'--setting', name]) '--setting', name])
except subprocess.CalledProcessError as error: 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')) 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): def get_blueline_template(config):
return get_fannie_config_value(config, 'BLUELINE_TEMPLATE') return get_fannie_config_value(config, 'BLUELINE_TEMPLATE')