Robust config file reading

This commit is contained in:
Jylpah committed 2020-07-08 22:53:11 +03:00
1 parent 34a8d70558
commit b24a2a8016
1 file changed
+20 -4
+20 -4
View File
@@ -28,21 +28,37 @@ async def main(argv):
# set the directory for the script
os.chdir(os.path.dirname(sys.argv[0]))
# options defaults
OPT_WORKERS_N = 5
WG_ID = None
WG_RATE_LIMIT = 10
try:
## Read config
if os.path.isfile(FILE_CONFIG):
config = configparser.ConfigParser()
config.read(FILE_CONFIG)
try:
configOptions = config['OPTIONS']
OPT_WORKERS_N = configOptions.getint('opt_uploader_workers', 5)
OPT_WORKERS_N = configOptions.getint('opt_uploader_workers', OPT_WORKERS_N)
except (KeyError, configparser.NoSectionError) as err:
bu.debug("No section 'OPTIONS' found in configuration file: " + FILE_CONFIG)
try:
configWG = config['WG']
# WG account id of the uploader:
# # Find it here: https://developers.wargaming.net/reference/all/wotb/account/list/
WG_ID = configWG.getint('wg_id', None)
WG_ID = configWG.getint('wg_id', WG_ID)
## WG API Rules limit 10 request / sec. Higher rate of requests will return errors ==> extra delay
WG_RATE_LIMIT = configWG.getint('wg_rate_limit', 10)
WG_RATE_LIMIT = configWG.getint('wg_rate_limit', WG_RATE_LIMIT)
except (KeyError, configparser.NoSectionError) as err:
bu.debug("No section 'WG' found in configuration file: " + FILE_CONFIG)
parser = argparse.ArgumentParser(description='Post replays(s) to WoTinspector.com and retrieve battle data')
except Exception as err:
bu.error(exception=err)
parser = argparse.ArgumentParser(description='Post replays(s) to WoTinspector.com and retrieve replay data as JSON')
parser.add_argument('-id', dest='accountID', type=int, default=WG_ID, help='WG account_id')
parser.add_argument('-a', '--account', dest='account', type=str, default=None, help='Uploader\'s WG account name. Format: ACCOUNT_NAME@SERVER')
parser.add_argument('-t','--title', type=str, default=None, help='Title for replays. Use NN for continous numbering. Default is filename-based numbering')