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

View File

@ -87,6 +87,12 @@ def int_none(x):
return None return None
return int(x) 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): def nofailrequest(function):
''' '''
Creates a function that will retry until it succeeds. Creates a function that will retry until it succeeds.

View File

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

View File

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

View File

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

View File

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