Skip to content

Commit

Permalink
use f-string (#436)
Browse files Browse the repository at this point in the history
Signed-off-by: Dirk Thomas <dirk-thomas@users.noreply.github.com>
  • Loading branch information
dirk-thomas authored Feb 4, 2020
1 parent 73877b4 commit 4cc7397
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 46 deletions.
3 changes: 1 addition & 2 deletions rosidl_adapter/rosidl_adapter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,4 @@ def convert_to_idl(package_dir, package_name, interface_file, output_dir):
return convert_action_to_idl(
package_dir, package_name, interface_file, output_dir / 'action')

assert False, "Unsupported interface type '{interface_file.suffix}'" \
.format_map(locals())
assert False, f"Unsupported interface type '{interface_file.suffix}'"
4 changes: 2 additions & 2 deletions rosidl_adapter/rosidl_adapter/action/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ def convert_action_to_idl(package_dir, package_name, input_file, output_dir):
assert input_file.suffix == '.action'

abs_input_file = package_dir / input_file
print('Reading input file: {abs_input_file}'.format_map(locals()))
print(f'Reading input file: {abs_input_file}')
abs_input_file = package_dir / input_file
content = abs_input_file.read_text(encoding='utf-8')
action = parse_action_string(package_name, input_file.stem, content)

output_file = output_dir / input_file.with_suffix('.idl').name
abs_output_file = output_file.absolute()
print('Writing output file: {abs_output_file}'.format_map(locals()))
print(f'Writing output file: {abs_output_file}')
data = {
'pkg_name': package_name,
'relative_input_file': input_file,
Expand Down
6 changes: 3 additions & 3 deletions rosidl_adapter/rosidl_adapter/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

def convert_files_to_idl(extension, conversion_function, argv=sys.argv[1:]):
parser = argparse.ArgumentParser(
description='Convert {extension} files to .idl'.format_map(locals()))
description=f'Convert {extension} files to .idl')
parser.add_argument(
'interface_files', nargs='+',
help='The interface files to convert')
Expand All @@ -38,8 +38,8 @@ def convert_files_to_idl(extension, conversion_function, argv=sys.argv[1:]):
package_dir = package_dir.parent
if not package_dir.parents:
print(
"Could not find package for '{interface_file}'"
.format_map(locals()), file=sys.stderr)
f"Could not find package for '{interface_file}'",
file=sys.stderr)
continue
warnings = []
pkg = parse_package(package_dir, warnings=warnings)
Expand Down
2 changes: 1 addition & 1 deletion rosidl_adapter/rosidl_adapter/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def main(argv=sys.argv[1:]):
output_file.parent.mkdir(exist_ok=True)
with output_file.open('w') as h:
for basepath, relative_path in idl_tuples:
line = '{basepath}:{relative_path}\n'.format_map(locals())
line = f'{basepath}:{relative_path}\n'
# use CMake friendly separator
line = line.replace(os.sep, '/')
h.write(line)
17 changes: 8 additions & 9 deletions rosidl_adapter/rosidl_adapter/msg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ def convert_msg_to_idl(package_dir, package_name, input_file, output_dir):
assert input_file.suffix == '.msg'

abs_input_file = package_dir / input_file
print('Reading input file: {abs_input_file}'.format_map(locals()))
print(f'Reading input file: {abs_input_file}')
abs_input_file = package_dir / input_file
content = abs_input_file.read_text(encoding='utf-8')
msg = parse_message_string(package_name, input_file.stem, content)

