From a47cdaaf040e81a6e5a66e3923685504c505ce8f Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Fri, 16 Dec 2016 17:59:43 -0800 Subject: [PATCH] misc cleanup --- README.md | 1 + etiquette_upgrader.py | 2 +- phototagger.py | 139 ++++++++++++++++++++++-------------------- 3 files changed, 76 insertions(+), 66 deletions(-) diff --git a/README.md b/README.md index e2a2dc3..f21ef31 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ This is the readme file. - Move out more helpers - Create objects.py - Debate whether the `UserMixin.login` method should accept usernames or I should standardize the usage of IDs only internally. +- Move config type variables out of constants and create a real config system. ### Changelog diff --git a/etiquette_upgrader.py b/etiquette_upgrader.py index 224fa31..02a47d5 100644 --- a/etiquette_upgrader.py +++ b/etiquette_upgrader.py @@ -48,7 +48,7 @@ def upgrade_all(database_filename): needed_version = phototagger.DATABASE_VERSION if current_version == needed_version: - print('Already up-to-date.') + print('Already up-to-date with version %d.' % needed_version) return for version_number in range(current_version + 1, needed_version + 1): diff --git a/phototagger.py b/phototagger.py index 96b5976..a403dee 100644 --- a/phototagger.py +++ b/phototagger.py @@ -498,11 +498,12 @@ class PDBAlbumMixin: if not isinstance(description, str): raise TypeError('Description must be string, not %s' % type(description)) - data = {} - data['id'] = albumid - data['title'] = title - data['description'] = description - data['associated_directory'] = associated_directory + data = { + 'id': albumid, + 'title': title, + 'description': description, + 'associated_directory': associated_directory, + } (qmarks, bindings) = binding_filler(SQL_ALBUM_COLUMNS, data) query = 'INSERT INTO albums VALUES(%s)' % qmarks @@ -557,23 +558,6 @@ class PDBPhotoMixin: if count <= 0: break - def purge_deleted_files(self): - ''' - Remove Photo entries if their corresponding file is no longer found. - ''' - photos = self.get_photos_by_recent() - for photo in photos: - if os.path.exists(photo.real_filepath): - continue - photo.delete() - - def purge_empty_albums(self): - albums = self.get_albums() - for album in albums: - if album.children() or album.photos(): - continue - album.delete() - def new_photo( self, filename, @@ -611,21 +595,22 @@ class PDBPhotoMixin: created = int(getnow()) photoid = self.generate_id('photos') - data = {} - data['id'] = photoid - data['filepath'] = filename - data['override_filename'] = None - data['extension'] = extension - data['created'] = created - data['tagged_at'] = None - # These will be filled in during the metadata stage. - data['bytes'] = None - data['width'] = None - data['height'] = None - data['area'] = None - data['ratio'] = None - data['duration'] = None - data['thumbnail'] = None + data = { + 'id': photoid, + 'filepath': filename, + 'override_filename': None, + 'extension': extension, + 'created': created, + 'tagged_at': None, + # These will be filled in during the metadata stage. + 'bytes': None, + 'width': None, + 'height': None, + 'area': None, + 'ratio': None, + 'duration': None, + 'thumbnail': None, + } (qmarks, bindings) = binding_filler(SQL_PHOTO_COLUMNS, data) query = 'INSERT INTO photos VALUES(%s)' % qmarks @@ -647,6 +632,23 @@ class PDBPhotoMixin: self.commit() return photo + def purge_deleted_files(self): + ''' + Remove Photo entries if their corresponding file is no longer found. + ''' + photos = self.get_photos_by_recent() + for photo in photos: + if os.path.exists(photo.real_filepath): + continue + photo.delete() + + def purge_empty_albums(self): + albums = self.get_albums() + for album in albums: + if album.children() or album.photos(): + continue + album.delete() + def search( self, *, @@ -1005,11 +1007,12 @@ class PDBUserMixin: hashed_password = bcrypt.hashpw(password, bcrypt.gensalt()) created = int(getnow()) - data = {} - data['id'] = user_id - data['username'] = username - data['password'] = hashed_password - data['created'] = created + data = { + 'id': user_id, + 'username': username, + 'password': hashed_password, + 'created': created, + } (qmarks, bindings) = binding_filler(SQL_USER_COLUMNS, data) query = 'INSERT INTO users VALUES(%s)' % qmarks @@ -1330,7 +1333,7 @@ class PhotoDB(PDBAlbumMixin, PDBPhotoMixin, PDBTagMixin, PDBUserMixin): return new_id def get_thing_by_id(self, thing_type, thing_id): - thing_map = self.thing_map(thing_type) + thing_map = _THING_CLASSES[thing_type] if isinstance(thing_id, thing_map['class']): thing_id = thing_id.id @@ -1344,7 +1347,7 @@ class PhotoDB(PDBAlbumMixin, PDBPhotoMixin, PDBTagMixin, PDBUserMixin): return thing def get_things(self, thing_type, orderby=None): - thing_map = self.thing_map(thing_type) + thing_map = _THING_CLASSES[thing_type] if orderby: self.cur.execute('SELECT * FROM %s ORDER BY %s' % (thing_map['table'], orderby)) @@ -1356,27 +1359,6 @@ class PhotoDB(PDBAlbumMixin, PDBPhotoMixin, PDBTagMixin, PDBUserMixin): thing = thing_map['class'](self, row_tuple=thing) yield thing - def thing_map(self, thing_type): - return { - 'album': - { - 'class': Album, - 'exception': exceptions.NoSuchAlbum, - 'table': 'albums', - }, - 'tag': - { - 'class': Tag, - 'exception': exceptions.NoSuchTag, - 'table': 'tags', - }, - 'photo': - { - 'class': Photo, - 'exception': exceptions.NoSuchPhoto, - 'table': 'photos', - }, - }[thing_type] #################################################################################################### #################################################################################################### @@ -2158,6 +2140,33 @@ class User(ObjectBase): return rep +_THING_CLASSES = { + 'album': + { + 'class': Album, + 'exception': exceptions.NoSuchAlbum, + 'table': 'albums', + }, + 'photo': + { + 'class': Photo, + 'exception': exceptions.NoSuchPhoto, + 'table': 'photos', + }, + 'tag': + { + 'class': Tag, + 'exception': exceptions.NoSuchTag, + 'table': 'tags', + }, + 'user': + { + 'class': User, + 'exception': exceptions.NoSuchUser, + 'table': 'users', + } +} + if __name__ == '__main__': p = PhotoDB() print(p)