Skip to content

Commit

Permalink
[SkyMarshal] Drop py2 syntax
Browse files Browse the repository at this point in the history
Topic: py-6to3
Relative: sky-binding-types
GitOrigin-RevId: 8503688e25589455f1993b6eee00a83aae3fce51
  • Loading branch information
aaron-skydio authored and nathan-skydio committed Mar 2, 2024
1 parent e0f4748 commit a2d7904
Show file tree
Hide file tree
Showing 17 changed files with 535 additions and 650 deletions.
5 changes: 3 additions & 2 deletions third_party/skymarshal/skymarshal/__main__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# aclint: py2 py3
# aclint: py3
"Allow the skymarshal package to be executable"
from __future__ import absolute_import

from __future__ import annotations

from skymarshal import skymarshal
from skymarshal.emit_cpp import SkymarshalCpp
Expand Down
19 changes: 8 additions & 11 deletions third_party/skymarshal/skymarshal/common_util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# aclint: py2 py3
from __future__ import absolute_import, print_function
# aclint: py3

from __future__ import annotations

import sys
import typing as T
Expand All @@ -15,26 +16,23 @@
StrType = T.TypeVar("StrType", str, T.Text)


def snakecase_to_camelcase(snake_string):
# type: (StrType) -> StrType
def snakecase_to_camelcase(snake_string: StrType) -> StrType:
return "".join(word.capitalize() for word in snake_string.split("_"))


def snakecase_to_lower_camelcase(snake_string):
# type: (StrType) -> StrType
def snakecase_to_lower_camelcase(snake_string: StrType) -> StrType:
words = snake_string.split("_")
return words[0] + "".join(word.capitalize() for word in words[1:])


def camelcase_to_snakecase(camelcase, to_upper=False):
# type: (StrType, bool) -> StrType
def camelcase_to_snakecase(camelcase: StrType, to_upper: bool = False) -> StrType:
if "_" in camelcase:
# This string is already using underscores.
if to_upper:
return camelcase.upper()
else:
return camelcase.lower()
out = [] # type: T.List[StrType]
out: T.List[StrType] = []
for char in camelcase:
if out and char in uppercase:
out.append("_")
Expand All @@ -46,8 +44,7 @@ def camelcase_to_snakecase(camelcase, to_upper=False):
return "".join(out)


def is_camelcase(string):
# type: (StrType) -> bool
def is_camelcase(string: StrType) -> bool:
if not string:
return False
if "_" in string or " " in string:
Expand Down
55 changes: 27 additions & 28 deletions third_party/skymarshal/skymarshal/emit_cpp.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# aclint: py2 py3
# mypy: allow-untyped-defs
from __future__ import absolute_import
# aclint: py3

from __future__ import annotations

import argparse # pylint: disable=unused-import
import copy
import os
import typing as T

import six
from six.moves import range # pylint: disable=redefined-builtin
from skymarshal import syntax_tree
from skymarshal.emit_helpers import BaseBuilder, Code, EnumBuilder, StructBuilder, render
from skymarshal.language_plugin import SkymarshalLanguage
Expand Down Expand Up @@ -68,9 +68,9 @@ def declare_member(member, suffix="", const_ref=False):
type_str = mapped_typename

if const_ref:
type_str = "const {}&".format(type_str)
type_str = f"const {type_str}&"

return "{} {}{}".format(type_str, member.name, suffix)
return f"{type_str} {member.name}{suffix}"


def map_to_cpptype(type_ref):
Expand All @@ -95,10 +95,10 @@ def dim_size_access(dim, array_member=None):
"""Get the size of a single array dimension"""
if dim.auto_member:
if array_member:
return "this->{}.size()".format(array_member.name)
return "v_{}".format(dim.size_str)
return f"this->{array_member.name}.size()"
return f"v_{dim.size_str}"
if dim.dynamic:
return "this->{}".format(dim.size_str)
return f"this->{dim.size_str}"
return dim.size_str


Expand All @@ -111,7 +111,7 @@ def namespace(self):

@property
def filename(self):
return "{}.hpp".format(os.path.join(self.namespace, self.name))
return f"{os.path.join(self.namespace, self.name)}.hpp"

@property
def fullpath(self):
Expand All @@ -122,7 +122,7 @@ def fullpath(self):

