Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revamp configuration #31

Merged
merged 3 commits into from
Sep 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 0 additions & 64 deletions aspen/__main__.py

This file was deleted.

9 changes: 5 additions & 4 deletions aspen/configuration/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ def update(value, extend):
d[name] = value

# get from the environment
envvar = env_prefix + name.upper()
raw = os.environ.get(envvar, '').strip()
if raw:
update(*parse_conf_var(raw, func, 'environment', envvar))
if env_prefix:
envvar = env_prefix + name.upper()
raw = os.environ.get(envvar, '').strip()
if raw:
update(*parse_conf_var(raw, func, 'environment', envvar))

# get from kwargs
raw = kwargs.get(name)
Expand Down
26 changes: 14 additions & 12 deletions aspen/request_processor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
# 'name': (default, from_unicode)
KNOBS = \
{ 'changes_reload': (False, parse.yes_no)
, 'charset_dynamic': ('UTF-8', parse.codec)
, 'charset_static': (None, parse.codec)
, 'encode_output_as': ('UTF-8', parse.codec)
, 'indices': (default_indices, parse.list_)
, 'media_type_default': ('text/plain', parse.media_type)
, 'media_type_json': ('application/json', parse.media_type)
Expand All @@ -44,6 +44,8 @@ class RequestProcessor(object):

def __init__(self, **kwargs):
"""Takes configuration in kwargs.

See the `KNOBS` global variable for valid keys and default values.
"""
self.algorithm = Algorithm.from_dotted_name('aspen.request_processor.algorithm')
self.configure(**kwargs)
Expand Down Expand Up @@ -81,10 +83,10 @@ def configure(self, **kwargs):
self.typecasters = default_typecasters


# Configure from defaults, environment, and kwargs.
# =================================================
# Configure from defaults and kwargs.
# ===================================

configure(KNOBS, self.__dict__, 'ASPEN_', kwargs)
configure(KNOBS, self.__dict__, None, kwargs)


# Set some attributes.
Expand All @@ -109,10 +111,10 @@ def safe_getcwd(errorstr):
if self.project_root is not None:
# canonicalize it
if not os.path.isabs(self.project_root):
cwd = safe_getcwd("Could not get a current working "
"directory. You can specify "
"ASPEN_PROJECT_ROOT in the environment, "
"or project_root in kwargs.")
cwd = safe_getcwd(
"Could not get a current working directory. You can specify "
"project_root in kwargs."
)
self.project_root = os.path.join(cwd, self.project_root)

self.project_root = os.path.realpath(self.project_root)
Expand All @@ -126,10 +128,10 @@ def safe_getcwd(errorstr):

# www_root
if self.www_root is None:
self.www_root = safe_getcwd("Could not get a current working "
"directory. You can specify "
"ASPEN_WWW_ROOT in the environment, "
"or www_root in kwargs.")
self.www_root = safe_getcwd(
"Could not get a current working directory. You can specify "
"www_root in kwargs."
)

self.www_root = os.path.realpath(self.www_root)

Expand Down
2 changes: 1 addition & 1 deletion aspen/request_processor/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,5 @@ def render_resource(state, resource):

def encode_output(output, request_processor):
if not isinstance(output.body, bytes):
output.charset = request_processor.charset_dynamic
output.charset = request_processor.encode_output_as
output.body = output.body.encode(output.charset)
4 changes: 2 additions & 2 deletions tests/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ def test_defaults_to_defaults(harness):
, rp.www_root

, rp.changes_reload
, rp.charset_dynamic
, rp.charset_static
, rp.encode_output_as
, rp.indices
, rp.media_type_default
, rp.media_type_json
, rp.renderer_default
)
expected = ( None, os.getcwd(), False, 'UTF-8', None
expected = ( None, os.getcwd(), False, None, 'UTF-8'
, ['index.html', 'index.json', 'index', 'index.html.spt', 'index.json.spt', 'index.spt']
, 'text/plain', 'application/json', 'stdlib_percent'
)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ def test_charset_static_None(harness):
assert output.media_type == 'text/html'
assert output.charset is None

def test_charset_dynamic_barely_working(harness):
def test_encode_output_as_barely_working(harness):
output = harness.simple( '[---]\n[---]\nGreetings, program!'
, 'index.html.spt'
, request_processor_configuration={'charset_dynamic': 'ascii'}
, request_processor_configuration={'encode_output_as': 'ascii'}
)
assert output.media_type == 'text/html'
assert output.charset == 'ascii'
Expand Down