108 lines
3.9 KiB
Python
108 lines
3.9 KiB
Python
''' Config reader '''
|
|
|
|
import json
|
|
import utils
|
|
|
|
def read_config(path: str) -> dict | None:
|
|
''' Read config '''
|
|
default = {
|
|
'management_token': 'TELEGRAM_BOT_TOKEN_HERE',
|
|
'tg_api_id': 12345,
|
|
'tg_api_hash': '0123456789abcdef0123456789abcdef',
|
|
'accounts': {
|
|
'account1': {
|
|
'login': '+71234567890',
|
|
'mod_basic': True,
|
|
'mod_eternal_online': False,
|
|
'mod_video_downloader': True
|
|
}
|
|
}
|
|
}
|
|
# root fields
|
|
root_required_fields = {
|
|
'management_token': [str],
|
|
'tg_api_id': [int],
|
|
'tg_api_hash': [str],
|
|
'tg_admin_uid': [int],
|
|
'accounts': [dict]
|
|
}
|
|
|
|
# account fields
|
|
required_fields = {
|
|
'login': [str]
|
|
}
|
|
optional_fields = {}
|
|
for mod_name in utils.get_all_mods():
|
|
optional_fields[mod_name] = ([bool], False)
|
|
optional_fields['mod_basic'] = ([bool], True)
|
|
try:
|
|
j = None
|
|
with open(path, 'r') as f:
|
|
j = json.loads(f.read())
|
|
# root fields check
|
|
for key in root_required_fields:
|
|
if key not in j:
|
|
print('[!] Config misses key \'%s\'' % key)
|
|
return None
|
|
if type(j[key]) not in root_required_fields[key]:
|
|
print('[!] Config has key \'%s\' of invalid type: must be one of %s' % (key, root_required_fields[key]))
|
|
return None
|
|
# check all accounts
|
|
for account_name in dict(j['accounts']):
|
|
# to remove the account?
|
|
to_remove = False
|
|
|
|
# save the account with short name
|
|
acc = j['accounts'][account_name]
|
|
# check if all required fields are present
|
|
for key in required_fields:
|
|
# missing field
|
|
if key not in acc:
|
|
to_remove = True
|
|
print('[!] Account \'%s\' misses required field \'%s\'' % (account_name, key))
|
|
break
|
|
# invalid type
|
|
t = type(acc[key])
|
|
if t not in required_fields[key]:
|
|
to_remove = True
|
|
print('[!] Account \'%s\' has required field of invalid type: \'%s\' must be one of %s' % (account_name, key, required_fields[key]))
|
|
break
|
|
# remove the account
|
|
if to_remove:
|
|
print('[!] Account \'%s\' will not be served until errors are fixed' % account_name)
|
|
del j[account_name]
|
|
continue
|
|
# check all optional fields
|
|
for key in optional_fields:
|
|
# get allowed types and default value
|
|
allowed_types, default_value = optional_fields[key]
|
|
# missing - add default value
|
|
if key not in acc:
|
|
print(
|
|
'[W] Account \'%s\' misses optional field \'%s\', using default value \'%s\'' % \
|
|
(account_name, key, default_value)
|
|
)
|
|
acc[key] = default_value
|
|
continue
|
|
# invalid type
|
|
t = type(acc[key])
|
|
if t not in allowed_types:
|
|
print(
|
|
'[W] Account \'%s\' has optional field of invalid type: \'%s\' must be one of %s. Using default value %s' % \
|
|
(account_name, key, types, default_value)
|
|
)
|
|
acc[key] = default_value
|
|
continue
|
|
# log
|
|
print('[I] Added account \'%s\' (login \'%s\')' % (account_name, acc['login']))
|
|
# return
|
|
return j
|
|
except:
|
|
try:
|
|
with open(path, 'w') as f:
|
|
f.write(json.dumps(default, indent=4))
|
|
print('[!] Saved default config to %s' % path)
|
|
except:
|
|
print('[!] Failed to save default config to %s' % path)
|
|
return None
|