Organize config by nesting related stuff.

I thought about switching to .ini files but that's even less desirable.
This commit is contained in:
voussoir 2017-07-29 16:23:15 -07:00
parent c3c86de7f8
commit bd847159fa
4 changed files with 105 additions and 75 deletions

View file

@ -150,35 +150,52 @@ DEFAULT_THUMBDIR = 'site_thumbnails'
DEFAULT_CONFIGURATION = { DEFAULT_CONFIGURATION = {
'log_level': logging.DEBUG, 'log_level': logging.DEBUG,
'cache_size_album': 1000, 'cache_size': {
'cache_size_photo': 100000, 'album': 1000,
'cache_size_tag': 1000, 'photo': 100000,
'cache_size_user': 200, 'tag': 1000,
'user': 200,
},
'enable_album_edit': True, 'enable_feature': {
'enable_login': True, 'album': {
'enable_new_album': True, 'edit': True,
'enable_new_bookmark': True, 'new': True,
'enable_new_photo': True, },
'enable_new_tag': True, 'bookmark': {
'enable_new_user': True, 'edit': True,
'enable_bookmark_edit': True, 'new': True,
'enable_photo_add_remove_tag': True, },
'enable_photo_edit': True, 'photo': {
'enable_photo_generate_thumbnail': True, 'add_remove_tag': True,
'enable_photo_reload_metadata': True, 'new': True,
'enable_tag_edit': True, 'edit': True,
'generate_thumbnail': True,
'reload_metadata': True,
},
'tag': {
'edit': True,
'new': True,
},
'user': {
'login': True,
'new': True,
},
},
'min_tag_name_length': 1, 'tag': {
'max_tag_name_length': 32, 'min_length': 1,
'valid_tag_chars': string.ascii_lowercase + string.digits + '_()', 'max_length': 32,
'valid_chars': string.ascii_lowercase + string.digits + '_()',
},
'min_username_length': 2, 'user': {
'max_username_length': 24, 'min_length': 2,
'valid_username_chars': string.ascii_letters + string.digits + '~!@#$%^*()[]{}:;,.<>/\\-_+=', 'min_password_length': 6,
'min_password_length': 6, 'max_length': 24,
'valid_chars': string.ascii_letters + string.digits + '~!@#$%^*()[]{}:;,.<>/\\-_+=',
},
'id_length': 12,
'digest_exclude_files': [ 'digest_exclude_files': [
'phototagger.db', 'phototagger.db',
'desktop.ini', 'desktop.ini',
@ -188,9 +205,10 @@ DEFAULT_CONFIGURATION = {
'_site_thumbnails', '_site_thumbnails',
], ],
'file_read_chunk': 2 ** 20, 'id_length': 12,
'thumbnail_width': 400, 'thumbnail_width': 400,
'thumbnail_height': 400, 'thumbnail_height': 400,
'motd_strings': [ 'motd_strings': [
'Good morning, Paul. What will your first sequence of the day be?', 'Good morning, Paul. What will your first sequence of the day be?',
], ],

View file

@ -22,8 +22,20 @@ def required_feature(features):
else: else:
config = self.config config = self.config
if not all(config[key] for key in features): config = config['enable_feature']
raise exceptions.FeatureDisabled(function.__name__)
# Using the received string like "photo.new", try to navigate the
# config and wind up at a True.
# Allow KeyErrors to raise themselves.
for feature in features:
cfg = config
pieces = feature.split('.')
for piece in pieces:
cfg = cfg[piece]
if cfg is False:
raise exceptions.FeatureDisabled(function.__qualname__)
if cfg is not True:
raise ValueError('Bad required_feature "%s" led to %s' % (feature, cfg))
return function(self, *args, **kwargs) return function(self, *args, **kwargs)
return wrapped return wrapped

View file

@ -241,11 +241,11 @@ class Album(ObjectBase, GroupableMixin):
self._sum_bytes_photos = None self._sum_bytes_photos = None
self._sum_bytes_albums = None self._sum_bytes_albums = None
@decorators.required_feature('enable_album_edit') @decorators.required_feature('album.edit')
def add(self, *args, **kwargs): def add(self, *args, **kwargs):
return super().add(*args, **kwargs) return super().add(*args, **kwargs)
@decorators.required_feature('enable_album_edit') @decorators.required_feature('album.edit')
@decorators.transaction @decorators.transaction
def add_associated_directory(self, filepath, *, commit=True): def add_associated_directory(self, filepath, *, commit=True):
filepath = pathclass.Path(filepath) filepath = pathclass.Path(filepath)
@ -277,7 +277,7 @@ class Album(ObjectBase, GroupableMixin):
self.photodb.log.debug('Committing - add associated directory') self.photodb.log.debug('Committing - add associated directory')
self.photodb.commit() self.photodb.commit()
@decorators.required_feature('enable_album_edit') @decorators.required_feature('album.edit')
@decorators.transaction @decorators.transaction
def add_photo(self, photo, *, commit=True): def add_photo(self, photo, *, commit=True):
if self.photodb != photo.photodb: if self.photodb != photo.photodb:
@ -325,7 +325,7 @@ class Album(ObjectBase, GroupableMixin):
directories = [pathclass.Path(x) for x in directories] directories = [pathclass.Path(x) for x in directories]
return directories return directories
@decorators.required_feature('enable_album_edit') @decorators.required_feature('album.edit')
@decorators.transaction @decorators.transaction
def delete(self, *, delete_children=False, commit=True): def delete(self, *, delete_children=False, commit=True):
self.photodb.log.debug('Deleting album {album:r}'.format(album=self)) self.photodb.log.debug('Deleting album {album:r}'.format(album=self))
@ -346,7 +346,7 @@ class Album(ObjectBase, GroupableMixin):
else: else:
return self.id return self.id
@decorators.required_feature('enable_album_edit') @decorators.required_feature('album.edit')
@decorators.transaction @decorators.transaction
def edit(self, title=None, description=None, *, commit=True): def edit(self, title=None, description=None, *, commit=True):
''' '''
@ -378,11 +378,11 @@ class Album(ObjectBase, GroupableMixin):
) )
return cur.fetchone() is not None return cur.fetchone() is not None
@decorators.required_feature('enable_album_edit') @decorators.required_feature('album.edit')
def join_group(self, *args, **kwargs): def join_group(self, *args, **kwargs):
return super().join_group(*args, **kwargs) return super().join_group(*args, **kwargs)
@decorators.required_feature('enable_album_edit') @decorators.required_feature('album.edit')
def leave_group(self, *args, **kwargs): def leave_group(self, *args, **kwargs):
return super().leave_group(*args, **kwargs) return super().leave_group(*args, **kwargs)
@ -400,7 +400,7 @@ class Album(ObjectBase, GroupableMixin):
photos.sort(key=lambda x: x.basename.lower()) photos.sort(key=lambda x: x.basename.lower())
return photos return photos
@decorators.required_feature('enable_album_edit') @decorators.required_feature('album.edit')
@decorators.transaction @decorators.transaction
def remove_photo(self, photo, *, commit=True): def remove_photo(self, photo, *, commit=True):
if not self.has_photo(photo): if not self.has_photo(photo):
@ -454,7 +454,7 @@ class Bookmark(ObjectBase):
def __repr__(self): def __repr__(self):
return 'Bookmark:{id}'.format(id=self.id) return 'Bookmark:{id}'.format(id=self.id)
@decorators.required_feature('enable_bookmark_edit') @decorators.required_feature('bookmark.edit')
@decorators.transaction @decorators.transaction
def delete(self, *, commit=True): def delete(self, *, commit=True):
cur = self.photodb.sql.cursor() cur = self.photodb.sql.cursor()
@ -462,7 +462,7 @@ class Bookmark(ObjectBase):
if commit: if commit:
self.photodb.commit() self.photodb.commit()
@decorators.required_feature('enable_bookmark_edit') @decorators.required_feature('bookmark.edit')
@decorators.transaction @decorators.transaction
def edit(self, title=None, url=None, *, commit=True): def edit(self, title=None, url=None, *, commit=True):
if title is None and url is None: if title is None and url is None:
@ -547,7 +547,7 @@ class Photo(ObjectBase):
def _uncache(self): def _uncache(self):
self.photodb.caches['photo'].remove(self.id) self.photodb.caches['photo'].remove(self.id)
@decorators.required_feature('enable_photo_add_remove_tag') @decorators.required_feature('photo.add_remove_tag')
@decorators.transaction @decorators.transaction
def add_tag(self, tag, *, commit=True): def add_tag(self, tag, *, commit=True):
tag = self.photodb.get_tag(tag) tag = self.photodb.get_tag(tag)
@ -599,7 +599,7 @@ class Photo(ObjectBase):
return bytestring.bytestring(self.bytes) return bytestring.bytestring(self.bytes)
return '??? b' return '??? b'
@decorators.required_feature('enable_photo_add_remove_tag') @decorators.required_feature('photo.add_remove_tag')
def copy_tags(self, other_photo): def copy_tags(self, other_photo):
''' '''
Take all of the tags owned by other_photo and apply them to this photo. Take all of the tags owned by other_photo and apply them to this photo.
@ -607,7 +607,7 @@ class Photo(ObjectBase):
for tag in other_photo.tags(): for tag in other_photo.tags():
self.add_tag(tag) self.add_tag(tag)
@decorators.required_feature('enable_photo_edit') @decorators.required_feature('photo.edit')
@decorators.transaction @decorators.transaction
def delete(self, *, delete_file=False, commit=True): def delete(self, *, delete_file=False, commit=True):
''' '''
@ -638,7 +638,7 @@ class Photo(ObjectBase):
return helpers.seconds_to_hms(self.duration) return helpers.seconds_to_hms(self.duration)
#@decorators.time_me #@decorators.time_me
@decorators.required_feature('enable_photo_generate_thumbnail') @decorators.required_feature('photo.generate_thumbnail')
@decorators.transaction @decorators.transaction
def generate_thumbnail(self, *, commit=True, **special): def generate_thumbnail(self, *, commit=True, **special):
''' '''
@ -773,7 +773,7 @@ class Photo(ObjectBase):
return hopeful_filepath return hopeful_filepath
#@decorators.time_me #@decorators.time_me
@decorators.required_feature('enable_photo_reload_metadata') @decorators.required_feature('photo.reload_metadata')
@decorators.transaction @decorators.transaction
def reload_metadata(self, *, commit=True): def reload_metadata(self, *, commit=True):
''' '''
@ -830,7 +830,7 @@ class Photo(ObjectBase):
self.photodb.log.debug('Committing - reload metadata') self.photodb.log.debug('Committing - reload metadata')
self.photodb.commit() self.photodb.commit()
@decorators.required_feature('enable_photo_edit') @decorators.required_feature('photo.edit')
@decorators.transaction @decorators.transaction
def relocate(self, new_filepath, *, allow_duplicates=False, commit=True): def relocate(self, new_filepath, *, allow_duplicates=False, commit=True):
''' '''
@ -864,7 +864,7 @@ class Photo(ObjectBase):
self.photodb.log.debug('Commit - relocate photo') self.photodb.log.debug('Commit - relocate photo')
self.photodb.commit() self.photodb.commit()
@decorators.required_feature('enable_photo_add_remove_tag') @decorators.required_feature('photo.add_remove_tag')
@decorators.transaction @decorators.transaction
def remove_tag(self, tag, *, commit=True): def remove_tag(self, tag, *, commit=True):
tag = self.photodb.get_tag(tag) tag = self.photodb.get_tag(tag)
@ -884,7 +884,7 @@ class Photo(ObjectBase):
self.photodb.log.debug('Committing - remove photo tag') self.photodb.log.debug('Committing - remove photo tag')
self.photodb.commit() self.photodb.commit()
@decorators.required_feature('enable_photo_edit') @decorators.required_feature('photo.edit')
@decorators.transaction @decorators.transaction
def rename_file(self, new_filename, *, move=False, commit=True): def rename_file(self, new_filename, *, move=False, commit=True):
''' '''
@ -1007,11 +1007,11 @@ class Tag(ObjectBase, GroupableMixin):
self.photodb.caches['tag'].remove(self.id) self.photodb.caches['tag'].remove(self.id)
self._cached_qualified_name = None self._cached_qualified_name = None
@decorators.required_feature('enable_tag_edit') @decorators.required_feature('tag.edit')
def add(self, *args, **kwargs): def add(self, *args, **kwargs):
return super().add(*args, **kwargs) return super().add(*args, **kwargs)
@decorators.required_feature('enable_tag_edit') @decorators.required_feature('tag.edit')
@decorators.transaction @decorators.transaction
def add_synonym(self, synname, *, commit=True): def add_synonym(self, synname, *, commit=True):
synname = self.photodb.normalize_tagname(synname) synname = self.photodb.normalize_tagname(synname)
@ -1037,7 +1037,7 @@ class Tag(ObjectBase, GroupableMixin):
return synname return synname
@decorators.required_feature('enable_tag_edit') @decorators.required_feature('tag.edit')
@decorators.transaction @decorators.transaction
def convert_to_synonym(self, mastertag, *, commit=True): def convert_to_synonym(self, mastertag, *, commit=True):
''' '''
@ -1087,7 +1087,7 @@ class Tag(ObjectBase, GroupableMixin):
self.photodb.log.debug('Committing - convert to synonym') self.photodb.log.debug('Committing - convert to synonym')
self.photodb.commit() self.photodb.commit()
@decorators.required_feature('enable_tag_edit') @decorators.required_feature('tag.edit')
@decorators.transaction @decorators.transaction
def delete(self, *, delete_children=False, commit=True): def delete(self, *, delete_children=False, commit=True):
self.photodb.log.debug('Deleting tag {tag:r}'.format(tag=self)) self.photodb.log.debug('Deleting tag {tag:r}'.format(tag=self))
@ -1102,7 +1102,7 @@ class Tag(ObjectBase, GroupableMixin):
self.photodb.log.debug('Committing - delete tag') self.photodb.log.debug('Committing - delete tag')
self.photodb.commit() self.photodb.commit()
@decorators.required_feature('enable_tag_edit') @decorators.required_feature('tag.edit')
@decorators.transaction @decorators.transaction
def edit(self, description=None, *, commit=True): def edit(self, description=None, *, commit=True):
''' '''
@ -1121,11 +1121,11 @@ class Tag(ObjectBase, GroupableMixin):
self.photodb.log.debug('Committing - edit tag') self.photodb.log.debug('Committing - edit tag')
self.photodb.commit() self.photodb.commit()
@decorators.required_feature('enable_tag_edit') @decorators.required_feature('tag.edit')
def join_group(self, *args, **kwargs): def join_group(self, *args, **kwargs):
return super().join_group(*args, **kwargs) return super().join_group(*args, **kwargs)
@decorators.required_feature('enable_tag_edit') @decorators.required_feature('tag.edit')
def leave_group(self, *args, **kwargs): def leave_group(self, *args, **kwargs):
return super().leave_group(*args, **kwargs) return super().leave_group(*args, **kwargs)
@ -1164,7 +1164,7 @@ class Tag(ObjectBase, GroupableMixin):
return qualname return qualname
@decorators.required_feature('enable_tag_edit') @decorators.required_feature('tag.edit')
@decorators.transaction @decorators.transaction
def remove_synonym(self, synname, *, commit=True): def remove_synonym(self, synname, *, commit=True):
''' '''
@ -1191,7 +1191,7 @@ class Tag(ObjectBase, GroupableMixin):
self.photodb.log.debug('Committing - remove synonym') self.photodb.log.debug('Committing - remove synonym')
self.photodb.commit() self.photodb.commit()
@decorators.required_feature('enable_tag_edit') @decorators.required_feature('tag.edit')
@decorators.transaction @decorators.transaction
def rename(self, new_name, *, apply_to_synonyms=True, commit=True): def rename(self, new_name, *, apply_to_synonyms=True, commit=True):
''' '''

View file

@ -215,7 +215,7 @@ class PDBAlbumMixin:
if album.parent() is None: if album.parent() is None:
yield album yield album
@decorators.required_feature('enable_new_album') @decorators.required_feature('album.new')
@decorators.transaction @decorators.transaction
def new_album( def new_album(
self, self,
@ -283,7 +283,7 @@ class PDBBookmarkMixin:
def get_bookmarks(self): def get_bookmarks(self):
yield from self.get_things(thing_type='bookmark') yield from self.get_things(thing_type='bookmark')
@decorators.required_feature('enable_new_bookmark') @decorators.required_feature('bookmark.new')
@decorators.transaction @decorators.transaction
def new_bookmark(self, url, title=None, *, author=None, commit=True): def new_bookmark(self, url, title=None, *, author=None, commit=True):
if not url: if not url:
@ -356,7 +356,7 @@ class PDBPhotoMixin:
if count <= 0: if count <= 0:
break break
@decorators.required_feature('enable_new_photo') @decorators.required_feature('photo.new')
@decorators.transaction @decorators.transaction
def new_photo( def new_photo(
self, self,
@ -894,7 +894,7 @@ class PDBTagMixin:
if tag.parent() is None: if tag.parent() is None:
yield tag yield tag
@decorators.required_feature('enable_new_tag') @decorators.required_feature('tag.new')
@decorators.transaction @decorators.transaction
def new_tag(self, tagname, description=None, *, commit=True): def new_tag(self, tagname, description=None, *, commit=True):
''' '''
@ -936,13 +936,13 @@ class PDBTagMixin:
tagname = tagname.lower() tagname = tagname.lower()
tagname = tagname.replace('-', '_') tagname = tagname.replace('-', '_')
tagname = tagname.replace(' ', '_') tagname = tagname.replace(' ', '_')
tagname = (c for c in tagname if c in self.config['valid_tag_chars']) tagname = (c for c in tagname if c in self.config['tag']['valid_chars'])
tagname = ''.join(tagname) tagname = ''.join(tagname)
if len(tagname) < self.config['min_tag_name_length']: if len(tagname) < self.config['tag']['min_length']:
raise exceptions.TagTooShort(original_tagname) raise exceptions.TagTooShort(original_tagname)
elif len(tagname) > self.config['max_tag_name_length']: elif len(tagname) > self.config['tag']['max_length']:
raise exceptions.TagTooLong(tagname) raise exceptions.TagTooLong(tagname)
else: else:
@ -1006,7 +1006,7 @@ class PDBUserMixin:
author_id = None author_id = None
return author_id return author_id
@decorators.required_feature('enable_login') @decorators.required_feature('user.login')
def login(self, user_id, password): def login(self, user_id, password):
cur = self.sql.cursor() cur = self.sql.cursor()
cur.execute('SELECT * FROM users WHERE id == ?', [user_id]) cur.execute('SELECT * FROM users WHERE id == ?', [user_id])
@ -1026,30 +1026,30 @@ class PDBUserMixin:
return objects.User(self, fetch) return objects.User(self, fetch)
@decorators.required_feature('enable_new_user') @decorators.required_feature('user.new')
@decorators.transaction @decorators.transaction
def register_user(self, username, password, commit=True): def register_user(self, username, password, commit=True):
if len(username) < self.config['min_username_length']: if len(username) < self.config['user']['min_length']:
raise exceptions.UsernameTooShort( raise exceptions.UsernameTooShort(
username=username, username=username,
min_length=self.config['min_username_length'] min_length=self.config['user']['min_length']
) )
if len(username) > self.config['max_username_length']: if len(username) > self.config['user']['max_length']:
raise exceptions.UsernameTooLong( raise exceptions.UsernameTooLong(
username=username, username=username,
max_length=self.config['max_username_length'] max_length=self.config['user']['max_length']
) )
badchars = [c for c in username if c not in self.config['valid_username_chars']] badchars = [c for c in username if c not in self.config['user']['valid_chars']]
if badchars: if badchars:
raise exceptions.InvalidUsernameChars(username=username, badchars=badchars) raise exceptions.InvalidUsernameChars(username=username, badchars=badchars)
if not isinstance(password, bytes): if not isinstance(password, bytes):
password = password.encode('utf-8') password = password.encode('utf-8')
if len(password) < self.config['min_password_length']: if len(password) < self.config['user']['min_password_length']:
raise exceptions.PasswordTooShort(min_length=self.config['min_password_length']) raise exceptions.PasswordTooShort(min_length=self.config['user']['min_password_length'])
try: try:
existing_user = self.get_user(username=username) existing_user = self.get_user(username=username)
@ -1141,10 +1141,10 @@ class PhotoDB(PDBAlbumMixin, PDBBookmarkMixin, PDBPhotoMixin, PDBTagMixin, PDBUs
self.on_commit_queue = [] self.on_commit_queue = []
self._cached_frozen_children = None self._cached_frozen_children = None
self._album_cache.maxlen = self.config['cache_size_album'] self._album_cache.maxlen = self.config['cache_size']['album']
self._photo_cache.maxlen = self.config['cache_size_photo'] self._photo_cache.maxlen = self.config['cache_size']['photo']
self._tag_cache.maxlen = self.config['cache_size_tag'] self._tag_cache.maxlen = self.config['cache_size']['tag']
self._user_cache.maxlen = self.config['cache_size_user'] self._user_cache.maxlen = self.config['cache_size']['user']
self.caches = { self.caches = {
'album': self._album_cache, 'album': self._album_cache,
'photo': self._photo_cache, 'photo': self._photo_cache,