Add the NotExclusive exception and common.is_xor.

master
Ethan Dalool 2017-12-13 14:35:36 -08:00
parent 8b98224465
commit 278923a000
7 changed files with 33 additions and 23 deletions

View File

@ -1,6 +1,7 @@
import traceback
from . import common
from . import exceptions
from . import tsdb
@ -17,6 +18,9 @@ def commentaugment(
Take the IDs of collected submissions, and gather comments from those threads.
Please see the global DOCSTRING_COMMENTAUGMENT variable.
'''
if not common.is_xor(subreddit, username):
raise exceptions.NotExclusive(['subreddit', 'username'])
common.bot.login(common.r)
if specific_submission is not None:
if not specific_submission.startswith('t3_'):
@ -24,9 +28,6 @@ def commentaugment(
specific_submission_obj = common.r.submission(specific_submission[3:])
subreddit = specific_submission_obj.subreddit.display_name
if (subreddit is None) == (username is None):
raise Exception('Enter subreddit or username but not both')
if subreddit:
if specific_submission is None:
database = tsdb.TSDB.for_subreddit(subreddit, do_create=False)

View File

@ -87,6 +87,12 @@ def int_none(x):
return None
return int(x)
def is_xor(*args):
'''
Return True if and only if one arg is truthy.
'''
return [bool(a) for a in args].count(True) == 1
def nofailrequest(function):
'''
Creates a function that will retry until it succeeds.

View File

@ -27,3 +27,9 @@ class DatabaseOutOfDate(TimesearchException):
class DatabaseNotFound(TimesearchException, FileNotFoundError):
error_message = 'Database file not found: "{}"'
class NotExclusive(TimesearchException):
'''
For when two or more mutually exclusive actions have been requested.
'''
error_message = 'One and only one of {} must be passed.'

View File

@ -3,6 +3,7 @@ import time
import traceback
from . import common
from . import exceptions
from . import tsdb
@ -62,11 +63,6 @@ def livestream(
print()
return
except Exception:
traceback.print_exc()
print('Retrying in 5...')
time.sleep(5)
hangman = lambda: livestream(
username='gallowboob',
do_submissions=True,
@ -83,10 +79,11 @@ def _livestream_as_a_generator(
params,
):
if bool(subreddit) == bool(username):
raise Exception('Require either username / subreddit parameter, but not both')
if bool(do_submissions) is bool(do_comments) is False:
raise Exception('Require do_submissions and/or do_comments parameter')
if not common.is_xor(subreddit, username):
raise exceptions.NotExclusive(['subreddit', 'username'])
if not any([do_submissions, do_comments]):
raise TypeError('Required do_submissions and/or do_comments parameter')
common.bot.login(common.r)
if subreddit:
@ -129,8 +126,8 @@ def _livestream_helper(
args and kwargs go into the collecting functions.
'''
if bool(submission_function) is bool(comment_function) is False:
raise Exception('Require submissions and/or comments parameter')
if not any([submission_function, comment_function]):
raise TypeError('Required submissions and/or comments parameter')
results = []
if submission_function:

View File

@ -2,6 +2,7 @@ import os
import markdown
from . import common
from . import exceptions
from . import tsdb
@ -221,8 +222,8 @@ def html_from_database(subreddit=None, username=None, specific_submission=None):
if markdown is None:
raise ImportError('Page cannot be rendered without the markdown module')
if (subreddit is None) == (username is None):
raise Exception('Enter subreddit or username but not both')
if not common.is_xor(subreddit, username):
raise exceptions.NotExclusive(['subreddit', 'username'])
if subreddit:
database = tsdb.TSDB.for_subreddit(subreddit, do_create=False)

View File

@ -2,6 +2,7 @@ import datetime
import os
from . import common
from . import exceptions
from . import tsdb
@ -52,8 +53,8 @@ def redmash(
html=False,
score_threshold=0,
):
if (subreddit is None) == (username is None):
raise Exception('Enter subreddit or username but not both')
if not common.is_xor(subreddit, username):
raise exceptions.NotExclusive(['subreddit', 'username'])
if subreddit:
database = tsdb.TSDB.for_subreddit(subreddit, do_create=False)
@ -159,9 +160,6 @@ def redmash_worker(
return mash_filepath
def redmash_argparse(args):
if args.subreddit is args.username is None:
raise ValueError('-r subreddit OR -u username must be provided')
return redmash(
subreddit=args.subreddit,
username=args.username,

View File

@ -2,6 +2,7 @@ import time
import traceback
from . import common
from . import exceptions
from . import tsdb
@ -21,8 +22,8 @@ def timesearch(
Collect submissions across time.
Please see the global DOCSTRING variable.
'''
if (subreddit is None) == (username is None):
raise Exception('Enter subreddit or username but not both')
if not common.is_xor(subreddit, username):
raise exceptions.NotExclusive(['subreddit', 'username'])
common.bot.login(common.r)