Skip to content

Commit

Permalink
config: read defaults from app config
Browse files Browse the repository at this point in the history
* Try to read configuration values from application config if a context
  is available otherwise return an instance config or extension default.
  (closes #23)
  • Loading branch information
jirikuncar committed May 16, 2017
1 parent ac17207 commit a59731b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
5 changes: 5 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ Initialize with flask application and default parameters:
use_ssl=False,
base_url=None)
Alternatively, the default parameters can be read from the application
config values in `GRAVATAR_SIZE`, `GRAVATAR_RATING`, `GRAVATAR_DEFAULT`,
`GRAVATAR_FORCE_DEFAULT`, `GRAVATAR_FORCE_LOWER`, `GRAVATAR_USE_SSL`,
and `GRAVATAR_BASE_URL`.

Then in your template:

.. code-block:: jinja
Expand Down
48 changes: 36 additions & 12 deletions flask_gravatar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# Copyright (C) 2014 Nauman Ahmad.
# Copyright (C) 2014 Tom Powell.
# Copyright (C) 2015 CERN.
# Copyright (C) 2017 Jiri Kuncar.
#
# Flask-Gravatar is free software; you can redistribute it and/or modify
# it under the terms of the Revised BSD License; see LICENSE file for
Expand All @@ -14,17 +15,38 @@

import hashlib

from flask import _request_ctx_stack, request, has_request_context
from flask import _request_ctx_stack, current_app, request, \
has_request_context

try:
from flask import _app_ctx_stack
from flask import _app_ctx_stack, has_app_context
except ImportError: # pragma: no cover
_app_ctx_stack = None
has_app_context = None

from .version import __version__

# Which stack should we use? _app_ctx_stack is new in 0.9
connection_stack = _app_ctx_stack or _request_ctx_stack
has_context = has_app_context or has_request_context


class Property(object):
"""A property descriptor that sets and returns values."""

def __init__(self, default, key=None):
self.default = default
self.key = key

def __get__(self, obj, objtype):
"""Return value from application config, instance value or default."""
if self.key and has_context() and self.key in current_app.config:
return current_app.config[self.key]
return getattr(obj, self.key, self.default)

def __set__(self, obj, val):
"""Set instance value."""
setattr(obj, self.key, val)


class Gravatar(object):
Expand All @@ -44,9 +66,15 @@ class Gravatar(object):
)
"""

def __init__(self, app=None, size=100, rating='g', default='retro',
force_default=False, force_lower=False, use_ssl=None,
base_url=None, **kwargs):
size = Property(100, key='GRAVATAR_SIZE')
rating = Property('g', key='GRAVATAR_RATING')
default = Property('retro', key='GRAVATAR_DEFAULT')
force_default = Property(False, key='GRAVATAR_FORCE_DEFAULT')
force_lower = Property(False, key='GRAVATAR_FORCE_LOWER')
use_ssl = Property(None, key='GRAVATAR_USE_SSL')
base_url = Property(None, key='GRAVATAR_BASE_URL')

def __init__(self, app=None, **kwargs):
"""Initialize the Flask-Gravatar extension.
:param app: Your Flask app instance
Expand All @@ -58,13 +86,9 @@ def __init__(self, app=None, size=100, rating='g', default='retro',
:param use_ssl: Use https rather than http
:param base_url: Use custom base url for build link
"""
self.size = size
self.rating = rating
self.default = default
self.force_default = force_default
self.force_lower = force_lower
self.use_ssl = use_ssl
self.base_url = base_url
for key, value in kwargs.items():
if hasattr(self, key):
setattr(self, key, value)

self.app = None

Expand Down

0 comments on commit a59731b

Please sign in to comment.