Files
PointsBot/pointsbot/config.py

145 lines
4.3 KiB
Python
Raw Normal View History

import os
2020-02-01 00:23:15 -08:00
import os.path
2020-02-04 23:21:53 -08:00
from collections import namedtuple
from copy import deepcopy
2020-02-03 18:53:23 -08:00
import toml
2020-02-01 00:23:15 -08:00
from .level import Level
### Globals ###
2020-02-04 23:21:53 -08:00
DATADIR = os.path.join(os.path.expanduser('~'), '.pointsbot')
CONFIGPATH = os.path.join(DATADIR, 'pointsbot.toml')
# Path to the sample config file
SAMPLEPATH = os.path.abspath(os.path.join(os.path.dirname(__file__),
'..',
'pointsbot.sample.toml'))
2020-02-01 00:23:15 -08:00
### Classes ###
class Config:
2020-02-03 18:53:23 -08:00
# Default config vals
2020-02-04 23:21:53 -08:00
DEFAULT_DBNAME = 'pointsbot.db'
2020-02-03 18:53:23 -08:00
2020-02-04 23:21:53 -08:00
def __init__(self, filepath, subreddit, client_id, client_secret, username,
password, levels, database_path=None):
self._filepath = filepath
self._dirname = os.path.dirname(filepath)
if not database_path:
database_path = os.path.join(self._dirname, self.DEFAULT_DBNAME)
2020-02-05 10:39:05 -08:00
elif os.path.isdir(database_path):
database_path = os.path.join(database_path, self.DEFAULT_DBNAME)
2020-02-01 00:23:15 -08:00
self.database_path = database_path
2020-02-03 18:53:23 -08:00
2020-02-04 23:21:53 -08:00
self.subreddit = subreddit
2020-02-03 18:53:23 -08:00
self.client_id = client_id
self.client_secret = client_secret
self.username = username
self.password = password
self.levels = levels
2020-02-01 00:23:15 -08:00
@classmethod
2020-02-04 23:21:53 -08:00
def from_toml(cls, filepath):
2020-02-03 18:53:23 -08:00
obj = toml.load(filepath)
2020-02-01 00:23:15 -08:00
# Create list of level objects, in ascending order by point value
2020-02-01 00:23:15 -08:00
levels = []
2020-02-03 18:53:23 -08:00
for lvl in obj['levels']:
2020-02-04 23:21:53 -08:00
flair_template_id = lvl.get('flair_template_id', None)
if flair_template_id == '':
2020-02-03 18:53:23 -08:00
flair_template_id = None
levels.append(Level(lvl['name'], lvl['points'], flair_template_id))
levels.sort(key=lambda l: l.points)
2020-02-05 10:39:05 -08:00
dbpath = obj['filepaths']['database']
if dbpath:
dbpath = os.path.abspath(os.path.expandvars(os.path.expanduser(dbpath)))
2020-02-01 00:23:15 -08:00
return cls(
2020-02-04 23:21:53 -08:00
filepath,
2020-02-03 18:53:23 -08:00
obj['core']['subreddit'],
obj['credentials']['client_id'],
obj['credentials']['client_secret'],
obj['credentials']['username'],
obj['credentials']['password'],
levels,
2020-02-05 10:39:05 -08:00
database_path=dbpath,
2020-02-01 00:23:15 -08:00
)
2020-02-04 23:21:53 -08:00
def save(self):
obj = deepcopy(vars(self))
orig_levels = obj['levels']
obj['levels'] = []
for level in orig_levels:
obj['levels'].append({
'name': level.name,
'points': level.points,
'flair_template_id': level.flair_template_id,
})
with open(self._filepath, 'w') as f:
toml.dump(obj, f)
### Functions ###
def load(filepath=CONFIGPATH):
# Prompt user for config values if file doesn't exist
if not os.path.exists(filepath):
datadir = os.path.dirname(filepath)
if not os.path.exists(datadir):
os.makedirs(datadir)
interactive_config(filepath)
return Config.from_toml(filepath)
### Interactive Config Editing ###
def interactive_config(dest):
configvals = {
'core': {},
'filepaths': {},
'credentials': {},
'levels': [],
}
print('#' * 80 + '\nCONFIGURING THE BOT\n' + '#' * 80)
print('\nType a value for each field, then press enter.')
print('\nIf the field is specified as optional, leave blank to skip.\n')
configvals['core']['subreddit'] = input('subreddit? ')
print()
configvals['filepaths']['database'] = input('database filename? (optional) ')
print()
configvals['credentials']['client_id'] = input('client_id? ')
configvals['credentials']['client_secret'] = input('client_secret? ')
configvals['credentials']['username'] = input('username? ')
configvals['credentials']['password'] = input('password? ')
add_another_level = True
while add_another_level:
level = {}
level['name'] = input('\nLevel name? ')
2020-02-05 10:39:05 -08:00
level['points'] = int(input('Level points? '))
2020-02-04 23:21:53 -08:00
level['flair_template_id'] = input('Flair template ID? (optional) ')
configvals['levels'].append(level)
response = input('\nAdd another level? (y/n) ')
add_another_level = response.lower().startswith('y')
with open(dest, 'w') as f:
toml.dump(configvals, f)
2020-02-05 10:39:05 -08:00
print('#' * 80 + f'\nConfig settings saved to {dest}\n' + '#' * 80)
2020-02-01 00:23:15 -08:00