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

Format() spec for deferred str.format() #127

Open
kurtbrose opened this issue Nov 27, 2019 · 2 comments
Open

Format() spec for deferred str.format() #127

kurtbrose opened this issue Nov 27, 2019 · 2 comments

Comments

@kurtbrose
Copy link
Collaborator

class _Format(object):
    def __init__(self, fmt_str):
        self.fmt_str = fmt_str
    def glomit(self, target, scope):
        return self.fmt_str.format(**target)

motivating use case: generating a SAML IDP config as needed by pysaml2:

_SAML_IDP_CONFIG = Fill({
    'debug': T['debug'],
    'xmlsec_binary': saml2.sigver.get_xmlsec_binary(['/opt/local/bin', '/usr/bin/xmlsec1']),
    'entityid': T['entity_id'],
    'description': 'EBill/Tableau IdP',
    'service': {
        'idp': {
            'name': 'Ebill IdP',
            'endpoints': {
                'single_sign_on_service': [
                    (_Format('{base_url}/sso/post/'), saml2.BINDING_HTTP_POST),
                    (_Format('{base_url}/sso/redirect/'), saml2.BINDING_HTTP_REDIRECT),
                ],
            },
            'name_id_format': [saml2.saml.NAMEID_FORMAT_EMAILADDRESS],
            'sign_response': False,
            'sign_assertion': True,
        },
    },
    'metadata': {
        'local': [T['metadata']],
    },
    # Signing
    'key_file': _Format('{cert_dir}/private.key'),
    'cert_file': _Format('{cert_dir}/public.cert'),
    # Encryption
    'encryption_keypairs': [{
        'key_file': _Format('{cert_dir}/private.key'),
        'cert_file': _Format('{cert_dir}/public.cert'),
    }],
    'valid_for': 365 * 24,
})
@kurtbrose
Copy link
Collaborator Author

maybe it could be made a BIT more general by taking a second argument of the dictionary which should be **'d into the format call (default T)

class _Format(object):
    def __init__(self, fmt_str, spec=T):
        self.fmt_str, self.spec = fmt_str, spec
    def glomit(self, target, scope):
        fmt_args = scope[glom](self.spec, target, scope)
        return self.fmt_str.format(**fmt_args)

@kurtbrose
Copy link
Collaborator Author

#131

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant