From 21c4041f3be5118822f221e9381afc11d057d4a3 Mon Sep 17 00:00:00 2001 From: Simon Conseil Date: Sat, 28 Sep 2013 23:06:33 +0200 Subject: [PATCH] py3 compat: print, iteritems, imports --- setup.py | 10 +++++++--- sigal/__init__.py | 17 ++++++++--------- sigal/compat.py | 14 ++++++++++++++ sigal/gallery.py | 7 ++++++- sigal/pkgmeta.py | 6 +++--- sigal/settings.py | 9 +++++++-- 6 files changed, 45 insertions(+), 18 deletions(-) create mode 100644 sigal/compat.py diff --git a/setup.py b/setup.py index f591bba..2f2adb1 100644 --- a/setup.py +++ b/setup.py @@ -22,8 +22,10 @@ with open('docs/changelog.rst') as f: # Load package meta from the pkgmeta module without loading the package. pkgmeta = {} -execfile(os.path.join(os.path.dirname(__file__), 'sigal', 'pkgmeta.py'), - pkgmeta) +pkgmeta_file = os.path.join(os.path.dirname(__file__), 'sigal', 'pkgmeta.py') +with open(pkgmeta_file) as f: + code = compile(f.read(), 'pkgmeta.py', 'exec') + exec(code, pkgmeta) setup( name='sigal', @@ -46,8 +48,10 @@ setup( 'Environment :: Console', 'License :: OSI Approved :: MIT License', 'Operating System :: OS Independent', - 'Programming Language :: Python :: 2.6', + 'Programming Language :: Python :: 2', 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.3', 'Topic :: Internet :: WWW/HTTP', 'Topic :: Multimedia :: Graphics :: Viewers', 'Topic :: Software Development :: Libraries :: Python Modules', diff --git a/sigal/__init__.py b/sigal/__init__.py index c4e1ec4..52fb2c6 100644 --- a/sigal/__init__.py +++ b/sigal/__init__.py @@ -30,9 +30,9 @@ sigal is yet another python script to prepare a static gallery of images: * generate html pages. """ -from __future__ import absolute_import +from __future__ import absolute_import, print_function -import codecs +import io import logging import os import sys @@ -72,9 +72,9 @@ def init(): from pkg_resources import resource_string conf = resource_string(__name__, 'templates/sigal.conf.py') - with codecs.open('sigal.conf.py', 'w', 'utf-8') as f: + with io.open('sigal.conf.py', 'w', 'utf-8') as f: f.write(conf) - print "Sample config file created: sigal.conf.py" + print("Sample config file created: sigal.conf.py") @arg('source', nargs='?', help='Input directory') @@ -97,7 +97,7 @@ def build(source, destination, debug=False, verbose=False, force=False, settings_file = config or _DEFAULT_CONFIG_FILE if not os.path.isfile(settings_file): - logger.error("Settings file not found (%s)", settings_file) + logger.error("Settings file not found: %s", settings_file) sys.exit(1) settings = read_settings(settings_file) @@ -108,8 +108,7 @@ def build(source, destination, debug=False, verbose=False, force=False, logger.info("Input : %s", settings['source']) if not settings['source'] or not os.path.isdir(settings['source']): - logger.error("Input directory '%s' does not exist.", - settings['source']) + logger.error("Input directory not found: %s", settings['source']) sys.exit(1) logger.info("Output : %s", settings['destination']) @@ -138,7 +137,7 @@ def serve(path): Handler = SimpleHTTPServer.SimpleHTTPRequestHandler httpd = SocketServer.TCPServer(("", PORT), Handler, False) - print " * Running on http://127.0.0.1:%i/" % PORT + print(" * Running on http://127.0.0.1:{}/".format(PORT)) try: httpd.allow_reuse_address = True @@ -146,7 +145,7 @@ def serve(path): httpd.server_activate() httpd.serve_forever() except KeyboardInterrupt: - print '\nAll done!' + print('\nAll done!') else: sys.stderr.write("The '%s' directory doesn't exist.\n" % path) diff --git a/sigal/compat.py b/sigal/compat.py new file mode 100644 index 0000000..ad1002b --- /dev/null +++ b/sigal/compat.py @@ -0,0 +1,14 @@ +# -*- coding: utf-8 -*- + +import sys + +PY2 = sys.version_info[0] == 2 + +if not PY2: + text_type = str + string_types = (str,) + unichr = chr +else: + text_type = unicode # NOQA + string_types = (str, unicode) # NOQA + unichr = unichr diff --git a/sigal/gallery.py b/sigal/gallery.py index 2b4b9a7..c0e10c8 100644 --- a/sigal/gallery.py +++ b/sigal/gallery.py @@ -38,9 +38,13 @@ from PIL import Image as PILImage import sigal.image import sigal.video +from . import compat from .settings import get_thumb from .writer import Writer +if not compat.PY2: + from functools import reduce + DESCRIPTION_FILE = "index.md" # Label with for the progress bar. The max value is 48 character = 80 - 32 for @@ -96,7 +100,8 @@ class PathsDb(object): } # get information for each directory - for path, dirnames, filenames in os.walk(self.basepath, followlinks=True): + for path, dirnames, filenames in os.walk(self.basepath, + followlinks=True): relpath = os.path.relpath(path, self.basepath) # sort images and sub-albums by name diff --git a/sigal/pkgmeta.py b/sigal/pkgmeta.py index af3426a..0264f42 100644 --- a/sigal/pkgmeta.py +++ b/sigal/pkgmeta.py @@ -1,8 +1,8 @@ # -*- coding:utf-8 -*- __title__ = 'sigal' -__author__ = u"Simon Conseil" -__version__ = "0.5.1" -__license__ = "MIT" +__author__ = 'Simon Conseil' +__version__ = '0.5.1' +__license__ = 'MIT' __url__ = 'https://github.com/saimn/sigal' __all__ = ['__title__', '__author__', '__version__', '__license__', '__url__'] diff --git a/sigal/settings.py b/sigal/settings.py index 32a18b8..b3af3e2 100644 --- a/sigal/settings.py +++ b/sigal/settings.py @@ -60,6 +60,7 @@ def get_thumb(settings, filename): """Return the path to the thumb. examples: + >>> default_settings = create_settings() >>> get_thumb(default_settings, "bar/foo.jpg") "bar/thumbnails/foo.jpg" >>> get_thumb(default_settings, "bar/foo.png") @@ -96,8 +97,12 @@ def read_settings(filename=None): if filename: logger.debug("Settings file: %s", filename) tempdict = {} - execfile(filename, tempdict) - settings.update((k, v) for k, v in tempdict.iteritems() + + with open(filename) as f: + code = compile(f.read(), filename, 'exec') + exec(code, tempdict) + + settings.update((k, v) for k, v in tempdict.items() if k not in ['__builtins__']) # Make the paths relative to the settings file