From 8aefb69293cd37bf81c178ab8102db980e6f46a3 Mon Sep 17 00:00:00 2001 From: Jack Date: Sat, 4 Jul 2015 12:37:58 +0000 Subject: 0.1 --- pyinfluxtools/__init__.py | 143 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 pyinfluxtools/__init__.py (limited to 'pyinfluxtools/__init__.py') diff --git a/pyinfluxtools/__init__.py b/pyinfluxtools/__init__.py new file mode 100644 index 0000000..74ec19a --- /dev/null +++ b/pyinfluxtools/__init__.py @@ -0,0 +1,143 @@ +#!/usr/bin/env python3 +import re + + +class WriteRequest(object): + @staticmethod + def parse(lines): + """ + Parse multiple Write objects separeted by new-line character. + + >>> lines = [] + >>> lines += ['cpu'] + >>> lines += ['cpu,host=serverA,region=us-west'] + >>> lines += ['cpu,host=serverA,region=us-west field1=1,field2=2'] + >>> lines += ['cpu,host=serverA,region=us-west field1=1,field2=2 1234'] + >>> print("\\n".join(map(str, WriteRequest.parse("\\n".join(lines))))) + cpu + cpu,host=serverA,region=us-west + cpu,host=serverA,region=us-west field1=1,field2=2 + cpu,host=serverA,region=us-west field1=1,field2=2 1234 + """ + writes = map(Write.parse, lines.split("\n")) + return list(writes) + + @staticmethod + def parseFile(file): + for line in file.readlines(): + yield Write.parse(line) + + +class Write(object): + def __init__(self, key, tags, fields, timestamp): + self.key = key + self.tags = tags + self.fields = fields + self.timestamp = timestamp + + @staticmethod + def parse(line): + """ + Parse a line from the POST request into a Write object. + + >>> Write.parse('cpu') + + >>> print(Write.parse('cpu')) + cpu + + >>> Write.parse('cpu,host=serverA,region=us-west') + + >>> print(Write.parse('cpu,host=serverA,region=us-west')) + cpu,host=serverA,region=us-west + + >>> Write.parse('cpu\\,01,host=serverA,region=us-west') + + >>> print(Write.parse('cpu\,01,host=serverA,region=us-west')) + cpu\,01,host=serverA,region=us-west + + >>> Write.parse('cpu,host=server\\ A,region=us\\ west') + + >>> print(Write.parse('cpu,host=server\\ A,region=us\\ west')) + cpu,host=server\ A,region=us\ west + + >>> Write.parse('cpu,ho\=st=server\ A,region=us\ west') + + >>> print(Write.parse('cpu,ho\=st=server\ A,region=us\ west')) + cpu,ho\=st=server\ A,region=us\ west + + >>> print(Write.parse('cpu,ho\=st=server\ A field=123')) + cpu,ho\=st=server\ A field=123 + >>> print(Write.parse('cpu,foo=bar,foo=bar field=123,field=123')) # error: double name is accepted + cpu,foo=bar,foo=bar field=123,field=123 + >>> print(Write.parse('cpu field12=12')) + cpu field12=12 + >>> print(Write.parse('cpu field12=12 123123123')) + cpu field12=12 123123123 + >>> print(Write.parse('cpu field12=12 1231abcdef123')) + Traceback (most recent call last): + ... + ValueError: invalid literal for int() with base 10: '1231abcdef123' + """ + def unescape(string): + return re.sub(r'(? 1: + fields = re.split(r"(? 2: + timestamp = int(args[2]) + else: + timestamp = None + + return Write(key, tags, fields, timestamp) + + def __repr__(self): + return "<{} key={} tags={} fields={} timestamp={}>".format( + self.__class__.__name__, self.key, self.tags, self.fields, self.timestamp) + + def __str__(self): + def escape(string): + return re.sub(r'([\\,= ])', '\\\\\\1', string) + + def escape_kv(kvlist): + return ",".join( + map(lambda kv: escape(kv[0]) + "=" + escape(kv[1]), + kvlist)) + + result = escape(self.key) + + if self.tags: + result += "," + result += escape_kv(self.tags) + + if self.fields: + result += " " + result += escape_kv(self.fields) + + if self.timestamp: + result += " " + result += str(self.timestamp) + + return result + + +if __name__ == "__main__": + import doctest + doctest.testmod() + -- cgit v1.2.1