Browse Source

Move the configuration to a python file.

After a long hesitation ... but this will allow to use tuple, etc.
pull/9/merge
Simon 14 years ago
parent
commit
7d3e7bb207
  1. 8
      docs/index.rst
  2. 2
      sigal/__init__.py
  3. 59
      sigal/settings.py
  4. 30
      tests/sample/sigal.conf.py
  5. 2
      tests/test_gallery.py
  6. 16
      tests/test_settings.py

8
docs/index.rst

@ -76,12 +76,12 @@ Optional arguments:
Configuration
-------------
The configuration for the gallery must be set in ``<input_dir>/sigal.conf``.
The configuration for the gallery must be set in ``<input_dir>/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

2
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):

59
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)
# 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')
settings = _DEFAULT_CONFIG.copy()
if filename:
tempdict = {}
execfile(filename, tempdict)
settings.update(tempdict)
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:

30
tests/sample/sigal.conf → 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"

2
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'))

16
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')

Loading…
Cancel
Save