server: make linters happier

This commit is contained in:
rr- 2017-02-03 21:42:15 +01:00
parent fd30675124
commit abf1fc2b2d
24 changed files with 153 additions and 106 deletions

View File

@ -1,3 +1,3 @@
#!/bin/sh #!/bin/sh
pylint szurubooru pylint szurubooru
pycodestyle szurubooru --ignore=E128,E131,W503 pycodestyle szurubooru

View File

@ -252,8 +252,8 @@ class Post(Base):
relation_count = column_property( relation_count = column_property(
select([func.count(PostRelation.child_id)]) select([func.count(PostRelation.child_id)])
.where( .where(
(PostRelation.parent_id == post_id) (PostRelation.parent_id == post_id) |
| (PostRelation.child_id == post_id)) (PostRelation.child_id == post_id))
.correlate_except(PostRelation)) .correlate_except(PostRelation))
__mapper_args__ = { __mapper_args__ = {

View File

@ -106,23 +106,29 @@ class Tag(Base):
.correlate_except(PostTag)) .correlate_except(PostTag))
first_name = column_property( first_name = column_property(
(
select([TagName.name]) select([TagName.name])
.where(TagName.tag_id == tag_id) .where(TagName.tag_id == tag_id)
.order_by(TagName.order) .order_by(TagName.order)
.limit(1) .limit(1)
.as_scalar(), .as_scalar()
),
deferred=True) deferred=True)
suggestion_count = column_property( suggestion_count = column_property(
(
select([func.count(TagSuggestion.child_id)]) select([func.count(TagSuggestion.child_id)])
.where(TagSuggestion.parent_id == tag_id) .where(TagSuggestion.parent_id == tag_id)
.as_scalar(), .as_scalar()
),
deferred=True) deferred=True)
implication_count = column_property( implication_count = column_property(
(
select([func.count(TagImplication.child_id)]) select([func.count(TagImplication.child_id)])
.where(TagImplication.parent_id == tag_id) .where(TagImplication.parent_id == tag_id)
.as_scalar(), .as_scalar()
),
deferred=True) deferred=True)
__mapper_args__ = { __mapper_args__ = {

View File

@ -37,7 +37,8 @@ class User(Base):
@property @property
def post_count(self): def post_count(self):
from szurubooru.db import session from szurubooru.db import session
return (session return (
session
.query(func.sum(1)) .query(func.sum(1))
.filter(Post.user_id == self.user_id) .filter(Post.user_id == self.user_id)
.one()[0] or 0) .one()[0] or 0)
@ -45,7 +46,8 @@ class User(Base):
@property @property
def comment_count(self): def comment_count(self):
from szurubooru.db import session from szurubooru.db import session
return (session return (
session
.query(func.sum(1)) .query(func.sum(1))
.filter(Comment.user_id == self.user_id) .filter(Comment.user_id == self.user_id)
.one()[0] or 0) .one()[0] or 0)
@ -53,7 +55,8 @@ class User(Base):
@property @property
def favorite_post_count(self): def favorite_post_count(self):
from szurubooru.db import session from szurubooru.db import session
return (session return (
session
.query(func.sum(1)) .query(func.sum(1))
.filter(PostFavorite.user_id == self.user_id) .filter(PostFavorite.user_id == self.user_id)
.one()[0] or 0) .one()[0] or 0)
@ -61,7 +64,8 @@ class User(Base):
@property @property
def liked_post_count(self): def liked_post_count(self):
from szurubooru.db import session from szurubooru.db import session
return (session return (
session
.query(func.sum(1)) .query(func.sum(1))
.filter(PostScore.user_id == self.user_id) .filter(PostScore.user_id == self.user_id)
.filter(PostScore.score == 1) .filter(PostScore.score == 1)
@ -70,7 +74,8 @@ class User(Base):
@property @property
def disliked_post_count(self): def disliked_post_count(self):
from szurubooru.db import session from szurubooru.db import session
return (session return (
session
.query(func.sum(1)) .query(func.sum(1))
.filter(PostScore.user_id == self.user_id) .filter(PostScore.user_id == self.user_id)
.filter(PostScore.score == -1) .filter(PostScore.score == -1)

View File

@ -127,4 +127,4 @@ def create_app():
return rest.application return rest.application
app = create_app() app = create_app() # pylint: disable=invalid-name

View File

@ -110,14 +110,22 @@ def _compute_mean_level(image, x_coords, y_coords, p):
def _compute_differentials(grey_level_matrix): def _compute_differentials(grey_level_matrix):
flipped = np.fliplr(grey_level_matrix) flipped = np.fliplr(grey_level_matrix)
right_neighbors = -np.concatenate(( right_neighbors = -np.concatenate(
(
np.diff(grey_level_matrix), np.diff(grey_level_matrix),
(
np.zeros(grey_level_matrix.shape[0]) np.zeros(grey_level_matrix.shape[0])
.reshape((grey_level_matrix.shape[0], 1))), axis=1) .reshape((grey_level_matrix.shape[0], 1))
down_neighbors = -np.concatenate(( )
), axis=1)
down_neighbors = -np.concatenate(
(
np.diff(grey_level_matrix, axis=0), np.diff(grey_level_matrix, axis=0),
(
np.zeros(grey_level_matrix.shape[1]) np.zeros(grey_level_matrix.shape[1])
.reshape((1, grey_level_matrix.shape[1])))) .reshape((1, grey_level_matrix.shape[1]))
)
))
left_neighbors = -np.concatenate( left_neighbors = -np.concatenate(
(right_neighbors[:, -1:], right_neighbors[:, :-1]), axis=1) (right_neighbors[:, -1:], right_neighbors[:, :-1]), axis=1)
up_neighbors = -np.concatenate((down_neighbors[-1:], down_neighbors[:-1])) up_neighbors = -np.concatenate((down_neighbors[-1:], down_neighbors[:-1]))
@ -146,15 +154,18 @@ def _compute_differentials(grey_level_matrix):
def _generate_signature(path_or_image): def _generate_signature(path_or_image):
im_array = _preprocess_image(path_or_image) im_array = _preprocess_image(path_or_image)
image_limits = _crop_image(im_array, image_limits = _crop_image(
im_array,
lower_percentile=LOWER_PERCENTILE, lower_percentile=LOWER_PERCENTILE,
upper_percentile=UPPER_PERCENTILE) upper_percentile=UPPER_PERCENTILE)
x_coords, y_coords = _compute_grid_points( x_coords, y_coords = _compute_grid_points(
im_array, n=N, window=image_limits) im_array, n=N, window=image_limits)
avg_grey = _compute_mean_level(im_array, x_coords, y_coords, p=P) avg_grey = _compute_mean_level(im_array, x_coords, y_coords, p=P)
diff_matrix = _compute_differentials(avg_grey) diff_matrix = _compute_differentials(avg_grey)
_normalize_and_threshold(diff_matrix, _normalize_and_threshold(
identical_tolerance=IDENTICAL_TOLERANCE, n_levels=N_LEVELS) diff_matrix,
identical_tolerance=IDENTICAL_TOLERANCE,
n_levels=N_LEVELS)
return np.ravel(diff_matrix).astype('int8') return np.ravel(diff_matrix).astype('int8')

View File

@ -111,4 +111,5 @@ class Image:
assert 'streams' in self.info assert 'streams' in self.info
if len(self.info['streams']) < 1: if len(self.info['streams']) < 1:
logger.warning('The video contains no video streams.') logger.warning('The video contains no video streams.')
raise errors.ProcessingError('The video contains no video streams.') raise errors.ProcessingError(
'The video contains no video streams.')

View File

@ -474,12 +474,15 @@ def merge_posts(source_post, target_post, replace_content):
def merge_tables(table, anti_dup_func, source_post_id, target_post_id): def merge_tables(table, anti_dup_func, source_post_id, target_post_id):
alias1 = table alias1 = table
alias2 = sqlalchemy.orm.util.aliased(table) alias2 = sqlalchemy.orm.util.aliased(table)
update_stmt = (sqlalchemy.sql.expression.update(alias1) update_stmt = (
sqlalchemy.sql.expression.update(alias1)
.where(alias1.post_id == source_post_id)) .where(alias1.post_id == source_post_id))
if anti_dup_func is not None: if anti_dup_func is not None:
update_stmt = (update_stmt update_stmt = (
.where(~sqlalchemy.exists() update_stmt
.where(
~sqlalchemy.exists()
.where(anti_dup_func(alias1, alias2)) .where(anti_dup_func(alias1, alias2))
.where(alias2.post_id == target_post_id))) .where(alias2.post_id == target_post_id)))
@ -513,19 +516,23 @@ def merge_posts(source_post, target_post, replace_content):
def merge_relations(source_post_id, target_post_id): def merge_relations(source_post_id, target_post_id):
alias1 = db.PostRelation alias1 = db.PostRelation
alias2 = sqlalchemy.orm.util.aliased(db.PostRelation) alias2 = sqlalchemy.orm.util.aliased(db.PostRelation)
update_stmt = (sqlalchemy.sql.expression.update(alias1) update_stmt = (
sqlalchemy.sql.expression.update(alias1)
.where(alias1.parent_id == source_post_id) .where(alias1.parent_id == source_post_id)
.where(alias1.child_id != target_post_id) .where(alias1.child_id != target_post_id)
.where(~sqlalchemy.exists() .where(
~sqlalchemy.exists()
.where(alias2.child_id == alias1.child_id) .where(alias2.child_id == alias1.child_id)
.where(alias2.parent_id == target_post_id)) .where(alias2.parent_id == target_post_id))
.values(parent_id=target_post_id)) .values(parent_id=target_post_id))
db.session.execute(update_stmt) db.session.execute(update_stmt)
update_stmt = (sqlalchemy.sql.expression.update(alias1) update_stmt = (
sqlalchemy.sql.expression.update(alias1)
.where(alias1.child_id == source_post_id) .where(alias1.child_id == source_post_id)
.where(alias1.parent_id != target_post_id) .where(alias1.parent_id != target_post_id)
.where(~sqlalchemy.exists() .where(
~sqlalchemy.exists()
.where(alias2.parent_id == alias1.parent_id) .where(alias2.parent_id == alias1.parent_id)
.where(alias2.child_id == target_post_id)) .where(alias2.child_id == target_post_id))
.values(child_id=target_post_id)) .values(child_id=target_post_id))
@ -567,7 +574,8 @@ def search_by_image(image_content):
def populate_reverse_search(): def populate_reverse_search():
excluded_post_ids = image_hash.get_all_paths() excluded_post_ids = image_hash.get_all_paths()
post_ids_to_hash = (db.session post_ids_to_hash = (
db.session
.query(db.Post.post_id) .query(db.Post.post_id)
.filter( .filter(
(db.Post.type == db.Post.TYPE_IMAGE) | (db.Post.type == db.Post.TYPE_IMAGE) |
@ -577,7 +585,8 @@ def populate_reverse_search():
.all()) .all())
for post_ids_chunk in util.chunks(post_ids_to_hash, 100): for post_ids_chunk in util.chunks(post_ids_to_hash, 100):
posts_chunk = (db.session posts_chunk = (
db.session
.query(db.Post) .query(db.Post)
.filter(db.Post.post_id.in_(post_ids_chunk)) .filter(db.Post.post_id.in_(post_ids_chunk))
.all()) .all())

View File

@ -86,10 +86,13 @@ def create(entity, auth_user):
def modify(entity, auth_user): def modify(entity, auth_user):
assert entity assert entity
model = next((model model = next(
(
model
for model in db.Base._decl_class_registry.values() for model in db.Base._decl_class_registry.values()
if hasattr(model, '__table__') if hasattr(model, '__table__')
and model.__table__.fullname == entity.__table__.fullname), and model.__table__.fullname == entity.__table__.fullname
),
None) None)
assert model assert model

View File

@ -104,7 +104,8 @@ def export_to_json():
'color': result[2], 'color': result[2],
} }
for result in (db.session for result in (
db.session
.query(db.TagName.tag_id, db.TagName.name) .query(db.TagName.tag_id, db.TagName.name)
.order_by(db.TagName.order) .order_by(db.TagName.order)
.all()): .all()):
@ -112,7 +113,8 @@ def export_to_json():
tags[result[0]] = {'names': []} tags[result[0]] = {'names': []}
tags[result[0]]['names'].append(result[1]) tags[result[0]]['names'].append(result[1])
for result in (db.session for result in (
db.session
.query(db.TagSuggestion.parent_id, db.TagName.name) .query(db.TagSuggestion.parent_id, db.TagName.name)
.join(db.TagName, db.TagName.tag_id == db.TagSuggestion.child_id) .join(db.TagName, db.TagName.tag_id == db.TagSuggestion.child_id)
.all()): .all()):
@ -120,7 +122,8 @@ def export_to_json():
tags[result[0]]['suggestions'] = [] tags[result[0]]['suggestions'] = []
tags[result[0]]['suggestions'].append(result[1]) tags[result[0]]['suggestions'].append(result[1])
for result in (db.session for result in (
db.session
.query(db.TagImplication.parent_id, db.TagName.name) .query(db.TagImplication.parent_id, db.TagName.name)
.join(db.TagName, db.TagName.tag_id == db.TagImplication.child_id) .join(db.TagName, db.TagName.tag_id == db.TagImplication.child_id)
.all()): .all()):
@ -146,7 +149,8 @@ def export_to_json():
def try_get_tag_by_name(name): def try_get_tag_by_name(name):
return (db.session return (
db.session
.query(db.Tag) .query(db.Tag)
.join(db.TagName) .join(db.TagName)
.filter(sqlalchemy.func.lower(db.TagName.name) == name.lower()) .filter(sqlalchemy.func.lower(db.TagName.name) == name.lower())
@ -198,7 +202,8 @@ def get_tag_siblings(tag):
tag_alias = sqlalchemy.orm.aliased(db.Tag) tag_alias = sqlalchemy.orm.aliased(db.Tag)
pt_alias1 = sqlalchemy.orm.aliased(db.PostTag) pt_alias1 = sqlalchemy.orm.aliased(db.PostTag)
pt_alias2 = sqlalchemy.orm.aliased(db.PostTag) pt_alias2 = sqlalchemy.orm.aliased(db.PostTag)
result = (db.session result = (
db.session
.query(tag_alias, sqlalchemy.func.count(pt_alias2.post_id)) .query(tag_alias, sqlalchemy.func.count(pt_alias2.post_id))
.join(pt_alias1, pt_alias1.tag_id == tag_alias.tag_id) .join(pt_alias1, pt_alias1.tag_id == tag_alias.tag_id)
.join(pt_alias2, pt_alias2.post_id == pt_alias1.post_id) .join(pt_alias2, pt_alias2.post_id == pt_alias1.post_id)
@ -230,10 +235,13 @@ def merge_tags(source_tag, target_tag):
def merge_posts(source_tag_id, target_tag_id): def merge_posts(source_tag_id, target_tag_id):
alias1 = db.PostTag alias1 = db.PostTag
alias2 = sqlalchemy.orm.util.aliased(db.PostTag) alias2 = sqlalchemy.orm.util.aliased(db.PostTag)
update_stmt = (sqlalchemy.sql.expression.update(alias1) update_stmt = (
sqlalchemy.sql.expression.update(alias1)
.where(alias1.tag_id == source_tag_id)) .where(alias1.tag_id == source_tag_id))
update_stmt = (update_stmt update_stmt = (
.where(~sqlalchemy.exists() update_stmt
.where(
~sqlalchemy.exists()
.where(alias1.post_id == alias2.post_id) .where(alias1.post_id == alias2.post_id)
.where(alias2.tag_id == target_tag_id))) .where(alias2.tag_id == target_tag_id)))
update_stmt = update_stmt.values(tag_id=target_tag_id) update_stmt = update_stmt.values(tag_id=target_tag_id)
@ -242,19 +250,23 @@ def merge_tags(source_tag, target_tag):
def merge_relations(table, source_tag_id, target_tag_id): def merge_relations(table, source_tag_id, target_tag_id):
alias1 = table alias1 = table
alias2 = sqlalchemy.orm.util.aliased(table) alias2 = sqlalchemy.orm.util.aliased(table)
update_stmt = (sqlalchemy.sql.expression.update(alias1) update_stmt = (
sqlalchemy.sql.expression.update(alias1)
.where(alias1.parent_id == source_tag_id) .where(alias1.parent_id == source_tag_id)
.where(alias1.child_id != target_tag_id) .where(alias1.child_id != target_tag_id)
.where(~sqlalchemy.exists() .where(
~sqlalchemy.exists()
.where(alias2.child_id == alias1.child_id) .where(alias2.child_id == alias1.child_id)
.where(alias2.parent_id == target_tag_id)) .where(alias2.parent_id == target_tag_id))
.values(parent_id=target_tag_id)) .values(parent_id=target_tag_id))
db.session.execute(update_stmt) db.session.execute(update_stmt)
update_stmt = (sqlalchemy.sql.expression.update(alias1) update_stmt = (
sqlalchemy.sql.expression.update(alias1)
.where(alias1.child_id == source_tag_id) .where(alias1.child_id == source_tag_id)
.where(alias1.parent_id != target_tag_id) .where(alias1.parent_id != target_tag_id)
.where(~sqlalchemy.exists() .where(
~sqlalchemy.exists()
.where(alias2.parent_id == alias1.parent_id) .where(alias2.parent_id == alias1.parent_id)
.where(alias2.child_id == target_tag_id)) .where(alias2.child_id == target_tag_id))
.values(child_id=target_tag_id)) .values(child_id=target_tag_id))

View File

@ -44,7 +44,6 @@ def get_avatar_url(user):
return 'https://gravatar.com/avatar/%s?d=retro&s=%d' % ( return 'https://gravatar.com/avatar/%s?d=retro&s=%d' % (
util.get_md5((user.email or user.name).lower()), util.get_md5((user.email or user.name).lower()),
config.config['thumbnails']['avatar_width']) config.config['thumbnails']['avatar_width'])
else:
assert user.name assert user.name
return '%s/avatars/%s.png' % ( return '%s/avatars/%s.png' % (
config.config['data_url'].rstrip('/'), user.name.lower()) config.config['data_url'].rstrip('/'), user.name.lower())
@ -126,7 +125,8 @@ def get_user_by_name(name):
def try_get_user_by_name_or_email(name_or_email): def try_get_user_by_name_or_email(name_or_email):
return (db.session return (
db.session
.query(db.User) .query(db.User)
.filter( .filter(
(func.lower(db.User.name) == func.lower(name_or_email)) | (func.lower(db.User.name) == func.lower(name_or_email)) |

View File

@ -102,6 +102,7 @@ def parse_time_range(value):
''' Return tuple containing min/max time for given text representation. ''' ''' Return tuple containing min/max time for given text representation. '''
one_day = timedelta(days=1) one_day = timedelta(days=1)
one_second = timedelta(seconds=1) one_second = timedelta(seconds=1)
almost_one_day = one_day - one_second
value = value.lower() value = value.lower()
if not value: if not value:
@ -111,8 +112,8 @@ def parse_time_range(value):
now = datetime.utcnow() now = datetime.utcnow()
return ( return (
datetime(now.year, now.month, now.day, 0, 0, 0), datetime(now.year, now.month, now.day, 0, 0, 0),
datetime(now.year, now.month, now.day, 0, 0, 0) datetime(now.year, now.month, now.day, 0, 0, 0) + almost_one_day
+ one_day - one_second) )
if value == 'yesterday': if value == 'yesterday':
now = datetime.utcnow() now = datetime.utcnow()

View File

@ -93,7 +93,7 @@ def application(env, start_response):
hook(ctx) hook(ctx)
try: try:
response = handler(ctx, match.groupdict()) response = handler(ctx, match.groupdict())
except: except Exception:
ctx.session.rollback() ctx.session.rollback()
raise raise
finally: finally:

View File

@ -44,9 +44,10 @@ class Context:
return self._headers.get(name, None) return self._headers.get(name, None)
def has_file(self, name, allow_tokens=True): def has_file(self, name, allow_tokens=True):
return (name in self._files return (
or name + 'Url' in self._params name in self._files or
or (allow_tokens and name + 'Token' in self._params)) name + 'Url' in self._params or
(allow_tokens and name + 'Token' in self._params))
def get_file(self, name, required=False, allow_tokens=True): def get_file(self, name, required=False, allow_tokens=True):
ret = None ret = None
@ -80,7 +81,7 @@ class Context:
if isinstance(value, list): if isinstance(value, list):
try: try:
value = ','.join(value) value = ','.join(value)
except: except TypeError:
raise errors.InvalidParameterError('Expected simple string.') raise errors.InvalidParameterError('Expected simple string.')
return value return value

View File

@ -5,7 +5,8 @@ from szurubooru.search import criteria
def wildcard_transformer(value): def wildcard_transformer(value):
return (value return (
value
.replace('\\', '\\\\') .replace('\\', '\\\\')
.replace('%', '\\%') .replace('%', '\\%')
.replace('_', '\\_') .replace('_', '\\_')

View File

@ -44,8 +44,7 @@ def test_using_special_tokens(user_factory, post_factory, context_factory):
db.session.add_all([post1, post2, auth_user]) db.session.add_all([post1, post2, auth_user])
db.session.flush() db.session.flush()
with patch('szurubooru.func.posts.serialize_post'): with patch('szurubooru.func.posts.serialize_post'):
posts.serialize_post.side_effect = \ posts.serialize_post.side_effect = lambda post, *_args, **_kwargs: \
lambda post, *_args, **_kwargs: \
'serialized post %d' % post.post_id 'serialized post %d' % post.post_id
result = api.post_api.get_posts( result = api.post_api.get_posts(
context_factory( context_factory(

View File

@ -14,8 +14,7 @@ def test_get_tag_siblings(user_factory, tag_factory, context_factory):
db.session.flush() db.session.flush()
with patch('szurubooru.func.tags.serialize_tag'), \ with patch('szurubooru.func.tags.serialize_tag'), \
patch('szurubooru.func.tags.get_tag_siblings'): patch('szurubooru.func.tags.get_tag_siblings'):
tags.serialize_tag.side_effect = \ tags.serialize_tag.side_effect = lambda tag, *args, **kwargs: \
lambda tag, *args, **kwargs: \
'serialized tag %s' % tag.names[0].name 'serialized tag %s' % tag.names[0].name
tags.get_tag_siblings.return_value = [ tags.get_tag_siblings.return_value = [
(tag_factory(names=['sib1']), 1), (tag_factory(names=['sib1']), 1),

View File

@ -494,8 +494,7 @@ def test_update_post_content_leaving_custom_thumbnail(
def test_update_post_tags(tag_factory): def test_update_post_tags(tag_factory):
post = db.Post() post = db.Post()
with patch('szurubooru.func.tags.get_or_create_tags_by_names'): with patch('szurubooru.func.tags.get_or_create_tags_by_names'):
tags.get_or_create_tags_by_names.side_effect \ tags.get_or_create_tags_by_names.side_effect = lambda tag_names: \
= lambda tag_names: \
([tag_factory(names=[name]) for name in tag_names], []) ([tag_factory(names=[name]) for name in tag_names], [])
posts.update_post_tags(post, ['tag1', 'tag2']) posts.update_post_tags(post, ['tag1', 'tag2'])
assert len(post.tags) == 2 assert len(post.tags) == 2

View File

@ -21,22 +21,22 @@ def test_get_avatar_path(user_name):
'user', 'user',
None, None,
db.User.AVATAR_GRAVATAR, db.User.AVATAR_GRAVATAR,
'https://gravatar.com/avatar/' + ('https://gravatar.com/avatar/' +
'ee11cbb19052e40b07aac0ca060c23ee?d=retro&s=100', 'ee11cbb19052e40b07aac0ca060c23ee?d=retro&s=100'),
), ),
( (
None, None,
'user@example.com', 'user@example.com',
db.User.AVATAR_GRAVATAR, db.User.AVATAR_GRAVATAR,
'https://gravatar.com/avatar/' + ('https://gravatar.com/avatar/' +
'b58996c504c5638798eb6b511e6f49af?d=retro&s=100', 'b58996c504c5638798eb6b511e6f49af?d=retro&s=100'),
), ),
( (
'user', 'user',
'user@example.com', 'user@example.com',
db.User.AVATAR_GRAVATAR, db.User.AVATAR_GRAVATAR,
'https://gravatar.com/avatar/' + ('https://gravatar.com/avatar/' +
'b58996c504c5638798eb6b511e6f49af?d=retro&s=100', 'b58996c504c5638798eb6b511e6f49af?d=retro&s=100'),
), ),
( (
'user', 'user',