@property
def underscored(self):
return "{}_{}".format(self.namespace, self.name)
return f"{self.namespace}_{self.name}"

@property
def comment(self):
Expand All @@ -143,8 +143,7 @@ def comment(self):
return "\n" + "\n".join(lines)

@property
def size_t(self):
# type: () -> str
def size_t(self) -> str:
"""
The C++ type to use for buffer size variables
"""
Expand All @@ -164,7 +163,7 @@ def string_cast_type(self):
return {"int8_t": "int16_t"}.get(storage_name, storage_name)


class CppInclude(object):
class CppInclude:
def __init__(self, member=None, std=None, prefix=None):
assert member or std
self.member = member
Expand All @@ -189,10 +188,10 @@ def absolute_path(self):
def directive(self):
name = self.absolute_path
if self.std is not None:
name = "<{}>".format(name)
name = f"<{name}>"
else:
name = '"{}"'.format(name)
return "#include {}\n".format(name)
name = f'"{name}"'
return f"#include {name}\n"

@property
def package(self):
Expand Down Expand Up @@ -236,7 +235,7 @@ def include_list(self):
# This is an lcmtype
if include.relative_path != self.filename:
includes.add(include)
std_includes = set(inc for inc in includes if inc.std)
std_includes = {inc for inc in includes if inc.std}
lcm_includes = includes - std_includes
return sorted(std_includes, key=str) + sorted(lcm_includes, key=str)

Expand Down Expand Up @@ -278,7 +277,7 @@ def define_constants(self):
else:
mapped_typename = map_to_cpptype(constant.type_ref)
if self.args.cpp_std not in ("c++98", "c++11"):
raise ValueError("Invalid C++ std: {}".format(self.args.cpp_std))
raise ValueError(f"Invalid C++ std: {self.args.cpp_std}")

if self.args.cpp_std == "c++11":
code(2, "// If you are getting compiler/linker errors saying things like")
Expand Down Expand Up @@ -324,8 +323,9 @@ def constructor_args(self):
def initializers(self):
return ",\n".join("{0}({0}_arg)".format(member.name) for member in self.members)

def encode_recursive(self, code, member, depth, extra_indent):
# type: (Code, syntax_tree.Member, int, int) -> None
def encode_recursive(
self, code: Code, member: syntax_tree.Member, depth: int, extra_indent: int
) -> None:
indent = extra_indent + 1 + depth
# primitive array
if member.ndim == (depth + 1) and member.type_ref.is_non_string_primitive_type():
Expand Down Expand Up @@ -444,15 +444,15 @@ def compute_hash(self):
for member in self.members:
if not member.type_ref.is_primitive_type():
scoped_name = map_to_cpptype(member.type_ref)
hash_calls.append("{}::_computeHash(&cp)".format(scoped_name))
hash_calls.append(f"{scoped_name}::_computeHash(&cp)")
return " +\n ".join(hash_calls)

def encoded_size(self):
if len(self.members) == 0:
return " return 0;"

code = Code()
code(1, "{} enc_size = 0;".format(self.size_t))
code(1, f"{self.size_t} enc_size = 0;")
for member in self.members:
self.encoded_size_member(code, member)

Expand Down Expand Up @@ -661,10 +661,9 @@ def add_args(cls, parser):
@classmethod
def create_files(
cls,
packages, # type: T.Iterable[syntax_tree.Package]
args, # type: argparse.Namespace
):
# type: (...) -> T.Dict[str, T.Union[str, bytes]]
packages: T.Iterable[syntax_tree.Package],
args: argparse.Namespace,
) -> T.Dict[str, T.Union[str, bytes]]:
"""Turn a list of lcm packages into C++ bindings for each struct.
@param packages: the list of syntax_tree.Package objects
Expand All @@ -682,7 +681,7 @@ def create_files(
file_map = {}
for package in packages:
for struct in package.struct_definitions:
cppclass = CppStruct(package, struct, args) # type: T.Union[CppStruct, CppEnum]
cppclass: T.Union[CppStruct, CppEnum] = CppStruct(package, struct, args)
file_map[cppclass.fullpath] = render(
"lcmtype.hpp.template",
lcmtype=cppclass,
Expand All @@ -699,7 +698,7 @@ def create_files(
)

type_names = sorted(
type_definition.name for type_definition in six.itervalues(package.type_definitions)
type_definition.name for type_definition in package.type_definitions.values()
)

if args.cpp_aggregate:
Expand Down
31 changes: 15 additions & 16 deletions third_party/skymarshal/skymarshal/emit_djinni.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# aclint: py2 py3
# mypy: allow-untyped-defs
# aclint: py3
"""Generate djinni enum definitions and conversions."""
from __future__ import absolute_import

from __future__ import annotations

import argparse # pylint: disable=unused-import
import os
Expand All @@ -14,7 +15,7 @@
from skymarshal.language_plugin import SkymarshalLanguage


class EnumCase(object):
class EnumCase:
"""A template-friendly wrapper object for LCM #djinni Enum Cases"""

