Fix some pep8 issues

This commit is contained in:
Simon Conseil
2018-12-17 00:42:56 +01:00
parent 73b6e9612c
commit 1c0b8d976d
5 changed files with 55 additions and 38 deletions

View File

@@ -254,7 +254,7 @@ def set_meta(target, keys, overwrite=False):
sys.exit(2)
with open(descfile, "w") as fp:
for i in range(len(keys)//2):
k, v = keys[i*2:(i+1)*2]
for i in range(len(keys) // 2):
k, v = keys[i * 2:(i + 1) * 2]
fp.write("{}: {}\n".format(k.capitalize(), v))
print("{} metadata key(s) written to {}".format(len(keys)//2, descfile))
print("{} metadata key(s) written to {}".format(len(keys) // 2, descfile))

View File

@@ -719,8 +719,10 @@ class Gallery(object):
self.remove_files(failed_files)
if self.settings['write_html']:
album_writer = AlbumPageWriter(self.settings, index_title=self.title)
album_list_writer = AlbumListPageWriter(self.settings, index_title=self.title)
album_writer = AlbumPageWriter(self.settings,
index_title=self.title)
album_list_writer = AlbumListPageWriter(self.settings,
index_title=self.title)
with progressbar(self.albums.values(),
label="%16s" % "Writing files",
item_show_func=log_func, show_eta=False,

View File

@@ -38,28 +38,27 @@ from click import progressbar
logger = logging.getLogger(__name__)
DEFAULT_SETTINGS = {
'suffixes': ['htm', 'html', 'css', 'js', 'svg'],
'method': 'gzip',
}
'suffixes': ['htm', 'html', 'css', 'js', 'svg'],
'method': 'gzip',
}
class BaseCompressor:
suffix = None
def __init__(self, settings):
self.suffixes_to_compress = settings.get('suffixes', DEFAULT_SETTINGS['suffixes'])
self.suffixes_to_compress = settings.get('suffixes',
DEFAULT_SETTINGS['suffixes'])
def do_compress(self, filename, compressed_filename):
'''
"""
Perform actual compression.
This should be implemented by subclasses.
'''
"""
raise NotImplementedError
def compress(self, filename):
'''
Compress a file, only if needed.
'''
"""Compress a file, only if needed."""
compressed_filename = self.get_compressed_filename(filename)
if not compressed_filename:
return
@@ -67,14 +66,18 @@ class BaseCompressor:
self.do_compress(filename, compressed_filename)
def get_compressed_filename(self, filename):
'''
If the given filename should be compressed, returns the compressed filename.
"""If the given filename should be compressed, returns the
compressed filename.
A file can be compressed if:
- It is a whitelisted extension
- The compressed file does not exist
- The compressed file exists by is older than the file itself
- It is a whitelisted extension
- The compressed file does not exist
- The compressed file exists by is older than the file itself
Otherwise, it returns False.
'''
"""
if not os.path.splitext(filename)[1][1:] in self.suffixes_to_compress:
return False
@@ -88,7 +91,9 @@ class BaseCompressor:
pass
if file_stats and compressed_stats:
return compressed_filename if file_stats.st_mtime > compressed_stats.st_mtime else False
return (compressed_filename
if file_stats.st_mtime > compressed_stats.st_mtime
else False)
else:
return compressed_filename
@@ -97,7 +102,8 @@ class GZipCompressor(BaseCompressor):
suffix = 'gz'
def do_compress(self, filename, compressed_filename):
with open(filename, 'rb') as f_in, gzip.open(compressed_filename, 'wb') as f_out:
with open(filename, 'rb') as f_in, \
gzip.open(compressed_filename, 'wb') as f_out:
shutil.copyfileobj(f_in, f_out)
@@ -106,7 +112,8 @@ class ZopfliCompressor(BaseCompressor):
def do_compress(self, filename, compressed_filename):
import zopfli.gzip
with open(filename, 'rb') as f_in, open(compressed_filename, 'wb') as f_out:
with open(filename, 'rb') as f_in, \
open(compressed_filename, 'wb') as f_out:
f_out.write(zopfli.gzip.compress(f_in.read()))
@@ -115,7 +122,8 @@ class BrotliCompressor(BaseCompressor):
def do_compress(self, filename, compressed_filename):
import brotli
with open(filename, 'rb') as f_in, open(compressed_filename, 'wb') as f_out:
with open(filename, 'rb') as f_in, \
open(compressed_filename, 'wb') as f_out:
f_out.write(brotli.compress(f_in.read(), mode=brotli.MODE_TEXT))
@@ -125,14 +133,14 @@ def get_compressor(settings):
return GZipCompressor(settings)
elif name == 'zopfli':
try:
import zopfli.gzip
import zopfli.gzip # noqa
return ZopfliCompressor(settings)
except ImportError:
logging.error('Unable to import zopfli module')
elif name == 'brotli':
try:
import brotli
import brotli # noqa
return BrotliCompressor(settings)
except ImportError:
logger.error('Unable to import brotli module')
@@ -143,7 +151,8 @@ def get_compressor(settings):
def compress_gallery(gallery):
logging.info('Compressing assets for %s', gallery.title)
compress_settings = gallery.settings.get('compress_assets_options', DEFAULT_SETTINGS)
compress_settings = gallery.settings.get('compress_assets_options',
DEFAULT_SETTINGS)
compressor = get_compressor(compress_settings)
if compressor is None:
@@ -151,13 +160,16 @@ def compress_gallery(gallery):
# Collecting theme assets
theme_assets = []
for current_directory, _, filenames in os.walk(os.path.join(gallery.settings['destination'], 'static')):
for current_directory, _, filenames in os.walk(
os.path.join(gallery.settings['destination'], 'static')):
for filename in filenames:
theme_assets.append(os.path.join(current_directory, filename))
with progressbar(length=len(gallery.albums) + len(theme_assets), label='Compressing static files') as bar:
with progressbar(length=len(gallery.albums) + len(theme_assets),
label='Compressing static files') as bar:
for album in gallery.albums.values():
compressor.compress(os.path.join(album.dst_path, album.output_file))
compressor.compress(os.path.join(album.dst_path,
album.output_file))
bar.update(1)
for theme_asset in theme_assets:

View File

@@ -32,17 +32,19 @@ def add_copyright(img, settings=None):
assert font_size >= 0
color = settings.get('copyright_text_color', (0, 0, 0))
bottom_margin = 3 # bottom margin for text
text_height = bottom_margin + 12 # default text height (of 15) for default font
text_height = bottom_margin + 12 # default text height (of 15)
if font:
try:
font = ImageFont.truetype(font, font_size)
text_height = font.getsize(text)[1] + bottom_margin
except: # load default font in case of any exception
logger.debug("Exception: Couldn't locate font %s, using default font", font)
except Exception: # load default font in case of any exception
logger.debug("Exception: Couldn't locate font %s, using "
"default font", font)
font = ImageFont.load_default()
else:
font = ImageFont.load_default()
left, top = settings.get('copyright_text_position', (5, img.size[1] - text_height))
left, top = settings.get('copyright_text_position',
(5, img.size[1] - text_height))
draw.text((left, top), text, fill=color, font=font)
return img

View File

@@ -40,7 +40,8 @@ def upload_s3(gallery, settings=None):
# Get local files
for root, dirs, files in os.walk(gallery.settings['destination']):
for f in files:
path = os.path.join(root[len(gallery.settings['destination']) + 1:], f)
path = os.path.join(
root[len(gallery.settings['destination']) + 1:], f)
size = os.path.getsize(os.path.join(root, f))
upload_files += [(path, size)]
@@ -49,9 +50,9 @@ def upload_s3(gallery, settings=None):
bucket = conn.get_bucket(gallery.settings['upload_s3_options']['bucket'])
# Upload the files
with progressbar(upload_files, label="Uploading files to S3") as progress_upload:
for (f, size) in progress_upload:
if gallery.settings['upload_s3_options']['overwrite'] == False:
with progressbar(upload_files, label="Uploading files to S3") as bar:
for (f, size) in bar:
if gallery.settings['upload_s3_options']['overwrite'] is False:
# Check if file was uploaded before
key = bucket.get_key(f)
if key is not None and key.size == size: