From deff37a30fab8841e2dcd7d7c80b70d9e7950b4b Mon Sep 17 00:00:00 2001 From: Collin R Date: Wed, 5 Feb 2020 10:39:05 -0800 Subject: [PATCH] Fixed config issues --- docs/TODO.md | 4 ++++ pointsbot/bot.py | 27 ++++++--------------------- pointsbot/config.py | 19 ++++++++----------- 3 files changed, 18 insertions(+), 32 deletions(-) diff --git a/docs/TODO.md b/docs/TODO.md index fc75cc0..9a10727 100644 --- a/docs/TODO.md +++ b/docs/TODO.md @@ -10,6 +10,8 @@ - [ ] Create a GUI for configuring and running the bot, and performing other jobs like adding or subtracting points for specific redditors - [ ] Check for updates in the Github repo and prompt user to update + - It turns out that this might only be possible with a Github user + account to authenticate with the API. - Github API overview: - https://developer.github.com/v3/ - https://developer.github.com/v4/ @@ -56,6 +58,8 @@ ### config.py +* [ ] Switch from `toml` package to `tomlkit` package + - Preserves style, comments, etc. * [X] Change from `ini` to `toml ` * [X] Consolidate `pointsbot.ini` and `praw.ini` into a single config file. * [X] Add option for flair_template_id diff --git a/pointsbot/bot.py b/pointsbot/bot.py index 1b55fb3..8f3a20f 100644 --- a/pointsbot/bot.py +++ b/pointsbot/bot.py @@ -19,7 +19,6 @@ TEST_COMMENTS = False def run(): - #cfg = config.Config.load() cfg = config.load() levels = cfg.levels @@ -42,10 +41,6 @@ def run(): db = database.Database(cfg.database_path) - # The pattern that determines whether a post is marked as solved - # Could also just use re.IGNORECASE flag - # solved_pat = re.compile('![Ss]olved') - # Monitor new comments for confirmed solutions # Passing pause_after=0 will bypass the internal exponential delay; instead, # have to check if any comments are returned with each query @@ -56,9 +51,7 @@ def run(): print_level(0, '\nFound comment') print_level(1, f'Comment text: "{comm.body}"') - # if marks_as_solved(comm, solved_pat): if marks_as_solved(comm): - # if not is_first_solution(comm, solved_pat): if not is_first_solution(comm): # Skip this "!solved" comment and wait for the next print_level(1, 'Not the first solution') @@ -100,30 +93,23 @@ def run(): ### Reddit Comment Functions ### -# def marks_as_solved(comment, solved_pattern): def marks_as_solved(comment): '''Return True if the comment meets the criteria for marking the submission as solved, False otherwise. ''' - op_resp_to_solver = (comment.is_submitter - and comment.parent() != comment.submission + op_resp_to_solver = (not comment.is_root + and comment.is_submitter and not comment.parent().is_submitter and SOLVED_PAT.search(comment.body)) - mod_resp_to_solver = (comment.subreddit.moderator(redditor=comment.author) + # Mod can only used MOD_SOLVED_PAT on any post, including their own + mod_resp_to_solver = (not comment.is_root + and comment.subreddit.moderator(redditor=comment.author) and MOD_SOLVED_PAT.search(comment.body)) - return not comment.is_root and (op_resp_to_solver or mod_resp_to_solver) - - """ - return (not comment.is_root - and comment.is_submitter - and not comment.parent().is_submitter - and solved_pattern.search(comment.body)) - """ + return op_resp_to_solver or mod_resp_to_solver -# def is_first_solution(solved_comment, solved_pattern): def is_first_solution(solved_comment): # Retrieve any comments hidden by "more comments" # Passing limit=0 will replace all "more comments" @@ -133,7 +119,6 @@ def is_first_solution(solved_comment): # Search the flattened comments tree for comment in submission.comments.list(): if (comment.id != solved_comment.id - # and marks_as_solved(comment, solved_pattern) and marks_as_solved(comment) and comment.created_utc < solved_comment.created_utc): # There is an earlier comment for the same submission diff --git a/pointsbot/config.py b/pointsbot/config.py index 67bab15..2aef8bb 100644 --- a/pointsbot/config.py +++ b/pointsbot/config.py @@ -23,7 +23,6 @@ SAMPLEPATH = os.path.abspath(os.path.join(os.path.dirname(__file__), class Config: # Default config vals - # DEFAULT_DBPATH = os.path.join(DATADIR, 'pointsbot.db') DEFAULT_DBNAME = 'pointsbot.db' def __init__(self, filepath, subreddit, client_id, client_secret, username, @@ -33,6 +32,8 @@ class Config: if not database_path: database_path = os.path.join(self._dirname, self.DEFAULT_DBNAME) + elif os.path.isdir(database_path): + database_path = os.path.join(database_path, self.DEFAULT_DBNAME) self.database_path = database_path self.subreddit = subreddit @@ -57,7 +58,9 @@ class Config: levels.append(Level(lvl['name'], lvl['points'], flair_template_id)) levels.sort(key=lambda l: l.points) - # database_path = os.path.join(DATADIR, obj['filepaths']['database']) + dbpath = obj['filepaths']['database'] + if dbpath: + dbpath = os.path.abspath(os.path.expandvars(os.path.expanduser(dbpath))) return cls( filepath, @@ -67,8 +70,7 @@ class Config: obj['credentials']['username'], obj['credentials']['password'], levels, - database_path=obj['filepaths']['database'], - # database_path=database_path, + database_path=dbpath, ) def save(self): @@ -95,11 +97,6 @@ def load(filepath=CONFIGPATH): datadir = os.path.dirname(filepath) if not os.path.exists(datadir): os.makedirs(datadir) - - # with open(SAMPLEPATH) as fin: - # with open(filepath, 'w') as fout: - # fout.write(fin.read()) - interactive_config(filepath) return Config.from_toml(filepath) @@ -133,7 +130,7 @@ def interactive_config(dest): while add_another_level: level = {} level['name'] = input('\nLevel name? ') - level['points'] = input('Level points? ') + level['points'] = int(input('Level points? ')) level['flair_template_id'] = input('Flair template ID? (optional) ') configvals['levels'].append(level) @@ -142,6 +139,6 @@ def interactive_config(dest): with open(dest, 'w') as f: toml.dump(configvals, f) - print(f'\nConfig settings saved to {dest}') + print('#' * 80 + f'\nConfig settings saved to {dest}\n' + '#' * 80)