Skip to content

Commit

Permalink
Add more command line arguments for more control
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasVautherin committed Jun 27, 2022
1 parent 1b97c68 commit 4a9886b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 21 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
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
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

0 comments on commit 4a9886b

Please sign in to comment.