[cli/core/lfs] Allow specifying custom config file

This commit is contained in:
derrod 2021-09-28 03:07:15 +02:00
parent b319cb505c
commit ee2432c443
3 changed files with 24 additions and 12 deletions

View file

@ -37,8 +37,8 @@ logger = logging.getLogger('cli')
class LegendaryCLI:
def __init__(self):
self.core = LegendaryCore()
def __init__(self, override_config=None):
self.core = LegendaryCore(override_config)
self.logger = logging.getLogger('cli')
self.logging_queue = None
@ -1195,6 +1195,8 @@ def main():
parser.add_argument('-v', '--debug', dest='debug', action='store_true', help='Set loglevel to debug')
parser.add_argument('-y', '--yes', dest='yes', action='store_true', help='Default to yes for all prompts')
parser.add_argument('-V', '--version', dest='version', action='store_true', help='Print version and exit')
parser.add_argument('-c', '--config-file', dest='config_file', action='store', metavar='<path/name>',
help='Specify custom config file or name for the config file in the default directory.')
# all the commands
subparsers = parser.add_subparsers(title='Commands', dest='subparser_name')
@ -1441,7 +1443,7 @@ def main():
print(subparser.format_help())
return
cli = LegendaryCLI()
cli = LegendaryCLI(override_config=args.config_file)
ql = cli.setup_threaded_logging()
config_ll = cli.core.lgd.config.get('Legendary', 'log_level', fallback='info')

View file

@ -51,10 +51,10 @@ class LegendaryCore:
"""
_egl_version = '11.0.1-14907503+++Portal+Release-Live'
def __init__(self):
def __init__(self, override_config=None):
self.log = logging.getLogger('Core')
self.egs = EPCAPI()
self.lgd = LGDLFS()
self.lgd = LGDLFS(config_file=override_config)
self.egl = EPCLFS()
self.lgdapi = LGDAPI()

View file

@ -14,7 +14,7 @@ from legendary.utils.lfs import clean_filename
class LGDLFS:
def __init__(self):
def __init__(self, config_file=None):
self.log = logging.getLogger('LGDLFS')
if config_path := os.environ.get('XDG_CONFIG_HOME'):
@ -35,6 +35,17 @@ class LGDLFS:
# Config with game specific settings (e.g. start parameters, env variables)
self.config = LGDConf(comment_prefixes='/', allow_no_value=True)
if config_file:
# if user specified a valid relative/absolute path use that,
# otherwise create file in legendary config directory
if os.path.exists(config_file):
self.config_path = os.path.abspath(config_file)
else:
self.config_path = os.path.join(self.path, clean_filename(config_file))
self.log.info(f'Using non-default config file "{self.config_path}"')
else:
self.config_path = os.path.join(self.path, 'config.ini')
# ensure folders exist.
for f in ['', 'manifests', 'metadata', 'tmp']:
if not os.path.exists(os.path.join(self.path, f)):
@ -69,7 +80,7 @@ class LGDLFS:
# try loading config
try:
self.config.read(os.path.join(self.path, 'config.ini'))
self.config.read(self.config_path)
except Exception as e:
self.log.error(f'Unable to read configuration file, please ensure that file is valid! '
f'(Error: {repr(e)})')
@ -283,15 +294,14 @@ class LGDLFS:
if self.config.read_only or not self.config.modified:
return
# if config file has been modified externally, back-up the user-modified version before writing
config_path = os.path.join(self.path, 'config.ini')
if os.path.exists(config_path):
if (modtime := int(os.stat(config_path).st_mtime)) != self.config.modtime:
if os.path.exists(self.config_path):
if (modtime := int(os.stat(self.config_path).st_mtime)) != self.config.modtime:
new_filename = f'config.{modtime}.ini'
self.log.warning(f'Configuration file has been modified while legendary was running, '
f'user-modified config will be renamed to "{new_filename}"...')
os.rename(os.path.join(self.path, 'config.ini'), os.path.join(self.path, new_filename))
os.rename(self.config_path, os.path.join(os.path.dirname(self.config_path), new_filename))
with open(config_path, 'w') as cf:
with open(self.config_path, 'w') as cf:
self.config.write(cf)
def get_dir_size(self):