Fixed config issues

This commit is contained in:
Collin R
2020-02-05 10:39:05 -08:00
parent a0462d3bda
commit deff37a30f
3 changed files with 18 additions and 32 deletions

View File

@@ -10,6 +10,8 @@
- [ ] Create a GUI for configuring and running the bot, and performing other jobs - [ ] Create a GUI for configuring and running the bot, and performing other jobs
like adding or subtracting points for specific redditors like adding or subtracting points for specific redditors
- [ ] Check for updates in the Github repo and prompt user to update - [ ] 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: - Github API overview:
- https://developer.github.com/v3/ - https://developer.github.com/v3/
- https://developer.github.com/v4/ - https://developer.github.com/v4/
@@ -56,6 +58,8 @@
### config.py ### config.py
* [ ] Switch from `toml` package to `tomlkit` package
- Preserves style, comments, etc.
* [X] Change from `ini` to `toml ` * [X] Change from `ini` to `toml `
* [X] Consolidate `pointsbot.ini` and `praw.ini` into a single config file. * [X] Consolidate `pointsbot.ini` and `praw.ini` into a single config file.
* [X] Add option for flair_template_id * [X] Add option for flair_template_id

View File

@@ -19,7 +19,6 @@ TEST_COMMENTS = False
def run(): def run():
#cfg = config.Config.load()
cfg = config.load() cfg = config.load()
levels = cfg.levels levels = cfg.levels
@@ -42,10 +41,6 @@ def run():
db = database.Database(cfg.database_path) 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 # Monitor new comments for confirmed solutions
# Passing pause_after=0 will bypass the internal exponential delay; instead, # Passing pause_after=0 will bypass the internal exponential delay; instead,
# have to check if any comments are returned with each query # have to check if any comments are returned with each query
@@ -56,9 +51,7 @@ def run():
print_level(0, '\nFound comment') print_level(0, '\nFound comment')
print_level(1, f'Comment text: "{comm.body}"') print_level(1, f'Comment text: "{comm.body}"')
# if marks_as_solved(comm, solved_pat):
if marks_as_solved(comm): if marks_as_solved(comm):
# if not is_first_solution(comm, solved_pat):
if not is_first_solution(comm): if not is_first_solution(comm):
# Skip this "!solved" comment and wait for the next # Skip this "!solved" comment and wait for the next
print_level(1, 'Not the first solution') print_level(1, 'Not the first solution')
@@ -100,30 +93,23 @@ def run():
### Reddit Comment Functions ### ### Reddit Comment Functions ###
# def marks_as_solved(comment, solved_pattern):
def marks_as_solved(comment): def marks_as_solved(comment):
'''Return True if the comment meets the criteria for marking the submission '''Return True if the comment meets the criteria for marking the submission
as solved, False otherwise. as solved, False otherwise.
''' '''
op_resp_to_solver = (comment.is_submitter op_resp_to_solver = (not comment.is_root
and comment.parent() != comment.submission and comment.is_submitter
and not comment.parent().is_submitter and not comment.parent().is_submitter
and SOLVED_PAT.search(comment.body)) 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)) and MOD_SOLVED_PAT.search(comment.body))
return not comment.is_root and (op_resp_to_solver or mod_resp_to_solver) return 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))
"""
# def is_first_solution(solved_comment, solved_pattern):
def is_first_solution(solved_comment): def is_first_solution(solved_comment):
# Retrieve any comments hidden by "more comments" # Retrieve any comments hidden by "more comments"
# Passing limit=0 will replace all "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 # Search the flattened comments tree
for comment in submission.comments.list(): for comment in submission.comments.list():
if (comment.id != solved_comment.id if (comment.id != solved_comment.id
# and marks_as_solved(comment, solved_pattern)
and marks_as_solved(comment) and marks_as_solved(comment)
and comment.created_utc < solved_comment.created_utc): and comment.created_utc < solved_comment.created_utc):
# There is an earlier comment for the same submission # There is an earlier comment for the same submission

View File

@@ -23,7 +23,6 @@ SAMPLEPATH = os.path.abspath(os.path.join(os.path.dirname(__file__),
class Config: class Config:
# Default config vals # Default config vals
# DEFAULT_DBPATH = os.path.join(DATADIR, 'pointsbot.db')
DEFAULT_DBNAME = 'pointsbot.db' DEFAULT_DBNAME = 'pointsbot.db'
def __init__(self, filepath, subreddit, client_id, client_secret, username, def __init__(self, filepath, subreddit, client_id, client_secret, username,
@@ -33,6 +32,8 @@ class Config:
if not database_path: if not database_path:
database_path = os.path.join(self._dirname, self.DEFAULT_DBNAME) 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.database_path = database_path
self.subreddit = subreddit self.subreddit = subreddit
@@ -57,7 +58,9 @@ class Config:
levels.append(Level(lvl['name'], lvl['points'], flair_template_id)) levels.append(Level(lvl['name'], lvl['points'], flair_template_id))
levels.sort(key=lambda l: l.points) 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( return cls(
filepath, filepath,
@@ -67,8 +70,7 @@ class Config:
obj['credentials']['username'], obj['credentials']['username'],
obj['credentials']['password'], obj['credentials']['password'],
levels, levels,
database_path=obj['filepaths']['database'], database_path=dbpath,
# database_path=database_path,
) )
def save(self): def save(self):
@@ -95,11 +97,6 @@ def load(filepath=CONFIGPATH):
datadir = os.path.dirname(filepath) datadir = os.path.dirname(filepath)
if not os.path.exists(datadir): if not os.path.exists(datadir):
os.makedirs(datadir) os.makedirs(datadir)
# with open(SAMPLEPATH) as fin:
# with open(filepath, 'w') as fout:
# fout.write(fin.read())
interactive_config(filepath) interactive_config(filepath)
return Config.from_toml(filepath) return Config.from_toml(filepath)
@@ -133,7 +130,7 @@ def interactive_config(dest):
while add_another_level: while add_another_level:
level = {} level = {}
level['name'] = input('\nLevel name? ') 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) ') level['flair_template_id'] = input('Flair template ID? (optional) ')
configvals['levels'].append(level) configvals['levels'].append(level)
@@ -142,6 +139,6 @@ def interactive_config(dest):
with open(dest, 'w') as f: with open(dest, 'w') as f:
toml.dump(configvals, f) toml.dump(configvals, f)
print(f'\nConfig settings saved to {dest}') print('#' * 80 + f'\nConfig settings saved to {dest}\n' + '#' * 80)