Fixed config issues
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user