diff options
Diffstat (limited to 'pyinfluxtools')
-rw-r--r-- | pyinfluxtools/__init__.py | 58 |
1 files changed, 43 insertions, 15 deletions
diff --git a/pyinfluxtools/__init__.py b/pyinfluxtools/__init__.py index 74ec19a..dd7d0a5 100644 --- a/pyinfluxtools/__init__.py +++ b/pyinfluxtools/__init__.py @@ -15,9 +15,9 @@ class WriteRequest(object): >>> 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 + 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) @@ -29,12 +29,18 @@ class WriteRequest(object): class Write(object): - def __init__(self, key, tags, fields, timestamp): + def __init__(self, key, tags, fields, timestamp=None): self.key = key self.tags = tags self.fields = fields self.timestamp = timestamp + if isinstance(self.tags, dict): + self.tags = self.tags.items() + + if isinstance(self.fields, dict): + self.fields = self.fields.items() + @staticmethod def parse(line): """ @@ -48,27 +54,27 @@ class Write(object): >>> Write.parse('cpu,host=serverA,region=us-west') <Write key=cpu tags=[('host', 'serverA'), ('region', 'us-west')] fields=None timestamp=None> >>> print(Write.parse('cpu,host=serverA,region=us-west')) - cpu,host=serverA,region=us-west + cpu,host="serverA",region="us-west" >>> Write.parse('cpu\\,01,host=serverA,region=us-west') <Write key=cpu,01 tags=[('host', 'serverA'), ('region', 'us-west')] fields=None timestamp=None> >>> print(Write.parse('cpu\,01,host=serverA,region=us-west')) - cpu\,01,host=serverA,region=us-west + cpu\,01,host="serverA",region="us-west" >>> Write.parse('cpu,host=server\\ A,region=us\\ west') <Write key=cpu tags=[('host', 'server A'), ('region', 'us west')] fields=None timestamp=None> >>> print(Write.parse('cpu,host=server\\ A,region=us\\ west')) - cpu,host=server\ A,region=us\ west + cpu,host="server A",region="us west" >>> Write.parse('cpu,ho\=st=server\ A,region=us\ west') <Write key=cpu tags=[('ho=st', 'server A'), ('region', 'us west')] fields=None timestamp=None> >>> print(Write.parse('cpu,ho\=st=server\ A,region=us\ west')) - 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 + 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 + 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')) @@ -81,13 +87,27 @@ class Write(object): def unescape(string): return re.sub(r'(?<!\\)([\\,=])', '', string) + def unescape_value(string): + if string.startswith("\"") and string.endswith("\""): + string = re.sub(r'(?<!\\)(["])', '', string) + else: + string = unescape(string) + if re.match("^[0-9]+$", string): + return int(string) + elif re.match("^[0-9]*\.[0-9]*$", string): + return float(string) + elif string.lower() in ["t", "true", "f", "false"]: + return string.lower in ["t", "true"] + else: + return string + args = re.split(r"(?<!\\) ", line) key, *tags = re.split(r"(?<!\\),", args[0]) key = unescape(key) if tags: tags = map(lambda tag: re.split(r"(?<!\\)=", tag), tags) - tags = map(lambda tag: (unescape(tag[0]), unescape(tag[1])), tags) + tags = map(lambda tag: (unescape(tag[0]), unescape_value(tag[1])), tags) tags = list(tags) else: tags = None @@ -95,7 +115,7 @@ class Write(object): if len(args) > 1: fields = re.split(r"(?<!\\),", args[1]) fields = map(lambda field: re.split(r"(?<!\\)=", field), fields) - fields = map(lambda field: (unescape(field[0]), unescape(field[1])), fields) + fields = map(lambda field: (unescape(field[0]), unescape_value(field[1])), fields) fields = list(fields) else: fields = None @@ -112,15 +132,23 @@ class Write(object): self.__class__.__name__, self.key, self.tags, self.fields, self.timestamp) def __str__(self): - def escape(string): + def escape_key(string): return re.sub(r'([\\,= ])', '\\\\\\1', string) + def escape_value(obj): + if isinstance(obj, float) or isinstance(obj, int) or isinstance(obj, bool): + return str(obj) + else: + obj = str(obj) + return "\"" + obj.replace("\"","\\\"") + "\"" + + def escape_kv(kvlist): return ",".join( - map(lambda kv: escape(kv[0]) + "=" + escape(kv[1]), + map(lambda kv: escape_key(kv[0]) + "=" + escape_value(kv[1]), kvlist)) - result = escape(self.key) + result = escape_key(self.key) if self.tags: result += "," |