diff options
author | Aleksander Morgado <aleksander@lanedo.com> | 2012-05-19 12:34:02 +0200 |
---|---|---|
committer | Aleksander Morgado <aleksander@lanedo.com> | 2012-07-03 16:08:53 +0200 |
commit | 16f5dc0e8e777ed3a76436f1bac35ef91ed11403 (patch) | |
tree | a63f2228526109eaf7cb1ab4ef42faca2310cdc0 /build-aux | |
parent | b97d32fd322b8c6c543ede58813556d0b01f31c8 (diff) | |
download | external_libqmi-16f5dc0e8e777ed3a76436f1bac35ef91ed11403.zip external_libqmi-16f5dc0e8e777ed3a76436f1bac35ef91ed11403.tar.gz external_libqmi-16f5dc0e8e777ed3a76436f1bac35ef91ed11403.tar.bz2 |
qmi-codegen: allow specifying files with common types
Some times, e.g. the Result TLV, are the same in every message defined, so
instead of re-defining them over and over, just provide a reference to a
common predefined type.
Diffstat (limited to 'build-aux')
-rw-r--r-- | build-aux/qmi-codegen/Container.py | 49 | ||||
-rw-r--r-- | build-aux/qmi-codegen/ContainerInput.py | 4 | ||||
-rw-r--r-- | build-aux/qmi-codegen/ContainerOutput.py | 4 | ||||
-rw-r--r-- | build-aux/qmi-codegen/Message.py | 8 | ||||
-rw-r--r-- | build-aux/qmi-codegen/MessageList.py | 4 | ||||
-rwxr-xr-x | build-aux/qmi-codegen/qmi-codegen | 23 |
6 files changed, 61 insertions, 31 deletions
diff --git a/build-aux/qmi-codegen/Container.py b/build-aux/qmi-codegen/Container.py index 2c4745f..b43bfa8 100644 --- a/build-aux/qmi-codegen/Container.py +++ b/build-aux/qmi-codegen/Container.py @@ -32,7 +32,7 @@ class Container: Output fields """ - def __init__(self, prefix, dictionary): + def __init__(self, prefix, dictionary, common_objects_dictionary): # The field container prefix usually contains the name of the Message, # e.g. "Qmi Message Ctl Something" self.prefix = prefix @@ -48,23 +48,40 @@ class Container: self.fields = None if dictionary is not None: self.fields = [] + + # First, look for references to common types + for field_dictionary in dictionary: + if field_dictionary['type'] == 'common-TLV': + for common in common_objects_dictionary: + if common['name'] == field_dictionary['name'] and \ + common['type'] == 'TLV': + # Replace the reference with the actual common dictionary + dictionary.remove(field_dictionary) + dictionary.append(common) + break + else: + raise RuntimeError('Common type \'%s\' not found' % field_dictionary['name']) + + + # Then, really parse each field for field_dictionary in dictionary: - if field_dictionary['format'] == 'array': - self.fields.append(FieldArray(self.fullname, field_dictionary)) - elif field_dictionary['format'] == 'struct': - if field_dictionary['name'] == 'Result': - self.fields.append(FieldStructResult(self.fullname, field_dictionary)) + if field_dictionary['type'] == 'TLV': + if field_dictionary['format'] == 'array': + self.fields.append(FieldArray(self.fullname, field_dictionary)) + elif field_dictionary['format'] == 'struct': + if field_dictionary['name'] == 'Result': + self.fields.append(FieldStructResult(self.fullname, field_dictionary)) + else: + self.fields.append(FieldStruct(self.fullname, field_dictionary)) + elif field_dictionary['format'] == 'guint8' or \ + field_dictionary['format'] == 'guint16' or \ + field_dictionary['format'] == 'guint32' or \ + field_dictionary['format'] == 'gint8' or \ + field_dictionary['format'] == 'gint16' or \ + field_dictionary['format'] == 'gint32': + self.fields.append(FieldBasic(self.fullname, field_dictionary)) else: - self.fields.append(FieldStruct(self.fullname, field_dictionary)) - elif field_dictionary['format'] == 'guint8' or \ - field_dictionary['format'] == 'guint16' or \ - field_dictionary['format'] == 'guint32' or \ - field_dictionary['format'] == 'gint8' or \ - field_dictionary['format'] == 'gint16' or \ - field_dictionary['format'] == 'gint32': - self.fields.append(FieldBasic(self.fullname, field_dictionary)) - else: - raise ValueError('Cannot handle type \'%s\'' % field_dictionary['type']) + raise ValueError('Cannot handle TLV format \'%s\'' % field_dictionary['format']) def __emit_tlv_ids_enum(self, f): diff --git a/build-aux/qmi-codegen/ContainerInput.py b/build-aux/qmi-codegen/ContainerInput.py index 2d5c34f..7c15306 100644 --- a/build-aux/qmi-codegen/ContainerInput.py +++ b/build-aux/qmi-codegen/ContainerInput.py @@ -29,10 +29,10 @@ class ContainerInput(Container): fields """ - def __init__(self, prefix, dictionary): + def __init__(self, prefix, dictionary, common_objects_dictionary): self.name = 'Input' self.readonly = False # Call the parent constructor - Container.__init__(self, prefix, dictionary) + Container.__init__(self, prefix, dictionary, common_objects_dictionary) diff --git a/build-aux/qmi-codegen/ContainerOutput.py b/build-aux/qmi-codegen/ContainerOutput.py index ccfc33c..8f17bcd 100644 --- a/build-aux/qmi-codegen/ContainerOutput.py +++ b/build-aux/qmi-codegen/ContainerOutput.py @@ -29,10 +29,10 @@ class ContainerOutput(Container): fields """ - def __init__(self, prefix, dictionary): + def __init__(self, prefix, dictionary, common_objects_dictionary): self.name = 'Output' self.readonly = True # Call the parent constructor - Container.__init__(self, prefix, dictionary) + Container.__init__(self, prefix, dictionary, common_objects_dictionary) diff --git a/build-aux/qmi-codegen/Message.py b/build-aux/qmi-codegen/Message.py index 0415704..4a67b07 100644 --- a/build-aux/qmi-codegen/Message.py +++ b/build-aux/qmi-codegen/Message.py @@ -25,7 +25,7 @@ from ContainerOutput import ContainerOutput from ContainerInput import ContainerInput class Message: - def __init__(self, dictionary): + def __init__(self, dictionary, common_objects_dictionary): # The message prefix self.prefix = 'Qmi Message' # The message service, e.g. "Ctl" @@ -49,14 +49,16 @@ class Message: # will generate a new Output type and public getters for each output # field self.output = ContainerOutput(self.fullname, - dictionary['output']) + dictionary['output'], + common_objects_dictionary) # Build input container. # Every defined message will have its own input container, which # will generate a new Input type and public getters for each input # field self.input = ContainerInput(self.fullname, - dictionary['input'] if 'input' in dictionary else None) + dictionary['input'] if 'input' in dictionary else None, + common_objects_dictionary) def __emit_request_creator(self, hfile, cfile): diff --git a/build-aux/qmi-codegen/MessageList.py b/build-aux/qmi-codegen/MessageList.py index 784100b..67af168 100644 --- a/build-aux/qmi-codegen/MessageList.py +++ b/build-aux/qmi-codegen/MessageList.py @@ -24,7 +24,7 @@ from Message import Message import utils class MessageList: - def __init__(self, objects_dictionary): + def __init__(self, objects_dictionary, common_objects_dictionary): self.list = [] self.message_id_enum_name = None @@ -32,7 +32,7 @@ class MessageList: # and looking for the special 'Message-ID-Enum' type for object_dictionary in objects_dictionary: if object_dictionary['type'] == 'Message': - message = Message(object_dictionary) + message = Message(object_dictionary, common_objects_dictionary) self.list.append(message) elif object_dictionary['type'] == 'Message-ID-Enum': self.message_id_enum_name = object_dictionary['name'] diff --git a/build-aux/qmi-codegen/qmi-codegen b/build-aux/qmi-codegen/qmi-codegen index 6c5b168..4f1f83e 100755 --- a/build-aux/qmi-codegen/qmi-codegen +++ b/build-aux/qmi-codegen/qmi-codegen @@ -34,6 +34,8 @@ def codegen_main(): help='Input JSON-formatted database') arg_parser.add_option('', '--output', metavar='OUTFILES', help='Generate C code in OUTFILES.[ch]') + arg_parser.add_option('', '--include', metavar='JSONFILE', action='append', + help='Additional common types in a JSON-formatted database') (opts, args) = arg_parser.parse_args(); if opts.input == None: @@ -45,20 +47,29 @@ def codegen_main(): output_file_c = open(opts.output + ".c", 'w') output_file_h = open(opts.output + ".h", 'w') - # Load database file contents - database_file = open(opts.input) - database_file_contents = database_file.read() - database_file.close() - # Add common stuff to the output files utils.add_copyright(output_file_c); utils.add_copyright(output_file_h); utils.add_header_start(output_file_h, os.path.basename(opts.output)) utils.add_source_start(output_file_c, os.path.basename(opts.output)) + # Load database file contents + database_file = open(opts.input) + database_file_contents = database_file.read() + database_file.close() + + # Load all common types + common_object_list_json = [] + if opts.include != None: + for include in opts.include: + include_file = open(include) + include_contents = include_file.read() + common_object_list_json.extend(json.loads(include_contents)) + include_file.close() + # Get our message collection object_list_json = json.loads(database_file_contents) - message_list = MessageList(object_list_json) + message_list = MessageList(object_list_json, common_object_list_json) # Emit the message creation/parsing code message_list.emit(output_file_h, output_file_c) |