Skip to content

Commit

Permalink
Redo error handling of XWLException
Browse files Browse the repository at this point in the history
Pass a dict back to make it easier to separate arguments
  • Loading branch information
aanker committed Nov 24, 2022
1 parent 2104a1a commit 256735a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 44 deletions.
100 changes: 60 additions & 40 deletions xwordlist/xwl.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@


class WordList:
def __init__(self, myList=[], color=''):
def __init__(self, myList=[]):
self.myList = myList
self.color = color

# List transformation options
def minimum(self, numChars):
Expand All @@ -20,8 +19,11 @@ def minimum(self, numChars):
newList.append(line)
self.myList = newList
else:
error = f'Exiting... argument for minimum must be an integer, not {self.err_text(numChars)}'
raise XWLException(error)
err_dict = {
'error': 'Exiting... argument for minimum must be an integer, not {}',
'arg': numChars,
}
raise XWLException(err_dict)

def strip(self, stripWhat):
newList = []
Expand All @@ -34,8 +36,11 @@ def strip(self, stripWhat):
newList.append(newWord)
self.myList = newList
else:
error = f'Exiting... unknown strip option {self.err_text(stripWhat)}'
raise XWLException(error)
err_dict = {
'error': 'Exiting... unknown strip option {}',
'arg': stripWhat,
}
raise XWLException(err_dict)

def case(self, newCase):
case_dict = {
Expand All @@ -45,8 +50,11 @@ def case(self, newCase):
if newCase in case_dict:
self.myList = list(case_dict[newCase](line) for line in self.myList)
elif newCase != 'none':
error = f'Exiting... unknown case option {self.err_text(newCase)}'
raise XWLException(error)
err_dict = {
'error': 'Exiting... unknown case option {}',
'arg': newCase,
}
raise XWLException(err_dict)

def dedupe(self, dedupeType):
if dedupeType == 'bycase':
Expand All @@ -61,17 +69,23 @@ def dedupe(self, dedupeType):
newList.append(line)
self.myList = newList
else:
error = f'Exiting... incorrect dedupe option {self.err_text(dedupeType)}'
raise XWLException(error)
err_dict = {
'error': 'Exiting... incorrect dedupe option {}',
'arg': dedupeType,
}
raise XWLException(err_dict)

def alphabetize(self, direction):
if direction == 'normal':
self.myList = sorted(self.myList, key=str.casefold)
elif direction == 'reverse':
self.myList = sorted(self.myList, key=str.casefold, reverse=True)
else:
error = f'Exiting... incorrect alphabetize option {self.err_text(direction)}'
raise XWLException(error)
err_dict = {
'error': 'Exiting... incorrect alphabetize option {}',
'arg': direction,
}
raise XWLException(err_dict)

# Content parsing options
def regex(self, regexInput):
Expand All @@ -82,12 +96,18 @@ def regex(self, regexInput):
self.myList = newList

except re.error:
error = f'Regex pattern {self.err_text(regexInput)} not valid, please check and try again'
raise XWLException(error)
err_dict = {
'error': 'Regex pattern {} not valid, please check and try again',
'arg': regexInput,
}
raise XWLException(err_dict)

except Exception as e:
error = f'Error {self.err_text(e)}'
raise XWLException(error)
err_dict = {
'error': 'Error {}',
'arg': e,
}
raise XWLException(err_dict)

def convert(self, parseChars):
# Add a space to parseChars since we always parse by that
Expand All @@ -101,18 +121,10 @@ def convert(self, parseChars):
newList.append(line)
self.myList = newList

def err_text(self, text):
if self.color:
return f'<{self.color}>{text}</{self.color}>'
else:
return text


class WebExtract:
def __init__(self, color=''):
self.returnWords = []
self.scrapeWords = []
self.color = color

def get_web_page(self, webURL, parseDict, webExtract):
try:
Expand All @@ -137,27 +149,41 @@ def get_web_page(self, webURL, parseDict, webExtract):
self.returnWords.extend(self.scrapeWords)

elif r.status_code == 403:
error = 'Unable to load webpage due to code 403: this usually means we are being blocked'
raise XWLException(error)
err_dict = {
'error': 'Unable to load webpage due to code 403: this usually means we are being blocked',
}
raise XWLException(err_dict)

else:
error = f'Unable to load webpage, status code = {self.err_text(r.status_code)}'
raise XWLException(error)
err_dict = {
'error': 'Unable to load webpage, status code = {}',
'arg': r.status_code,
}
raise XWLException(err_dict)

except XWLException:
raise

except (requests.URLRequired, requests.RequestException):
error = f'No content retrieved, error URL: {self.err_text(webURL)}'
raise XWLException(error)
err_dict = {
'error': 'No content retrieved, error URL: {}',
'arg': self.webURL,
}
raise XWLException(err_dict)

except AttributeError:
error = f'HTML attribute {self.err_text(parseDict)} not found, check document and try again'
raise XWLException(error)
err_dict = {
'error': 'HTML attribute {} not found, check document and try again',
'arg': self.parseDict,
}
raise XWLException(err_dict)

except Exception as e:
error = f'Web error {self.err_text(e)}'
raise XWLException(error)
err_dict = {
'error': 'Web error {}',
'arg': e,
}
raise XWLException(err_dict)

def _extract_from_web(self, extractWhat, soup, extractURL):
# A few ways the default option can come in, try that first
Expand Down Expand Up @@ -185,12 +211,6 @@ def _extract_from_web(self, extractWhat, soup, extractURL):

self.scrapeWords = list(line for line in self.scrapeWords)

def err_text(self, text):
if self.color:
return f'<{self.color}>{text}</{self.color}>'
else:
return text


class XWLException(Exception):
pass
11 changes: 7 additions & 4 deletions xwordlist/xwordlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ def setup_input(localArgs, otherArgs):
if localArgs.webpage:
if localArgs.container:
parseDict = create_dict(localArgs.container)
webScrape = xwl.WebExtract(color=IMPACT_COLOR)
webScrape.get_web_page(localArgs.webpage, parseDict, localArgs.webextract)
returnWords.extend(webScrape.returnWords)

Expand All @@ -140,7 +139,6 @@ def setup_input(localArgs, otherArgs):
'urlLength': urlLength,
}
print_line(print_text, arg_dict, endText='')
webScrape = xwl.WebExtract(color=IMPACT_COLOR)
webScrape.get_web_page(oneUrl, parseDict, localArgs.webextract)
returnWords.extend(webScrape.returnWords)
if urlCount < urlLength:
Expand Down Expand Up @@ -281,7 +279,7 @@ def main():
# Set up output file to make sure it doesn't exist then grab all inputs (including web parsing)
outputFile = setup_output(confArgs, defArgs)
try:
inputWords = xwl.WordList(myList=setup_input(confArgs, defArgs['globals']), color=IMPACT_COLOR)
inputWords = xwl.WordList(myList=setup_input(confArgs, defArgs['globals']))

# Do any text parsing
if confArgs.regex is not None:
Expand All @@ -308,7 +306,12 @@ def main():
do_ignore(option, thatOption) if thatOption else getattr(inputWords, option)(confOption)

except xwl.XWLException as e:
print_formatted_text(HTML(e))
(err_info, ) = e.args
if 'arg' in err_info:
arg = f"<{IMPACT_COLOR}>{err_info['arg']}</{IMPACT_COLOR}>"
print_formatted_text(HTML(err_info['error'].format(arg)))
else:
print_formatted_text(HTML(err_info['error']))
sys.exit()

# Now save the file
Expand Down

0 comments on commit 256735a

Please sign in to comment.