Skip to content

Commit

Permalink
Merge pull request #275 from strands-project/revert-274-revert-272-me…
Browse files Browse the repository at this point in the history
…lodic-devel

Revert "Revert "Fix Python 3 bugs in mongodb_store""
  • Loading branch information
hawesie authored Aug 5, 2022
2 parents 7ea3cb4 + 52c2613 commit 3cce5bd
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 25 deletions.
15 changes: 12 additions & 3 deletions mongodb_store/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,18 @@ target_link_libraries(example_multi_event_log

## Mark executable scripts (Python etc.) for installation
## in contrast to setup.py, you can choose the destination
install(DIRECTORY scripts/
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
USE_SOURCE_PERMISSIONS)
catkin_install_python(PROGRAMS
scripts/config_manager.py
scripts/example_message_store_client.py
scripts/example_multi_event_log.py
scripts/message_store_node.py
scripts/mongo_bridge.py
scripts/mongodb_play.py
scripts/mongodb_server.py
scripts/replicator_client.py
scripts/replicator_node.py
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

# Mark other files for installation (e.g. launch and bag files, etc.)
install(
Expand Down
6 changes: 3 additions & 3 deletions mongodb_store/src/mongodb_store/message_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,13 @@ def query(self, type, message_query = {}, meta_query = {}, single = False, sort_
messages = []
metas = []
else:
messages = map(dc_util.deserialise_message, response.messages)
metas = map(dc_util.string_pair_list_to_dictionary, response.metas)
messages = list(map(dc_util.deserialise_message, response.messages))
metas = list(map(dc_util.string_pair_list_to_dictionary, response.metas))

if single:
if len(messages) > 0:
return [messages[0], metas[0]]
else:
return [None, None]
else:
return zip(messages,metas)
return list(zip(messages,metas))
16 changes: 6 additions & 10 deletions mongodb_store/src/mongodb_store/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
import platform
if float(platform.python_version()[0:2]) >= 3.0:
_PY3 = True
import io as StringIO
from io import BytesIO as Buffer
else:
_PY3 = False
import StringIO
from StringIO import StringIO as Buffer
from mongodb_store_msgs.msg import SerialisedMessage
from mongodb_store_msgs.srv import MongoQueryMsgRequest

Expand Down Expand Up @@ -201,16 +201,13 @@ def sanitize_value(attr, v, type):
else:
# ensure unicode
try:
if _PY3:
v = str(v, "utf-8")
else:
if not _PY3: # All strings are unicode in Python 3
v = unicode(v, "utf-8")
except UnicodeDecodeError as e:
# at this point we can deal with the encoding, so treat it as binary
v = Binary(v)
# no need to carry on with the other type checks below
return v

if isinstance(v, rospy.Message):
return msg_to_document(v)
elif isinstance(v, genpy.rostime.Time):
Expand Down Expand Up @@ -332,8 +329,7 @@ def fill_message(message, document):
lst.append(msg)
setattr(message, slot, lst)
else:
if ( (not _PY3 and isinstance(value, unicode)) or
(_PY3 and isinstance(value, str)) ):
if not _PY3 and isinstance(value, unicode): # All strings are unicode in Python 3
setattr(message, slot, value.encode('utf-8'))
else:
setattr(message, slot, value)
Expand Down Expand Up @@ -518,9 +514,9 @@ def serialise_message(message):
:Args:
| message (ROS message): The message to serialise
:Returns:
| mongodb_store_msgs.msg.SerialisedMessage: A serialies copy of message
| mongodb_store_msgs.msg.SerialisedMessage: A serialised copy of message
"""
buf=StringIO.StringIO()
buf = Buffer()
message.serialize(buf)
serialised_msg = SerialisedMessage()
serialised_msg.msg = buf.getvalue()
Expand Down
15 changes: 6 additions & 9 deletions mongodb_store/tests/test_messagestore.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,16 @@ def test_add_message(self):
# get documents with limit
result_limited = msg_store.query(Pose._type, message_query={'orientation.z': {'$gt': 10} }, sort_query=[("$natural", 1)], limit=10)
self.assertEqual(len(result_limited), 10)
self.assertListEqual([int(doc[0].orientation.x) for doc in result_limited], range(10))
self.assertListEqual([int(doc[0].orientation.x) for doc in result_limited], list(range(10)))

#get documents without "orientation" field
result_no_id = msg_store.query(Pose._type, message_query={}, projection_query={"orientation": 0})
#get documents without "orientation" field
result_no_id = msg_store.query(Pose._type, message_query={}, projection_query={"orientation": 0})
for doc in result_no_id:
self.assertEqual(int(doc[0].orientation.z),0 )



self.assertEqual(int(doc[0].orientation.z), 0)

# must remove the item or unittest only really valid once
print meta["_id"]
print str(meta["_id"])
print(meta["_id"])
print(str(meta["_id"]))
deleted = msg_store.delete(str(meta["_id"]))
self.assertTrue(deleted)

Expand Down

0 comments on commit 3cce5bd

Please sign in to comment.