Start exception hierarchy; rename DBNotFound to DatabaseNotFound.

master
Ethan Dalool 2017-12-13 14:07:52 -08:00
parent f4d3679063
commit b7fcad7cc1
3 changed files with 22 additions and 7 deletions

View File

@ -423,10 +423,9 @@ def main(argv):
args = parser.parse_args(argv)
try:
args.func(args)
except exceptions.DBNotFound as e:
message = '"%s" is not an existing database.'
except exceptions.DatabaseNotFound as e:
message = str(e)
message += '\nHave you used any of the other utilities to collect data?'
message = message % e.path.absolute_path
print(message)
return 1

View File

@ -1,3 +1,19 @@
class DBNotFound(FileNotFoundError):
def __init__(self, path):
self.path = path
class TimesearchException(Exception):
'''
Base type for all of the Timesearch exceptions.
Subtypes should have a class attribute `error_message`. The error message
may contain {format} strings which will be formatted using the
Exception's constructor arguments.
'''
error_message = ''
def __init__(self, *args, **kwargs):
self.given_args = args
self.given_kwargs = kwargs
self.error_message = self.error_message.format(*args, **kwargs)
self.args = (self.error_message, args, kwargs)
def __str__(self):
return self.error_message
class DatabaseNotFound(TimesearchException, FileNotFoundError):
error_message = 'Database file not found: "{}"'

View File

@ -124,7 +124,7 @@ class TSDB:
self.filepath = pathclass.Path(filepath)
if not self.filepath.is_file:
if not do_create:
raise exceptions.DBNotFound(self.filepath)
raise exceptions.DatabaseNotFound(self.filepath)
print('New database', self.filepath.relative_path)
os.makedirs(self.filepath.parent.absolute_path, exist_ok=True)