1
0
mirror of https://github.com/lutris/lutris synced 2024-07-05 08:28:41 +00:00

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
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``,
``key`` and ``value`` parameters. Note that the file is entirely rewritten and
comments are left out; Make sure to compare the initial and resulting file
to spot any potential parsing issues.
``key`` and ``value`` parameters or the ``data`` parameter. Set ``merge: false``
to first truncate the file. Note that the file is entirely rewritten and
comments are left out; Make sure to compare the initial and resulting file to
spot any potential parsing issues.
Example:
@ -335,6 +336,16 @@ Example:
key: Renderer
value: OpenGL
::
- write_config:
file: $GAMEDIR/myfile.ini
data:
General:
iNumHWThreads: 2
bUseThreadedAI: 1
Writing into a JSON type file
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -457,7 +457,10 @@ class CommandsMixin:
def write_config(self, params):
"""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')
# Get file
config_file_path = self._get_file(params['file'])
@ -467,17 +470,29 @@ class CommandsMixin:
if not os.path.exists(basedir):
os.makedirs(basedir)
merge = params.get('merge', True)
parser = EvilConfigParser(allow_no_value=True,
dict_type=MultiOrderedDict,
strict=False)
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']):
parser.add_section(params['section'])
parser.set(params['section'], params['key'], value)
for section, keys in data.items():
if not parser.has_section(section):
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:
parser.write(config_file)