Skip to content

Instantly share code, notes, and snippets.

@richmondwang
Last active February 21, 2017 09:00
Show Gist options
  • Save richmondwang/72ea42140ba7c16a835afd22acc1042d to your computer and use it in GitHub Desktop.
Save richmondwang/72ea42140ba7c16a835afd22acc1042d to your computer and use it in GitHub Desktop.
Concat Key Marker
# -*- coding: utf-8 -*-
from voluptuous import Optional
from operator import is_not
from functools import partial
class Concat(object):
"""
Creates a concatenated Optional Marker for voluptuous
Example:
>>> from voluptuous import Schema
>>> ENV = Concat(prefix="MOD_WSGI_")
>>> schema = Schema({ENV('HOST', default='0.0.0.0'): str})
>>> schema({})
{'MOD_WSGI_HOST': '0.0.0.0'}
"""
def __init__(self, prefix = None, suffix = None, **kwargs):
self.separator = kwargs.pop('separator', "")
self.marker = kwargs.pop('marker', Optional)
self.prefix = prefix
self.suffix = suffix
def __call__(self, name, marker=None, *args, **kwargs):
marker = marker or self.marker
names = self.prefix, name, self.suffix
names = list(filter(partial(is_not, None), names))
name = self.separator.join(names)
return marker(name , *args, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment