コード例 #1
0
def generate(schema_files: List[str], outdir: str) -> None:
    schemas: Dict[str, str] = {}
    for schema_file in schema_files:
        schema = load_schema_file(schema_file)
        schemas[Path(schema_file).stem] = schema

    merged_schema = merge_schemas(list(schemas.values()))

    write_schema_files(merged_schema, outdir)
    with open(f"{outdir}/__init__.py", "w"):
        # Truncate this file.
        pass

    # Save raw schema files in codegen as well.
    schema_save_dir = Path(outdir) / "schemas"
    schema_save_dir.mkdir()
    for schema_out_file, schema in schemas.items():
        (schema_save_dir / f"{schema_out_file}.avsc").write_text(schema)

    # Add load_schema method.
    with open(schema_save_dir / "__init__.py", "a") as schema_dir_init:
        schema_dir_init.write(make_load_schema_methods(schemas.keys()))

    # Add headers for all generated files
    generated_files = Path(outdir).glob("**/*.py")
    for file in generated_files:
        suppress_checks_in_file(file)
コード例 #2
0
ファイル: avro_codegen.py プロジェクト: taufiqibrahim/datahub
def generate(schema_files: List[str], outdir: str) -> None:
    schemas: Dict[str, str] = {}
    for schema_file in schema_files:
        schema = load_schema_file(schema_file)
        schemas[Path(schema_file).stem] = schema

    merged_schema = merge_schemas(list(schemas.values()))

    write_schema_files(merged_schema, outdir)

    # Schema files post-processing.
    (Path(outdir) /
     "__init__.py").write_text("# This file is intentionally empty.\n")
    add_avro_python3_warning(Path(outdir) / "schema_classes.py")

    # Save raw schema files in codegen as well.
    schema_save_dir = Path(outdir) / "schemas"
    schema_save_dir.mkdir()
    for schema_out_file, schema in schemas.items():
        (schema_save_dir / f"{schema_out_file}.avsc").write_text(schema)

    # Add load_schema method.
    with open(schema_save_dir / "__init__.py", "a") as schema_dir_init:
        schema_dir_init.write(make_load_schema_methods(schemas.keys()))

    # Add headers for all generated files
    generated_files = Path(outdir).glob("**/*.py")
    for file in generated_files:
        suppress_checks_in_file(file)
コード例 #3
0
def generate(schema_file: str, outdir: str):
    # print(f'using {schema_file}')
    with open(schema_file) as f:
        raw_schema_text = f.read()

    no_spaces_schema = json.dumps(json.loads(raw_schema_text))
    schema_json = no_spaces_schema.replace(
        '{"type": "string", "avro.java.string": "String"}', '"string"')

    redo_spaces = json.dumps(json.loads(schema_json), indent=2)

    write_schema_files(redo_spaces, outdir)
コード例 #4
0
def generate(schema_file: str, outdir: str) -> None:
    # print(f'using {schema_file}')
    with open(schema_file) as f:
        raw_schema_text = f.read()

    no_spaces_schema = json.dumps(json.loads(raw_schema_text))
    schema_json = no_spaces_schema.replace(
        '{"type": "string", "avro.java.string": "String"}', '"string"')

    redo_spaces = json.dumps(json.loads(schema_json), indent=2)

    write_schema_files(redo_spaces, outdir)
    suppress_checks_in_file(f"{outdir}/schema_classes.py")
    suppress_checks_in_file(f"{outdir}/__init__.py")
 def generateObjects(self):
     write_schema_files(self.schemaAsJson, self.parentDirectory)
コード例 #6
0
import json
from os.path import join

import avrogen

generated_dir = "{{cookiecutter.full_destination_path}}/{{cookiecutter.schema_id}}"

schema_contents = open('{{cookiecutter.schema_filepath}}', 'r').read()
avrogen.write_schema_files(schema_contents, generated_dir)

avro_schema = avrogen.schema.make_avsc_object(json.loads(schema_contents))
classname = f"SchemaClasses.{avro_schema.fullname}Class"

with open(join(generated_dir, "schema_classes.py"), "a") as _f:
    _f.write(f"""# Stream Machine additions
from streammachine.schemas.common import StreamMachineEvent

def get_strm_schema_id(self) -> str:
    return self.strmMeta.schemaId

def get_strm_schema(self):
    return self.RECORD_SCHEMA

def get_strm_schema_type(self):
    return "avro"

setattr({classname}, "get_strm_schema_id", get_strm_schema_id)
setattr({classname}, "get_strm_schema", get_strm_schema)
setattr({classname}, "get_strm_schema_type", get_strm_schema_type)

# TODO this needs some python guru-ism
コード例 #7
0
ファイル: genPython.py プロジェクト: mutazalsallal/sf-apis
# Frederico Araujo <*****@*****.**>
# Teryl Taylor <*****@*****.**>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import sys
import glob
import os
schema_json = "....."
output_directory = "python"
from avrogen import write_schema_files

for file in glob.glob("../avsc/SysFlow.avsc"):
    with open(file, 'r') as myfile:
        schema_json = myfile.read().replace('\n', '')
    base = os.path.basename(file)
    name = os.path.splitext(base)[0].lower()
    dir = "classes/" + name
    write_schema_files(schema_json, dir)