diff --git a/timesearch/commentaugment.py b/timesearch/commentaugment.py index 73d622a..1514938 100644 --- a/timesearch/commentaugment.py +++ b/timesearch/commentaugment.py @@ -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) diff --git a/timesearch/common.py b/timesearch/common.py index b9120b1..3d6d09d 100644 --- a/timesearch/common.py +++ b/timesearch/common.py @@ -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. diff --git a/timesearch/exceptions.py b/timesearch/exceptions.py index 9bd74b5..90b557c 100644 --- a/timesearch/exceptions.py +++ b/timesearch/exceptions.py @@ -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.' diff --git a/timesearch/livestream.py b/timesearch/livestream.py index c3a931d..8e8737d 100644 --- a/timesearch/livestream.py +++ b/timesearch/livestream.py @@ -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: diff --git a/timesearch/offline_reading.py b/timesearch/offline_reading.py index 16d6ae0..d1d91a2 100644 --- a/timesearch/offline_reading.py +++ b/timesearch/offline_reading.py @@ -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) diff --git a/timesearch/redmash.py b/timesearch/redmash.py index 01cd482..54d28e1 100644 --- a/timesearch/redmash.py +++ b/timesearch/redmash.py @@ -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, diff --git a/timesearch/timesearch.py b/timesearch/timesearch.py index ed7b0b7..bfbca27 100644 --- a/timesearch/timesearch.py +++ b/timesearch/timesearch.py @@ -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)