Files
PointsBot/pointsbot/config.py

66 lines
1.7 KiB
Python
Raw Normal View History

import os
2020-02-01 00:23:15 -08:00
import os.path
# from os.path import abspath, dirname, expanduser, join
2020-02-03 18:53:23 -08:00
import toml
2020-02-01 00:23:15 -08:00
from .level import Level
### Globals ###
# ROOTPATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
2020-02-01 00:23:15 -08:00
### Classes ###
class Config:
DATADIR = os.path.join(os.path.expanduser('~'), '.pointsbot')
PATH = os.path.join(DATADIR, 'pointsbot.toml')
2020-02-03 18:53:23 -08:00
# Default config vals
DEFAULT_DBPATH = os.path.join(DATADIR, 'pointsbot.db')
2020-02-03 18:53:23 -08:00
def __init__(self, subreddit, client_id, client_secret, username, password,
levels, database_path=DEFAULT_DBPATH):
self.subreddit = subreddit
2020-02-01 00:23:15 -08:00
self.database_path = database_path
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
def load(cls, filepath=PATH):
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']:
flair_template_id = lvl['flair_template_id']
if flair_template_id == "":
flair_template_id = None
levels.append(Level(lvl['name'], lvl['points'], flair_template_id))
levels.sort(key=lambda l: l.points)
database_path = os.path.join(cls.DATADIR, obj['filepaths']['database'])
2020-02-01 00:23:15 -08:00
return cls(
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-01 00:23:15 -08:00
database_path=database_path,
)
@classmethod
def dump(cls, obj, filepath=PATH):
2020-02-03 18:53:23 -08:00
pass
2020-02-01 00:23:15 -08:00