escape characters in `_line()`
paulfantom committed
Jun 23, 2016 at 17:00 UTC
5d3d64607672ba3fbe5faaa5411366c51f248946
2 files changed
+18
-16
python.d/example.chart.py
+8
-8
@@ -22,15 +22,15 @@ class Service(BaseService):
22
return True
23
24
def create(self):
25
- chart = "CHART example.python_random '' 'A random number' 'random number' random random line " + \
26
- str(self.priority) + " " + \
27
- str(self.update_every) + "\n" + \
28
- "DIMENSION random1 '' absolute 1 1"
29
- print(chart)
25
+ self.chart("example.python_random", '', 'A random number', 'random number',
26
+ 'random', 'random', 'line', self.priority, self.update_every)
27
+ self.dimension('random1', '', 'absolute', 1, 1)
28
+ self.commit()
29
return True
30
31
def update(self, interval):
33
- chart = "BEGIN example.python_random "+str(interval)+"\n"
34
- chart += "SET random1 = "+str(random.randint(0,100))+"\n"
35
- print(chart + "END")
32
+ self.begin("example.python_random", interval)
33
+ self.set("random1", random.randint(0, 100))
34
+ self.end()
35
+ self.commit()
36
return True
python.d/python_modules/base.py
+10
-8
@@ -137,20 +137,22 @@ class BaseService(threading.Thread):
137
else:
138
time.sleep(self.timetable['freq'])
139
140
- def _line(self, *params):
140
+ def _line(self, instruction, *params):
141
"""
142
Converts *params to string and joins them with one space between every one.
143
:param params: str/int/float
144
"""
145
+ self.data_stream += instruction + " "
146
for p in params:
147
+ p = str(p)
148
if len(p) == 0:
147
- p = "''"
148
- self.data_stream += str(p)
149
- self.data_stream += " "
149
+ p = ""
150
+ self.data_stream += "'" + p
151
+ self.data_stream += "' "
152
self.data_stream += "\n"
153
152
- def chart(self, type_id, name="''", title="''", units="''", family="''",
153
- category="''", charttype="line", priority="''", update_every="''"):
154
+ def chart(self, type_id, name="", title="", units="", family="",
155
+ category="", charttype="line", priority="", update_every=""):
156
"""
157
Defines a new chart.
158
:param type_id: str
@@ -211,7 +213,7 @@ class BaseService(threading.Thread):
213
int(microseconds)
214
except TypeError:
215
self.error("malformed begin statement: microseconds are not a number:", microseconds)
214
- microseconds = "''"
216
+ microseconds = ""
217
218
self._line("BEGIN", type_id, microseconds)
219
return True
@@ -237,7 +239,7 @@ class BaseService(threading.Thread):
239
def end(self):
240
self._line("END")
241
240
- def send(self):
242
+ def commit(self):
243
"""
244
Upload new data to netdata
245
"""