From 095cec445973b55c36d857bee83a56e0ac8b7fed Mon Sep 17 00:00:00 2001 From: Jack Date: Sat, 4 Jul 2015 19:58:18 +0000 Subject: escaping for strings --- pyinfluxtools/__init__.py | 58 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 15 deletions(-) (limited to 'pyinfluxtools') 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') >>> 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') >>> 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') >>> 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') >>> 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'(? 1: fields = re.split(r"(?