Add function to read configuration from file or directory

- Introduced `read_config_file` function to handle reading configuration settings.
- Supports both direct file paths and project root directory structure.
- Extracts various database and update type settings into a dictionary.
- Handles timestamp calculations for update types.
This commit is contained in:
2025-11-25 08:13:59 -05:00
parent 7837ca2dd4
commit b390fc9f9a
2 changed files with 84355 additions and 14 deletions
+84300 -14
View File
File diff suppressed because it is too large Load Diff
+55
View File
@@ -61,3 +61,58 @@ class Config(object):
def __del__(self):
pass
def read_config_file(path_or_rootdir):
"""Read configuration and return a dict of options.
If `path_or_rootdir` is a file path to a config file, it will be used
directly. Otherwise it's treated as the project root directory and
the config file is expected at `<ROOTDIR>/settings/tvsync_settings.cfg`.
"""
configurer = configparser.ConfigParser()
# determine config file path
if os.path.isfile(path_or_rootdir):
cfg_path = path_or_rootdir
else:
cfg_path = os.path.join(path_or_rootdir, 'settings', 'tvsync_settings.cfg')
configurer.read(cfg_path)
options = {}
# Mirror keys used by the Config class
options['loglevel'] = configurer.get('global', 'loglevel')
options['hostname'] = configurer.get('dbsettings', 'hostname')
options['mghostname'] = configurer.get('dbsettings', 'mghostname')
options['dbname'] = configurer.get('dbsettings', 'dbname')
options['mgdbname'] = configurer.get('dbsettings', 'mgdbname')
options['mysqlusername'] = configurer.get('dbsettings', 'mysqlusername')
options['pgsqlusername'] = configurer.get('dbsettings', 'pgsqlusername')
options['mssqlusername'] = configurer.get('dbsettings', 'mssqlusername')
options['mysqlpassword'] = configurer.get('dbsettings', 'mysqlpassword')
options['pgsqlpassword'] = configurer.get('dbsettings', 'pgsqlpassword')
options['mssqlpassword'] = configurer.get('dbsettings', 'mssqlpassword')
options['pgsqlport'] = configurer.get('dbsettings', 'pgsqlport')
options['mysqlport'] = configurer.get('dbsettings', 'mysqlport')
options['mssqlport'] = configurer.get('dbsettings', 'mssqlport')
options['mgport'] = configurer.get('dbsettings', 'mgport')
options['updatetype'] = configurer.get('updatetype', 'type')
options['apitype'] = configurer.get('api', 'type')
options['updateschema'] = configurer.get('database', 'updateschema')
options['dbtype'] = configurer.get('database', 'dbtype')
options['initdb'] = configurer.get('database', 'initialize')
options['initload'] = configurer.get('database', 'initload')
options['skipcache'] = configurer.get('updatetype', 'cacheskip')
options['refreshcache'] = configurer.get('updatetype', 'refreshcache')
options['full'] = configurer.get('updatetype', 'full')
options['monthly'] = configurer.get('updatetype', 'monthly')
options['weekly'] = configurer.get('updatetype', 'weekly')
options['daily'] = configurer.get('updatetype', 'daily')
options['12h'] = configurer.get('updatetype', '12h')
options['6h'] = configurer.get('updatetype', '6h')
options['custom'] = configurer.get('updatetype', 'custom')
options['serieslist'] = list()
options['timedifference'] = configurer.get('updatetype', options['updatetype'])
options['current_timestamp'] = time.time()
if isinstance(options['timedifference'], str) and options['timedifference'].lower() == 'full':
options['timedifference'] = int(options['current_timestamp'])
return options