diff --git a/generator/sbpg/generator.py b/generator/sbpg/generator.py index e5da3aa883..6f86715a5e 100755 --- a/generator/sbpg/generator.py +++ b/generator/sbpg/generator.py @@ -18,16 +18,6 @@ import os import pprint import sbpg.specs.yaml2 as yaml -import sbpg.targets.c as c -import sbpg.targets.test_c as test_c -import sbpg.targets.java as java -import sbpg.targets.latex as tex -import sbpg.targets.haskell as hs -import sbpg.targets.protobuf as pb -import sbpg.targets.python as py -import sbpg.targets.pythonNG as pyNG -import sbpg.targets.javascript as js -import sbpg.targets.rust as rs def get_args(): parser = argparse.ArgumentParser(description='Swift Navigation SBP generator.') @@ -89,15 +79,15 @@ def main(): args = get_args().parse_args() verbose = args.verbose assert args.pythonNG or args.python or args.javascript or args.c or args.test_c or args.haskell or args.latex or args.protobuf or args.java or args.rust, \ - "Please specify a target language." + "Please specify a target language." input_file = os.path.abspath(args.input_file[0]) assert len(args.input_file) == 1 assert os.path.exists(input_file), \ - "Invalid input file: %s. Exiting!" % input_file + "Invalid input file: %s. Exiting!" % input_file output_dir = os.path.abspath(args.output_dir[0]) assert len(args.output_dir) == 1, "Only 1 output directory at a time." assert os.path.exists(output_dir), \ - "Invalid output directory: %s. Exiting!" % output_dir + "Invalid output directory: %s. Exiting!" % output_dir # Ingest, parse, and validate. test_mode = args.test_c @@ -114,6 +104,7 @@ def main(): pprint.pprint(list(file_index.keys())) print("Writing to %s" % output_dir) if args.latex: + import sbpg.targets.latex as tex parsed = [yaml.parse_spec(spec) for spec in file_index.values()] tex.render_source(output_dir, parsed, args.release[0]) else: @@ -129,22 +120,31 @@ def main(): if not parsed.render_source: continue if args.python: + import sbpg.targets.python as py py.render_source(output_dir, parsed) if args.pythonNG: + import sbpg.targets.pythonNG as pyNG pyNG.render_source(output_dir, parsed) elif args.javascript: + import sbpg.targets.javascript as js js.render_source(output_dir, parsed) elif args.c: + import sbpg.targets.c as c c.render_source(output_dir, parsed) elif args.test_c: + import sbpg.targets.test_c as test_c test_c.render_source(output_dir, parsed) elif args.haskell: + import sbpg.targets.haskell as hs hs.render_source(output_dir, parsed) elif args.java: + import sbpg.targets.java as java java.render_source(output_dir, parsed) elif args.rust: + import sbpg.targets.rust as rs rs.render_source(output_dir, parsed) elif args.protobuf: + import sbpg.targets.protobuf as pb pb.render_source(output_dir, parsed) if args.c: c.render_version(output_dir, args.release[0]) @@ -167,5 +167,6 @@ def main(): except KeyboardInterrupt: pass + if __name__ == '__main__': main() diff --git a/generator/sbpg/syntax.py b/generator/sbpg/syntax.py index de22424a63..b8321f93ad 100755 --- a/generator/sbpg/syntax.py +++ b/generator/sbpg/syntax.py @@ -137,6 +137,13 @@ def static(self): def __repr__(self): return fmt_repr(self) + def __lt__(self, o): + # Default Python 2 behavior according to: + # https://yz.mit.edu/wp/default-behavior-of-pythons-cmp/ + if self.__class__.__name__ == o.__class__.__name__: + return id(self) < id(o) + return self.__class__.__name__ < o.__class__.__name__ + class Struct(Definition): def __init__(self, defn): self.__dict__.update(defn.__dict__) diff --git a/generator/sbpg/targets/rust.py b/generator/sbpg/targets/rust.py index 11acb1877b..8a228ac608 100644 --- a/generator/sbpg/targets/rust.py +++ b/generator/sbpg/targets/rust.py @@ -53,7 +53,7 @@ def commentify(value): 'string': 'String'} def type_map(field): - if TYPE_MAP.has_key(field.type_id): + if field.type_id in TYPE_MAP: return TYPE_MAP[field.type_id] elif field.type_id == 'array': t = field.options['fill'].value @@ -69,7 +69,7 @@ def parse_type(field): Function to pull a type from the binary payload. """ if field.type_id == 'string': - if field.options.has_key('size'): + if 'size' in field.options: return "::parser::read_string_limit(_buf, %s)" % field.options['size'].value else: return "::parser::read_string(_buf)" @@ -84,12 +84,12 @@ def parse_type(field): # Call function to build array t = field.options['fill'].value if t in TYPE_MAP.keys(): - if field.options.has_key('size'): + if 'size' in field.options: return '::parser::read_%s_array_limit(_buf, %d)' % (t, field.options['size'].value) else: return '::parser::read_%s_array(_buf)' % t else: - if field.options.has_key('size'): + if 'size' in field.options: return '%s::parse_array_limit(_buf, %d)' % (t, field.options['size'].value) else: return '%s::parse_array(_buf)' % t diff --git a/rust/sbp/rustfmt.toml b/rust/sbp/rustfmt.toml new file mode 100644 index 0000000000..a2b6833e12 --- /dev/null +++ b/rust/sbp/rustfmt.toml @@ -0,0 +1,2 @@ +comment_width = 95 +wrap_comments = true