Add data and merge parameters to write_config installer command

This commit is contained in:
daniel-j 2018-06-18 14:06:18 +02:00
parent d0b2242982
commit d3384d276d
2 changed files with 35 additions and 9 deletions

View file

@ -321,9 +321,10 @@ Writing into an INI type config file
Modify or create a config file with the ``write_config`` directive. A config file Modify or create a config file with the ``write_config`` directive. A config file
is a text file composed of key=value (or key: value) lines grouped under is a text file composed of key=value (or key: value) lines grouped under
[sections]. Use the ``file`` (an absolute path or a ``file id``), ``section``, [sections]. Use the ``file`` (an absolute path or a ``file id``), ``section``,
``key`` and ``value`` parameters. Note that the file is entirely rewritten and ``key`` and ``value`` parameters or the ``data`` parameter. Set ``merge: false``
comments are left out; Make sure to compare the initial and resulting file to first truncate the file. Note that the file is entirely rewritten and
to spot any potential parsing issues. comments are left out; Make sure to compare the initial and resulting file to
spot any potential parsing issues.
Example: Example:
@ -335,6 +336,16 @@ Example:
key: Renderer key: Renderer
value: OpenGL value: OpenGL
::
- write_config:
file: $GAMEDIR/myfile.ini
data:
General:
iNumHWThreads: 2
bUseThreadedAI: 1
Writing into a JSON type file Writing into a JSON type file
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View file

@ -457,7 +457,10 @@ class CommandsMixin:
def write_config(self, params): def write_config(self, params):
"""Write a key-value pair into an INI type config file.""" """Write a key-value pair into an INI type config file."""
self._check_required_params(['file', 'section', 'key', 'value'], if params.get('data', None):
self._check_required_params(['file', 'data'], params, 'write_config')
else:
self._check_required_params(['file', 'section', 'key', 'value'],
params, 'write_config') params, 'write_config')
# Get file # Get file
config_file_path = self._get_file(params['file']) config_file_path = self._get_file(params['file'])
@ -467,17 +470,29 @@ class CommandsMixin:
if not os.path.exists(basedir): if not os.path.exists(basedir):
os.makedirs(basedir) os.makedirs(basedir)
merge = params.get('merge', True)
parser = EvilConfigParser(allow_no_value=True, parser = EvilConfigParser(allow_no_value=True,
dict_type=MultiOrderedDict, dict_type=MultiOrderedDict,
strict=False) strict=False)
parser.optionxform = str # Preserve text case parser.optionxform = str # Preserve text case
parser.read(config_file_path) if merge:
parser.read(config_file_path)
value = self._substitute(params['value']) data = {}
if params.get('data', None):
data = params['data']
else:
data[params['section']] = {}
data[params['section']][params['key']] = params['value']
if not parser.has_section(params['section']): for section, keys in data.items():
parser.add_section(params['section']) if not parser.has_section(section):
parser.set(params['section'], params['key'], value) parser.add_section(section)
for key, value in keys.items():
value = self._substitute(value)
print(section, key, value)
parser.set(section, key, value)
with open(config_file_path, 'wb') as config_file: with open(config_file_path, 'wb') as config_file:
parser.write(config_file) parser.write(config_file)