data_provider.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import os
3 import rospkg
4 import rosbag
5 
6 from .logger import Logger
7 
8 
9 class DataProvider(object):
10  """ Provides an interface for required test case data. """
11 
12  def __init__(self, bagfile=None):
13  self._bag = None
14 
15  if bagfile is not None:
16  bagpath = ''
17  # absolute path
18  if bagfile.startswith('~') or bagfile.startswith('/'):
19  bagpath = os.path.expanduser(bagfile)
20  # package-relative path
21  else:
22  rp = rospkg.RosPack()
23  pkgpath = rp.get_path(bagfile.split('/')[0])
24  bagpath = os.path.join(pkgpath, '/'.join(bagfile.split('/')[1:]))
25  self._bag = rosbag.Bag(bagpath)
26  Logger.print_positive('using data source: %s' % bagpath)
27 
28  def parse(self, value):
29  """ Replace special values according to the specification. """
30  result = value
31  try:
32  # message data
33  if (isinstance(value, str) and len(value) > 1 and value[0] == '/' and value[1] != '/' and
34  self._bag is not None):
35  try:
36  (_, result, _) = list(self._bag.read_messages(topics=[value]))[0]
37  except IndexError:
38  (_, result, _) = list(self._bag.read_messages(topics=[value[1:]]))[0]
39  # anonymous function
40  elif isinstance(value, str) and value.startswith('lambda '):
41  result = eval(value)
42  # None
43  elif value == 'None':
44  result = None
45  # escaped backslash at the beginning
46  elif isinstance(value, str) and len(value) > 1 and value[0] == '/' and value[1] == '/':
47  result = value[1:]
48  except Exception as e:
49  Logger.print_error('unable to parse value "%s" (will be considered as string):\n\t%s' % (
50  str(value), str(e)
51  ))
52  result = str(value)
53  return result


flexbe_testing
Author(s): Philipp Schillinger
autogenerated on Sun Dec 13 2020 04:01:44