Somewhat simplify a few conditions in photodb init.

This commit is contained in:
voussoir 2018-11-24 22:11:49 -08:00
parent 1eb5a6f465
commit 7674610b88

View file

@ -1262,19 +1262,22 @@ class PhotoDB(
): ):
super().__init__() super().__init__()
ephemeral = bool(ephemeral)
if data_directory is not None and ephemeral:
raise exceptions.NotExclusive(['data_directory', 'ephemeral'])
self.ephemeral = ephemeral self.ephemeral = ephemeral
# DATA DIR PREP # DATA DIR PREP
if data_directory is None: if data_directory is not None:
if self.ephemeral: pass
# In addition to the data_dir as a pathclass object, keep the
# TempDir object so we can use the cleanup method later.
self.ephemeral_directory = tempfile.TemporaryDirectory(prefix='etiquette_ephem_')
data_directory = self.ephemeral_directory.name
else:
data_directory = constants.DEFAULT_DATADIR
elif self.ephemeral: elif self.ephemeral:
raise exceptions.NotExclusive(['data_directory', 'ephemeral']) # In addition to the data_dir as a pathclass object, keep the
# TempDir object so we can use the cleanup method later.
self.ephemeral_directory = tempfile.TemporaryDirectory(prefix='etiquette_ephem_')
data_directory = self.ephemeral_directory.name
else:
data_directory = constants.DEFAULT_DATADIR
data_directory = helpers.remove_path_badchars(data_directory, allowed=':/\\') data_directory = helpers.remove_path_badchars(data_directory, allowed=':/\\')
self.data_directory = pathclass.Path(data_directory) self.data_directory = pathclass.Path(data_directory)
@ -1287,16 +1290,15 @@ class PhotoDB(
# DATABASE # DATABASE
if self.ephemeral: if self.ephemeral:
self.sql = sqlite3.connect(':memory:')
existing_database = False existing_database = False
self.sql = sqlite3.connect(':memory:')
else: else:
self.database_filepath = self.data_directory.with_child(constants.DEFAULT_DBNAME) self.database_filepath = self.data_directory.with_child(constants.DEFAULT_DBNAME)
existing_database = self.database_filepath.exists existing_database = self.database_filepath.exists
if not create and not self.ephemeral and not existing_database: if not existing_database and not create:
raise FileNotFoundError('"%s" does not exist and create is off.' % self.data_directory) raise FileNotFoundError(f'"{self.data_directory}" does not exist and create is off.')
if not self.ephemeral:
os.makedirs(self.data_directory.absolute_path, exist_ok=True) os.makedirs(self.data_directory.absolute_path, exist_ok=True)
self.sql = sqlite3.connect(self.database_filepath.absolute_path) self.sql = sqlite3.connect(self.database_filepath.absolute_path)
@ -1317,7 +1319,6 @@ class PhotoDB(
self.log.setLevel(self.config['log_level']) self.log.setLevel(self.config['log_level'])
# OTHER # OTHER
self._cached_frozen_children = None self._cached_frozen_children = None
self.caches = { self.caches = {
@ -1338,14 +1339,13 @@ class PhotoDB(
raise exceptions.DatabaseOutOfDate(existing=existing, new=constants.DATABASE_VERSION) raise exceptions.DatabaseOutOfDate(existing=existing, new=constants.DATABASE_VERSION)
def _first_time_setup(self): def _first_time_setup(self):
self.log.debug('Running first-time setup.') self.log.debug('Running first-time database setup.')
cur = self.sql.cursor() self.sql.executescript(constants.DB_INIT)
cur.executescript(constants.DB_INIT)
self.sql.commit() self.sql.commit()
def _load_pragmas(self): def _load_pragmas(self):
cur = self.sql.cursor() self.log.debug('Reloading pragmas.')
cur.executescript(constants.DB_PRAGMAS) self.sql.executescript(constants.DB_PRAGMAS)
self.sql.commit() self.sql.commit()
def __del__(self): def __del__(self):