Skip to content

Commit 503202d

Browse files
Merge branch 'master' of github.com:ChrisTimperley/roswire
2 parents 81850c4 + a311252 commit 503202d

File tree

4 files changed

+83
-34
lines changed

4 files changed

+83
-34
lines changed

src/roswire/definitions/format.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,25 @@
1313

1414

1515
class FormatDatabase:
16+
"""
17+
An immutable database of ROS definitions that maintains the parsed
18+
contents of :code:`.msg`, :code:`.srv`, and :code:`.action` files
19+
for messages, services, and action definitions.
20+
Note that implicit message definitions, i.e., those associated with a
21+
service or action definition, are also represented by the database.
22+
23+
Attributes
24+
----------
25+
messages: Mapping[str, MsgFormat]
26+
An immutable mapping from message name to its definition.
27+
services: Mapping[str, SrvFormat]
28+
An immutable mapping from service name to definition.
29+
actions: Mapping[str, ActionFormat]
30+
An immutable mapping from action name to definition.
31+
"""
1632
@staticmethod
1733
def build(db: PackageDatabase) -> 'FormatDatabase':
34+
"""Constructs a format database from a given package database."""
1835
messages: Set[MsgFormat] = set()
1936
services: Set[SrvFormat] = set()
2037
actions: Set[ActionFormat] = set()

src/roswire/definitions/package.py

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# -*- coding: utf-8 -*-
12
__all__ = ('Package', 'PackageDatabase')
23

