Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use latin-1 encoding when reading/writing .idl files, prepend BOM to generated C/C++ files when necessary #391

Merged
merged 3 commits into from
Jul 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rosidl_adapter/rosidl_adapter/action/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ def convert_action_to_idl(package_dir, package_name, input_file, output_dir):
'action': action,
}

expand_template('action.idl.em', data, output_file)
expand_template('action.idl.em', data, output_file, encoding='iso-8859-1')
return output_file
2 changes: 1 addition & 1 deletion rosidl_adapter/rosidl_adapter/msg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def convert_msg_to_idl(package_dir, package_name, input_file, output_dir):
'msg': msg,
}

expand_template('msg.idl.em', data, output_file)
expand_template('msg.idl.em', data, output_file, encoding='iso-8859-1')
return output_file


Expand Down
6 changes: 3 additions & 3 deletions rosidl_adapter/rosidl_adapter/resource/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@
import em


def expand_template(template_name, data, output_file):
def expand_template(template_name, data, output_file, encoding='utf-8'):
content = evaluate_template(template_name, data)

if output_file.exists():
existing_content = output_file.read_text(encoding='utf-8')
existing_content = output_file.read_text(encoding=encoding)
if existing_content == content:
return
elif output_file.parent:
os.makedirs(str(output_file.parent), exist_ok=True)

output_file.write_text(content, encoding='utf-8')
output_file.write_text(content, encoding=encoding)


_interpreter = None
Expand Down
2 changes: 1 addition & 1 deletion rosidl_adapter/rosidl_adapter/srv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ def convert_srv_to_idl(package_dir, package_name, input_file, output_dir):
'srv': srv,
}

expand_template('srv.idl.em', data, output_file)
expand_template('srv.idl.em', data, output_file, encoding='iso-8859-1')
return output_file
17 changes: 12 additions & 5 deletions rosidl_cmake/rosidl_cmake/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ def get_newest_modification_time(target_dependencies):
return newest_timestamp


def generate_files(generator_arguments_file, mapping, additional_context=None, keep_case=False):
def generate_files(
generator_arguments_file, mapping, additional_context=None,
keep_case=False, post_process_callback=None
):
args = read_generator_arguments(generator_arguments_file)

template_basepath = pathlib.Path(args['template_dir'])
Expand Down Expand Up @@ -82,7 +85,8 @@ def generate_files(generator_arguments_file, mapping, additional_context=None, k
expand_template(
os.path.basename(template_file), data,
generated_file, minimum_timestamp=latest_target_timestamp,
template_basepath=template_basepath)
template_basepath=template_basepath,
post_process_callback=post_process_callback)
except Exception as e:
print(
'Error processing idl file: ' +
Expand Down Expand Up @@ -110,7 +114,7 @@ def get_template_path(template_name):

def expand_template(
template_name, data, output_file, minimum_timestamp=None,
template_basepath=None
template_basepath=None, post_process_callback=None
):
# in the legacy API the first argument was the path to the template
if template_basepath is None:
Expand Down Expand Up @@ -155,12 +159,15 @@ def expand_template(
content = output.getvalue()
interpreter.shutdown()

if post_process_callback:
content = post_process_callback(content)

# only overwrite file if necessary
# which is either when the timestamp is too old or when the content is different
if os.path.exists(output_file):
timestamp = os.path.getmtime(output_file)
if minimum_timestamp is None or timestamp > minimum_timestamp:
with open(output_file, 'r') as h:
with open(output_file, 'r', encoding='utf-8') as h:
if h.read() == content:
return
else:
Expand All @@ -170,7 +177,7 @@ def expand_template(
except FileExistsError:
pass

with open(output_file, 'w') as h:
with open(output_file, 'w', encoding='utf-8') as h:
h.write(content)


Expand Down
15 changes: 14 additions & 1 deletion rosidl_generator_c/rosidl_generator_c/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,20 @@ def generate_c(generator_arguments_file):
'idl__struct.h.em': '%s__struct.h',
'idl__type_support.h.em': '%s__type_support.h',
}
generate_files(generator_arguments_file, mapping)
generate_files(
generator_arguments_file, mapping,
post_process_callback=prefix_with_bom_if_necessary)


def prefix_with_bom_if_necessary(content):
try:
content.encode('ASCII')
except UnicodeError:
prefix = '\ufeff' + \
'// NOLINT: This file starts with a BOM ' + \
'since it contain non-ASCII characters\n'
content = prefix + content
return content


BASIC_IDL_TYPES_TO_C = {
Expand Down
15 changes: 14 additions & 1 deletion rosidl_generator_cpp/rosidl_generator_cpp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,20 @@ def generate_cpp(generator_arguments_file):
'idl__struct.hpp.em': '%s__struct.hpp',
'idl__traits.hpp.em': '%s__traits.hpp',
}
generate_files(generator_arguments_file, mapping)
generate_files(
generator_arguments_file, mapping,
post_process_callback=prefix_with_bom_if_necessary)


def prefix_with_bom_if_necessary(content):
try:
content.encode('ASCII')
except UnicodeError:
prefix = '\ufeff' + \
'// NOLINT: This file starts with a BOM ' + \
'since it contain non-ASCII characters\n'
content = prefix + content
return content


MSG_TYPE_TO_CPP = {
Expand Down