move BaseService to base.py
paulfantom committed
Jun 17, 2016 at 15:11 UTC
883489e832200aefa968885a21f77aa0615797b1
2 files changed
+9
-53
python.d/mysql.chart.py
+3
-47
@@ -17,6 +17,8 @@ except ImportError:
17
except ImportError:
18
sys.stderr.write(NAME + ": You need to install PyMySQL module to use mysql.chart.py plugin\n")
19
20
+from base import BaseService
21
+
22
# default configuration (overriden by python.d.plugin)
23
config = [
24
{
@@ -323,53 +325,6 @@ CHARTS = {
325
}
326
327
326
-class BaseService(object):
327
- def __init__(self,configuration,update_every,priority,retries):
328
- if configuration is None:
329
- # use defaults
330
- configuration = config
331
- self.error(NAME+": no configuration supplied. using defaults.")
332
-
333
- self._parse_base_config(configuration,update_every,priority,retries)
334
-
335
- def _parse_base_config(self,config,update_every,priority,retries):
336
- # parse configuration options to run this Service
337
- try:
338
- self.update_every = int(config['update_every'])
339
- except (KeyError, ValueError):
340
- self.update_every = update_every
341
- try:
342
- self.priority = int(config['priority'])
343
- except (KeyError, ValueError):
344
- self.priority = priority
345
- try:
346
- self.retries = int(config['retries'])
347
- except (KeyError, ValueError):
348
- self.retries = retries
349
- self.retries_left = self.retries
350
-
351
- def error(self, msg, exception=""):
352
- if exception != "":
353
- exception = " " + str(exception).replace("\n"," ")
354
- sys.stderr.write(str(msg)+exception+"\n")
355
- sys.stderr.flush()
356
-
357
- def check(self):
358
- # TODO notify about not overriden function
359
- self.error("Where is your check()?")
360
- return False
361
-
362
- def create(self):
363
- # TODO notify about not overriden function
364
- self.error("Where is your create()?")
365
- return False
366
-
367
- def update(self):
368
- # TODO notify about not overriden function
369
- self.error("Where is your update()?")
370
- return False
371
-
372
-
328
class Service(BaseService):
329
def __init__(self,configuration=None,update_every=3,priority=90000,retries=2):
330
super().__init__(configuration,update_every,priority,retries)
@@ -491,6 +446,7 @@ class Service(BaseService):
446
return True
447
448
if __name__ == "__main__":
449
+ sys.path.append("./python_modules")
450
my = Service(config[0])
451
my.check()
452
my.create()
python.d/python_modules/base.py
+6
-6
@@ -2,13 +2,13 @@
2
# Author: Pawel Krupa (paulfantom)
3
4
class BaseService(object):
5
- def __init__(self,configuration,update_every,priority,retries):
6
- if configuration is None:
5
+ def __init__(self,configuration=None,update_every=None,priority=None,retries=None):
6
+ if None in (configuration,update_every,priority,retries):
7
# use defaults
8
- configuration = config
9
- self.error(NAME+": no configuration supplied. using defaults.")
10
-
11
- self._parse_base_config(configuration)
8
+ self.error("BaseService: no configuration parameters supplied. Cannot create Service.")
9
+ raise RuntimeError
10
+ else:
11
+ self._parse_base_config(configuration,update_every,priority,retries)
12
13
def _parse_base_config(self,config,update_every,priority,retries):
14
# parse configuration options to run this Service