@cryptotaxi247 / netdata-1 / commits / 648084a29

move backward compatibility classes and functions to single file

lgz committed Oct 13, 2017 at 00:10 UTC 648084a291cc981fa7e8f18627d77ed6c13c7824
1 file changed +169
python.d/python_modules/bases/collection.py new
+169
@@ -0,0 +1,169 @@
1 +# -*- coding: utf-8 -*-
2 +# Description:
3 +# Author: Ilya Mashchenko (l2isbad)
4 +
5 +import os
6 +
7 +PATH = os.getenv('PATH', '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin').split(':')
8 +
9 +CHART_BEGIN = 'BEGIN {0} {1}\n'
10 +CHART_CREATE = "CHART {0} '{1}' '{2}' '{3}' '{4}' '{5}' {6} {7} {8}\n"
11 +DIMENSION_CREATE = "DIMENSION '{0}' '{1}' {2} {3} {4} '{5}'\n"
12 +DIMENSION_SET = "SET '{0}' = {1}\n"
13 +
14 +
15 +def setdefault_values(config, base_dict):
16 + for key, value in base_dict.items():
17 + config.setdefault(key, value)
18 + return config
19 +
20 +
21 +def run_and_exit(func):
22 + def wrapper(*args, **kwargs):
23 + func(*args, **kwargs)
24 + exit(1)
25 + return wrapper
26 +
27 +
28 +def on_try_except_finally(on_except=(None, ), on_finally=(None, )):
29 + except_func = on_except[0]
30 + finally_func = on_finally[0]
31 +
32 + def decorator(func):
33 + def wrapper(*args, **kwargs):
34 + try:
35 + func(*args, **kwargs)
36 + except Exception:
37 + if not except_func:
38 + return
39 + except_func(*on_except[1:])
40 + finally:
41 + if not finally_func:
42 + return
43 + finally_func(*on_finally[1:])
44 + return wrapper
45 + return decorator
46 +
47 +
48 +def static_vars(**kwargs):
49 + def decorate(func):
50 + for k in kwargs:
51 + setattr(func, k, kwargs[k])
52 + return func
53 + return decorate
54 +
55 +
56 +@on_try_except_finally(on_except=(exit, 1))
57 +def safe_print(msg):
58 + """
59 + :param msg: <str>
60 + :return:
61 + """
62 + print(msg)
63 +
64 +
65 +class OldVersionCompatibility:
66 +
67 + def __init__(self):
68 + self._data_stream = str()
69 +
70 + def begin(self, type_id, microseconds=0):
71 + """
72 + :param type_id: <str>
73 + :param microseconds: <str> or <int>: must be a digit
74 + :return:
75 + """
76 + self._data_stream += CHART_BEGIN.format(type_id, microseconds)
77 +
78 + def set(self, dim_id, value):
79 + """
80 + :param dim_id: <str>
81 + :param value: <int> or <str>: must be a digit
82 + :return:
83 + """
84 + self._data_stream += DIMENSION_SET.format(dim_id, value)
85 +
86 + def end(self):
87 + self._data_stream += 'END\n'
88 +
89 + def chart(self, type_id, name='', title='', units='', family='', category='', chart_type='line',
90 + priority='', update_every=''):
91 + """
92 + :param type_id: <str>
93 + :param name: <str>
94 + :param title: <str>
95 + :param units: <str>
96 + :param family: <str>
97 + :param category: <str>
98 + :param chart_type: <str>
99 + :param priority: <str> or <int>
100 + :param update_every: <str> or <int>
101 + :return:
102 + """
103 + self._data_stream += CHART_CREATE.format(type_id, name, title, units,
104 + family, category, chart_type,
105 + priority, update_every)
106 +
107 + def dimension(self, dim_id, name=None, algorithm="absolute", multiplier=1, divisor=1, hidden=False):
108 + """
109 + :param dim_id: <str>
110 + :param name: <str> or None
111 + :param algorithm: <str>
112 + :param multiplier: <str> or <int>: must be a digit
113 + :param divisor: <str> or <int>: must be a digit
114 + :param hidden: <str>: literally "hidden" or ""
115 + :return:
116 + """
117 + self._data_stream += DIMENSION_CREATE.format(dim_id, name or dim_id, algorithm,
118 + multiplier, divisor, hidden or str())
119 +
120 + @on_try_except_finally(on_except=(exit, 1))
121 + def commit(self):
122 + print(self._data_stream)
123 + self._data_stream = str()
124 +
125 +
126 +class UsefulFuncs:
127 + @staticmethod
128 + def find_binary(binary):
129 + """
130 + :param binary: <str>
131 + :return:
132 + """
133 + for directory in PATH:
134 + binary_name = '/'.join([directory, binary])
135 + if os.path.isfile(binary_name) and os.access(binary_name, os.X_OK):
136 + return binary_name
137 + return None
138 +
139 + @staticmethod
140 + def is_file_readable(f):
141 + """
142 + :param f: <str>
143 + :return:
144 + """
145 + return os.path.isfile(f) and os.access(f, os.R_OK)
146 +
147 + @staticmethod
148 + def is_file_executable(f):
149 + """
150 + :param f: <str>
151 + :return:
152 + """
153 + return os.path.isfile(f) and os.access(f, os.X_OK)
154 +
155 + @staticmethod
156 + def get_file_size(f):
157 + """
158 + :param f: <str>
159 + :return:
160 + """
161 + return os.path.getsize(f)
162 +
163 + @staticmethod
164 + def is_directory(d):
165 + """
166 + :param d: <str>
167 + :return:
168 + """
169 + return os.path.isdir(d)