From ef75512e752fd80b3db350877d0c43969860208e Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Wed, 21 Oct 2015 12:22:59 +0200 Subject: [PATCH] added Python scripts for file manipulation --- contrib/python/file-merge.py | 35 +++++++++++++++++++++++++ contrib/python/s2ss/__init__.py | 4 +++ contrib/python/s2ss/msg.py | 24 +++++++++++++++++ contrib/python/s2ss/ocb.py | 38 +++++++++++++++++++++++++++ contrib/python/s2ss/ts.py | 46 +++++++++++++++++++++++++++++++++ 5 files changed, 147 insertions(+) create mode 100755 contrib/python/file-merge.py create mode 100644 contrib/python/s2ss/__init__.py create mode 100644 contrib/python/s2ss/msg.py create mode 100644 contrib/python/s2ss/ocb.py create mode 100644 contrib/python/s2ss/ts.py diff --git a/contrib/python/file-merge.py b/contrib/python/file-merge.py new file mode 100755 index 000000000..c2e58a8cd --- /dev/null +++ b/contrib/python/file-merge.py @@ -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() diff --git a/contrib/python/s2ss/__init__.py b/contrib/python/s2ss/__init__.py new file mode 100644 index 000000000..d3cf5a604 --- /dev/null +++ b/contrib/python/s2ss/__init__.py @@ -0,0 +1,4 @@ +from msg import Message +from ts import Timestamp + +__all__ = [ "msg", "ts" ] \ No newline at end of file diff --git a/contrib/python/s2ss/msg.py b/contrib/python/s2ss/msg.py new file mode 100644 index 000000000..a7cb0dada --- /dev/null +++ b/contrib/python/s2ss/msg.py @@ -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) \ No newline at end of file diff --git a/contrib/python/s2ss/ocb.py b/contrib/python/s2ss/ocb.py new file mode 100644 index 000000000..8231957e5 --- /dev/null +++ b/contrib/python/s2ss/ocb.py @@ -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" + } + }) diff --git a/contrib/python/s2ss/ts.py b/contrib/python/s2ss/ts.py new file mode 100644 index 000000000..0b4ac7384 --- /dev/null +++ b/contrib/python/s2ss/ts.py @@ -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)) \ No newline at end of file