Skip to content

Commit

Permalink
Merge pull request #292 from mavlink/meetup
Browse files Browse the repository at this point in the history
Various improvements
  • Loading branch information
JonasVautherin authored Jun 27, 2022
2 parents 4feaa6a + bd288c4 commit 4bb3945
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 33 deletions.
42 changes: 36 additions & 6 deletions pb_plugins/protoc_gen_mavsdk/autogen.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,27 @@ class AutoGen(object):
def generate_reactive(request):

params = AutoGen.parse_parameter(request.parameter)
is_java = params["file_ext"] == "java"
name_parser_factory.set_template_path(params["template_path"])
type_info_factory.set_template_path(params["template_path"])
template_env = get_template_env(params["template_path"])

if "output_file" in params:
is_java = params["output_file"].endswith("java")
else:
is_java = params["file_ext"] == "java"

# Load initialisms
if "initialisms_file" in params:
initialisms_path = params["initialisms_file"]
else:
initialisms_path = f"{params['template_path']}/initialisms"
name_parser_factory.set_initialisms_path(initialisms_path)

# Load type conversions
if "conversions_file" in params:
conversion_path = params["conversions_file"]
else:
conversion_path = f"{params['template_path']}/type_conversions"
type_info_factory.set_conversion_path(conversion_path)

template_env = get_template_env(params["template_path"], params["lstrip_blocks"], params["trim_blocks"])

_codegen_response = plugin_pb2.CodeGeneratorResponse()

