Beispiel #1
0
def generate_vegalite_mark_mixin(schemafile, markdefs):
    with open(schemafile, encoding="utf8") as f:
        schema = json.load(f)

    imports = [
        "from altair.utils.schemapi import Undefined", "from . import core"
    ]

    code = [
        "class MarkMethodMixin(object):",
        '    """A mixin class that defines mark methods"""',
    ]

    for mark_enum, mark_def in markdefs.items():
        if "enum" in schema["definitions"][mark_enum]:
            marks = schema["definitions"][mark_enum]["enum"]
        else:
            marks = [schema["definitions"][mark_enum]["const"]]
        info = SchemaInfo({"$ref": "#/definitions/" + mark_def},
                          rootschema=schema)

        # adapted from SchemaInfo.init_code
        nonkeyword, required, kwds, invalid_kwds, additional = codegen._get_args(
            info)
        required -= {"type"}
        kwds -= {"type"}

        def_args = ["self"] + [
            "{}=Undefined".format(p) for p in (sorted(required) + sorted(kwds))
        ]
        dict_args = [
            "{0}={0}".format(p) for p in (sorted(required) + sorted(kwds))
        ]

        if additional or invalid_kwds:
            def_args.append("**kwds")
            dict_args.append("**kwds")

        for mark in marks:
            # TODO: only include args relevant to given type?
            mark_method = MARK_METHOD.format(
                mark=mark,
                mark_def=mark_def,
                def_arglist=indent_arglist(def_args,
                                           indent_level=10 + len(mark)),
                dict_arglist=indent_arglist(dict_args, indent_level=16),
            )
            code.append("\n    ".join(mark_method.splitlines()))

    return imports, "\n".join(code)
def generate_vegalite_mark_mixin(schemafile, markdefs):
    with open(schemafile, encoding='utf8') as f:
        schema = json.load(f)

    imports = [
        "from altair.utils.schemapi import Undefined", "from . import core"
    ]

    code = [
        "class MarkMethodMixin(object):",
        '    """A mixin class that defines mark methods"""'
    ]

    for mark_enum, mark_def in markdefs.items():
        marks = schema['definitions'][mark_enum]['enum']
        info = SchemaInfo({'$ref': '#/definitions/' + mark_def},
                          rootschema=schema)

        # adapted from SchemaInfo.init_code
        nonkeyword, required, kwds, invalid_kwds, additional = codegen._get_args(
            info)
        required -= {'type'}
        kwds -= {'type'}

        def_args = ['self'] + [
            '{}=Undefined'.format(p) for p in (sorted(required) + sorted(kwds))
        ]
        dict_args = [
            '{0}={0}'.format(p) for p in (sorted(required) + sorted(kwds))
        ]

        if additional or invalid_kwds:
            def_args.append('**kwds')
            dict_args.append('**kwds')

        for mark in marks:
            # TODO: only include args relevant to given type?
            mark_method = MARK_METHOD.format(
                mark=mark,
                mark_def=mark_def,
                def_arglist=indent_arglist(def_args,
                                           indent_level=10 + len(mark)),
                dict_arglist=indent_arglist(dict_args, indent_level=16))
            code.append('\n    '.join(mark_method.splitlines()))

    return imports, '\n'.join(code)
def generate_vegalite_mark_mixin(schemafile, mark_enum='Mark',
                                 mark_def='MarkDef'):
    with open(schemafile, encoding='utf8') as f:
        schema = json.load(f)
    marks = schema['definitions'][mark_enum]['enum']
    info = SchemaInfo({'$ref': '#/definitions/' + mark_def},
                      rootschema=schema)

    # adapted from SchemaInfo.init_code
    nonkeyword, required, kwds, invalid_kwds, additional = codegen._get_args(info)
    required -= {'type'}
    kwds -= {'type'}

    def_args = ['self'] + ['{}=Undefined'.format(p)
                           for p in (sorted(required) + sorted(kwds))]
    dict_args = ['{0}={0}'.format(p)
                 for p in (sorted(required) + sorted(kwds))]

    if additional or invalid_kwds:
        def_args.append('**kwds')
        dict_args.append('**kwds')

    imports = ["from altair.utils.schemapi import Undefined",
               "from . import core"]

    code = ["class MarkMethodMixin(object):",
            '    """A mixin class that defines mark methods"""']

    for mark in marks:
        # TODO: only include args relevant to given type?
        mark_method = MARK_METHOD.format(mark=mark,
                                         def_arglist=indent_arglist(def_args, indent_level=10 + len(mark)),
                                         dict_arglist=indent_arglist(dict_args, indent_level=16))
        code.append('\n    '.join(mark_method.splitlines()))

    return imports, '\n'.join(code)