diff options
author | Florian Bruhin <git@the-compiler.org> | 2017-12-15 16:03:34 +0100 |
---|---|---|
committer | Florian Bruhin <git@the-compiler.org> | 2017-12-15 16:33:47 +0100 |
commit | 97a4e8d8471bd946e820aad928a371cbb793a544 (patch) | |
tree | c743a32471613b9090fb9f1d3625e74e32c4e2fc /scripts/hist_importer.py | |
parent | 1a4a9b43928783c5402a62d1384216e88be0c41e (diff) | |
download | qutebrowser-97a4e8d8471bd946e820aad928a371cbb793a544.tar.gz qutebrowser-97a4e8d8471bd946e820aad928a371cbb793a544.zip |
Rewrite error handling in hist_importer script
Raise an exception instead of calling sys.exit
Diffstat (limited to 'scripts/hist_importer.py')
-rwxr-xr-x | scripts/hist_importer.py | 28 |
1 files changed, 19 insertions, 9 deletions
diff --git a/scripts/hist_importer.py b/scripts/hist_importer.py index f4ad47062..1b7bbde53 100755 --- a/scripts/hist_importer.py +++ b/scripts/hist_importer.py @@ -29,6 +29,11 @@ import sys import os +class Error(Exception): + + pass + + def parse(): """Parse command line arguments.""" description = ("This program is meant to extract browser history from your" @@ -63,10 +68,8 @@ def parse(): def open_db(data_base): """Open connection with database.""" if os.path.isfile(data_base): - conn = sqlite3.connect(data_base) - return conn - else: - sys.exit('The file {} does not exist.'.format(data_base)) + return sqlite3.connect(data_base) + raise Error('The file {} does not exist.'.format(data_base)) def extract(source, query): @@ -87,8 +90,8 @@ def extract(source, query): conn.close() return history except sqlite3.OperationalError as op_e: - sys.exit('Could not perform queries on the source database: ' - '{}'.format(op_e)) + raise Error('Could not perform queries on the source database: ' + '{}'.format(op_e)) def clean(history): @@ -130,7 +133,7 @@ def insert_qb(history, dest): conn.close() -def main(): +def run(): """Main control flux of the script.""" args = parse() browser = args.browser.lower() @@ -142,13 +145,20 @@ def main(): 'from urls', } if browser not in query: - sys.exit('Sorry, the selected browser: "{}" is not supported.'.format( - browser)) + raise Error('Sorry, the selected browser: "{}" is not ' + 'supported.'.format(browser)) else: history = extract(source, query[browser]) history = clean(history) insert_qb(history, dest) +def main(): + try: + run() + except Error as e: + sys.exit(str(e)) + + if __name__ == "__main__": main() |