Esempio n. 1
0
    def __new__(cls, name, bases, namespace):
        base_variables = {}
        base_bindings = {}
        for base in bases:
            base_variables.update(getattr(base, "variables", {}))
            base_bindings.update(getattr(base, "bindings", {}))

        variables = []
        bindings = []
        for key in list(namespace.keys()):
            value = namespace[key]
            if isinstance(value, Binding):
                dest_list = bindings
            elif isinstance(value, Variable):
                dest_list = variables
            else:
                dest_list = None

            if dest_list is not None:
                dest_list.append((key, value))
                del namespace[key]

        namespace.setdefault("variables", {}).update(base_variables)
        namespace.setdefault("variables", {}).update(variables)
        namespace.setdefault("bindings", {}).update(base_bindings)
        namespace.setdefault("bindings", {}).update(bindings)

        # Figure out some sane defaults
        if "identifier" not in namespace:
            namespace["identifier"] = snake_case(camel_case_to_spaces(name))
        if namespace.get("identifier") and not namespace.get("name"):
            namespace["name"] = space_case(namespace["identifier"]).title()

        return type.__new__(cls, name, bases, namespace)
Esempio n. 2
0
    def __new__(cls, name, bases, namespace):
        variables = []
        bindings = []
        for key in list(namespace.keys()):
            value = namespace[key]
            if isinstance(value, Binding):
                dest_list = bindings
            elif isinstance(value, Variable):
                dest_list = variables
            else:
                dest_list = None

            if dest_list is not None:
                dest_list.append((key, value))
                del namespace[key]

        namespace.setdefault("variables", {}).update(variables)
        namespace.setdefault("bindings", {}).update(bindings)

        # Figure out some sane defaults
        if "identifier" not in namespace:
            namespace["identifier"] = snake_case(camel_case_to_spaces(name))
        if namespace.get("identifier") and not namespace.get("name"):
            namespace["name"] = space_case(namespace["identifier"]).title()

        return type.__new__(cls, name, bases, namespace)
Esempio n. 3
0
def test_casers():
    text = u"What is Love? Baby Don't Hurt Me"
    assert snake_case(text) == "what_is_love?_baby_don't_hurt_me"
    assert kebab_case(text) == "what-is-love?-baby-don't-hurt-me"
    assert camel_case(text) == "WhatIsLove?BabyDon'THurtMe"
    assert identifierify(snake_case(text)) == "what_is_love_baby_dont_hurt_me"
    assert identifierify(kebab_case(text), "-") == "what-is-love-baby-dont-hurt-me"
    assert identifierify(camel_case(text)) == "WhatIsLoveBabyDonTHurtMe"
    assert space_case("some_identifier") == "some identifier"
Esempio n. 4
0
def test_casers():
    text = u"What is Love? Baby Don't Hurt Me"
    assert snake_case(text) == "what_is_love?_baby_don't_hurt_me"
    assert kebab_case(text) == "what-is-love?-baby-don't-hurt-me"
    assert camel_case(text) == "WhatIsLove?BabyDon'THurtMe"
    assert identifierify(snake_case(text)) == "what_is_love_baby_dont_hurt_me"
    assert identifierify(kebab_case(text), "-") == "what-is-love-baby-dont-hurt-me"
    assert identifierify(camel_case(text)) == "WhatIsLoveBabyDonTHurtMe"
    assert space_case("some_identifier") == "some identifier"
Esempio n. 5
0
def templated_plugin_factory(identifier, template_name, **kwargs):
    """
    A factory (akin to `modelform_factory`) to quickly create simple plugins.

    :param identifier: The unique identifier for the new plugin.
    :type identifier: str
    :param template_name: The template file path this plugin should render
    :type template_name: str
    :param kwargs: Other arguments for the `TemplatedPlugin`/`Plugin` classes.
    :type kwargs: dict
    :return: New `TemplatedPlugin` subclass
    :rtype: class[TemplatedPlugin]
    """
    ns = {
        "identifier": identifier,
        "template_name": template_name,
    }
    ns.update(kwargs)
    ns.setdefault("name", space_case(identifier).title())
    return type(str("%sPlugin" % identifier), (TemplatedPlugin,), ns)
Esempio n. 6
0
def templated_plugin_factory(identifier, template_name, **kwargs):
    """
    A factory (akin to `modelform_factory`) to quickly create simple plugins.

    :param identifier: The unique identifier for the new plugin.
    :type identifier: str
    :param template_name: The template file path this plugin should render
    :type template_name: str
    :param kwargs: Other arguments for the `TemplatedPlugin`/`Plugin` classes.
    :type kwargs: dict
    :return: New `TemplatedPlugin` subclass
    :rtype: class[TemplatedPlugin]
    """
    ns = {
        "identifier": identifier,
        "template_name": template_name,
    }
    ns.update(kwargs)
    ns.setdefault("name", space_case(identifier).title())
    return type(str("%sPlugin" % identifier), (TemplatedPlugin, ), ns)