-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor selector into a singleton and more selection tools
- Loading branch information
Showing
15 changed files
with
638 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Copyright 2016 Open Source Robotics Foundation, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# 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. | ||
|
||
|
||
class NotInitializedException(Exception): | ||
"""Raised when the rclpy implementation is accessed before rclpy.init().""" | ||
|
||
def __init__(self, *args): | ||
Exception.__init__(self, 'rclpy.init() has not been called', *args) | ||
|
||
|
||
class ImplementationAlreadyImportedException(Exception): | ||
"""Raised on select_rmw_implemenation() after import_rmw_implementation() has been called.""" | ||
|
||
def __init__(self, *args): | ||
Exception.__init__(self, 'rmw implementation already imported', *args) | ||
|
||
|
||
class InvalidRCLPYImplementation(Exception): | ||
"""Raised when an invalid RCLPYImplementation is requested.""" | ||
|
||
def __init__(self, *args): | ||
Exception.__init__(self, 'requested invalid rmw implementation', *args) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# Copyright 2016 Open Source Robotics Foundation, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# 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. | ||
|
||
"""Provide singleton access to the rclpy implementation. | ||
The singleton is called ``rclpy_implementation`` and is in this module. | ||
For example, you might use it like this: | ||
.. code:: | ||
from rclpy.impl.implementation_singleton import rclpy_implementation as _rclpy | ||
_rclpy.rclpy_init() | ||
while _rclpy.rclpy_ok(): | ||
# ... | ||
Before you call :py:func:`rclpy.init` this singleton will point to an instance | ||
of the :py:class:`ImplementationPlaceholder` class, which will raise a | ||
:py:class:`rclpy.exceptions.NotInitializedException` when ever accessed. | ||
After initialization it will point to the rclpy implementation C module. | ||
""" | ||
|
||
from rclpy.exceptions import NotInitializedException | ||
from rclpy.impl.object_proxy import ObjectProxy | ||
|
||
|
||
class ImplementationPlaceholder: | ||
"""Placeholder for the rclpy implementation module. | ||
This class will raise a :py:class:`NotInitializedException` when used. | ||
""" | ||
|
||
def __getattr__(self, key): | ||
if key in ['__repr__']: | ||
return object.__getattr__(key) | ||
raise NotInitializedException() | ||
|
||
rclpy_implementation = ObjectProxy(ImplementationPlaceholder()) | ||
|
||
|
||
def set_rclpy_implementation(implementation): | ||
"""Set the rclpy implementation singleton.""" | ||
# Update the ObjectProxy to point to the rmw implementation specific module. | ||
rclpy_implementation.__actual__ = implementation | ||
|
||
|
||
def rclpy_implementation_is_placeholder(implementation=None): | ||
"""Return True if the implementation is a placeholder, else False. | ||
:param implementation: implementation to check, defaults to the singleton | ||
:returns: bool | ||
""" | ||
implementation = rclpy_implementation if implementation is None else implementation | ||
return isinstance(implementation, ImplementationPlaceholder) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
# Copyright 2016 Open Source Robotics Foundation, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# 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. | ||
|
||
"""Provides an Object proxy that allows the underlying class to change.""" | ||
|
||
import ast | ||
import inspect | ||
|
||
# Inspired from: | ||
# https://pypi.python.org/pypi/ProxyTypes (PSF/ZPL license) | ||
|
||
|
||
class AbstractProxy(object): | ||
"""Delegates all operations (except .__actual__) to another object.""" | ||
|
||
__slots__ = () | ||
|
||
def __call__(self, *args, **kw): | ||
return self.__actual__(*args, **kw) | ||
|
||
def __getattribute__(self, attr): | ||
subject = object.__getattribute__(self, '__actual__') | ||
if attr == '__actual__': | ||
return subject | ||
return getattr(subject, attr) | ||
|
||
def __setattr__(self, attr, val): | ||
if attr == '__actual__': | ||
object.__setattr__(self, attr, val) | ||
else: | ||
setattr(self.__actual__, attr, val) | ||
|
||
def __delattr__(self, attr): | ||
if attr == '__actual__': | ||
object.__delattr__(self, attr) | ||
else: | ||
delattr(self.__actual__, attr) | ||
|
||
def __nonzero__(self): | ||
return bool(self.__actual__) | ||
|
||
def __getitem__(self, arg): | ||
return self.__actual__[arg] | ||
|
||
def __setitem__(self, arg, val): | ||
self.__actual__[arg] = val | ||
|
||
def __delitem__(self, arg): | ||
del self.__actual__[arg] | ||
|
||
def __getslice__(self, i, j): | ||
return self.__actual__[i:j] | ||
|
||
def __setslice__(self, i, j, val): | ||
self.__actual__[i:j] = val | ||
|
||
def __delslice__(self, i, j): | ||
del self.__actual__[i:j] | ||
|
||
def __contains__(self, ob): | ||
return ob in self.__actual__ | ||
|
||
for name in [ | ||
'repr', 'str', 'hash', 'len', 'abs', 'complex', 'int', 'long', 'float', | ||
'iter', 'oct', 'hex' | ||
]: | ||
code_object = compile( | ||
'def __{0}__(self): return {0}(self.__actual__)'.format(name), | ||
__file__, | ||
'exec', | ||
ast.PyCF_ONLY_AST, | ||
) | ||
ast.increment_lineno(code_object, inspect.getframeinfo(inspect.currentframe()).lineno - 6) | ||
exec(compile(code_object, __file__, 'exec')) | ||
|
||
for name in ['cmp', 'coerce', 'divmod']: | ||
code_object = compile( | ||
'def __{0}__(self, ob): return {0}(self.__actual__, ob)'.format(name), | ||
__file__, | ||
'exec', | ||
ast.PyCF_ONLY_AST, | ||
) | ||
ast.increment_lineno(code_object, inspect.getframeinfo(inspect.currentframe()).lineno - 6) | ||
exec(compile(code_object, __file__, 'exec')) | ||
|
||
for name, op in [ | ||
('lt', '<'), ('gt', '>'), ('le', '<='), ('ge', '>='), ('eq', '=='), ('ne', '!=') | ||
]: | ||
code_object = compile( | ||
'def __{0}__(self, ob): return self.__actual__ {1} ob'.format(name, op), | ||
__file__, | ||
'exec', | ||
ast.PyCF_ONLY_AST, | ||
) | ||
ast.increment_lineno(code_object, inspect.getframeinfo(inspect.currentframe()).lineno - 6) | ||
exec(compile(code_object, __file__, 'exec')) | ||
|
||
for name, op in [('neg', '-'), ('pos', '+'), ('invert', '~')]: | ||
code_object = compile( | ||
'def __{0}__(self): return {1} self.__actual__'.format(name, op), | ||
__file__, | ||
'exec', | ||
ast.PyCF_ONLY_AST, | ||
) | ||
ast.increment_lineno(code_object, inspect.getframeinfo(inspect.currentframe()).lineno - 6) | ||
exec(compile(code_object, __file__, 'exec')) | ||
|
||
for name, op in [ | ||
('or', '|'), ('and', '&'), ('xor', '^'), ('lshift', '<<'), ('rshift', '>>'), | ||
('add', '+'), ('sub', '-'), ('mul', '*'), ('div', '/'), ('mod', '%'), | ||
('truediv', '/'), ('floordiv', '//') | ||
]: | ||
code_object = compile( | ||
""" | ||
def __{name}__(self,ob): | ||
return self.__actual__ {op} ob | ||
def __r{name}__(self,ob): | ||
return ob {op} self.__actual__ | ||
def __i{name}__(self,ob): | ||
self.__actual__ {op}=ob | ||
return self | ||
""".format(name=name, op=op), | ||
__file__, | ||
'exec', | ||
ast.PyCF_ONLY_AST, | ||
) | ||
ast.increment_lineno(code_object, inspect.getframeinfo(inspect.currentframe()).lineno - 6) | ||
exec(compile(code_object, __file__, 'exec')) | ||
|
||
del name, op | ||
|
||
def __rdivmod__(self, ob): | ||
return divmod(ob, self.__actual__) | ||
|
||
def __pow__(self, *args): | ||
return pow(self.__actual__, *args) | ||
|
||
def __ipow__(self, ob): | ||
self.__actual__ **= ob | ||
return self | ||
|
||
def __rpow__(self, ob): | ||
return pow(ob, self.__actual__) | ||
|
||
|
||
class ObjectProxy(AbstractProxy): | ||
"""Proxy for an internally stored object.""" | ||
|
||
__slots__ = '__actual__' | ||
|
||
def __init__(self, subject): | ||
self.__actual__ = subject |
Oops, something went wrong.