From fc2eedc5be15eee4a3676e706d963b50c3ab4499 Mon Sep 17 00:00:00 2001 From: Changaco Date: Fri, 9 Sep 2016 16:13:35 +0200 Subject: [PATCH 1/3] stop reading config from the OS environment --- aspen/configuration/__init__.py | 9 +++++---- aspen/request_processor/__init__.py | 24 +++++++++++++----------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/aspen/configuration/__init__.py b/aspen/configuration/__init__.py index 5eb71a68..ab66f1d2 100644 --- a/aspen/configuration/__init__.py +++ b/aspen/configuration/__init__.py @@ -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) diff --git a/aspen/request_processor/__init__.py b/aspen/request_processor/__init__.py index ae360afb..0ba687e0 100644 --- a/aspen/request_processor/__init__.py +++ b/aspen/request_processor/__init__.py @@ -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) @@ -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. @@ -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) @@ -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) From 4f3ca0145f6eaed99fdfcb7b401c3b18e74808a0 Mon Sep 17 00:00:00 2001 From: Changaco Date: Fri, 9 Sep 2016 16:14:57 +0200 Subject: [PATCH 2/3] prune `__main__.py` Aspen isn't a WSGI app --- aspen/__main__.py | 64 ----------------------------------------------- 1 file changed, 64 deletions(-) delete mode 100644 aspen/__main__.py diff --git a/aspen/__main__.py b/aspen/__main__.py deleted file mode 100644 index aa8c791b..00000000 --- a/aspen/__main__.py +++ /dev/null @@ -1,64 +0,0 @@ -""" -python -m aspen -=============== - -Aspen ships with a server (wsgiref.simple_server) that is -suitable for development and testing. It can be invoked via: - - python -m aspen - -though even for development you'll likely want to specify a -project root, so a more likely incantation is: - - ASPEN_PROJECT_ROOT=/path/to/wherever python -m aspen - -For production deployment, you should probably deploy using a higher -performance WSGI server like Gunicorn, uwsgi, Spawning, or the like. - -Also, you'll likely want to configure logging your own way, and -pass more configuration options to the Website() constructor. - -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function -from __future__ import unicode_literals - -import os -import logging.config -from wsgiref.simple_server import make_server - -from . import website -from .logging import log_dammit - - -logging_cfg = { - 'version': 1, - 'formatters': { - 'threadinfo': { - 'format': "%(asctime)s pid-%(process)d thread-%(thread)d (%(threadName)s) %(levelname)s: %(message)s" - } - }, - 'handlers': { - 'console': { - 'class': 'logging.StreamHandler', - 'formatter': 'threadinfo', - 'level': 'INFO', - 'stream': 'ext://sys.stderr' - } - }, - 'root': { - 'handlers': [ 'console' ] - } -} - - -if __name__ == '__main__': - logging.config.dictConfig(logging_cfg) - port = int(os.environ.get('PORT', '8080')) # get the port, defaulting to 8080 - host = os.environ.get('ASPEN_HOST', '0.0.0.0') # get the IP to bind to, or default to all - project_root = os.environ.get('ASPEN_PROJECT_ROOT', None) - log_dammit("Greetings, program! Now serving on http://{0}:{1}/.".format(host, port)) - website = website.Website(project_root=project_root) - make_server(host, port, website).serve_forever() - From e185bf43e8392d2990cb44417aaddf28e7a4ec95 Mon Sep 17 00:00:00 2001 From: Changaco Date: Fri, 9 Sep 2016 17:06:53 +0200 Subject: [PATCH 3/3] rename `charset_dynamic` to `encode_output_as` per https://github.com/AspenWeb/aspen.py/pull/9#discussion-diff-67594324 --- aspen/request_processor/__init__.py | 2 +- aspen/request_processor/algorithm.py | 2 +- tests/test_configuration.py | 4 ++-- tests/test_resources.py | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/aspen/request_processor/__init__.py b/aspen/request_processor/__init__.py index 0ba687e0..5bc36e8b 100644 --- a/aspen/request_processor/__init__.py +++ b/aspen/request_processor/__init__.py @@ -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) diff --git a/aspen/request_processor/algorithm.py b/aspen/request_processor/algorithm.py index 51a18dad..c3f9140a 100644 --- a/aspen/request_processor/algorithm.py +++ b/aspen/request_processor/algorithm.py @@ -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) diff --git a/tests/test_configuration.py b/tests/test_configuration.py index 8ff5ff51..457be246 100644 --- a/tests/test_configuration.py +++ b/tests/test_configuration.py @@ -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' ) diff --git a/tests/test_resources.py b/tests/test_resources.py index 78f6eff7..ef9e0312 100644 --- a/tests/test_resources.py +++ b/tests/test_resources.py @@ -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'