def generate_vegalite_schema_wrapper(schema_file): """Generate a schema wrapper at the given path.""" # TODO: generate simple tests for each wrapper basename = "VegaLiteSchema" with open(schema_file, encoding="utf8") as f: rootschema = json.load(f) definitions = {} for name in rootschema["definitions"]: defschema = {"$ref": "#/definitions/" + name} defschema_repr = {"$ref": "#/definitions/" + name} name = get_valid_identifier(name) definitions[name] = SchemaGenerator( name, schema=defschema, schemarepr=defschema_repr, rootschema=rootschema, basename=basename, rootschemarepr=CodeSnippet("{}._rootschema".format(basename)), ) graph = {} for name, schema in definitions.items(): graph[name] = [] for child in schema.subclasses(): child = get_valid_identifier(child) graph[name].append(child) child = definitions[child] if child.basename == basename: child.basename = [name] else: child.basename.append(name) contents = [ HEADER, "from altair.utils.schemapi import SchemaBase, Undefined, _subclasses", LOAD_SCHEMA.format(schemafile="vega-lite-schema.json"), ] contents.append(BASE_SCHEMA.format(basename=basename)) contents.append( schema_class( "Root", schema=rootschema, basename=basename, schemarepr=CodeSnippet("{}._rootschema".format(basename)), ) ) for name in toposort(graph): contents.append(definitions[name].schema_class()) contents.append("") # end with newline return "\n".join(contents)
def generate_vega_schema_wrapper(schema_file): """Generate a schema wrapper at the given path.""" # TODO: generate simple tests for each wrapper basename = 'VegaSchema' with open(schema_file, encoding='utf8') as f: rootschema = json.load(f) contents = [ HEADER, "from altair.utils.schemapi import SchemaBase, Undefined", LOAD_SCHEMA.format(schemafile='vega-schema.json') ] contents.append(BASE_SCHEMA.format(basename=basename)) contents.append( schema_class('Root', schema=rootschema, basename=basename, schemarepr=CodeSnippet('load_schema()'))) for deflist in ['defs', 'refs']: for name in rootschema[deflist]: defschema = {'$ref': '#/{}/{}'.format(deflist, name)} defschema_repr = {'$ref': '#/{}/{}'.format(deflist, name)} contents.append( schema_class(get_valid_identifier(name), schema=defschema, schemarepr=defschema_repr, rootschema=rootschema, basename=basename, rootschemarepr=CodeSnippet("Root._schema"))) contents.append('') # end with newline return '\n'.join(contents)
def generate_vega_schema_wrapper(schema_file): """Generate a schema wrapper at the given path.""" # TODO: generate simple tests for each wrapper basename = "VegaSchema" with open(schema_file, encoding="utf8") as f: rootschema = json.load(f) contents = [ HEADER, "from altair.utils.schemapi import SchemaBase, Undefined, _subclasses", LOAD_SCHEMA.format(schemafile="vega-schema.json"), ] contents.append(BASE_SCHEMA.format(basename=basename)) contents.append( schema_class( "Root", schema=rootschema, basename=basename, schemarepr=CodeSnippet("{}._rootschema".format(basename)), )) for deflist in ["defs", "refs"]: for name in rootschema[deflist]: defschema = {"$ref": "#/{}/{}".format(deflist, name)} defschema_repr = {"$ref": "#/{}/{}".format(deflist, name)} contents.append( schema_class( get_valid_identifier(name), schema=defschema, schemarepr=defschema_repr, rootschema=rootschema, basename=basename, rootschemarepr=CodeSnippet("Root._schema"), )) contents.append("") # end with newline return "\n".join(contents)
def generate_schema_wrapper(schema_file): """Generate a schema wrapper at the given path.""" with open(schema_file) as f: rootschema = json.load(f) contents = [ "# The contents of this file are automatically generated", "# at time {0}\n".format(datetime.now().strftime('%Y-%m-%d %H:%M:%S')), "from altair.utils.schemapi import SchemaBase, Undefined", LOAD_SCHEMA ] contents.append( schema_class('Root', schema=rootschema, schemarepr=CodeSnippet('load_schema()'))) for name in rootschema['definitions']: defschema = {'$ref': '#/definitions/' + name} defschema_repr = {'$ref': '#/definitions/' + name} contents.append( schema_class(get_valid_identifier(name), schema=defschema, schemarepr=defschema_repr, rootschema=rootschema, rootschemarepr=CodeSnippet("Root._schema"))) contents.append('') # end with newline return '\n'.join(contents)
def generate_vegalite_schema_wrapper(schema_file): """Generate a schema wrapper at the given path.""" # TODO: generate simple tests for each wrapper with open(schema_file) as f: rootschema = json.load(f) contents = [ HEADER, "from altair.utils.schemapi import SchemaBase, Undefined", LOAD_SCHEMA.format(schemafile='vega-lite-schema.json') ] contents.append( schema_class('Root', schema=rootschema, schemarepr=CodeSnippet('load_schema()'))) for name in rootschema['definitions']: defschema = {'$ref': '#/definitions/' + name} defschema_repr = {'$ref': '#/definitions/' + name} contents.append( schema_class(get_valid_identifier(name), schema=defschema, schemarepr=defschema_repr, rootschema=rootschema, rootschemarepr=CodeSnippet("Root._schema"))) contents.append('') # end with newline return '\n'.join(contents)
def generate_vega_schema_wrapper(schema_file): """Generate a schema wrapper at the given path.""" # TODO: generate simple tests for each wrapper basename = 'VegaSchema' with open(schema_file, encoding='utf8') as f: rootschema = json.load(f) contents = [HEADER, "from altair.utils.schemapi import SchemaBase, Undefined", LOAD_SCHEMA.format(schemafile='vega-schema.json')] contents.append(BASE_SCHEMA.format(basename=basename)) contents.append(schema_class('Root', schema=rootschema, basename=basename, schemarepr=CodeSnippet('load_schema()'))) for deflist in ['defs', 'refs']: for name in rootschema[deflist]: defschema = {'$ref': '#/{}/{}'.format(deflist, name)} defschema_repr = {'$ref': '#/{}/{}'.format(deflist,name)} contents.append(schema_class(get_valid_identifier(name), schema=defschema, schemarepr=defschema_repr, rootschema=rootschema, basename=basename, rootschemarepr=CodeSnippet("Root._schema"))) contents.append('') # end with newline return '\n'.join(contents)
def generate_schema_wrapper(schema): schema = load_schema() contents = [ "# The contents of this file are automatically generated", "# at time {0}\n".format(datetime.now().strftime('%Y-%m-%d %H:%M:%S')), "from altair.schema.base import SchemaBase, Undefined", "from altair.schema.loader import load_schema", "" ] contents.append(schema_class('Root', schema, CodeSnippet('load_schema()'))) for name in schema['definitions']: pyname = get_valid_identifier(name) defschema = { '$ref': '#/definitions/' + name, 'definitions': schema['definitions'] } defschema_repr = { '$ref': '#/definitions/' + name, 'definitions': CodeSnippet("Root._Root__schema['definitions']") } contents.append(schema_class(pyname, defschema, defschema_repr)) contents.append('') # end with newline return '\n'.join(contents)
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(): def_dict = get_field_datum_value_defs(propschema, schema) for encoding_spec, definition in def_dict.items(): classname = prop[0].upper() + prop[1:] basename = definition.split("/")[-1] basename = get_valid_identifier(basename) defschema = {"$ref": definition} if encoding_spec == "field": 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"] elif encoding_spec == "datum": Generator = DatumSchemaGenerator classname += "Datum" nodefault = ["datum"] elif encoding_spec == "value": Generator = ValueSchemaGenerator classname += "Value" nodefault = ["value"] gen = Generator( classname=classname, basename=basename, schema=defschema, rootschema=schema, encodingname=prop, nodefault=nodefault, ) contents.append(gen.schema_class()) return "\n".join(contents)