output_file = output_dir / input_file.with_suffix('.idl').name
abs_output_file = output_file.absolute()
print('Writing output file: {abs_output_file}'.format_map(locals()))
print(f'Writing output file: {abs_output_file}')
data = {
'pkg_name': package_name,
'relative_input_file': input_file,
Expand Down Expand Up @@ -89,7 +89,7 @@ def string_to_idl_wstring_literal(string):
def get_include_file(base_type):
if base_type.is_primitive_type():
return None
return '{base_type.pkg_name}/msg/{base_type.type}.idl'.format_map(locals())
return f'{base_type.pkg_name}/msg/{base_type.type}.idl'


def get_idl_type(type_):
Expand All @@ -101,18 +101,17 @@ def get_idl_type(type_):
identifier in ('string', 'wstring') and
type_.string_upper_bound is not None
):
identifier += '<{type_.string_upper_bound}>'.format_map(locals())
identifier += f'<{type_.string_upper_bound}>'
else:
identifier = '{type_.pkg_name}::msg::{type_.type}' \
.format_map(locals())
identifier = f'{type_.pkg_name}::msg::{type_.type}'

if isinstance(type_, str) or not type_.is_array:
return identifier

if type_.is_fixed_size_array():
return '{identifier}[{type_.array_size}]'.format_map(locals())
return f'{identifier}[{type_.array_size}]'

if not type_.is_upper_bound:
return 'sequence<{identifier}>'.format_map(locals())
return f'sequence<{identifier}>'

return 'sequence<{identifier}, {type_.array_size}>'.format_map(locals())
return f'sequence<{identifier}, {type_.array_size}>'
8 changes: 4 additions & 4 deletions rosidl_adapter/rosidl_adapter/resource/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ def evaluate_template(template_name, data):
return output.getvalue()
except Exception as e: # noqa: F841
print(
"{e.__class__.__name__} processing template '{template_name}'"
.format_map(locals()), file=sys.stderr)
f"{e.__class__.__name__} processing template '{template_name}'",
file=sys.stderr)
raise
finally:
_interpreter.shutdown()
Expand All @@ -81,7 +81,7 @@ def _evaluate_template(template_name, **kwargs):
_interpreter.string(content, template_path, kwargs)
except Exception as e: # noqa: F841
print(
"{e.__class__.__name__} processing template '{template_name}': {e}"
.format_map(locals()), file=sys.stderr)
f"{e.__class__.__name__} processing template '{template_name}': "
f'{e}', file=sys.stderr)
sys.exit(1)
_interpreter.invoke('afterInclude')
4 changes: 2 additions & 2 deletions rosidl_adapter/rosidl_adapter/srv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ def convert_srv_to_idl(package_dir, package_name, input_file, output_dir):
assert input_file.suffix == '.srv'

abs_input_file = package_dir / input_file
print('Reading input file: {abs_input_file}'.format_map(locals()))
print(f'Reading input file: {abs_input_file}')
abs_input_file = package_dir / input_file
content = abs_input_file.read_text(encoding='utf-8')
srv = parse_service_string(package_name, input_file.stem, content)

