From d85d911b2202d8020717f9d5def8d0cf0e63737e Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Tue, 13 Mar 2018 02:24:11 -0700 Subject: [PATCH] Add PhotoDB init option `create`. Disable to require existing. --- etiquette/photodb.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/etiquette/photodb.py b/etiquette/photodb.py index 7eb03df..7a0834e 100644 --- a/etiquette/photodb.py +++ b/etiquette/photodb.py @@ -1075,6 +1075,8 @@ class PhotoDB( def __init__( self, data_directory=None, + *, + create=True, ephemeral=False, ): super().__init__() @@ -1098,7 +1100,6 @@ class PhotoDB( if self.data_directory.exists and not self.data_directory.is_dir: raise exceptions.BadDataDirectory(self.data_directory.absolute_path) - os.makedirs(self.data_directory.absolute_path, exist_ok=True) self.log = logging.getLogger('etiquette:%s' % self.data_directory.absolute_path) self.log.setLevel(logging.DEBUG) @@ -1110,6 +1111,12 @@ class PhotoDB( else: self.database_filepath = self.data_directory.with_child(constants.DEFAULT_DBNAME) existing_database = self.database_filepath.exists + + if not create and not self.ephemeral and not existing_database: + raise FileNotFoundError('"%s" does not exist and create is off.' % self.data_directory) + + if not self.ephemeral: + os.makedirs(self.data_directory.absolute_path, exist_ok=True) self.sql = sqlite3.connect(self.database_filepath.absolute_path) if existing_database: