def parse_schema_descriptor(proto_descriptor): """Parse schema descriptor and return schema object. Args: proto_descriptor: Path to a protobuf file set descriptor binary containing schema files. Returns: A schema.Schema object built from the provided schema_files. Raises: Exception: No schema files found. """ schema_obj = schema.Schema() schema_parser = nwv_parser.Parser(schema_obj) logging.debug('Parse schema descriptor.') schema_parser.add_file_set(proto_descriptor.file) return schema_obj
def setUp(cls): cls.schema = schema.Schema() cls.vendor = schema.Vendor('test', 1, '') cls.trait = schema.Trait('Test', 1, '') cls.schema.vendor_list.append(cls.vendor) cls.vendor.trait_list.append(cls.trait)
def codegen(request, response): """Generates wdl c code for devices from Jinja templates. Args: request: CodeGeneratorRequest, see google/protobuf/compiler/plugin.proto response: CodeGeneratorResponse, see google/protobuf/compiler/plugin.proto. Output files filled with generated files. Raises: Exception: Valid Jinja template directory required for wdl plugin. """ args = {} for argument in request.parameter.split(','): (key, value) = argument.split('=', 1) values = value.split(':') if len(values) == 1: args[key] = values[0] else: args[key] = values legacy_mode_enabled = ('legacy_mode_enabled' in args and args['legacy_mode_enabled'].lower() == 'true') gen_dependencies = ('gen_dependencies' in args and args['gen_dependencies'].lower() == 'true') codegen_reference_mode = ('codegen_reference_mode' in args and args['codegen_reference_mode'].lower() == 'true') if args['templates'] is None or not args['templates']: raise Exception('wdl_plugin: \'templates\' argument is empty') if isinstance(args['templates'], list): template_files = args['templates'] else: template_files = [ args['templates'], ] template_languages = { 'c': template_set.TemplateLanguage.C, 'cpp': template_set.TemplateLanguage.CPLUSPLUS, 'java': template_set.TemplateLanguage.JAVA, 'js': template_set.TemplateLanguage.JAVASCRIPT, 'md': template_set.TemplateLanguage.MARKDOWN, 'objc': template_set.TemplateLanguage.OBJECTIVEC, } if 'language' not in args or args['language'] not in template_languages: language = template_set.TemplateLanguage.BASE else: language = template_languages[args['language']] schema_obj = schema.Schema() schema_parser = nwv_parser.Parser(schema_obj) file_descs_to_gen = [ proto_file for proto_file in request.proto_file if (('semantic' not in proto_file.name) and ( 'retention' not in proto_file.name)) ] dependency_set = [] # This file needs to get added to the dependency list if we're in # codegen mode, since this file doesn't show up as a dependency by # default, but is still necessary for some code-generated targets. if (codegen_reference_mode): dependency_set.append('google/protobuf/field_mask.proto') for proto_file in file_descs_to_gen: dependency_set.append(proto_file.name) schema_parser.add_file_set(file_descs_to_gen) gwvc.check(schema_obj) # Add two spaces to each log messages to make it line up with our output. template_set.log = lambda *a: print(' ', *a) templates = template_set.TemplateSet( template_files, legacy_mode_enabled=legacy_mode_enabled, language=language) if gen_dependencies: templates.codegen(schema_obj, None, dependency_set) else: templates.codegen(schema_obj, None, request.file_to_generate) for filename, content in templates.output_files: out_file = response.file.add() out_file.name = filename # The newline was added in the legacy template_set file writer, # so it's included here to preserve compatibility. out_file.content = content.encode('utf-8') + '\n'
def gen_test_schema(self): schema_obj = schema.Schema() vendor = schema.Vendor('test', 0xfe00, '') schema_obj.vendor_list.append(vendor) resource = schema.Resource('TestResource', 0x0001, '') resource.file = schema.File('test_resource', 1, '') resource.version = 2 vendor.resource_list.append(resource) iface = schema.Interface('TestIface', 0x0001, '') iface.file = schema.File('test_iface', 1, '') vendor.interface_list.append(iface) trait = schema.Trait('TestTrait', 1, '') trait.file = schema.File('test_trait', 1, '') vendor.trait_list.append(trait) typespace = schema.Typespace('TestTypespace', 1, '') typespace.file = schema.File('test_typespace', 1, '') vendor.typespace_list.append(typespace) trait.version = 2 trait.stability = schema.Stability.ALPHA field = schema.Field('test_field', 1, '', schema.Field.DataType.INT32, None) trait.state_list.append(field) resource_component = schema.ResourceComponent('test_component', 1, '') resource_component.trait = trait resource.component_list.append(resource_component) interface_component = schema.InterfaceComponent( 'test_component', 1, '') interface_component.trait = trait iface.component_list.append(interface_component) group = schema.Group('test_group', 1, '') group.interface = iface resource.group_list.append(group) struct = schema.Struct('TestStruct', 1, '') trait.struct_list.append(struct) enum = schema.Enum('TestEnum', 1, '') enum.pair_list.append(schema.EnumPair('TEST_ENUM_UNSPECIFIED', 0, '')) enum.pair_list.append(schema.EnumPair('TEST_ENUM_ONE', 1, '')) enum.pair_list.append(schema.EnumPair('TEST_ENUM_TWO', 2, '')) trait.enum_list.append(enum) event = schema.Event('TestEvent', 1, '') event.field_list.append(field) trait.event_list.append(event) response = schema.CommandResponse('TestRequestResponse', 1, '') response.field_list.append(field) command = schema.Command('TestRequest', 1, '') command.parameter_list.append(field) trait.command_list.append(command) command.response = response response.parent = command return schema_obj