Skip to content

Commit

Permalink
Revert "Add types to rosidl_cli (#826)" (#830)
Browse files Browse the repository at this point in the history
This reverts commit c71febc.
  • Loading branch information
clalancette authored Oct 7, 2024
1 parent c71febc commit 21fa68f
Show file tree
Hide file tree
Showing 22 changed files with 112 additions and 182 deletions.
1 change: 0 additions & 1 deletion rosidl_cli/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

<test_depend>ament_copyright</test_depend>
<test_depend>ament_flake8</test_depend>
<test_depend>ament_mypy</test_depend>
<test_depend>ament_pep257</test_depend>
<test_depend>ament_xmllint</test_depend>
<test_depend>python3-pytest</test_depend>
Expand Down
Empty file removed rosidl_cli/py.typed
Empty file.
12 changes: 3 additions & 9 deletions rosidl_cli/rosidl_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,13 @@

import argparse
import signal
from typing import Any, List, Union

from rosidl_cli.command.generate import GenerateCommand
from rosidl_cli.command.translate import TranslateCommand
from rosidl_cli.common import get_first_line_doc


def add_subparsers(
parser: argparse.ArgumentParser,
cli_name: str,
commands: List[Union[GenerateCommand, TranslateCommand]]
) -> argparse._SubParsersAction[argparse.ArgumentParser]:
def add_subparsers(parser, cli_name, commands):
"""
Create argparse subparser for each command.
Expand Down Expand Up @@ -68,7 +63,7 @@ def add_subparsers(
return subparser


def main() -> Union[str, signal.Signals, Any]:
def main():
script_name = 'rosidl'
description = f'{script_name} is an extensible command-line tool ' \
'for ROS interface generation.'
Expand All @@ -79,8 +74,7 @@ def main() -> Union[str, signal.Signals, Any]:
formatter_class=argparse.RawDescriptionHelpFormatter
)

commands: List[Union[GenerateCommand, TranslateCommand]] = \
[GenerateCommand(), TranslateCommand()]
commands = [GenerateCommand(), TranslateCommand()]

# add arguments for command extension(s)
add_subparsers(
Expand Down
6 changes: 2 additions & 4 deletions rosidl_cli/rosidl_cli/command/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse


class Command:
"""
Expand All @@ -24,8 +22,8 @@ class Command:
* `add_arguments`
"""

def add_arguments(self, parser: argparse.ArgumentParser) -> None:
def add_arguments(self, parser):
pass

def main(self, *, args: argparse.Namespace) -> None:
def main(self, *, parser, args):
raise NotImplementedError()
5 changes: 2 additions & 3 deletions rosidl_cli/rosidl_cli/command/generate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse
import pathlib

from rosidl_cli.command import Command
Expand All @@ -25,7 +24,7 @@ class GenerateCommand(Command):

name = 'generate'

def add_arguments(self, parser: argparse.ArgumentParser) -> None:
def add_arguments(self, parser):
parser.add_argument(
'-o', '--output-path', metavar='PATH',
type=pathlib.Path, default=None,
Expand All @@ -51,7 +50,7 @@ def add_arguments(self, parser: argparse.ArgumentParser) -> None:
"If prefixed by another path followed by a colon ':', "
'path resolution is performed against such path.'))

def main(self, *, args: argparse.Namespace) -> None:
def main(self, *, args):
generate(
package_name=args.package_name,
interface_files=args.interface_files,
Expand Down
18 changes: 8 additions & 10 deletions rosidl_cli/rosidl_cli/command/generate/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,20 @@

import os
import pathlib
from typing import List, Optional

from .extensions import GenerateCommandExtension
from .extensions import load_type_extensions
from .extensions import load_typesupport_extensions


def generate(
*,
package_name: str,
interface_files: List[str],
include_paths: Optional[List[str]] = None,
output_path: Optional[pathlib.Path] = None,
types: Optional[List[str]] = None,
typesupports: Optional[List[str]] = None
) -> List[List[str]]:
package_name,
interface_files,
include_paths=None,
output_path=None,
types=None,
typesupports=None
):
"""
Generate source code from interface definition files.
Expand Down Expand Up @@ -62,7 +60,7 @@ def generate(
:returns: list of lists of paths to generated source code files,
one group per type or type support extension invoked
"""
extensions: List[GenerateCommandExtension] = []
extensions = []

unspecific_generation = not types and not typesupports

Expand Down
27 changes: 9 additions & 18 deletions rosidl_cli/rosidl_cli/command/generate/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from pathlib import Path
from typing import cast, List, Optional

from rosidl_cli.extensions import Extension
from rosidl_cli.extensions import load_extensions

Expand All @@ -29,11 +26,11 @@ class GenerateCommandExtension(Extension):

def generate(
self,
package_name: str,
interface_files: List[str],
include_paths: List[str],
output_path: Path
) -> List[str]:
package_name,
interface_files,
include_paths,
output_path
):
"""
Generate source code.
Expand All @@ -51,17 +48,11 @@ def generate(
raise NotImplementedError()


def load_type_extensions(*, specs: Optional[List[str]],
strict: bool) -> List[GenerateCommandExtension]:
def load_type_extensions(**kwargs):
"""Load extensions for type representation source code generation."""
extensions = load_extensions('rosidl_cli.command.generate.type_extensions', specs=specs,
strict=strict)
return cast(List[GenerateCommandExtension], extensions)
return load_extensions('rosidl_cli.command.generate.type_extensions', **kwargs)


def load_typesupport_extensions(*, specs: Optional[List[str]], strict: bool
) -> List[GenerateCommandExtension]:
def load_typesupport_extensions(**kwargs):
"""Load extensions for type support source code generation."""
extensions = load_extensions('rosidl_cli.command.generate.typesupport_extensions',
specs=specs, strict=strict)
return cast(List[GenerateCommandExtension], extensions)
return load_extensions('rosidl_cli.command.generate.typesupport_extensions', **kwargs)
49 changes: 23 additions & 26 deletions rosidl_cli/rosidl_cli/command/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
import os
import pathlib
import tempfile
from typing import Generator, List, Tuple


def package_name_from_interface_file_path(path: pathlib.Path) -> str:
def package_name_from_interface_file_path(path):
"""
Derive ROS package name from a ROS interface definition file path.
Expand All @@ -30,7 +29,7 @@ def package_name_from_interface_file_path(path: pathlib.Path) -> str:
return pathlib.Path(os.path.abspath(path)).parents[1].name


def dependencies_from_include_paths(include_paths: List[str]) -> List[str]:
def dependencies_from_include_paths(include_paths):
"""
Collect dependencies' ROS interface definition files from include paths.
Expand All @@ -46,7 +45,7 @@ def dependencies_from_include_paths(include_paths: List[str]) -> List[str]:
})


def interface_path_as_tuple(path: str) -> Tuple[pathlib.Path, pathlib.Path]:
def interface_path_as_tuple(path):
"""
Express interface definition file path as an (absolute prefix, relative path) tuple.
Expand All @@ -62,43 +61,41 @@ def interface_path_as_tuple(path: str) -> Tuple[pathlib.Path, pathlib.Path]:
"""
path_as_string = str(path)
if ':' not in path_as_string:
prefix_path = pathlib.Path.cwd()
prefix = pathlib.Path.cwd()
else:
prefix, _, path = path_as_string.rpartition(':')
prefix_path = pathlib.Path(os.path.abspath(prefix))
path_as_path = pathlib.Path(path)
if path_as_path.is_absolute():
prefix = pathlib.Path(os.path.abspath(prefix))
path = pathlib.Path(path)
if path.is_absolute():
raise ValueError('Interface definition file path '
f"'{path_as_path}' cannot be absolute")
return prefix_path, path_as_path
f"'{path}' cannot be absolute")
return prefix, path


def idl_tuples_from_interface_files(
interface_files: List[str]
) -> List[str]:
def idl_tuples_from_interface_files(interface_files):
"""
Express ROS interface definition file paths as IDL tuples.
An IDL tuple is a relative path prefixed by an absolute path against
which to resolve it followed by a colon ':'. This function then applies
the same logic as `interface_path_as_tuple`.
"""
idl_tuples: List[str] = []
for interface_path in interface_files:
prefix, path = interface_path_as_tuple(interface_path)
idl_tuples = []
for path in interface_files:
prefix, path = interface_path_as_tuple(path)
idl_tuples.append(f'{prefix}:{path.as_posix()}')
return idl_tuples


@contextlib.contextmanager
def legacy_generator_arguments_file(
*,
package_name: str,
interface_files: List[str],
include_paths: List[str],
templates_path: str,
output_path: str
) -> Generator[str, None, None]:
package_name,
interface_files,
include_paths,
templates_path,
output_path
):
"""
Generate a temporary rosidl generator arguments file.
Expand Down Expand Up @@ -141,10 +138,10 @@ def legacy_generator_arguments_file(

def generate_visibility_control_file(
*,
package_name: str,
template_path: str,
output_path: str
) -> None:
package_name,
template_path,
output_path
):
"""
Generate a visibility control file from a template.
Expand Down
6 changes: 2 additions & 4 deletions rosidl_cli/rosidl_cli/command/translate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse
import pathlib