34
from typing import (Tuple, List, Dict, Union, Any, Iterator, Collection,
@@ -23,9 +24,7 @@ class Package:
2324

2425
@staticmethod
2526
def build(path: str, files: FileProxy) -> 'Package':
26-
"""
27-
Constructs a description of a package located at a given path.
28-
"""
27+
"""Constructs a description of a package at a given path."""
2928
name: str = os.path.basename(path)
3029
messages: List[MsgFormat] = []
3130
services: List[SrvFormat] = []
@@ -82,12 +81,23 @@ def to_dict(self) -> Dict[str, Any]:
8281

8382

8483
class PackageDatabase(Mapping[str, Package]):
84+
"""
85+
An immutable database of packages, represented as :class:`Package`
86+
instances, indexed by their names, given as :class:`str`.
87+
88+
Note
89+
----
90+
Implements most :class:`dict` operations via
91+
:class:`abc.collections.Mapping`,
92+
including :code:`db['name']`, :code:`len(db)`), :code:`db.keys()`,
93+
:code:`db.values()`, and :code:`iter(db)`.
94+
As instances of this class are immutable, no destructive
95+
:class:`dict` operations are provided (e.g., :code:`del db['foo'])`
96+
and `db['foo'] = bar`).
97+
"""
8598
@staticmethod
8699
def paths(shell: ShellProxy, files: FileProxy) -> List[str]:
87-
"""
88-
Parses the contents of the ROS_PACKAGE_PATH environment variable for a
89-
given shell.
90-
"""
100+
"""Parses :code:`ROS_PACKAGE_PATH` for a given shell."""
91101
path_str = shell.environ('ROS_PACKAGE_PATH')
92102
package_paths: List[str] = path_str.strip().split(':')
93103
paths: List[str] = []
@@ -110,9 +120,20 @@ def from_paths(files: FileProxy,
110120
Constructs a package database from a list of the paths of the packages
111121
belonging to the database.
112122
113-
Parameters:
114-
files: access to the filesystem.
115-
paths: a list of the absolute paths of the packages.
123+
Parameters
124+
----------
125+
files: FileProxy
126+
access to the filesystem.
127+
paths: List[str]
128+
a list of the absolute paths of the packages.
129+
ignore_bad_paths: bool
130+
If :code:`True`, non-existent paths will be ignored.
131+
If :code:`False`, a :exc:`FileNotFoundError` will be raised.
132+
133+
Raises
134+
------
135+
FileNotFoundError
136+
if no package is found at a given path.
116137
"""
117138
packages: List[Package] = []
118139
for p in paths:
@@ -128,17 +149,16 @@ def __init__(self, packages: Collection[Package]) -> None:
128149
self.__contents = {p.name: p for p in packages}
129150

130151
def __len__(self) -> int:
131-
"""
132-
Returns a count of the number of packages within this database.
133-
"""
152+
"""Returns the number of packages within this database."""
134153
return len(self.__contents)
135154

136155
def __getitem__(self, name: str) -> Package:
137-
"""
138-
Fetches the description for a given package.
156+
"""Fetches the description for a given package.
139157
140-
Raises:
141-
KeyError: if no package exists with the given name.
158+
Raises
159+
------
160+
KeyError
161+
if no package exists with the given name.
142162
"""
143163
return self.__contents[name]
144164

src/roswire/definitions/type_db.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# -*- coding: utf-8 -*-
12
__all__ = ('TypeDatabase',)
23

34
from typing import (Collection, Type, Mapping, Iterator, Dict, ClassVar, Any,
@@ -25,6 +26,15 @@
2526

2627

2728
class TypeDatabase(Mapping[str, Type[Message]]):
29+
"""
30+
An immutable database of ROS types, represented as :class:`Message`
31+
subclasses, indexed by the fully qualified names of those types.
32+
33+
Note
34+
----
35+
Implements the set of non-destructive :class:`dict` operations as
36+
:class:`PackageDatabase`.
37+
"""
2838
@classmethod
2939
def build(cls, db_format: FormatDatabase) -> 'TypeDatabase':
3040
formats = list(db_format.messages.values())

src/roswire/description.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,20 @@
2525
@attr.s(slots=True)
2626
class SystemDescription:
2727
"""
28-
A description of the packages, types, and specifications within
29-
a containerised ROS application.
28+
An immutable description of all of the packages, types, and
29+
specifications (i.e., :code:`.msg`, :code:`.srv`, and :code:`.action`
30+
files) within a containerised ROS application.
3031
3132
Attributes
3233
----------
33-
sha256: str
34-
The ID of the Docker image for the application.
35-
types: TypeDatabase
36-
A database of types for the application.
37-
formats: FormatDatabase
38-
A database of message, service and action specifications.
39-
packages:
40-
A database of the packages contained within the application.
34+
sha256: str
35+
The ID of the Docker image for the application.
36+
types: TypeDatabase
37+
A database of types for the application.
38+
formats: FormatDatabase
39+
A database of message, service and action specifications.
40+
packages: PackageDatabase
41+
A database of the packages contained within the application.
4142
"""
4243
sha256: str = attr.ib()
4344
types: TypeDatabase = attr.ib()
@@ -46,14 +47,12 @@ class SystemDescription:
4647

4748
@staticmethod
4849
def from_file(fn: str) -> 'SystemDescription':
49-
"""Loads a description from a given file."""
5050
with open(fn, 'r') as f:
5151
d = yaml.safe_load(f)
5252
return SystemDescription.from_dict(d)
5353

5454
@staticmethod
5555
def from_dict(d: Dict[str, Any]) -> 'SystemDescription':
56-
"""Constructs a description from a JSON dictionary."""
5756
sha256: str = d['sha256']
5857
packages = PackageDatabase.from_dict(d['packages'])
5958
formats = FormatDatabase.build(packages)
@@ -64,7 +63,6 @@ def from_dict(d: Dict[str, Any]) -> 'SystemDescription':
6463
types=types)
6564

6665
def to_dict(self) -> Dict[str, Any]:
67-
"""Produces a JSON dictionary for this description."""
6866
return {'sha256': self.sha256,
6967
'packages': self.packages.to_dict()}
7068

@@ -110,7 +108,7 @@ def load(self,
110108
111109
Raises
112110
------
113-
FileNotFoundError:
111+
FileNotFoundError
114112
if no description for the given image is stored on disk.
115113
"""
116114
sha256 = self.__containers.image_sha256(image_or_tag)
@@ -123,9 +121,7 @@ def load(self,
123121
raise
124122

125123
def saved(self, image_or_tag: Union[str, DockerImage]) -> bool:
126-
"""
127-
Determines whether a description for a given image has been saved.
128-
"""
124+
"""Determines whether a description for an image has been saved."""
129125
sha256 = self.__containers.image_sha256(image_or_tag)
130126
fn = os.path.join(self.__dir_cache, sha256)
131127
return os.path.exists(fn)
@@ -145,6 +141,12 @@ def build(self,
145141
Builds a description of the ROS application contained within a given
146142
image and optionally saves that description to disk.
147143
144+
Parameters
145+
----------
146+
image_or_tag: Union[str, DockerImage]
147+
The name or object for the Docker image.
148+
save: bool
149+
If :code:`True`, the description will be saved to disk.
148150
149151
Returns
150152
-------

0 commit comments

Comments
 (0)