Expand Down Expand Up @@ -71,6 +88,7 @@ def generate_reactive(request):
out_file = File(plugin_name,
package,
template_env,
params['template_file'],
docs,
enums,
structs,
Expand All @@ -80,7 +98,10 @@ def generate_reactive(request):

# Fill response
f = _codegen_response.file.add()
f.name = f"{plugin_dir}/{plugin_name}.{params['file_ext']}"
if "output_file" in params:
f.name = params["output_file"]
else:
f.name = f"{plugin_dir}/{plugin_name}.{params['file_ext']}"
f.content = str(out_file)

return _codegen_response
Expand All @@ -95,7 +116,16 @@ def parse_parameter(parameter):
if len(split_param) == 2:
params_dict[split_param[0]] = split_param[1]

if 'file_ext' not in params_dict:
if 'template_file' not in params_dict:
params_dict["template_file"] = None

if 'lstrip_blocks' not in params_dict:
params_dict["lstrip_blocks"] = False

if 'trim_blocks' not in params_dict:
params_dict["trim_blocks"] = False

if 'file_ext' not in params_dict and 'output_file' not in params_dict:
raise Exception("'file_ext' option was not specified! See " +
"--[name]_out=file_ext=<value>,<other_options>:/path/to/output " +
"or --[name]_opt=file_ext=<value>,<other_options> in the protoc" +
Expand Down
7 changes: 6 additions & 1 deletion pb_plugins/protoc_gen_mavsdk/autogen_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def __init__(
plugin_name,
package,
template_env,
template_file,
docs,
enums,
structs,
Expand All @@ -18,14 +19,18 @@ def __init__(
is_server):
self._package = name_parser_factory.create(package)
self._plugin_name = name_parser_factory.create(plugin_name)
self._template = template_env.get_template("file.j2")
self._class_description = docs['class'].strip()
self._enums = enums
self._structs = structs
self._methods = methods
self._has_result = has_result
self._is_server = is_server

if template_file is None:
self._template = template_env.get_template("file.j2")
else:
self._template = template_env.get_template(template_file)

def __repr__(self):
return self._template.render(package=self._package,
plugin_name=self._plugin_name,
Expand Down
17 changes: 16 additions & 1 deletion pb_plugins/protoc_gen_mavsdk/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ def __init__(
parent_struct=None):
self._plugin_name = name_parser_factory.create(plugin_name)
self._package = name_parser_factory.create(package)
self._template = template_env.get_template("enum.j2")
try:
self._template = template_env.get_template("enum.j2")
except:
self._template = None
self._enum_description = enum_docs['description'].strip(
) if enum_docs else None
self._name = name_parser_factory.create(pb_enum.name)
Expand All @@ -38,6 +41,18 @@ def __init__(
self._values.append({'name': value_name, 'description': enum_docs['params'][value_id], 'has_prefix': has_prefix})
value_id += 1

@property
def name(self):
return self._name

@property
def values(self):
return self._values

@property
def parent_struct(self):
return self._parent_struct

def __repr__(self):
return self._template.render(plugin_name=self._plugin_name,
package=self._package,
Expand Down
65 changes: 56 additions & 9 deletions pb_plugins/protoc_gen_mavsdk/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ def __init__(
pb_method,
requests,
responses):
self._is_stream = False
self._no_return = False
self._has_result = False
self._returns = False
Expand Down Expand Up @@ -101,10 +100,6 @@ def extract_is_finite(self, pb_method):
if method_descriptor.name == "is_finite":
self._is_finite = pb_method.options.Extensions[method_descriptor]

@property
def is_stream(self):
return self._is_stream

@property
def no_return(self):
return self._no_return
Expand All @@ -129,10 +124,18 @@ def is_server(self):
def name(self):
return self._name

@property
def params(self):
return self._params

@property
def return_type_required(self):
return False

@property
def method_description(self):
return self._method_description

@staticmethod
def collect_methods(
plugin_name,
Expand Down Expand Up @@ -214,9 +217,16 @@ def __init__(
pb_method,
requests,
responses)
self._template = template_env.get_template("call.j2")
try:
self._template = template_env.get_template("call.j2")
except:
self._template = None
self._no_return = True

@property
def is_call(self):
return True

def __repr__(self):
return self._template.render(name=self._name,
params=self._params,
Expand Down Expand Up @@ -250,10 +260,29 @@ def __init__(
pb_method,
requests,
responses)
self._template = template_env.get_template("request.j2")
try:
self._template = template_env.get_template("request.j2")
except:
self._template = None
self._method_description = method_description
self._returns = True

@property
def is_request(self):
return True

@property
def return_name(self):
return self._return_name

@property
def return_type(self):
return self._return_type

@property
def return_description(self):
return self._return_description

def __repr__(self):
return self._template.render(
name=self._name,
Expand Down Expand Up @@ -291,15 +320,33 @@ def __init__(
pb_method,
requests,
responses)
self._is_stream = True
self._name = name_parser_factory.create(
remove_subscribe(pb_method.name))
self._template = template_env.get_template("stream.j2")
try:
self._template = template_env.get_template("stream.j2")
except:
self._template = None

@property
def return_type_required(self):
return True

@property
def is_stream(self):
return True

@property
def return_name(self):
return self._return_name

@property
def return_type(self):
return self._return_type

@property
def return_description(self):
return self._return_description

def __repr__(self):
return self._template.render(
name=self._name,
Expand Down
11 changes: 5 additions & 6 deletions pb_plugins/protoc_gen_mavsdk/name_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ class NameParserFactory:
def create(self, name):
return NameParser(name, self._initialisms)

def set_template_path(self, template_path):
self._initialisms = self._load_initialisms(template_path)
def set_initialisms_path(self, initialisms_path):
self._initialisms = self._load_initialisms(initialisms_path)

def _load_initialisms(self, template_path):
def _load_initialisms(self, initialisms_path):
try:
_initialisms_path = f"{template_path}/initialisms"
with open(_initialisms_path, "r") as handle:
with open(initialisms_path, "r") as handle:
return json.loads(handle.read())
except FileNotFoundError:
except:
return []


Expand Down
21 changes: 20 additions & 1 deletion pb_plugins/protoc_gen_mavsdk/struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ def __init__(self, plugin_name, package,
template_env, pb_struct, struct_docs):
self._plugin_name = name_parser_factory.create(plugin_name)
self._package = name_parser_factory.create(package)
self._template = template_env.get_template("struct.j2")
try:
self._template = template_env.get_template("struct.j2")
except:
self._template = None
self._struct_description = struct_docs['description'].strip(
) if struct_docs else None
self._name = name_parser_factory.create(pb_struct.name)
Expand Down Expand Up @@ -69,6 +72,22 @@ def __repr__(self):
def name(self):
return self._name

@property
def fields(self):
return self._fields

@property
def nested_enums(self):
return self._nested_enums

@property
def nested_structs(self):
return self._nested_structs

@property
def struct_description(self):
return self._struct_description

@staticmethod
def collect_structs(plugin_name, package, structs, template_env, docs):
_structs = {}
Expand Down
11 changes: 5 additions & 6 deletions pb_plugins/protoc_gen_mavsdk/type_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ class TypeInfoFactory:
def create(self, field):
return TypeInfo(field, self._conversion_dict)

def set_template_path(self, template_path):
self._conversion_dict = self._load_conversions_dict(template_path)
def set_conversion_path(self, conversions_path):
self._conversion_dict = self._load_conversions_dict(conversions_path)

def _load_conversions_dict(self, template_path):
def _load_conversions_dict(self, conversions_path):
try:
_conversion_dict_path = f"{template_path}/type_conversions"
with open(_conversion_dict_path, "r") as handle:
with open(conversions_path, "r") as handle:
return json.loads(handle.read())
except FileNotFoundError:
except:
return {}


Expand Down
4 changes: 2 additions & 2 deletions pb_plugins/protoc_gen_mavsdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ def jinja_indent(_in_str, level):
)


def get_template_env(_searchpath):
def get_template_env(_searchpath, _lstrip_blocks, _trim_blocks):
""" Generates the template environment """
_template_env = Environment(loader=FileSystemLoader(
searchpath=_searchpath))
searchpath=_searchpath), lstrip_blocks=_lstrip_blocks, trim_blocks=_trim_blocks)

# Register some functions we need to access in the template
_template_env.globals.update(indent=jinja_indent)
Expand Down
2 changes: 1 addition & 1 deletion pb_plugins/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def parse_requirements(filename):

setup(
name="protoc-gen-mavsdk",
version="1.0.3",
version="1.1.0",
description="Protoc plugin used to generate MAVSDK bindings",
url="https://github.com/mavlink/MAVSDK-Proto",
maintainer="Jonas Vautherin, Julian Oes",
Expand Down

0 comments on commit 4bb3945

Please sign in to comment.