/usr/lib/python3/dist-packages/morse/middleware/text_datastream.py is in python3-morse-simulator 1.4-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | import logging; logger = logging.getLogger("morse." + __name__)
from morse.core import blenderapi
import re
from morse.core.datastream import *
from morse.middleware.abstract_datastream import AbstractDatastream
class BasePublisher(AbstractDatastream):
def initialize(self):
self.filename = self._get_filename()
self.file = open(self.filename, 'wb')
self.index = 0
line = self.header()
self.file.write(line.encode())
def finalize(self):
self.file.close()
def _get_filename(self):
if 'file' in self.kwargs:
return self.kwargs['file']
else:
filename = re.sub(r'\.([0-9]+)', r'\1', self.component_name)
return filename + '.txt'
def default(self, ci):
line = self.encode_data()
self.index += 1
self.file.write(line.encode())
self.file.flush()
def header(self):
return ""
def encode_data(self):
return ""
class Publisher(BasePublisher):
_type_name = "key = value format with timestamp and index value"
def header(self):
lines = ['ROBOT %s || SENSOR %s\n' % (self.component_instance.robot_parent.name(), self.component_name),
'(distance, globalVector(3), localVector(3))\n',
repr(self.component_instance.relative_position) + '\n\n']
return ''.join(lines)
def encode_data(self):
parent_position = self.component_instance.robot_parent.position_3d
lines = ['==> Data at X,Y,Z: [%.6f %.6f %.6f]'
'yaw,pitch,roll: [%.6f %.6f %.6f] | index %d | time %.2f\n'
% (parent_position.x, parent_position.y, parent_position.z,
parent_position.yaw, parent_position.pitch, parent_position.roll,
self.index, blenderapi.persistantstorage().time.time)]
for variable, data in self.data.items():
if isinstance(data, float):
lines.append("\t%s = %.6f\n" % (variable, data))
else:
lines.append("\t%s = %s\n" % (variable, repr(data)))
return ''.join(lines)
class CSVPublisher(BasePublisher):
_type_name = "CSV like : values separated by semi-column"
def header(self):
lines = ['ROBOT %s || SENSOR %s\n' % (self.component_instance.robot_parent.name(), self.component_name),
'(distance, globalVector(3), localVector(3))\n',
repr(self.component_instance.relative_position) + '\n\n']
return ''.join(lines)
def encode_data(self):
lines = []
for variable, data in self.data.items():
if isinstance(data, float):
lines.append("%.6f;" % data)
else:
lines.append("%s;" % repr(data))
return ''.join(lines) + '\n'
class TextDatastreamManager(DatastreamManager):
""" Produce text files as output for the components """
|