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

propagate configuration from paste.ini #156

Merged
merged 3 commits into from Feb 21, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 36 additions & 5 deletions pypiserver/__init__.py
Expand Up @@ -139,10 +139,27 @@ def str2bool(s, default):

def paste_app_factory(global_config, **local_conf):
import os
# let's get unicode.strip in Python 2 or str.strip in Python 3:
str_strip = type(u"").strip

def upd_conf_with_bool_item(conf, attr, sdict):
conf[attr] = str2bool(sdict.pop(attr, None), conf[attr])

def upd_conf_with_str_item(conf, attr, sdict):
value = sdict.pop(attr, None)
if value is not None:
conf[attr] = value

def upd_conf_with_int_item(conf, attr, sdict):
value = sdict.pop(attr, None)
if value is not None:
conf[attr] = int(value)

def upd_conf_with_list_item(conf, attr, sdict, sep=' ', parse=str_strip):
values = sdict.pop(attr, None)
if values:
conf[attr] = list(filter(None, map(parse, values.split(sep))))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: use list comprehension here, as it is easier to read:

conf[attr] = [parse(v) for v in values.split(sep) if v]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is easier to read but your list comprehension is not exactly equivalent to the code it replaces.
To make it equivalent, we must call parse() twice for each value:

conf[attr] = [parse(v) for v in values.split(sep) if parse(v)]

That's why I opted for the list/map/filter combination.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Please let me know if you prefer the list comprehension with the two parse() calls)


def _make_root(root):
root = root.strip()
if root.startswith("~"):
Expand All @@ -151,12 +168,26 @@ def _make_root(root):

c = default_config()

root = local_conf.get("root")
if root:
c['root'] = [_make_root(x) for x in root.split("\n") if x.strip()]

upd_conf_with_bool_item(c, 'redirect_to_fallback', local_conf)
upd_conf_with_bool_item(c, 'overwrite', local_conf)
upd_conf_with_bool_item(c, 'redirect_to_fallback', local_conf)
upd_conf_with_list_item(c, 'authenticated', local_conf, sep=' ')
upd_conf_with_list_item(c, 'root', local_conf, sep='\n', parse=_make_root)
upd_conf_with_int_item(c, 'verbosity', local_conf)
str_items = [
'fallback_url',
'hash_algo',
'log_err_frmt',
'log_file',
'log_frmt',
'log_req_frmt',
'log_res_frmt',
'password_file',
'welcome_file'
]
for str_item in str_items:
upd_conf_with_str_item(c, str_item, local_conf)
# cache_control is undocumented; don't know what type is expected:
# upd_conf_with_str_item(c, 'cache_control', local_conf)

return app(**c)

Expand Down