Commit 965cf04d authored by Francois Marier's avatar Francois Marier

Fix a few pylint errors and disable the rest as locally as possible

parent a55d3e8c
[MESSAGES CONTROL]
#disable=no-value-for-parameter,too-many-public-methods,bad-continuation,no-member,unexpected-keyword-arg,fixme,maybe-no-member,locally-disabled,invalid-name,missing-docstring,line-too-long
disable=C0111,W0702,C0301,W0511,I0011,C0103,W0232,E1101,R0903,E1002,R0904,R0911,W0222,W0221,E1102,E1103,old-style-class,no-value-for-parameter
disable=old-style-class,no-value-for-parameter,missing-docstring,line-too-long,locally-disabled,fixme,no-member,maybe-no-member,too-many-public-methods,no-init,too-few-public-methods,super-on-old-class
[REPORTS]
output-format=text
......@@ -23,7 +22,7 @@ attr-rgx=[a-z_][a-z0-9_]{2,30}$
argument-rgx=[a-z_][a-z0-9_]{2,30}$
variable-rgx=[a-z_][a-z0-9_]{2,30}$
inlinevar-rgx=[A-Za-z_][A-Za-z0-9_]*$
good-names=i,j,k,ex,Run,_
good-names=i,j,k,ex,Run,_,s,x,y,z,h,w,e,l
bad-names=foo,bar,baz,toto,tutu,tata
no-docstring-rgx=__.*__
......
......@@ -117,15 +117,15 @@ class UploadPhotoForm(forms.Form):
can_distribute = forms.BooleanField(label=_('can be freely copied'), required=True,
error_messages={'required': _('This field must be checked since we need to be able to distribute photos to third parties.')})
# pylint: disable=R0201
# pylint: disable=no-self-use
def save(self, user, ip_address, image):
# Link this file to the user's profile
p = Photo()
p.user = user
p.ip_address = ip_address
if not p.save(image):
photo = Photo()
photo.user = user
photo.ip_address = ip_address
if not photo.save(image):
return None
return p
return photo
class PasswordResetForm(forms.Form):
......
......@@ -129,6 +129,7 @@ class Photo(models.Model):
def exists(self):
return path.isfile(settings.USER_FILES_ROOT + self.full_filename())
# pylint: disable=arguments-differ
def save(self, image, force_insert=False, force_update=False):
hash_object = hashlib.new('sha256')
hash_object.update(urandom(1024) + str(self.user.username))
......@@ -283,6 +284,7 @@ class ConfirmedEmail(models.Model):
def __unicode__(self):
return self.email
# pylint: disable=arguments-differ
def delete(self):
self.set_photo(None)
super(ConfirmedEmail, self).delete()
......@@ -328,6 +330,7 @@ class UnconfirmedEmail(models.Model):
def __unicode__(self):
return self.email + ' ' + _('(unconfirmed)')
# pylint: disable=arguments-differ
def save(self, force_insert=False, force_update=False):
hash_object = hashlib.new('sha256')
hash_object.update(urandom(1024) + str(self.user.username))
......@@ -364,6 +367,7 @@ class ConfirmedOpenId(models.Model):
def __unicode__(self):
return self.openid
# pylint: disable=arguments-differ
def delete(self):
self.set_photo(None)
super(ConfirmedOpenId, self).delete()
......@@ -480,12 +484,13 @@ class DjangoOpenIDStore(OpenIDStore):
return False
def cleanupNonces(self):
ts = int(time.time()) - oidnonce.SKEW
OpenIDNonce.objects.filter(timestamp__lt=ts).delete()
timestamp = int(time.time()) - oidnonce.SKEW
OpenIDNonce.objects.filter(timestamp__lt=timestamp).delete()
def cleanupAssociations(self):
OpenIDAssociation.objects.extra(where=['issued + lifetimeint < (%s)' % time.time()]).delete()
# pylint: disable=invalid-name
def getAuthKey(self):
# Use first AUTH_KEY_LEN characters of md5 hash of SECRET_KEY
hash_object = hashlib.new('md5')
......
......@@ -20,6 +20,7 @@
from django.conf.urls import url, patterns
# pylint: disable=invalid-name
urlpatterns = patterns('',
url('login_embedded/$',
'libravatar.account.views.login_embedded'),
......
......@@ -135,10 +135,10 @@ def import_photo(request, user_id):
if 'photo_Gravatar' in request.POST:
photos_to_import = True
p = Photo()
p.user = user
p.ip_address = request.META['REMOTE_ADDR']
if p.import_image('Gravatar', email.email):
photo = Photo()
photo.user = user
photo.ip_address = request.META['REMOTE_ADDR']
if photo.import_image('Gravatar', email.email):
photos_imported = True
if photos_imported:
......@@ -172,12 +172,12 @@ def successfully_authenticated(request):
# add photo to database, bung LDAP photo into the expected file
photo_contents = request.user.ldap_user.attrs[settings.AUTH_LDAP_USER_PHOTO][0]
fp = StringIO(photo_contents) # file pointer to in-memory string buffer
image = File(fp)
p = Photo()
p.user = request.user
p.save(image)
return HttpResponseRedirect(reverse('libravatar.account.views.crop_photo', args=[p.id]))
file_ptr = StringIO(photo_contents) # file pointer to in-memory string buffer
image = File(file_ptr)
photo = Photo()
photo.user = request.user
photo.save(image)
return HttpResponseRedirect(reverse('libravatar.account.views.crop_photo', args=[photo.id]))
return HttpResponseRedirect(reverse('libravatar.account.views.profile'))
......@@ -205,14 +205,14 @@ def _confirm_claimed_openid(user, remote_address):
@csrf_protect
@login_required
def profile(request):
u = request.user
_confirm_claimed_openid(u, request.META['REMOTE_ADDR'])
confirmed_emails = u.confirmed_emails.order_by('email')
unconfirmed_emails = u.unconfirmed_emails.order_by('email')
confirmed_openids = u.confirmed_openids.order_by('openid')
unconfirmed_openids = u.unconfirmed_openids.order_by('openid')
photos = u.photos.order_by('add_date')
usr = request.user
_confirm_claimed_openid(usr, request.META['REMOTE_ADDR'])
confirmed_emails = usr.confirmed_emails.order_by('email')
unconfirmed_emails = usr.unconfirmed_emails.order_by('email')
confirmed_openids = usr.confirmed_openids.order_by('openid')
unconfirmed_openids = usr.unconfirmed_openids.order_by('openid')
photos = usr.photos.order_by('add_date')
max_photos = len(photos) >= settings.MAX_NUM_PHOTOS
max_emails = len(unconfirmed_emails) >= settings.MAX_NUM_UNCONFIRMED_EMAILS
......
......@@ -20,13 +20,13 @@ import json
from os import link, path, umask
import sys
# pylint: disable=W0403
# pylint: disable=relative-import
import settings
from resizeavatar import resize_image
from utils import create_logger, delete_if_exists, is_hex
umask(022)
logger = create_logger('changephoto')
LOGGER = create_logger('changephoto')
def link_image(source_filename, destination_hash, size=None):
......@@ -38,12 +38,12 @@ def link_image(source_filename, destination_hash, size=None):
try:
link(source_filename, destination_filename)
except OSError:
logger.error("Unable to link '%s' to %s", source_filename, destination_filename)
LOGGER.error("Unable to link '%s' to %s", source_filename, destination_filename)
def create_links(source_filename, md5_hash, sha256_hash):
if not path.isfile(source_filename):
logger.warning("the cropped photo '%s' does not exist", source_filename)
LOGGER.warning("the cropped photo '%s' does not exist", source_filename)
return 0
if md5_hash:
......@@ -73,16 +73,16 @@ def main(argv=None):
# Validate inputs
if photo_hash and not is_hex(photo_hash):
logger.error('photo_hash is not a hexadecimal value')
LOGGER.error('photo_hash is not a hexadecimal value')
return 1
if photo_format and photo_format != 'jpg' and photo_format != 'png' and photo_format != 'gif':
logger.error('photo_format is not recognized')
LOGGER.error('photo_format is not recognized')
return 1
if md5_hash and not is_hex(md5_hash):
logger.error('md5_hash is not a hexadecimal value')
LOGGER.error('md5_hash is not a hexadecimal value')
return 1
if not is_hex(sha256_hash): # mandatory
logger.error('sha256_hash is not a hexadecimal value')
LOGGER.error('sha256_hash is not a hexadecimal value')
return 1
# Remove old image
......
......@@ -23,12 +23,12 @@ import os
import subprocess
import sys
# pylint: disable=W0403
# pylint: disable=bare-except,relative-import
import settings
from utils import create_logger, delete_if_exists, is_hex, is_hash_pair
os.umask(022)
logger = create_logger('cropresize')
LOGGER = create_logger('cropresize')
MAX_PIXELS = 7000
......@@ -54,11 +54,11 @@ def crop(filename, x=0, y=0, w=0, h=0):
dest = settings.READY_FILES_ROOT + filename
if os.path.isfile(dest):
logger.info('Already done')
LOGGER.info('Already done')
return 0 # already done, skip
if not os.path.isfile(source):
logger.error('Source image missing')
LOGGER.error('Source image missing')
return 1 # source image doesn't exist, can't crop it
broken_file = settings.MEDIA_ROOT + 'img/broken'
......@@ -68,25 +68,25 @@ def crop(filename, x=0, y=0, w=0, h=0):
img = Image.open(source)
except:
create_broken_image(broken_file + '.png', dest)
logger.error('Cannot open image')
LOGGER.error('Cannot open image')
return 2
ext = pil_format_to_ext(img.format)
if not ext:
create_broken_image(broken_file + ext, dest)
logger.error('Invalid extension')
LOGGER.error('Invalid extension')
return 3
try:
img.verify()
except:
create_broken_image(broken_file + ext, dest)
logger.error('Image failed verification')
LOGGER.error('Image failed verification')
return 2
# Need to reopen the image after verify()
img = Image.open(source)
a, b = img.size
a, b = img.size # pylint: disable=invalid-name
if a > MAX_PIXELS or b > MAX_PIXELS:
logger.error('Image dimensions are too big (max: %s x %s)', MAX_PIXELS, MAX_PIXELS)
LOGGER.error('Image dimensions are too big (max: %s x %s)', MAX_PIXELS, MAX_PIXELS)
return 6
if w == 0 and h == 0:
......@@ -94,7 +94,7 @@ def crop(filename, x=0, y=0, w=0, h=0):
i = min(w, h)
w, h = i, i
elif w < 0 or (x + w) > a or h < 0 or (y + h) > b:
logger.error('Crop dimensions outside of original image bounding box')
LOGGER.error('Crop dimensions outside of original image bounding box')
return 6
cropped = img.crop((x, y, x + w, y + h))
......@@ -116,36 +116,36 @@ def optimize_image(dest, img_format, ext, broken_file):
process = subprocess.Popen(['/usr/bin/jpegoptim', '-p', '--strip-all', dest], stdout=subprocess.PIPE)
if process.wait() != 0:
create_broken_image(broken_file + ext, dest)
logger.error('JPEG optimisation failed: %s', process.communicate()[0])
LOGGER.error('JPEG optimisation failed: %s', process.communicate()[0])
return 4
elif 'PNG' == img_format:
process = subprocess.Popen(['/usr/bin/pngcrush', '-rem', 'gAMA', '-rem', 'alla', '-rem', 'text', dest, dest + '.tmp'], stdout=subprocess.PIPE)
if process.wait() != 0:
delete_if_exists(dest + '.tmp')
create_broken_image(broken_file + ext, dest)
logger.error('PNG optimisation (pngcrush) failed: %s', process.communicate()[0])
LOGGER.error('PNG optimisation (pngcrush) failed: %s', process.communicate()[0])
return 4
delete_if_exists(dest)
process = subprocess.Popen(['/usr/bin/optipng', '-o9', '-preserve', '--force', '-out', dest, dest + '.tmp'], stdout=subprocess.PIPE)
if process.wait() != 0:
delete_if_exists(dest + '.tmp')
create_broken_image(broken_file + ext, dest)
logger.error('PNG optimisation (optipng) failed: %s', process.communicate()[0])
LOGGER.error('PNG optimisation (optipng) failed: %s', process.communicate()[0])
return 4
delete_if_exists(dest + '.tmp')
process = subprocess.Popen(['/usr/bin/advpng', '--recompress', '--shrink-insane', dest], stdout=subprocess.PIPE)
if process.wait() != 0:
create_broken_image(broken_file + ext, dest)
logger.error('PNG optimisation (advpng) failed: %s', process.communicate()[0])
LOGGER.error('PNG optimisation (advpng) failed: %s', process.communicate()[0])
return 4
elif 'GIF' == img_format:
process = subprocess.Popen(['/usr/bin/gifsicle', '-O2', '-b', dest], stdout=subprocess.PIPE)
if process.wait() != 0:
create_broken_image(broken_file + ext, dest)
logger.error('GIF optimisation failed: %s', process.communicate()[0])
LOGGER.error('GIF optimisation failed: %s', process.communicate()[0])
return 4
else:
logger.error('Unexpected error while cropping')
LOGGER.error('Unexpected error while cropping')
return 5
return 0
......@@ -168,17 +168,17 @@ def main(argv=None):
# Validate inputs
if not is_hex(file_hash):
logger.error('file_hash is not a hexadecimal value')
LOGGER.error('file_hash is not a hexadecimal value')
return 1
if file_format != 'jpg' and file_format != 'png' and file_format != 'gif':
logger.error('file_format is not recognized')
LOGGER.error('file_format is not recognized')
return 1
if not isinstance(links, list):
logger.error('links is not a list')
LOGGER.error('links is not a list')
return 1
for l in links:
if not is_hash_pair(l):
logger.error('links is not a list of hash pairs')
LOGGER.error('links is not a list of hash pairs')
return 1
filename = "%s.%s" % (file_hash, file_format)
......
#!/usr/bin/python
# Copyright (C) 2011, 2013 Francois Marier <francois@libravatar.org>
# Copyright (C) 2011, 2013, 2016 Francois Marier <francois@libravatar.org>
#
# This file is part of Libravatar
#
......@@ -20,12 +20,12 @@ import json
import os
import sys
# pylint: disable=W0403
# pylint: disable=relative-import
import settings
from utils import create_logger, delete_if_exists, is_hex
os.umask(022)
logger = create_logger('deletephoto')
LOGGER = create_logger('deletephoto')
def main(argv=None):
......@@ -40,10 +40,10 @@ def main(argv=None):
# Validate inputs
if not is_hex(file_hash):
logger.error('file_hash is not a hexadecimal value')
LOGGER.error('file_hash is not a hexadecimal value')
return 1
if file_format != 'jpg' and file_format != 'png' and file_format != 'gif':
logger.error('file_format is not recognized')
LOGGER.error('file_format is not recognized')
return 1
filename = "%s.%s" % (file_hash, file_format)
......
......@@ -24,12 +24,12 @@ import os
import sys
from xml.sax import saxutils
# pylint: disable=W0403
# pylint: disable=relative-import
import settings
from utils import create_logger, is_hex
os.umask(022)
logger = create_logger('exportaccount')
LOGGER = create_logger('exportaccount')
SCHEMA_ROOT = 'https://www.libravatar.org/schemas/export/0.2'
SCHEMA_XSD = '%s/export.xsd' % SCHEMA_ROOT
......@@ -76,7 +76,7 @@ def xml_photos(photos):
def encode_photo(photo_filename, photo_format):
filename = settings.USER_FILES_ROOT + photo_filename + '.' + photo_format
if not os.path.isfile(filename):
logger.warning('Photo not found: %s', filename)
LOGGER.warning('Photo not found: %s', filename)
return None
photo_content = None
......@@ -84,7 +84,7 @@ def encode_photo(photo_filename, photo_format):
photo_content = photo.read()
if not photo_content:
logger.warning('Could not read photo: %s', filename)
LOGGER.warning('Could not read photo: %s', filename)
return None
return base64.b64encode(photo_content)
......@@ -106,15 +106,15 @@ def main(argv=None):
# Validate inputs
if file_hash and not is_hex(file_hash):
logger.error('file_hash is not a hexadecimal value')
LOGGER.error('file_hash is not a hexadecimal value')
return 1
for photo in photos:
(photo_filename, photo_format) = photo
if not is_hex(photo_filename):
logger.error("photo_filename '%s' is not a hexadecimal value", photo_filename)
LOGGER.error("photo_filename '%s' is not a hexadecimal value", photo_filename)
return 1
if photo_format != 'jpg' and photo_format != 'png' and photo_format != 'gif':
logger.error("photo_format '%s' is not recognized", photo_format)
LOGGER.error("photo_format '%s' is not recognized", photo_format)
return 1
dest_filename = settings.EXPORT_FILES_ROOT + file_hash + '.xml.gz'
......
......@@ -15,7 +15,7 @@
# You should have received a copy of the GNU Affero General Public License
# along with Libravatar. If not, see <http://www.gnu.org/licenses/>.
# pylint: disable=bad-continuation
# pylint: disable=bad-continuation,invalid-name
import random
from django.test import TestCase
......
......@@ -60,44 +60,44 @@ def srv_hostname(records):
return (None, None)
if 1 == len(records):
rr = records[0]
return (rr['target'], rr['port'])
ret = records[0]
return (ret['target'], ret['port'])
# Keep only the servers in the top priority
priority_records = []
total_weight = 0
top_priority = records[0]['priority'] # highest priority = lowest number
for rr in records:
if rr['priority'] > top_priority:
# ignore the record (rr has lower priority)
for ret in records:
if ret['priority'] > top_priority:
# ignore the record (ret has lower priority)
continue
elif rr['priority'] < top_priority:
# reset the array (rr has higher priority)
top_priority = rr['priority']
elif ret['priority'] < top_priority:
# reset the aretay (ret has higher priority)
top_priority = ret['priority']
total_weight = 0
priority_records = []
total_weight += rr['weight']
total_weight += ret['weight']
if rr['weight'] > 0:
priority_records.append((total_weight, rr))
if ret['weight'] > 0:
priority_records.append((total_weight, ret))
else:
# zero-weigth elements must come first
priority_records.insert(0, (0, rr))
priority_records.insert(0, (0, ret))
if 1 == len(priority_records):
unused, rr = priority_records[0]
return (rr['target'], rr['port'])
unused, ret = priority_records[0]
return (ret['target'], ret['port'])
# Select first record according to RFC2782 weight ordering algorithm (page 3)
random_number = random.randint(0, total_weight)
for record in priority_records:
weighted_index, rr = record
weighted_index, ret = record
if weighted_index >= random_number:
return (rr['target'], rr['port'])
return (ret['target'], ret['port'])
print 'There is something wrong with our SRV weight ordering algorithm'
return (None, None)
......@@ -142,10 +142,10 @@ def lookup_avatar_server(domain, https):
if ('data' not in answer) or (not answer['data']) or (not answer['typename']) or (answer['typename'] != 'SRV'):
continue
rr = {'priority': int(answer['data'][0]), 'weight': int(answer['data'][1]),
'port': int(answer['data'][2]), 'target': answer['data'][3]}
record = {'priority': int(answer['data'][0]), 'weight': int(answer['data'][1]),
'port': int(answer['data'][2]), 'target': answer['data'][3]}
records.append(rr)
records.append(record)
target, port = srv_hostname(records)
......
......@@ -22,12 +22,12 @@ import os
import shutil
import sys
# pylint: disable=W0403
# pylint: disable=bare-except,relative-import
import settings
from utils import create_logger, delete_if_exists, is_hex, is_hash_pair
os.umask(022)
logger = create_logger('ready2user')
LOGGER = create_logger('ready2user')
def main(argv=None):
......@@ -43,17 +43,17 @@ def main(argv=None):
# Validate inputs
if not is_hex(file_hash):
logger.error('file_hash is not a hexadecimal value')
LOGGER.error('file_hash is not a hexadecimal value')
return 1
if file_format != 'jpg' and file_format != 'png' and file_format != 'gif':
logger.error('file_format is not recognized')
LOGGER.error('file_format is not recognized')
return 1
if not isinstance(links, list):
logger.error('links is not a list')
LOGGER.error('links is not a list')
return 1
for l in links:
if not is_hash_pair(l):
logger.error('links is not a list of hash pairs')
LOGGER.error('links is not a list of hash pairs')
return 1
filename = "%s.%s" % (file_hash, file_format)
......@@ -62,18 +62,18 @@ def main(argv=None):
# Sanity checks
if os.path.isfile(dest):
logger.warning('Destination already exists')
LOGGER.warning('Destination already exists')
return 0
if not os.path.isfile(source):
logger.error('Source file not found')
LOGGER.error('Source file not found')
return 1
# Remove from /ready and move to /user
try:
shutil.move(source, dest)
except:
logger.error('Cannot move file')
LOGGER.error('Cannot move file')
return 2
# All done, we can delete the original file as uploaded by the user
......
#!/usr/bin/python
# Copyright (C) 2011, 2013 Francois Marier <francois@libravatar.org>
# Copyright (C) 2011, 2013, 2016 Francois Marier <francois@libravatar.org>
#
# This file is part of Libravatar
#
......@@ -21,12 +21,12 @@ import json
import os
import sys
# pylint: disable=W0403
# pylint: disable=relative-import
import settings
from utils import create_logger, is_hex
os.umask(022)
logger = create_logger('resizeavatar')
LOGGER = create_logger('resizeavatar')
def resize_image(email_hash, size):
......@@ -57,7 +57,7 @@ def main(argv=None):
# Validate inputs
if not is_hex(email_hash):
logger.error('email_hash is not a hexadecimal value')
LOGGER.error('email_hash is not a hexadecimal value')
return 1
resize_image(email_hash, size)
......
......@@ -47,6 +47,7 @@ TIME_ZONE = 'UTC'
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'
# pylint: disable=invalid-name
ugettext = lambda s: s
LANGUAGES = (
('ca', ugettext('Catalan')),
......
......@@ -15,7 +15,7 @@
# You should have received a copy of the GNU Affero General Public License
# along with Libravatar. If not, see <http://www.gnu.org/licenses/>.
# pylint: disable=bad-continuation
# pylint: disable=bad-continuation,invalid-name
import re
from django.test import TestCase
......
......@@ -17,6 +17,7 @@
from django.conf.urls import url, patterns
# pylint: disable=invalid-name
urlpatterns = patterns('',
url('check/$', 'libravatar.tools.views.check'),
url('check_domain/$',
......
......@@ -20,6 +20,7 @@ from django.conf.urls import url, patterns, include, handler404, handler500
handler404 # make pyflakes happy, pylint: disable=W0104
handler500 # make pyflakes happy, pylint: disable=W0104
# pylint: disable=invalid-name
urlpatterns = patterns('',
url(r'^account/', include('libravatar.account.urls')),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment