Skip to content

Commit

Permalink
Generate apache configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
Stéphane Brunner committed Jun 1, 2013
1 parent 427f52f commit f881f42
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 17 deletions.
40 changes: 28 additions & 12 deletions tilecloud_chain/__init__.py
Expand Up @@ -2,6 +2,7 @@

import sys
import os
import re
import logging
import yaml
import sqlite3
Expand Down Expand Up @@ -125,7 +126,9 @@ def __init__(self, config_file, options=None, layer_name=None):
error = False
for gname, grid in self.config['grids'].items():
name = "grid[%s]" % gname
error = self.validate(grid, name, 'name', attribute_type=str, default=gname) or error
error = self.validate(
grid, name, 'name', attribute_type=str, default=gname, regex="^[a-zA-Z0-9_]+$"
) or error
error = self.validate(
grid, name, 'resolution_scale',
attribute_type=int
Expand Down Expand Up @@ -168,7 +171,9 @@ def __init__(self, config_file, options=None, layer_name=None):
if k not in layer:
layer[k] = v

error = self.validate(layer, name, 'name', attribute_type=str, default=lname) or error
error = self.validate(
layer, name, 'name', attribute_type=str, default=lname, regex="^[a-zA-Z0-9_]+$"
) or error
error = self.validate(layer, name, 'grid', attribute_type=str, required=True) or error
error = self.validate(layer, name, 'min_resolution_seed', attribute_type=float) or error
error = self.validate(layer, name, 'px_buffer', attribute_type=float, default=False) or error
Expand Down Expand Up @@ -212,17 +217,25 @@ def __init__(self, config_file, options=None, layer_name=None):

error = self.validate(layer, name, 'extension', attribute_type=str, required=True) or error
error = self.validate(layer, name, 'mime_type', attribute_type=str, required=True) or error
error = self.validate(layer, name, 'wmts_style', attribute_type=str, required=True) or error
error = self.validate(
layer, name, 'wmts_style', attribute_type=str, required=True, regex="^[a-zA-Z0-9_]+$"
) or error
error = self.validate(layer, name, 'dimensions', is_array=True, default=[]) or error
for d in layer['dimensions']:
dname = name + ".dimensions[%s]" % d.get('name', '')
error = self.validate(d, dname, 'name', attribute_type=str, required=True) or error
error = self.validate(d, dname, 'value', attribute_type=str, required=True) or error
error = self.validate(
d, dname, 'values', attribute_type=str, is_array=True,
default=[d['value']]
d, dname, 'name', attribute_type=str, required=True, regex="^[a-zA-Z0-9_]+$"
) or error
error = self.validate(
d, dname, 'value', attribute_type=str, required=True, regex="^[a-zA-Z0-9_]+$"
) or error
error = self.validate(
d, dname, 'values', attribute_type=str, is_array=True, default=[d['value']]
) or error
error = self.validate(
d, dname, 'default', attribute_type=str, default=d['value'],
regex="^[a-zA-Z0-9_]+$"
) or error
error = self.validate(d, dname, 'default', attribute_type=str, default=d['value']) or error

if 'empty_tile_detection' in layer:
error = self.validate(
Expand Down Expand Up @@ -453,7 +466,7 @@ def validate_exists(self, obj, obj_name, attribute):
logger.error("The attribute '%s' is required in the object %s." % (attribute, obj_name))
exit(1)

def _validate_type(self, value, attribute_type, enumeration):
def _validate_type(self, value, attribute_type, enumeration, regex=None):
if attribute_type is not None:
if attribute_type == int and type(value) == str:
try:
Expand All @@ -476,6 +489,9 @@ def _validate_type(self, value, attribute_type, enumeration):
return (True, None, str(attribute_type))
if typ != str:
value = str(value)
if regex:
if re.search(regex, value) is None:
return (True, None, "value '%s' don't respect regex '%s'" % (value, regex))
else:
if type(value) != attribute_type:
return (True, None, str(attribute_type))
Expand All @@ -485,12 +501,12 @@ def _validate_type(self, value, attribute_type, enumeration):

def validate(
self, obj, obj_name, attribute, attribute_type=None, is_array=False,
default=None, required=False, enumeration=None):
default=None, required=False, enumeration=None, **kargs):
if attribute in obj:
if is_array:
if type(obj[attribute]) == list:
for n, v in enumerate(obj[attribute]):
result, value, type_error = self._validate_type(v, attribute_type, enumeration)
result, value, type_error = self._validate_type(v, attribute_type, enumeration, **kargs)
if result:
logger.error(
"The attribute '%s' of the object %s has an element who is not a %s." %
Expand All @@ -502,7 +518,7 @@ def validate(
logger.error("The attribute '%s' of the object %s is not an array." % (attribute, obj_name))
return True
else:
result, value, type_error = self._validate_type(obj[attribute], attribute_type, enumeration)
result, value, type_error = self._validate_type(obj[attribute], attribute_type, enumeration, **kargs)
if result:
logger.error(
"The attribute '%s' of the object %s is not a %s." %
Expand Down
78 changes: 76 additions & 2 deletions tilecloud_chain/controller.py
Expand Up @@ -78,6 +78,10 @@ def main():
'--mapcache', '--generate-mapcache-config', default=False, action="store_true", dest='mapcache',
help='Generate MapCache configuration file'
)
parser.add_option(
'--apache', '--generate-apache-config', default=False, action="store_true", dest='apache',
help='Generate Apache configuration file'
)
parser.add_option(
'--dump-config', default=False, action="store_true",
help='Dump the used config with default values and exit'
Expand All @@ -101,7 +105,11 @@ def main():
sys.exit(0)

if options.mapcache:
_generate_mapcache_config(gene, options)
_generate_mapcache_config(gene)
sys.exit(0)

if options.apache:
_generate_apache_config(gene, options)
sys.exit(0)

if options.ol:
Expand All @@ -117,6 +125,7 @@ def main():
_validate_calculate_cost(gene)
_validate_generate_wmts_capabilities(gene, gene.caches[options.cache])
_validate_generate_mapcache_config(gene)
_validate_generate_apache_config(gene)
_validate_generate_openlayers(gene)
for grild in gene.config['grids'].values():
del grild['obj']
Expand Down Expand Up @@ -744,7 +753,7 @@ def _validate_generate_mapcache_config(gene):
exit(1) # pragma: no cover


def _generate_mapcache_config(gene, options):
def _generate_mapcache_config(gene):
from tilecloud_chain.mapcache_config_template import mapcache_config_template

_validate_generate_mapcache_config(gene)
Expand All @@ -762,6 +771,71 @@ def _generate_mapcache_config(gene, options):
f.close()


def _validate_generate_apache_config(gene):
error = False
error = gene.validate(gene.config, 'config', 'apache', attribute_type=dict, default={}) or error
error = gene.validate(
gene.config['apache'], 'apache', 'config_file', attribute_type=str,
default='apache/tiles.conf.in'
) or error
error = gene.validate(
gene.config['apache'], 'apache', 'expires', attribute_type=int,
default=8
) or error

if error:
exit(1) # pragma: no cover


def _generate_apache_config(gene, options):
_validate_generate_apache_config(gene)
cache = gene.caches[options.cache]

f = open(gene.config['apache']['config_file'], 'w')
f.write("""<Location /${vars:instanceid}/tiles>
ExpiresActive on
ExpiresDefault "now plus %(expires)i hours"
</Location>
""" % {
'expires': gene.config['apache']['expires']
})
if cache['type'] == 'filesystem':
f.write("""Alias /${vars:instanceid}/tiles %(files_folder)s
""" % {
'files_folder': cache['folder']
})

use_mapcache = False
for l in gene.config['layers']:
layer = gene.config['layers'][l]
if 'min_resolution_seed' in layer:
res = [r for r in layer['grid_ref']['resolutions'] if r < layer['min_resolution_seed']]
dim = len(layer['dimensions'])
for r in res:
use_mapcache = True
f.write("""RewriteRule ^/${vars:instanceid}/tiles/1.0.0/%(layer)s/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/"""
"""%(dimensions_re)s/%(zoom)s/(.*)$ /${vars:instanceid}/mapcache/wmts/1.0.0/%(layer)s/$1/$2/"""
"""%(dimensions_rep)s/%(zoom)s/%(final)s [PT]
""" % {
'layer': layer['name'],
'dimensions_re': '/'.join(['([a-zA-Z0-9_]+)' for e in range(dim)]),
'dimensions_rep': '/'.join(['$%i' % (e + 3) for e in range(dim)]),
'final': '$%i' % (3 + dim),
'zoom': layer['grid_ref']['resolutions'].index(r)
})

if use_mapcache:
_validate_generate_mapcache_config(gene)
mcf = gene.config['mapcache']['config_file']
f.write("""MapCacheAlias /${vars:instanceid}/mapcache "%(mapcache_config)s"
""" % {
'mapcache_config': ('' if mcf.startswith('/') else
'${buildout:directory}/') + gene.config['mapcache']['config_file']
})

f.close()


def _validate_generate_openlayers(gene):
error = False
error = gene.validate(gene.config, 'config', 'openlayers', attribute_type=dict, default={}) or error
Expand Down
4 changes: 2 additions & 2 deletions tilecloud_chain/tests/__init__.py
Expand Up @@ -57,7 +57,7 @@ def assert_cmd_exit_equals(self, cmd, main_func, result):
except SystemExit as e:
self.assertEquals(e.message, result)

def assert_main_equals(self, cmd, main_func, results):
def assert_main_equals(self, cmd, main_func, results, **kargs):
sys.argv = cmd.split(' ')
try:
main_func()
Expand All @@ -66,7 +66,7 @@ def assert_main_equals(self, cmd, main_func, results):
if results:
for result in results:
f = open(result[0], 'r')
self.assert_result_equals(f.read(), result[1])
self.assert_result_equals(f.read(), result[1], **kargs)

def assert_yaml_equals(self, content, value):
import yaml
Expand Down
21 changes: 20 additions & 1 deletion tilecloud_chain/tests/test_controller.py
Expand Up @@ -1236,7 +1236,26 @@ def test_mapcache(self):
<lock_dir>/tmp</lock_dir>
</mapcache>"""]])

CONFIG = """caches:
@attr(apache=True)
@attr(general=True)
def test_apache(self):
self.assert_main_equals(
cmd='./buildout/bin/generate_controller --apache -c tilegeneration/test.yaml',
main_func=controller.main,
results=[['tiles.conf.in', """<Location /${vars:instanceid}/tiles>
ExpiresActive on
ExpiresDefault "now plus 8 hours"
</Location>
Alias /${vars:instanceid}/tiles /tmp/tiles
RewriteRule ^/${vars:instanceid}/tiles/1.0.0/point_hash/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/4/(.*)$ """
"""/${vars:instanceid}/mapcache/wmts/1.0.0/point_hash/$1/$2/$3/4/$4 [PT]
RewriteRule ^/${vars:instanceid}/tiles/1.0.0/point/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/4/(.*)$ """
"""/${vars:instanceid}/mapcache/wmts/1.0.0/point/$1/$2/$3/4/$4 [PT]
MapCacheAlias /${vars:instanceid}/mapcache "${buildout:directory}/mapcache.xml"
"""]])

CONFIG = """apache: {config_file: apache/tiles.conf.in, expires: 8}
caches:
local: {folder: /tmp/tiles, hosts: false, http_url: 'http://taurus/tiles', http_urls: false,
name: local, type: filesystem, wmtscapabilities_file: /1.0.0/WMTSCapabilities.xml}
mbtiles: {folder: /tmp/tiles/mbtiles, http_url: 'http://taurus/tiles', name: mbtiles, type: mbtiles}
Expand Down
3 changes: 3 additions & 0 deletions tilecloud_chain/tests/tilegeneration/test.yaml
Expand Up @@ -218,5 +218,8 @@ cost:
mapcache:
config_file: mapcache.xml

apache:
config_file: tiles.conf.in

sns:
topic: sns_topic

0 comments on commit f881f42

Please sign in to comment.