Skip to content

Commit

Permalink
Merge pull request #91 from gramaziokohler/validate_time_input
Browse files Browse the repository at this point in the history
Validate time input
  • Loading branch information
gonzalocasas authored Feb 22, 2022
2 parents 715be2c + f5fe263 commit a0cc505
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 32 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,9 @@ trim_trailing_whitespace = false
indent_style = tab
indent_size = 4

[*.yml]
indent_style = space
indent_size = 2

[LICENSE]
insert_final_newline = false
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Unreleased

* Fixed #87 where a goal could be marked as terminal on result alone rather
than both result and status.
* Ensure input of ``Time`` is always two integers.

**Deprecated**

Expand Down
7 changes: 5 additions & 2 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
attrs>=19.2.0
bump2version
autopep8
bump2version >=1.0.1
check-manifest>=0.36
doc8
flake8
invoke>=0.14
isort>=4.3.21
pydocstyle
pytest
sphinx>=1.5
sphinx >=3.4
twine
-e .
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ line_length = 180
known_first_party = roslibpy
default_section = THIRDPARTY
forced_separate = test_roslibpy
skip = migrations
skip = migrations, __init__.py
68 changes: 47 additions & 21 deletions src/roslibpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,24 +90,50 @@ class and are passed around via :class:`Topics <Topic>` using a **publish/subscr
"""

from .__version__ import __author__
from .__version__ import __author_email__
from .__version__ import __copyright__
from .__version__ import __description__
from .__version__ import __license__
from .__version__ import __title__
from .__version__ import __url__
from .__version__ import __version__
from .core import Header
from .core import Message
from .core import Param
from .core import Service
from .core import ServiceRequest
from .core import ServiceResponse
from .core import Time
from .core import Topic
from .ros import Ros
from .ros import set_rosapi_timeout

__all__ = ['Ros', 'set_rosapi_timeout', 'Message', 'Header', 'Time', 'Param', 'Service', 'ServiceRequest', 'ServiceResponse', 'Topic',
'__author__', '__author_email__', '__copyright__', '__description__', '__license__', '__title__', '__url__', '__version__']
from .__version__ import (
__author__,
__author_email__,
__copyright__,
__description__,
__license__,
__title__,
__url__,
__version__
)
from .core import (
Header,
Message,
Param,
Service,
ServiceRequest,
ServiceResponse,
Time,
Topic
)
from .ros import (
set_rosapi_timeout,
Ros
)

__all__ = [
'__author__',
'__author_email__',
'__copyright__',
'__description__',
'__license__',
'__title__',
'__url__',
'__version__',

'Header',
'Message',
'Param',
'Service',
'ServiceRequest',
'ServiceResponse',
'Time',
'Topic',

'set_rosapi_timeout',
'Ros',
]
27 changes: 19 additions & 8 deletions src/roslibpy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@

LOGGER = logging.getLogger('roslibpy')

__all__ = ['Message',
'ServiceRequest',
'ServiceResponse',
'Topic',
'Service',
'Param']
__all__ = [
'Header',
'Message',
'Param',
'Service',
'ServiceRequest',
'ServiceResponse',
'Time',
'Topic'
]


class Message(UserDict):
Expand All @@ -44,8 +48,15 @@ class Time(UserDict):
"""Represents ROS time with two integers: seconds since epoch and nanoseconds since seconds."""
def __init__(self, secs, nsecs):
self.data = {}
self.data['secs'] = secs
self.data['nsecs'] = nsecs
self.data['secs'] = self._ensure_int(secs)
self.data['nsecs'] = self._ensure_int(nsecs)

def _ensure_int(self, n):
if isinstance(n, int):
return n
if isinstance(n, float) and n.is_integer():
return int(n)
raise ValueError('argument must be an integer')

@property
def secs(self):
Expand Down
21 changes: 21 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

from roslibpy import Header
from roslibpy import Time

Expand Down Expand Up @@ -37,3 +39,22 @@ def test_header_ctor_supports_dict():
assert header['stamp']['secs'] == 1610122759
assert header['stamp']['secs'] == header['stamp'].secs
assert header['stamp'].to_sec() == REF_FLOAT_SECS_TIME


def test_time_accepts_only_ints():
with pytest.raises(ValueError):
Time(1.3, 1.0)
with pytest.raises(ValueError):
Time(100.0, 3.1)

t = Time(110.0, 0.0)
assert t.secs == 110
assert t.nsecs == 0


def test_time_properties_are_readonly():
t = Time.now()
with pytest.raises(AttributeError):
t.secs = 10
with pytest.raises(AttributeError):
t.nsecs = 10

0 comments on commit a0cc505

Please sign in to comment.