def __init__(self, int_value, name):
Expand All @@ -27,7 +28,7 @@ def __init__(self, int_value, name):
self.proto_name = name


class EnumType(object):
class EnumType:
"""A template-friendly wrapper object for LCM #djinni Enums"""

# pylint: disable=too-many-instance-attributes
Expand All @@ -49,20 +50,20 @@ def __init__(self, package_name, enum, args):
self.default_case = self.cases[0]

# names for templating
self.definition_name = "{}.{}".format(package_name, enum.name)
self.definition_name = f"{package_name}.{enum.name}"
self.djinni_idl_name = snakecase_name
self.djinni_name = camelcase_name
self.djinni_namespace = args.djinni_module
self.lcm_name = enum.name
self.lcm_package = package_name

# filenames for generated converter sources
self.filename_h = "{}/converters/{}.h".format(self.djinni_namespace, snakecase_name)
self.filename_cc = "{}/converters/{}.cc".format(self.djinni_namespace, snakecase_name)
self.filename_h = f"{self.djinni_namespace}/converters/{snakecase_name}.h"
self.filename_cc = f"{self.djinni_namespace}/converters/{snakecase_name}.cc"

# header paths to the underlying type definitions
self.djinni_header = "djinni/{}/{}.hpp".format(self.djinni_namespace, snakecase_name)
self.lcm_header = "lcmtypes/{}/{}.hpp".format(self.lcm_package, self.lcm_name)
self.djinni_header = f"djinni/{self.djinni_namespace}/{snakecase_name}.hpp"
self.lcm_header = f"lcmtypes/{self.lcm_package}/{self.lcm_name}.hpp"

if enum.get_notation("#protobuf") is None:
self.is_protobuf = False
Expand All @@ -78,8 +79,7 @@ def __init__(self, package_name, enum, args):

class SkymarshalDjinni(SkymarshalLanguage):
@classmethod
def add_args(cls, parser):
# type: (argparse.ArgumentParser) -> None
def add_args(cls, parser: argparse.ArgumentParser) -> None:
parser.add_argument("--djinni", action="store_true", help="generate converters for djinni")
parser.add_argument("--djinni-idl-path", help="Full path of the .djinni file")
parser.add_argument("--djinni-path", help="Location of the .cc and .h files")
Expand All @@ -90,10 +90,9 @@ def add_args(cls, parser):
@classmethod
def create_files(
cls,
packages, # type: T.Iterable[syntax_tree.Package]
args, # type: argparse.Namespace
):
# type: (...) -> T.Dict[str, T.Union[str, bytes]]
packages: T.Iterable[syntax_tree.Package],
args: argparse.Namespace,
) -> T.Dict[str, T.Union[str, bytes]]:
"""
Turn a list of lcm packages into a djinni definition file and source files for lcm
converters.
Expand All @@ -118,7 +117,7 @@ def create_files(
enum_types.sort(key=lambda x: x.djinni_idl_name)

for enum_type in enum_types:
idl_file = "{}.djinni".format(args.djinni_module)
idl_file = f"{args.djinni_module}.djinni"
if args.djinni_idl_path:
idl_file = os.path.join(args.djinni_idl_path, idl_file, enum_type.djinni_idl_name)
file_map[idl_file] = render("djinni_idl.djinni.template", enum_types=[enum_type])
Expand Down
Loading

0 comments on commit a2d7904

Please sign in to comment.