Skip to content
This repository has been archived by the owner on Mar 28, 2019. It is now read-only.

Commit

Permalink
Use project name as setting prefix (fixes #448)
Browse files Browse the repository at this point in the history
  • Loading branch information
leplatrem committed Sep 30, 2015
1 parent 19081bd commit bbdcff9
Show file tree
Hide file tree
Showing 20 changed files with 185 additions and 104 deletions.
14 changes: 8 additions & 6 deletions cliquet/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Main entry point
"""
import functools
import warnings
import pkg_resources

Expand Down Expand Up @@ -41,7 +42,7 @@
'cliquet.http_scheme': None,
'cliquet.id_generator': 'cliquet.storage.generators.UUID4',
'cliquet.initialization_sequence': (
'cliquet.initialization.setup_request_bound_data',
'cliquet.initialization.setup_request',
'cliquet.initialization.setup_json_serializer',
'cliquet.initialization.setup_logging',
'cliquet.initialization.setup_storage',
Expand Down Expand Up @@ -125,13 +126,13 @@ def load_default_settings(config, default_settings):

def includeme(config):
settings = config.get_settings()
project_setting = functools.partial(utils.project_setting, settings)

# Add CORS settings to the base cliquet Service class.
cors_origins = settings['cliquet.cors_origins']
Service.cors_origins = tuple(aslist(cors_origins))
Service.cors_origins = tuple(aslist(project_setting('cors_origins')))
Service.default_cors_headers = ('Backoff', 'Retry-After', 'Alert',
'Content-Length')
cors_max_age = settings['cliquet.cors_max_age_seconds']
cors_max_age = project_setting('cors_max_age_seconds')
Service.cors_max_age = int(cors_max_age) if cors_max_age else None

Service.error_handler = lambda self, e: errors.json_error_handler(e)
Expand All @@ -140,10 +141,11 @@ def includeme(config):
config.registry.heartbeats = {}

# Public settings registry.
config.registry.public_settings = {'cliquet.batch_max_requests'}
project_name = settings['cliquet.project_name']
config.registry.public_settings = {'%s.batch_max_requests' % project_name}

# Setup components.
for step in aslist(settings['cliquet.initialization_sequence']):
for step in aslist(project_setting('initialization_sequence')):
step_func = config.maybe_dotted(step)
step_func(config)

Expand Down
4 changes: 1 addition & 3 deletions cliquet/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ def noop_check(*a):
**kwargs)

def unauthenticated_userid(self, request):
settings = request.registry.settings

credentials = self._get_credentials(request)
if credentials:
username, password = credentials
if not username:
return

hmac_secret = settings['cliquet.userid_hmac_secret']
hmac_secret = request.setting('userid_hmac_secret')
credentials = '%s:%s' % credentials
userid = utils.hmac_digest(hmac_secret, credentials)
return userid
7 changes: 3 additions & 4 deletions cliquet/authorization.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,9 @@ def __init__(self, request):
request.registry.permission.principals_accessible_objects,
object_id_match=object_id_match)

settings = request.registry.settings
setting = 'cliquet.%s_%s_principals' % (self.resource_name,
self.required_permission)
self.allowed_principals = aslist(settings.get(setting, ''))
setting = '%s_%s_principals' % (self.resource_name,
self.required_permission)
self.allowed_principals = aslist(request.setting(setting, ''))

def check_permission(self, *args, **kw):
return self._check_permission(self.permission_object_id, *args, **kw)
Expand Down
10 changes: 5 additions & 5 deletions cliquet/cache/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import redis
from six.moves.urllib import parse as urlparse

from cliquet import utils
from cliquet.cache import CacheBase
from cliquet.storage.redis import wrap_redis_error
from cliquet.utils import json


class Redis(CacheBase):
Expand Down Expand Up @@ -51,7 +51,7 @@ def expire(self, key, ttl):

@wrap_redis_error
def set(self, key, value, ttl=None):
value = json.dumps(value)
value = utils.json.dumps(value)
if ttl:
self._client.psetex(key, int(ttl * 1000), value)
else:
Expand All @@ -62,7 +62,7 @@ def get(self, key):
value = self._client.get(key)
if value:
value = value.decode('utf-8')
return json.loads(value)
return utils.json.loads(value)

@wrap_redis_error
def delete(self, key):
Expand All @@ -71,9 +71,9 @@ def delete(self, key):

def load_from_config(config):
settings = config.get_settings()
uri = settings['cliquet.cache_url']
uri = utils.project_setting(settings, 'cache_url')
uri = urlparse.urlparse(uri)
pool_size = int(settings['cliquet.cache_pool_size'])
pool_size = int(utils.project_setting(settings, 'cache_pool_size'))

return Redis(max_connections=pool_size,
host=uri.hostname or 'localhost',
Expand Down
2 changes: 1 addition & 1 deletion cliquet/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def send_alert(request, message=None, url=None, code='soft-eol'):
:param url: The URL for more information, default to the documentation url.
"""
if url is None:
url = request.registry.settings['cliquet.project_docs']
url = request.setting('project_docs')

request.response.headers['Alert'] = encode_header(json.dumps({
'code': code,
Expand Down
127 changes: 70 additions & 57 deletions cliquet/initialization.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import functools
import warnings
from datetime import datetime
from dateutil import parser as dateparser
Expand Down Expand Up @@ -31,10 +32,19 @@
from pyramid_multiauth import MultiAuthPolicySelected


def setup_request_bound_data(config):
"""Attach custom data on request object, and share it with parent
requests during batch."""
def setup_request(config):
"""Attach helpers on request object.
"""
def get_setting(request, setting, *args):
settings = request.registry.settings
return utils.project_setting(settings, setting, *args)

config.add_request_method(get_setting, 'setting')

def attach_bound_data(request):
"""Attach custom bound data, and share it with parent requests
during batch.
"""
parent = getattr(request, 'parent', None)
return parent.bound_data if parent else {}

Expand All @@ -55,8 +65,8 @@ def setup_trailing_slash_redirection(config):
"""URLs of Webservices built with Django usually have a trailing slash.
Cliquet does not, and removes it with a redirection.
"""
settings = config.get_settings()
redirect_enabled = settings['cliquet.trailing_slash_redirect_enabled']
settings = functools.partial(utils.project_setting, config.get_settings())
redirect_enabled = settings('trailing_slash_redirect_enabled')
trailing_slash_redirection_enabled = asbool(redirect_enabled)

if not trailing_slash_redirection_enabled:
Expand All @@ -80,8 +90,8 @@ def _view(request):
def setup_version_redirection(config):
"""Add a view which redirects to the current version of the API.
"""
settings = config.get_settings()
redirect_enabled = settings['cliquet.version_prefix_redirect_enabled']
settings = functools.partial(utils.project_setting, config.get_settings())
redirect_enabled = settings('version_prefix_redirect_enabled')
version_prefix_redirection_enabled = asbool(redirect_enabled)

route_prefix = config.route_prefix
Expand Down Expand Up @@ -150,7 +160,7 @@ def setup_backoff(config):
"""
def on_new_response(event):
# Add backoff in response headers.
backoff = config.registry.settings['cliquet.backoff']
backoff = event.request.setting('backoff')
if backoff is not None:
backoff = utils.encode_header('%s' % backoff)
event.response.headers['Backoff'] = backoff
Expand All @@ -160,10 +170,10 @@ def on_new_response(event):

def setup_requests_scheme(config):
"""Force server scheme, host and port at the application level."""
settings = config.get_settings()
settings = functools.partial(utils.project_setting, config.get_settings())

http_scheme = settings['cliquet.http_scheme']
http_host = settings['cliquet.http_host']
http_scheme = settings('http_scheme')
http_host = settings('http_host')

def on_new_request(event):
if http_scheme:
Expand All @@ -185,9 +195,9 @@ def _end_of_life_tween_factory(handler, registry):
" at this location.")

def eos_tween(request):
eos_date = registry.settings['cliquet.eos']
eos_url = registry.settings['cliquet.eos_url']
eos_message = registry.settings['cliquet.eos_message']
eos_date = request.setting('eos')
eos_url = request.setting('eos_url')
eos_message = request.setting('eos_message')
if not eos_date:
return handler(request)

Expand All @@ -209,21 +219,21 @@ def eos_tween(request):


def setup_storage(config):
settings = config.get_settings()
storage_class = settings['cliquet.storage_backend']
settings = functools.partial(utils.project_setting, config.get_settings())
storage_class = settings('storage_backend')
if not storage_class:
return

storage = config.maybe_dotted(storage_class)
config.registry.storage = storage.load_from_config(config)
config.registry.heartbeats['storage'] = config.registry.storage.ping
id_generator = config.maybe_dotted(settings['cliquet.id_generator'])
id_generator = config.maybe_dotted(settings('id_generator'))
config.registry.id_generator = id_generator()


def setup_permission(config):
settings = config.get_settings()
permission_class = settings['cliquet.permission_backend']
settings = functools.partial(utils.project_setting, config.get_settings())
permission_class = settings('permission_backend')
if not permission_class:
return

Expand All @@ -233,8 +243,8 @@ def setup_permission(config):


def setup_cache(config):
settings = config.get_settings()
cache_class = settings['cliquet.cache_backend']
settings = functools.partial(utils.project_setting, config.get_settings())
cache_class = settings('cache_backend')
if not cache_class:
return

Expand All @@ -244,61 +254,63 @@ def setup_cache(config):


def setup_statsd(config):
settings = config.get_settings()
config.registry.statsd = None
settings = functools.partial(utils.project_setting, config.get_settings())

if settings['cliquet.statsd_url']:
client = statsd.load_from_config(config)
if not settings('statsd_url'):
config.registry.statsd = None
return None

config.registry.statsd = client
client = statsd.load_from_config(config)
config.registry.statsd = client

client.watch_execution_time(config.registry.cache, prefix='cache')
client.watch_execution_time(config.registry.storage, prefix='storage')
client.watch_execution_time(config.registry.permission,
prefix='permission')
client.watch_execution_time(config.registry.cache, prefix='cache')
client.watch_execution_time(config.registry.storage, prefix='storage')
client.watch_execution_time(config.registry.permission,
prefix='permission')

# Commit so that configured policy can be queried.
config.commit()
policy = config.registry.queryUtility(IAuthenticationPolicy)
client.watch_execution_time(policy, prefix='authentication')
# Commit so that configured policy can be queried.
config.commit()
policy = config.registry.queryUtility(IAuthenticationPolicy)
client.watch_execution_time(policy, prefix='authentication')

def on_new_response(event):
request = event.request
def on_new_response(event):
request = event.request

# Count unique users.
user_id = request.authenticated_userid
if user_id:
client.count('users', unique=user_id)
# Count unique users.
user_id = request.authenticated_userid
if user_id:
client.count('users', unique=user_id)

# Count authentication verifications.
if hasattr(request, 'authn_type'):
client.count('%s.%s' % ('authn_type', request.authn_type))
# Count authentication verifications.
if hasattr(request, 'authn_type'):
client.count('%s.%s' % ('authn_type', request.authn_type))

# Count view calls.
pattern = request.matched_route.pattern
services = request.registry.cornice_services
service = services.get(pattern)
if service:
client.count('view.%s.%s' % (service.name, request.method))
# Count view calls.
pattern = request.matched_route.pattern
services = request.registry.cornice_services
service = services.get(pattern)
if service:
client.count('view.%s.%s' % (service.name, request.method))

config.add_subscriber(on_new_response, NewResponse)
config.add_subscriber(on_new_response, NewResponse)

return client
return client


def install_middlewares(app, settings):
"Install a set of middlewares defined in the ini file on the given app."
settings = functools.partial(utils.project_setting, settings)

# Setup new-relic.
if settings.get('cliquet.newrelic_config'):
ini_file = settings['cliquet.newrelic_config']
env = settings['cliquet.newrelic_env']
ini_file = settings('newrelic_config', None)
if ini_file:
env = settings('newrelic_env')
newrelic.agent.initialize(ini_file, env)
app = newrelic.agent.WSGIApplicationWrapper(app)

# Adds the Werkzeug profiler.
if asbool(settings.get('cliquet.profiler_enabled')):
profile_dir = settings['cliquet.profiler_dir']
if asbool(settings('profiler_enabled', False)):
profile_dir = settings('profiler_dir')
app = ProfilerMiddleware(app, profile_dir=profile_dir,
restrictions=('*cliquet*'))

Expand All @@ -314,7 +326,8 @@ def setup_logging(config):
"""
settings = config.get_settings()

renderer_klass = config.maybe_dotted(settings['cliquet.logging_renderer'])
logging_renderer = utils.project_setting(settings, 'logging_renderer')
renderer_klass = config.maybe_dotted(logging_renderer)
renderer = renderer_klass(settings)

structlog.configure(
Expand Down
5 changes: 3 additions & 2 deletions cliquet/permission/postgresql/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from six.moves.urllib import parse as urlparse

from cliquet import logger
from cliquet import utils
from cliquet.permission import PermissionBase
from cliquet.storage.postgresql import PostgreSQLClient

Expand Down Expand Up @@ -310,9 +311,9 @@ def delete_object_permissions(self, *object_id_list):

def load_from_config(config):
settings = config.get_settings()
uri = settings['cliquet.permission_url']
uri = utils.project_setting(settings, 'permission_url')
uri = urlparse.urlparse(uri)
pool_size = int(settings['cliquet.permission_pool_size'])
pool_size = int(utils.project_setting(settings, 'permission_pool_size'))

conn_kwargs = dict(pool_size=pool_size,
host=uri.hostname,
Expand Down
Loading

0 comments on commit bbdcff9

Please sign in to comment.