from rosidl_cli.command import Command

from .api import translate
Expand All @@ -26,7 +24,7 @@ class TranslateCommand(Command):

name = 'translate'

def add_arguments(self, parser: argparse.ArgumentParser) -> None:
def add_arguments(self, parser):
parser.add_argument(
'-o', '--output-path', metavar='PATH',
type=pathlib.Path, default=None,
Expand Down Expand Up @@ -66,7 +64,7 @@ def add_arguments(self, parser: argparse.ArgumentParser) -> None:
'path resolution is performed against such path.')
)

def main(self, *, args: argparse.Namespace) -> None:
def main(self, *, args):
translate(
package_name=args.package_name,
interface_files=args.interface_files,
Expand Down
20 changes: 9 additions & 11 deletions rosidl_cli/rosidl_cli/command/translate/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,20 @@
import collections
import os
import pathlib
from typing import DefaultDict, Dict, List, Optional, Union

from .extensions import load_translate_extensions


def translate(
*,
package_name: str,
interface_files: List[str],
output_format: str,
input_format: Optional[str] = None,
include_paths: Optional[List[str]] = None,
output_path: Optional[pathlib.Path] = None,
translators: Optional[List[str]] = None
) -> List[str]:
package_name,
interface_files,
output_format,
input_format=None,
include_paths=None,
output_path=None,
translators=None
):
"""
Translate interface definition files from one format to another.
Expand Down Expand Up @@ -65,8 +64,7 @@ def translate(
raise RuntimeError('No translate extensions found')

if not input_format:
interface_files_per_format: Union[DefaultDict[str, List[str]],
Dict[str, List[str]]] = collections.defaultdict(list)
interface_files_per_format = collections.defaultdict(list)
for interface_file in interface_files:
input_format = os.path.splitext(interface_file)[-1][1:]
interface_files_per_format[input_format].append(interface_file)
Expand Down
23 changes: 8 additions & 15 deletions rosidl_cli/rosidl_cli/command/translate/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from pathlib import Path
from typing import cast, ClassVar, List, Optional

from rosidl_cli.extensions import Extension
from rosidl_cli.extensions import load_extensions
Expand All @@ -30,16 +28,13 @@ class TranslateCommandExtension(Extension):
* `translate`
"""

input_format: ClassVar[str]
output_format: ClassVar[str]

def translate(
self,
package_name: str,
interface_files: List[str],
include_paths: List[str],
output_path: Path
) -> List[str]:
package_name,
interface_files,
include_paths,
output_path
):
"""
Translate interface definition files.
Expand All @@ -62,10 +57,8 @@ def translate(
raise NotImplementedError()


def load_translate_extensions(*, specs: Optional[List[str]], strict: bool
) -> List[TranslateCommandExtension]:
def load_translate_extensions(**kwargs):
"""Load extensions for interface definition translation."""
extensions = load_extensions(
'rosidl_cli.command.translate.extensions', specs=specs, strict=strict
return load_extensions(
'rosidl_cli.command.translate.extensions', **kwargs
)
return cast(List[TranslateCommandExtension], extensions)
Loading

0 comments on commit 21fa68f

Please sign in to comment.