From 7d3e7bb207b90f0392a9ce99714f0ad5e82bd53a Mon Sep 17 00:00:00 2001 From: Simon Date: Sat, 15 Dec 2012 00:13:08 +0100 Subject: [PATCH] Move the configuration to a python file. After a long hesitation ... but this will allow to use tuple, etc. --- docs/index.rst | 8 +-- sigal/__init__.py | 2 +- sigal/settings.py | 57 +++++++++------------- tests/sample/{sigal.conf => sigal.conf.py} | 30 ++++++------ tests/test_gallery.py | 2 +- tests/test_settings.py | 16 ++---- 6 files changed, 47 insertions(+), 68 deletions(-) rename tests/sample/{sigal.conf => sigal.conf.py} (59%) diff --git a/docs/index.rst b/docs/index.rst index e6bf775..4a045e4 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -76,12 +76,12 @@ Optional arguments: Configuration ------------- -The configuration for the gallery must be set in ``/sigal.conf``. +The configuration for the gallery must be set in ``/sigal.conf.py``. An example file with explanations on the settings is available in -``tests/sample/sigal.conf`` and is shown below: +``tests/sample/sigal.conf.py`` and is shown below: -.. literalinclude:: ../tests/sample/sigal.conf - :language: ini +.. literalinclude:: ../tests/sample/sigal.conf.py + :language: python Album information diff --git a/sigal/__init__.py b/sigal/__init__.py index 92731b1..d5b80ab 100644 --- a/sigal/__init__.py +++ b/sigal/__init__.py @@ -46,7 +46,7 @@ from logging import Formatter from .gallery import Gallery from .settings import read_settings -_DEFAULT_CONFIG_FILE = 'sigal.conf' +_DEFAULT_CONFIG_FILE = 'sigal.conf.py' def init_logging(level=logging.INFO): diff --git a/sigal/settings.py b/sigal/settings.py index 74b2fd5..2c78a3f 100644 --- a/sigal/settings.py +++ b/sigal/settings.py @@ -20,39 +20,29 @@ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS # IN THE SOFTWARE. -import ConfigParser import logging import os _DEFAULT_CONFIG = { - 'img_size': '640x480', - 'make_thumbs': '1', + 'img_size': (640, 480), + 'make_thumbs': True, 'thumb_prefix': '', 'thumb_suffix': '', - 'thumb_size': '150x112', + 'thumb_size': (150, 112), 'thumb_dir': 'thumbnails', - 'thumb_fit': '1', - 'keep_orig': '0', + 'thumb_fit': True, + 'keep_orig': False, 'orig_dir': 'original', - 'jpg_quality': '90', - 'exif': '0', + 'jpg_quality': 90, + 'exif': False, 'copyright': '', - 'ext_list': '.jpg,.jpeg,.JPG,.JPEG,.png', + 'ext_list': ['.jpg', '.jpeg', '.JPG', '.JPEG', '.png'], 'theme': 'colorbox' } -def get_size(string): - "Split size string to a tuple of int" - - size = [int(i) for i in string.split("x")] - if size[1] > size[0]: - size[0], size[1] = size[1], size[0] - return tuple(size) - - def get_thumb(settings, filename): - "Return the path to the thumb" + """Return the path to the thumb.""" name, ext = os.path.splitext(filename) return os.path.join(settings['thumb_dir'], settings['thumb_prefix'] + @@ -60,26 +50,23 @@ def get_thumb(settings, filename): def read_settings(filename=None): - "Read settings from a config file in the source_dir root" + """Read settings from a config file in the source_dir root.""" logger = logging.getLogger(__name__) - # Read the default configuration - config = ConfigParser.ConfigParser(defaults=_DEFAULT_CONFIG) + settings = _DEFAULT_CONFIG.copy() + if filename: + tempdict = {} + execfile(filename, tempdict) + settings.update(tempdict) - # Load the config file - if filename and os.path.isfile(filename): - config.read(filename) - - settings = dict(config.items('sigal')) - settings['ext_list'] = settings['ext_list'].split(',') - settings['img_size'] = get_size(settings['img_size']) - settings['thumb_size'] = get_size(settings['thumb_size']) - settings['jpg_quality'] = config.getint('sigal', 'jpg_quality') - settings['keep_orig'] = config.getboolean('sigal', 'keep_orig') - settings['exif'] = config.getboolean('sigal', 'exif') - settings['make_thumbs'] = config.getboolean('sigal', 'make_thumbs') - settings['thumb_fit'] = config.getboolean('sigal', 'thumb_fit') + for key in ('img_size', 'thumb_size'): + size = settings[key] + if size[1] > size[0]: + size[0], size[1] = size[1], size[0] + settings[key] = size + logger.warning("The %s setting should be specified with the " + "largest value first.", key) if settings['exif']: try: diff --git a/tests/sample/sigal.conf b/tests/sample/sigal.conf.py similarity index 59% rename from tests/sample/sigal.conf rename to tests/sample/sigal.conf.py index 14c6a3c..d9ebbd4 100644 --- a/tests/sample/sigal.conf +++ b/tests/sample/sigal.conf.py @@ -1,43 +1,41 @@ -[sigal] - # All configuration values have a default; values that are commented out serve # to show the default. Default values are specified when modified in this # example config file # theme : # - colorbox (default), galleria, or the path to a custom theme directory -# theme = colorbox +# theme = 'colorbox' # size of resized image -# img_size = 640x480 +# img_size = (640, 480) # generate thumbnails -# make_thumbs = 1 +# make_thumbs = True # subdirectory of the thumbnails -# thumb_dir = thumbnails +# thumb_dir = 'thumbnails' # prefix and/or suffix for thumbnail names (default: '') # thumb_prefix = -thumb_suffix = .tn +thumb_suffix = '.tn' -# thumbnail size (default: 150x112) -thumb_size = 200x150 +# thumbnail size (default: (150, 112)) +thumb_size = (200, 150) # crop the image to fill the box -# thumb_fit = 1 +# thumb_fit = True -# keep original image (default: 0) -keep_orig = 1 +# keep original image (default: False) +keep_orig = True # subdirectory for original images -# orig_dir = original +# orig_dir = 'original' # jpeg quality # jpg_quality = 90 -# keep exif metadatas in output image (default: 0) -exif = 1 +# keep exif metadatas in output image (default: False) +exif = True # add a copyright text on the image (default: '') -copyright = An example copyright message +copyright = "An example copyright message" diff --git a/tests/test_gallery.py b/tests/test_gallery.py index 4645cc5..4dac44c 100644 --- a/tests/test_gallery.py +++ b/tests/test_gallery.py @@ -20,7 +20,7 @@ class TestGallery(unittest.TestCase): def setUp(cls): """Read the sample config file.""" - default_conf = os.path.join(CURRENT_DIR, 'sample', 'sigal.conf') + default_conf = os.path.join(CURRENT_DIR, 'sample', 'sigal.conf.py') settings = read_settings(default_conf) cls.gal = Gallery(settings, os.path.join(CURRENT_DIR, 'sample'), os.path.join(CURRENT_DIR, 'output')) diff --git a/tests/test_settings.py b/tests/test_settings.py index 8efee57..3c2d83f 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -7,7 +7,7 @@ try: except ImportError: import unittest # NOQA -from sigal.settings import read_settings, get_size +from sigal.settings import read_settings class TestSettings(unittest.TestCase): @@ -16,19 +16,13 @@ class TestSettings(unittest.TestCase): def setUp(self): "Read the sample config file" self.path = os.path.abspath(os.path.dirname(__file__)) - default_conf = os.path.join(self.path, 'sample', 'sigal.conf') + default_conf = os.path.join(self.path, 'sample', 'sigal.conf.py') self.settings = read_settings(default_conf) - def test_get_size(self): + def test_sizes(self): "Test that image sizes are correctly read" - self.assertTupleEqual(get_size('640x480'), (640, 480)) - self.assertTupleEqual(get_size('480x640'), (640, 480)) self.assertTupleEqual(self.settings['img_size'], (640, 480)) self.assertTupleEqual(self.settings['thumb_size'], (200, 150)) - def test_ext_list(self): - self.assertListEqual(self.settings['ext_list'], - ['.jpg', '.jpeg', '.JPG', '.JPEG', '.png']) - - def test_type_conversion(self): - self.assertEqual(self.settings['jpg_quality'], 90) + def test_settings(self): + self.assertEqual(self.settings['thumb_suffix'], '.tn')