diff options
author | Petteri Aimonen <jpa@git.mail.kapsi.fi> | 2013-03-09 14:21:21 +0200 |
---|---|---|
committer | Petteri Aimonen <jpa@git.mail.kapsi.fi> | 2013-03-09 14:21:21 +0200 |
commit | 6f8dbc73eb3e595934751f5b33c7f61a902e946a (patch) | |
tree | 763b029dac24d869e68b43d9a547297bd675d80b | |
parent | e1b8a555f30d77a3c0094cf3678666b38f4b4bd3 (diff) |
Add simple support for separate options file.
Update issue 12
Still needs documentation.
-rw-r--r-- | generator/nanopb.proto | 6 | ||||
-rw-r--r-- | generator/nanopb_generator.py | 114 |
2 files changed, 87 insertions, 33 deletions
diff --git a/generator/nanopb.proto b/generator/nanopb.proto index 40662522..7b73c7b3 100644 --- a/generator/nanopb.proto +++ b/generator/nanopb.proto @@ -16,6 +16,9 @@ enum FieldType { FT_IGNORE = 3; // Ignore the field completely. } +// This is the inner options message, which basically defines options for +// a field. When it is used in message or file scope, it applies to all +// fields. message NanoPBOptions { // Allocated size for 'bytes' and 'string' fields. optional int32 max_size = 1; @@ -33,6 +36,9 @@ message NanoPBOptions { optional bool packed_struct = 5 [default = false]; } +// Extensions to protoc 'Descriptor' type in order to define options +// inside a .proto file. +// // Protocol Buffers extension number registry // -------------------------------- // Project: Nanopb diff --git a/generator/nanopb_generator.py b/generator/nanopb_generator.py index 663745a1..0e898749 100644 --- a/generator/nanopb_generator.py +++ b/generator/nanopb_generator.py @@ -3,6 +3,7 @@ nanopb_version = "nanopb-0.2.1-dev" try: import google.protobuf.descriptor_pb2 as descriptor + import google.protobuf.text_format as text_format except: print print "*************************************************************" @@ -54,9 +55,7 @@ datatypes = { } class Names: - '''Keeps a set of nested names and formats them to C identifier. - You can subclass this with your own implementation. - ''' + '''Keeps a set of nested names and formats them to C identifier.''' def __init__(self, parts = ()): if isinstance(parts, Names): parts = parts.parts @@ -286,7 +285,7 @@ class Message: self.fields = [] for f in desc.field: - field_options = get_nanopb_suboptions(f, message_options) + field_options = get_nanopb_suboptions(f, message_options, self.name + f.name) if field_options.type != nanopb_pb2.FT_IGNORE: self.fields.append(Field(self.name, f, field_options)) @@ -383,14 +382,14 @@ def parse_file(fdesc, file_options): base_name = Names() for enum in fdesc.enum_type: - enum_options = get_nanopb_suboptions(enum, file_options) + enum_options = get_nanopb_suboptions(enum, file_options, base_name + enum.name) enums.append(Enum(base_name, enum, enum_options)) for names, message in iterate_messages(fdesc, base_name): - message_options = get_nanopb_suboptions(message, file_options) + message_options = get_nanopb_suboptions(message, file_options, names) messages.append(Message(names, message, message_options)) for enum in message.enum_type: - enum_options = get_nanopb_suboptions(enum, message_options) + enum_options = get_nanopb_suboptions(enum, message_options, names + enum.name) enums.append(Enum(names, enum, enum_options)) # Fix field default values where enum short names are used. @@ -575,6 +574,67 @@ def generate_source(headername, enums, messages): yield '\n' +# --------------------------------------------------------------------------- +# Options parsing for the .proto files +# --------------------------------------------------------------------------- + +from fnmatch import fnmatch + +def read_options_file(infile): + '''Parse a separate options file to list: + [(namemask, options), ...] + ''' + results = [] + for line in infile: + line = line.strip() + if not line or line.startswith('//') or line.startswith('#'): + continue + + parts = line.split(None, 1) + opts = nanopb_pb2.NanoPBOptions() + text_format.Merge(parts[1], opts) + results.append((parts[0], opts)) + + return results + +class Globals: + '''Ugly global variables, should find a good way to pass these.''' + verbose_options = False + separate_options = [] + +def get_nanopb_suboptions(subdesc, options, name): + '''Get copy of options, and merge information from subdesc.''' + new_options = nanopb_pb2.NanoPBOptions() + new_options.CopyFrom(options) + + # Handle options defined in a separate file + dotname = '.'.join(name.parts) + for namemask, options in Globals.separate_options: + if fnmatch(dotname, namemask): + new_options.MergeFrom(options) + + # Handle options defined in .proto + if isinstance(subdesc.options, descriptor.FieldOptions): + ext_type = nanopb_pb2.nanopb + elif isinstance(subdesc.options, descriptor.FileOptions): + ext_type = nanopb_pb2.nanopb_fileopt + elif isinstance(subdesc.options, descriptor.MessageOptions): + ext_type = nanopb_pb2.nanopb_msgopt + elif isinstance(subdesc.options, descriptor.EnumOptions): + ext_type = nanopb_pb2.nanopb_enumopt + else: + raise Exception("Unknown options type") + + if subdesc.options.HasExtension(ext_type): + ext = subdesc.options.Extensions[ext_type] + new_options.MergeFrom(ext) + + if Globals.verbose_options: + print "Options for " + dotname + ":" + print text_format.MessageToString(new_options) + + return new_options + # --------------------------------------------------------------------------- # Command line interface @@ -583,7 +643,6 @@ def generate_source(headername, enums, messages): import sys import os.path from optparse import OptionParser -import google.protobuf.text_format as text_format optparser = OptionParser( usage = "Usage: nanopb_generator.py [options] file.pb ...", @@ -593,6 +652,8 @@ optparser.add_option("-x", dest="exclude", metavar="FILE", action="append", defa help="Exclude file from generated #include list.") optparser.add_option("-e", "--extension", dest="extension", metavar="EXTENSION", default="pb", help="Set extension to use instead of 'pb' for generated files. [default: %default]") +optparser.add_option("-f", "--options-file", dest="options_file", metavar="FILE", default="%s.options", + help="Set name of a separate generator options file.") optparser.add_option("-Q", "--generated-include-format", dest="genformat", metavar="FORMAT", default='#include "%s"\n', help="Set format string to use for including other .pb.h files. [default: %default]") @@ -606,28 +667,6 @@ optparser.add_option("-v", "--verbose", dest="verbose", action="store_true", def optparser.add_option("-s", dest="settings", metavar="OPTION:VALUE", action="append", default=[], help="Set generator option (max_size, max_count etc.).") -def get_nanopb_suboptions(subdesc, options): - '''Get copy of options, and merge information from subdesc.''' - new_options = nanopb_pb2.NanoPBOptions() - new_options.CopyFrom(options) - - if isinstance(subdesc.options, descriptor.FieldOptions): - ext_type = nanopb_pb2.nanopb - elif isinstance(subdesc.options, descriptor.FileOptions): - ext_type = nanopb_pb2.nanopb_fileopt - elif isinstance(subdesc.options, descriptor.MessageOptions): - ext_type = nanopb_pb2.nanopb_msgopt - elif isinstance(subdesc.options, descriptor.EnumOptions): - ext_type = nanopb_pb2.nanopb_enumopt - else: - raise Exception("Unknown options type") - - if subdesc.options.HasExtension(ext_type): - ext = subdesc.options.Extensions[ext_type] - new_options.MergeFrom(ext) - - return new_options - def process(filenames, options): '''Process the files given on the command line.''' @@ -637,6 +676,8 @@ def process(filenames, options): if options.quiet: options.verbose = False + + Globals.verbose_options = options.verbose toplevel_options = nanopb_pb2.NanoPBOptions() for s in options.settings: @@ -646,12 +687,19 @@ def process(filenames, options): data = open(filename, 'rb').read() fdesc = descriptor.FileDescriptorSet.FromString(data) - file_options = get_nanopb_suboptions(fdesc.file[0], toplevel_options) + # Check if any separate options are specified + optfilename = options.options_file % os.path.splitext(filename)[0] if options.verbose: - print "Options for " + filename + ":" - print text_format.MessageToString(file_options) + print 'Reading options from ' + optfilename + + if os.path.isfile(optfilename): + Globals.separate_options = read_options_file(open(optfilename, "rU")) + else: + Globals.separate_options = [] + # Parse the file + file_options = get_nanopb_suboptions(fdesc.file[0], toplevel_options, Names([filename])) enums, messages = parse_file(fdesc.file[0], file_options) noext = os.path.splitext(filename)[0] |