mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-09 00:00:00 +01:00
added Python scripts for file manipulation
This commit is contained in:
parent
b7e0edf5fa
commit
ef75512e75
5 changed files with 147 additions and 0 deletions
35
contrib/python/file-merge.py
Executable file
35
contrib/python/file-merge.py
Executable file
|
@ -0,0 +1,35 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import sys
|
||||
|
||||
import s2ss
|
||||
from s2ss import *
|
||||
|
||||
def main():
|
||||
files = sys.argv[1:]
|
||||
|
||||
all = [ ]
|
||||
last = { }
|
||||
|
||||
for file in files:
|
||||
handle = sys.stdin if file == '-' else open(file, "r")
|
||||
|
||||
msgs = [ ]
|
||||
for line in handle.xreadlines():
|
||||
msgs.append(s2ss.Message.parse(line, file))
|
||||
|
||||
all += msgs
|
||||
last[file] = s2ss.Message(s2ss.Timestamp(), [0] * len(msgs[0].values), file)
|
||||
|
||||
all.sort()
|
||||
for msg in all:
|
||||
last[msg.source] = msg
|
||||
|
||||
values = [ ]
|
||||
for file in files:
|
||||
values += last[file].values
|
||||
|
||||
print s2ss.Message(msg.ts, values, "")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
4
contrib/python/s2ss/__init__.py
Normal file
4
contrib/python/s2ss/__init__.py
Normal file
|
@ -0,0 +1,4 @@
|
|||
from msg import Message
|
||||
from ts import Timestamp
|
||||
|
||||
__all__ = [ "msg", "ts" ]
|
24
contrib/python/s2ss/msg.py
Normal file
24
contrib/python/s2ss/msg.py
Normal file
|
@ -0,0 +1,24 @@
|
|||
from . import ts
|
||||
|
||||
class Message:
|
||||
"""Parsing a S2SS from a file (not a UDP package!!)"""
|
||||
|
||||
def __init__(self, ts, values, source = None):
|
||||
self.source = source
|
||||
self.ts = ts
|
||||
self.values = values
|
||||
|
||||
@classmethod
|
||||
def parse(self, line, source = None):
|
||||
csv = line.split()
|
||||
|
||||
t = ts.Timestamp.parse(csv[0])
|
||||
v = map(float, csv[1:])
|
||||
|
||||
return Message(t, v, source)
|
||||
|
||||
def __str__(self):
|
||||
return '%s %s' % (self.ts, self.values)
|
||||
|
||||
def __cmp__(self, other):
|
||||
return cmp(self.ts, other.ts)
|
38
contrib/python/s2ss/ocb.py
Normal file
38
contrib/python/s2ss/ocb.py
Normal file
|
@ -0,0 +1,38 @@
|
|||
import re
|
||||
|
||||
class OcbMapping:
|
||||
def __init__(self, entityId, entityType, attributeName, attributeType):
|
||||
self.entityId = entityId
|
||||
self.entityType = entityType
|
||||
self.attributeName = attributeName
|
||||
self.attributeType = attributeType
|
||||
|
||||
@classmethod
|
||||
def parse(self, lines):
|
||||
m = re.match('([a-z]+)\(([a-z]+)\)\.([a-z]+).\(([a-z]+)\)', re.I)
|
||||
return OcbMapping(m.group(1), m.group(2), m.group(3), m.group(4))
|
||||
|
||||
@classMethod
|
||||
def parseFile(self, file):
|
||||
|
||||
class OcbEntity:
|
||||
|
||||
class OcbAttribute:
|
||||
|
||||
class OcbResponse:
|
||||
def __init__(self, mapping):
|
||||
self.mapping = mapping
|
||||
|
||||
def getJson(self, msg):
|
||||
json = { "contextResponses" : [ ] }
|
||||
|
||||
for (value in msg.values):
|
||||
json["contextResponses"].append({
|
||||
"statusCode" : { "code" : 200, "reasonPhrase" : "OK" }
|
||||
"contextElement" : {
|
||||
"attributes" = [ ],
|
||||
"id" : "",
|
||||
"isPattern" : false,
|
||||
"type"
|
||||
}
|
||||
})
|
46
contrib/python/s2ss/ts.py
Normal file
46
contrib/python/s2ss/ts.py
Normal file
|
@ -0,0 +1,46 @@
|
|||
import re
|
||||
|
||||
class Timestamp:
|
||||
"""Parsing the S2SS timestamp format"""
|
||||
|
||||
def __init__(self, seconds = 0, nanoseconds = None, offset = None, sequence = None):
|
||||
self.seconds = seconds
|
||||
self.nanoseconds = nanoseconds
|
||||
self.offset = offset
|
||||
self.sequence = sequence
|
||||
|
||||
@classmethod
|
||||
def parse(self, ts):
|
||||
m = re.match('(\d+)(?:\.(\d+))?([-+]\d+(?:\.\d+)?(?:e[+-]?\d+)?)?(?:\((\d+)\))?', ts)
|
||||
|
||||
seconds = int(m.group(1)); # Mandatory
|
||||
nanoseconds = int(m.group(2)) if m.group(2) else None
|
||||
offset = float(m.group(3)) if m.group(3) else None
|
||||
sequence = int(m.group(4)) if m.group(4) else None
|
||||
|
||||
return Timestamp(seconds, nanoseconds, offset, sequence)
|
||||
|
||||
def __str__(self):
|
||||
str = "%u" % (self.seconds)
|
||||
|
||||
if self.nanoseconds is not None:
|
||||
str += ".%09u" % self.nanoseconds
|
||||
if self.offset is not None:
|
||||
str += "+%u" % self.offset
|
||||
if self.sequence is not None:
|
||||
str += "(%u)" % self.sequence
|
||||
|
||||
return str
|
||||
|
||||
def __float__(self):
|
||||
sum = float(self.seconds)
|
||||
|
||||
if self.nanoseconds is not None:
|
||||
sum += self.nanoseconds * 1e-9
|
||||
if self.offset is not None:
|
||||
sum += self.offset
|
||||
|
||||
return sum
|
||||
|
||||
def __cmp__(self, other):
|
||||
return cmp(float(self), float(other))
|
Loading…
Add table
Reference in a new issue