Rewrite error handling in hist_importer script
Raise an exception instead of calling sys.exit
This commit is contained in:
parent
1a4a9b4392
commit
97a4e8d847
@ -29,6 +29,11 @@ import sys
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
class Error(Exception):
|
||||||
|
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
def parse():
|
def parse():
|
||||||
"""Parse command line arguments."""
|
"""Parse command line arguments."""
|
||||||
description = ("This program is meant to extract browser history from your"
|
description = ("This program is meant to extract browser history from your"
|
||||||
@ -63,10 +68,8 @@ def parse():
|
|||||||
def open_db(data_base):
|
def open_db(data_base):
|
||||||
"""Open connection with database."""
|
"""Open connection with database."""
|
||||||
if os.path.isfile(data_base):
|
if os.path.isfile(data_base):
|
||||||
conn = sqlite3.connect(data_base)
|
return sqlite3.connect(data_base)
|
||||||
return conn
|
raise Error('The file {} does not exist.'.format(data_base))
|
||||||
else:
|
|
||||||
sys.exit('The file {} does not exist.'.format(data_base))
|
|
||||||
|
|
||||||
|
|
||||||
def extract(source, query):
|
def extract(source, query):
|
||||||
@ -87,8 +90,8 @@ def extract(source, query):
|
|||||||
conn.close()
|
conn.close()
|
||||||
return history
|
return history
|
||||||
except sqlite3.OperationalError as op_e:
|
except sqlite3.OperationalError as op_e:
|
||||||
sys.exit('Could not perform queries on the source database: '
|
raise Error('Could not perform queries on the source database: '
|
||||||
'{}'.format(op_e))
|
'{}'.format(op_e))
|
||||||
|
|
||||||
|
|
||||||
def clean(history):
|
def clean(history):
|
||||||
@ -130,7 +133,7 @@ def insert_qb(history, dest):
|
|||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def run():
|
||||||
"""Main control flux of the script."""
|
"""Main control flux of the script."""
|
||||||
args = parse()
|
args = parse()
|
||||||
browser = args.browser.lower()
|
browser = args.browser.lower()
|
||||||
@ -142,13 +145,20 @@ def main():
|
|||||||
'from urls',
|
'from urls',
|
||||||
}
|
}
|
||||||
if browser not in query:
|
if browser not in query:
|
||||||
sys.exit('Sorry, the selected browser: "{}" is not supported.'.format(
|
raise Error('Sorry, the selected browser: "{}" is not '
|
||||||
browser))
|
'supported.'.format(browser))
|
||||||
else:
|
else:
|
||||||
history = extract(source, query[browser])
|
history = extract(source, query[browser])
|
||||||
history = clean(history)
|
history = clean(history)
|
||||||
insert_qb(history, dest)
|
insert_qb(history, dest)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
try:
|
||||||
|
run()
|
||||||
|
except Error as e:
|
||||||
|
sys.exit(str(e))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
Loading…
Reference in New Issue
Block a user