output_file = output_dir / input_file.with_suffix('.idl').name
abs_output_file = output_file.absolute()
print('Writing output file: {abs_output_file}'.format_map(locals()))
print(f'Writing output file: {abs_output_file}')
data = {
'pkg_name': package_name,
'relative_input_file': input_file,
Expand Down
11 changes: 5 additions & 6 deletions rosidl_cmake/rosidl_cmake/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ def get_template_path(template_name):
template_path = basepath / template_name
if template_path.exists():
return template_path
raise RuntimeError(
"Failed to find template '{template_name}'".format_map(locals()))
raise RuntimeError(f"Failed to find template '{template_name}'")


interpreter = None
Expand Down Expand Up @@ -150,8 +149,8 @@ def expand_template(
except Exception as e: # noqa: F841
if os.path.exists(output_file):
os.remove(output_file)
print("{e.__class__.__name__} when expanding '{template_name}' into "
"'{output_file}': {e}".format_map(locals()), file=sys.stderr)
print(f"{e.__class__.__name__} when expanding '{template_name}' into "
f"'{output_file}': {e}", file=sys.stderr)
raise
finally:
template_prefix_path.pop()
Expand Down Expand Up @@ -196,7 +195,7 @@ def _expand_template(template_name, **kwargs):
try:
interpreter.string(content, str(template_path), kwargs)
except Exception as e: # noqa: F841
print("{e.__class__.__name__} in template '{template_path}': {e}"
.format_map(locals()), file=sys.stderr)
print(f"{e.__class__.__name__} in template '{template_path}': {e}",
file=sys.stderr)
raise
interpreter.invoke('afterInclude')
12 changes: 6 additions & 6 deletions rosidl_generator_c/rosidl_generator_c/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,26 +179,26 @@ def basic_value_to_c(type_, value):
# Specifically, MSVC is not happy in this case
if -2147483648 == value:
return '({0}l - 1)'.format(value + 1)
return '{value}l'.format_map(locals())
return f'{value}l'

if type_.typename == 'uint32':
return '{value}ul'.format_map(locals())
return f'{value}ul'

if type_.typename == 'int64':
# Handle edge case for INT64_MIN
# See https://en.cppreference.com/w/cpp/language/integer_literal
if -9223372036854775808 == value:
return '({0}ll - 1)'.format(value + 1)
return '{value}ll'.format_map(locals())
return f'{value}ll'

if type_.typename == 'uint64':
return '{value}ull'.format_map(locals())
return f'{value}ull'

if 'float' == type_.typename:
return '{value}f'.format_map(locals())
return f'{value}f'

if 'double' == type_.typename:
return '{value}l'.format_map(locals())
return f'{value}l'

assert False, "unknown basic type '%s'" % type_

Expand Down
4 changes: 2 additions & 2 deletions rosidl_generator_cpp/resource/msg__traits.hpp.em
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ for member in message.structure.members:
break
if isinstance(type_, NamespacedType):
typename = '::'.join(type_.namespaced_name())
fixed_template_strings.add('has_fixed_size<{typename}>::value'.format_map(locals()))
fixed_template_strings.add(f'has_fixed_size<{typename}>::value')
else:
if fixed_template_strings:
fixed_template_string = ' && '.join(sorted(fixed_template_strings))
Expand All @@ -105,7 +105,7 @@ for member in message.structure.members:
break
if isinstance(type_, NamespacedType):
typename = '::'.join(type_.namespaced_name())
bounded_template_strings.add('has_bounded_size<{typename}>::value'.format_map(locals()))
bounded_template_strings.add(f'has_bounded_size<{typename}>::value')
else:
if bounded_template_strings:
bounded_template_string = ' && '.join(sorted(bounded_template_strings))
Expand Down
10 changes: 4 additions & 6 deletions rosidl_parser/bin/idl2png
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,23 @@ def main(argv=sys.argv[1:]):
print('idl_file', idl_file)
idl_file = pathlib.Path(idl_file)
if not idl_file.exists():
print("File '{idl_file} not found".format_map(locals()),
file=sys.stderr)
print(f"File '{idl_file} not found", file=sys.stderr)
continue

string = idl_file.read_text()
try:
tree = get_ast_from_idl_string(string)
except Exception as e: # noqa: F841
print("Failed to parse '{idl_file}': {e}", file=sys.stderr)
print(f"Failed to parse '{idl_file}': {e}", file=sys.stderr)
continue

png_file = idl_file.with_suffix('.png')
try:
pydot__tree_to_png(tree, png_file)
except ImportError as e: # noqa: F841
print('Failed to generate png files: {e}'.format_map(locals()),
file=sys.stderr)
print(f'Failed to generate png files: {e}', file=sys.stderr)
return 1
print("Generated '{png_file}'".format_map(locals()))
print(f"Generated '{png_file}'")


if __name__ == '__main__':
Expand Down
5 changes: 2 additions & 3 deletions rosidl_parser/rosidl_parser/definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,10 +423,9 @@ def get_annotation_value(self, name):
"""
values = self.get_annotation_values(name)
if not values:
raise ValueError("No '{name}' annotation".format_map(locals()))
raise ValueError(f"No '{name}' annotation")
if len(values) > 1:
raise ValueError(
"Multiple '{name}' annotations".format_map(locals()))
raise ValueError(f"Multiple '{name}' annotations")
return values[0]

def get_annotation_values(self, name):
Expand Down

0 comments on commit 4cc7397

Please sign in to comment.