mirror of
https://github.com/lutris/lutris
synced 2024-10-02 22:14:23 +00:00
Add write_config
installer directive
This commit is contained in:
parent
cd7ab429f0
commit
05ed438ea0
|
@ -204,6 +204,27 @@ Example:
|
|||
args: --argh
|
||||
file: great-id
|
||||
|
||||
Writing into an INI type config file
|
||||
------------------------------------
|
||||
|
||||
Modify or create a config file with the ``set_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. Not 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:
|
||||
|
||||
::
|
||||
|
||||
- set_config:
|
||||
file: $GAMEDIR/game.ini
|
||||
section: Engine
|
||||
key: Renderer
|
||||
value: OpenGL
|
||||
|
||||
|
||||
Running a task provided by a runner
|
||||
-----------------------------------
|
||||
|
||||
|
|
|
@ -14,6 +14,7 @@ from gi.repository import Gtk, Gdk
|
|||
|
||||
from lutris import pga
|
||||
from lutris.util import extract, devices
|
||||
from lutris.util.fileio import EvilConfigParser, MultiOrderedDict
|
||||
from lutris.util.jobs import async_call
|
||||
from lutris.util.log import logger
|
||||
from lutris.util.strings import slugify, add_url_tags
|
||||
|
@ -558,6 +559,33 @@ class ScriptInterpreter(object):
|
|||
logger.debug("extracting file %s to %s", filename, dest_path)
|
||||
extract.extract_archive(filename, dest_path, merge_single, extractor)
|
||||
|
||||
def write_config(self, params):
|
||||
"""Writes a key-value pair into an INI type config file."""
|
||||
# Get file
|
||||
if 'file' not in params:
|
||||
raise ScriptingError('"file" parameter is mandatory for the '
|
||||
'write_conf command', params)
|
||||
config_file = self._get_file(params['file'])
|
||||
if not config_file:
|
||||
config_file = self._substitute(params['file'])
|
||||
|
||||
# Create it if necessary
|
||||
basedir = os.path.dirname(config_file)
|
||||
if not os.path.exists(basedir):
|
||||
os.makedirs(basedir)
|
||||
|
||||
parser = EvilConfigParser(allow_no_value=True,
|
||||
dict_type=MultiOrderedDict)
|
||||
parser.optionxform = str # Preserve text case
|
||||
parser.read(config_file)
|
||||
|
||||
if not parser.has_section(params['section']):
|
||||
parser.add_section(params['section'])
|
||||
parser.set(params['section'], params['key'], params['value'])
|
||||
|
||||
with open(config_file, 'wb') as f:
|
||||
parser.write(f)
|
||||
|
||||
def _append_steam_data_to_files(self, runner_class):
|
||||
steam_runner = runner_class()
|
||||
data_path = steam_runner.get_game_path_from_appid(
|
||||
|
|
27
lutris/util/fileio.py
Normal file
27
lutris/util/fileio.py
Normal file
|
@ -0,0 +1,27 @@
|
|||
from collections import OrderedDict
|
||||
from ConfigParser import RawConfigParser
|
||||
|
||||
|
||||
class EvilConfigParser(RawConfigParser):
|
||||
"""ConfigParser with support for evil INIs using duplicate keys."""
|
||||
def write(self, fp):
|
||||
for section in self._sections:
|
||||
fp.write("[%s]\n" % section)
|
||||
for (key, value) in self._sections[section].items():
|
||||
if key == "__name__":
|
||||
continue
|
||||
if (value is not None) or (self._optcre == self.OPTCRE):
|
||||
# Duplicate keys writing support inside
|
||||
key = "=".join((key,
|
||||
str(value).replace('\n', '\n%s=' % key)))
|
||||
fp.write("%s\n" % (key))
|
||||
fp.write("\n")
|
||||
|
||||
|
||||
class MultiOrderedDict(OrderedDict):
|
||||
"""dict_type to use with an EvilConfigParser instance."""
|
||||
def __setitem__(self, key, value):
|
||||
if isinstance(value, list) and key in self:
|
||||
self[key].extend(value)
|
||||
else:
|
||||
super(MultiOrderedDict, self).__setitem__(key, value)
|
Loading…
Reference in a new issue