def generate_vegalite_channel_wrappers(schemafile, version, imports=None):
    # TODO: generate __all__ for top of file
    with open(schemafile, encoding='utf8') as f:
        schema = json.load(f)
    if imports is None:
        imports = ["import six",
                   "from . import core",
                   "import pandas as pd",
                   "from altair.utils.schemapi import Undefined",
                   "from altair.utils import parse_shorthand"]
    contents = [HEADER]
    contents.extend(imports)
    contents.append('')

    contents.append(CHANNEL_MIXINS)

    if version == 'v1':
        encoding_def = 'Encoding'
    elif version == 'v2':
        encoding_def = 'EncodingWithFacet'
    else:
        encoding_def = 'FacetedEncoding'

    encoding = SchemaInfo(schema['definitions'][encoding_def],
                          rootschema=schema)

    for prop, propschema in encoding.properties.items():
        if propschema.is_reference():
            definitions = [propschema.ref]
        elif propschema.is_anyOf():
            definitions = [s.ref for s in propschema.anyOf if s.is_reference()]
        else:
            raise ValueError("either $ref or anyOf expected")
        for definition in definitions:
            defschema = {'$ref': definition}
            basename = definition.split('/')[-1]
            classname = prop.title()

            if 'Value' in basename:
                Generator = ValueSchemaGenerator
                classname += 'Value'
                nodefault = ['value']
            else:
                Generator = FieldSchemaGenerator
                nodefault = []
                defschema = copy.deepcopy(resolve_references(defschema, schema))

                # For Encoding field definitions, we patch the schema by adding the
                # shorthand property.
                defschema['properties']['shorthand'] = {'type': 'string',
                                                        'description': 'shorthand for field, aggregate, and type'}
                defschema['required'] = ['shorthand']

            gen = Generator(classname=classname, basename=basename,
                            schema=defschema, rootschema=schema,
                            nodefault=nodefault)
            contents.append(gen.schema_class())
    return '\n'.join(contents)
Exemple #2
0
def recursive_dict_update(schema, root, def_dict):
    if "$ref" in schema:
        next_schema = resolve_references(schema, root)
        if "properties" in next_schema:
            definition = schema["$ref"]
            properties = next_schema["properties"]
            for k in def_dict.keys():
                if k in properties:
                    def_dict[k] = definition
        else:
            recursive_dict_update(next_schema, root, def_dict)
    elif "anyOf" in schema:
        for sub_schema in schema["anyOf"]:
            recursive_dict_update(sub_schema, root, def_dict)
def generate_vegalite_channel_wrappers(schemafile, version, imports=None):
    # TODO: generate __all__ for top of file
    with open(schemafile, encoding="utf8") as f:
        schema = json.load(f)
    if imports is None:
        imports = [
            "from . import core",
            "import pandas as pd",
            "from altair.utils.schemapi import Undefined",
            "from altair.utils import parse_shorthand",
        ]
    contents = [HEADER]
    contents.extend(imports)
    contents.append("")

    contents.append(CHANNEL_MIXINS)

    if version == "v2":
        encoding_def = "EncodingWithFacet"
    else:
        encoding_def = "FacetedEncoding"

    encoding = SchemaInfo(schema["definitions"][encoding_def],
                          rootschema=schema)

    for prop, propschema in encoding.properties.items():
        if propschema.is_reference():
            definitions = [propschema.ref]
        elif propschema.is_anyOf():
            definitions = [s.ref for s in propschema.anyOf if s.is_reference()]
        else:
            raise ValueError("either $ref or anyOf expected")
        for definition in definitions:
            defschema = {"$ref": definition}
            basename = definition.split("/")[-1]
            classname = prop[0].upper() + prop[1:]

            if "Value" in basename:
                Generator = ValueSchemaGenerator
                classname += "Value"
                nodefault = ["value"]
            else:
                Generator = FieldSchemaGenerator
                nodefault = []
                defschema = copy.deepcopy(resolve_references(
                    defschema, schema))

                # For Encoding field definitions, we patch the schema by adding the
                # shorthand property.
                defschema["properties"]["shorthand"] = {
                    "type": "string",
                    "description": "shorthand for field, aggregate, and type",
                }
                defschema["required"] = ["shorthand"]

            gen = Generator(
                classname=classname,
                basename=basename,
                schema=defschema,
                rootschema=schema,
                encodingname=prop,
                nodefault=nodefault,
            )
            contents.append(gen.schema_class())
    return "\n".join(contents)