@@ -1,167 +1,157 | |||
|
1 | 1 | import click |
|
2 | 2 | import schainpy |
|
3 | 3 | import subprocess |
|
4 | 4 | import os |
|
5 | 5 | import sys |
|
6 | 6 | import glob |
|
7 | 7 | save_stdout = sys.stdout |
|
8 | 8 | sys.stdout = open('trash', 'w') |
|
9 | 9 | from multiprocessing import cpu_count |
|
10 | 10 | from schaincli import templates |
|
11 |
from schainpy import |
|
|
11 | from schainpy.controller import Project | |
|
12 | 12 | from schainpy.model import Operation, ProcessingUnit |
|
13 | 13 | from schainpy.utils import log |
|
14 | 14 | from importlib import import_module |
|
15 | 15 | from pydoc import locate |
|
16 | 16 | from fuzzywuzzy import process |
|
17 | 17 | from schainpy.utils import paramsFinder |
|
18 | 18 | sys.stdout = save_stdout |
|
19 | 19 | |
|
20 | 20 | |
|
21 | 21 | def print_version(ctx, param, value): |
|
22 | 22 | if not value or ctx.resilient_parsing: |
|
23 | 23 | return |
|
24 | 24 | click.echo(schainpy.__version__) |
|
25 | 25 | ctx.exit() |
|
26 | 26 | |
|
27 | 27 | |
|
28 | 28 | cliLogger = log.makelogger('schain cli') |
|
29 | 29 | PREFIX = 'experiment' |
|
30 | 30 | |
|
31 | 31 | |
|
32 | 32 | @click.command() |
|
33 | 33 | @click.option('--version', '-v', is_flag=True, callback=print_version, help='SChain version', type=str) |
|
34 | 34 | @click.option('--xml', '-x', default=None, help='run an XML file', type=click.Path(exists=True, resolve_path=True)) |
|
35 | 35 | @click.argument('command', default='run', required=True) |
|
36 | 36 | @click.argument('nextcommand', default=None, required=False, type=str) |
|
37 | 37 | def main(command, nextcommand, version, xml): |
|
38 | 38 | """COMMAND LINE INTERFACE FOR SIGNAL CHAIN - JICAMARCA RADIO OBSERVATORY \n |
|
39 | 39 | Available commands.\n |
|
40 | 40 | --xml: runs a schain XML generated file\n |
|
41 | 41 | run: runs any python script starting 'experiment_'\n |
|
42 | 42 | generate: generates a template schain script\n |
|
43 | 43 | search: return avilable operations, procs or arguments of the give operation/proc\n""" |
|
44 | 44 | if xml is not None: |
|
45 | 45 | runFromXML(xml) |
|
46 | 46 | elif command == 'generate': |
|
47 | 47 | generate() |
|
48 | 48 | elif command == 'test': |
|
49 | 49 | test() |
|
50 | 50 | elif command == 'run': |
|
51 | 51 | runschain(nextcommand) |
|
52 | 52 | elif command == 'search': |
|
53 | 53 | search(nextcommand) |
|
54 | 54 | else: |
|
55 | 55 | log.error('Command {} is not defined'.format(command)) |
|
56 | 56 | |
|
57 | 57 | def check_module(possible, instance): |
|
58 | 58 | def check(x): |
|
59 | 59 | try: |
|
60 | 60 | instancia = locate('schainpy.model.{}'.format(x)) |
|
61 | 61 | return isinstance(instancia(), instance) |
|
62 | 62 | except Exception as e: |
|
63 | 63 | return False |
|
64 | 64 | clean = clean_modules(possible) |
|
65 | 65 | return [x for x in clean if check(x)] |
|
66 | 66 | |
|
67 | 67 | |
|
68 | 68 | def clean_modules(module): |
|
69 | 69 | noEndsUnder = [x for x in module if not x.endswith('__')] |
|
70 | 70 | noStartUnder = [x for x in noEndsUnder if not x.startswith('__')] |
|
71 | 71 | noFullUpper = [x for x in noStartUnder if not x.isupper()] |
|
72 | 72 | return noFullUpper |
|
73 | 73 | |
|
74 | 74 | |
|
75 | 75 | def search(nextcommand): |
|
76 | 76 | if nextcommand is None: |
|
77 | 77 | log.error('There is no Operation/ProcessingUnit to search') |
|
78 | 78 | elif nextcommand == 'procs': |
|
79 | 79 | procs = paramsFinder.getProcs() |
|
80 | 80 | log.success('Current ProcessingUnits are:\n\033[1m{}\033[0m'.format('\n'.join(procs))) |
|
81 | 81 | |
|
82 | 82 | elif nextcommand == 'operations': |
|
83 | 83 | operations = paramsFinder.getOperations() |
|
84 | 84 | log.success('Current Operations are:\n\033[1m{}\033[0m'.format('\n'.join(operations))) |
|
85 | 85 | else: |
|
86 | 86 | try: |
|
87 | 87 | args = paramsFinder.getArgs(nextcommand) |
|
88 | 88 | log.warning('Use this feature with caution. It may not return all the allowed arguments') |
|
89 | 89 | if len(args) == 0: |
|
90 | 90 | log.success('{} has no arguments'.format(nextcommand)) |
|
91 | 91 | else: |
|
92 | 92 | log.success('Showing arguments of {} are:\n\033[1m{}\033[0m'.format(nextcommand, '\n'.join(args))) |
|
93 | 93 | except Exception as e: |
|
94 | 94 | log.error('Module {} does not exists'.format(nextcommand)) |
|
95 | 95 | allModules = paramsFinder.getAll() |
|
96 | 96 | similar = process.extractOne(nextcommand, allModules)[0] |
|
97 | 97 | log.success('Showing {} instead'.format(similar)) |
|
98 | 98 | search(similar) |
|
99 | 99 | |
|
100 | 100 | |
|
101 | 101 | def runschain(nextcommand): |
|
102 | 102 | if nextcommand is None: |
|
103 | 103 | currentfiles = glob.glob('./{}_*.py'.format(PREFIX)) |
|
104 | 104 | numberfiles = len(currentfiles) |
|
105 | 105 | if numberfiles > 1: |
|
106 | 106 | log.error('There is more than one file to run') |
|
107 | 107 | elif numberfiles == 1: |
|
108 | 108 | subprocess.call(['python ' + currentfiles[0]], shell=True) |
|
109 | 109 | else: |
|
110 | 110 | log.error('There is no file to run') |
|
111 | 111 | else: |
|
112 | 112 | try: |
|
113 | 113 | subprocess.call(['python ' + nextcommand], shell=True) |
|
114 | 114 | except Exception as e: |
|
115 | 115 | log.error("I cannot run the file. Does it exists?") |
|
116 | 116 | |
|
117 | 117 | |
|
118 | 118 | def basicInputs(): |
|
119 | 119 | inputs = {} |
|
120 | 120 | inputs['desc'] = click.prompt('Enter a description', default="A schain project", type=str) |
|
121 | 121 | inputs['name'] = click.prompt('Name of the project', default="project", type=str) |
|
122 | 122 | inputs['path'] = click.prompt('Data path', default=os.getcwd(), type=click.Path(exists=True, resolve_path=True)) |
|
123 | 123 | inputs['startDate'] = click.prompt('Start date', default='1970/01/01', type=str) |
|
124 | 124 | inputs['endDate'] = click.prompt('End date', default='2017/12/31', type=str) |
|
125 | 125 | inputs['startHour'] = click.prompt('Start hour', default='00:00:00', type=str) |
|
126 | 126 | inputs['endHour'] = click.prompt('End hour', default='23:59:59', type=str) |
|
127 | 127 | inputs['figpath'] = inputs['path'] + '/figs' |
|
128 | 128 | return inputs |
|
129 | 129 | |
|
130 | 130 | |
|
131 | 131 | def generate(): |
|
132 | 132 | inputs = basicInputs() |
|
133 | 133 | inputs['multiprocess'] = click.confirm('Is this a multiprocess script?') |
|
134 | 134 | if inputs['multiprocess']: |
|
135 | 135 | inputs['nProcess'] = click.prompt('How many process?', default=cpu_count(), type=int) |
|
136 | 136 | current = templates.multiprocess.format(**inputs) |
|
137 | 137 | else: |
|
138 | 138 | current = templates.basic.format(**inputs) |
|
139 | 139 | scriptname = '{}_{}.py'.format(PREFIX, inputs['name']) |
|
140 | 140 | script = open(scriptname, 'w') |
|
141 | 141 | try: |
|
142 | 142 | script.write(current) |
|
143 | 143 | log.success('Script {} generated'.format(scriptname)) |
|
144 | 144 | except Exception as e: |
|
145 | 145 | log.error('I cannot create the file. Do you have writing permissions?') |
|
146 | 146 | |
|
147 | 147 | |
|
148 | 148 | def test(): |
|
149 | 149 | log.warning('testing') |
|
150 | 150 | |
|
151 | 151 | |
|
152 | 152 | def runFromXML(filename): |
|
153 |
controller = |
|
|
153 | controller = Project() | |
|
154 | 154 | if not controller.readXml(filename): |
|
155 | 155 | return |
|
156 | ||
|
157 | plotterObj = controller.useExternalPlotter() | |
|
158 | ||
|
159 | 156 | controller.start() |
|
160 | plotterObj.start() | |
|
161 | ||
|
162 | cliLogger("Finishing all processes") | |
|
163 | ||
|
164 | controller.join(5) | |
|
165 | ||
|
166 | cliLogger("End of script") | |
|
167 | 157 | return |
@@ -1,1325 +1,1330 | |||
|
1 | 1 | ''' |
|
2 | 2 | Created on September , 2012 |
|
3 | 3 | @author: |
|
4 | 4 | ''' |
|
5 | 5 | |
|
6 | 6 | import sys |
|
7 | ||
|
8 | # save_stdout = sys.stdout | |
|
9 | # logToFile = newLogger() | |
|
10 | # sys.stdout = logToFile | |
|
11 | # sys.stderr = logToFile | |
|
12 | ||
|
7 | 13 | import ast |
|
8 | 14 | import datetime |
|
9 | 15 | import traceback |
|
10 | 16 | import math |
|
11 | 17 | import time |
|
12 | 18 | from multiprocessing import Process, Queue, cpu_count |
|
13 | 19 | |
|
14 | 20 | import schainpy |
|
15 | 21 | import schainpy.admin |
|
22 | from schainpy.utils.log import logToFile | |
|
16 | 23 | |
|
17 | 24 | from xml.etree.ElementTree import ElementTree, Element, SubElement, tostring |
|
18 | 25 | from xml.dom import minidom |
|
19 | 26 | |
|
20 | 27 | from schainpy.model import * |
|
21 | 28 | from time import sleep |
|
22 | 29 | |
|
23 | 30 | |
|
24 | 31 | |
|
25 | 32 | def prettify(elem): |
|
26 | 33 | """Return a pretty-printed XML string for the Element. |
|
27 | 34 | """ |
|
28 | 35 | rough_string = tostring(elem, 'utf-8') |
|
29 | 36 | reparsed = minidom.parseString(rough_string) |
|
30 | 37 | return reparsed.toprettyxml(indent=" ") |
|
31 | 38 | |
|
32 | 39 | def multiSchain(child, nProcess=cpu_count(), startDate=None, endDate=None, by_day=False): |
|
33 | 40 | skip = 0 |
|
34 | 41 | cursor = 0 |
|
35 | 42 | nFiles = None |
|
36 | 43 | processes = [] |
|
37 | 44 | dt1 = datetime.datetime.strptime(startDate, '%Y/%m/%d') |
|
38 | 45 | dt2 = datetime.datetime.strptime(endDate, '%Y/%m/%d') |
|
39 | 46 | days = (dt2 - dt1).days |
|
40 | 47 | |
|
41 | 48 | for day in range(days+1): |
|
42 | 49 | skip = 0 |
|
43 | 50 | cursor = 0 |
|
44 | 51 | q = Queue() |
|
45 | 52 | processes = [] |
|
46 | 53 | dt = (dt1 + datetime.timedelta(day)).strftime('%Y/%m/%d') |
|
47 | 54 | firstProcess = Process(target=child, args=(cursor, skip, q, dt)) |
|
48 | 55 | firstProcess.start() |
|
49 | 56 | if by_day: |
|
50 | 57 | continue |
|
51 | 58 | nFiles = q.get() |
|
52 | 59 | if nFiles==0: |
|
53 | 60 | continue |
|
54 | 61 | firstProcess.terminate() |
|
55 | 62 | skip = int(math.ceil(nFiles/nProcess)) |
|
56 | 63 | while True: |
|
57 | 64 | processes.append(Process(target=child, args=(cursor, skip, q, dt))) |
|
58 | 65 | processes[cursor].start() |
|
59 | 66 | if nFiles < cursor*skip: |
|
60 | 67 | break |
|
61 | 68 | cursor += 1 |
|
62 | 69 | |
|
63 | 70 | def beforeExit(exctype, value, trace): |
|
64 | 71 | for process in processes: |
|
65 | 72 | process.terminate() |
|
66 | 73 | process.join() |
|
67 | 74 | print traceback.print_tb(trace) |
|
68 | 75 | |
|
69 | 76 | sys.excepthook = beforeExit |
|
70 | 77 | |
|
71 | 78 | for process in processes: |
|
72 | 79 | process.join() |
|
73 | 80 | process.terminate() |
|
74 | 81 | |
|
75 | 82 | time.sleep(3) |
|
76 | 83 | |
|
77 | 84 | |
|
78 | 85 | class ParameterConf(): |
|
79 | 86 | |
|
80 | 87 | id = None |
|
81 | 88 | name = None |
|
82 | 89 | value = None |
|
83 | 90 | format = None |
|
84 | 91 | |
|
85 | 92 | __formated_value = None |
|
86 | 93 | |
|
87 | 94 | ELEMENTNAME = 'Parameter' |
|
88 | 95 | |
|
89 | 96 | def __init__(self): |
|
90 | 97 | |
|
91 | 98 | self.format = 'str' |
|
92 | 99 | |
|
93 | 100 | def getElementName(self): |
|
94 | 101 | |
|
95 | 102 | return self.ELEMENTNAME |
|
96 | 103 | |
|
97 | 104 | def getValue(self): |
|
98 | 105 | |
|
99 | 106 | value = self.value |
|
100 | 107 | format = self.format |
|
101 | 108 | |
|
102 | 109 | if self.__formated_value != None: |
|
103 | 110 | |
|
104 | 111 | return self.__formated_value |
|
105 | 112 | |
|
106 | 113 | if format == 'obj': |
|
107 | 114 | return value |
|
108 | 115 | |
|
109 | 116 | if format == 'str': |
|
110 | 117 | self.__formated_value = str(value) |
|
111 | 118 | return self.__formated_value |
|
112 | 119 | |
|
113 | 120 | if value == '': |
|
114 | 121 | raise ValueError, "%s: This parameter value is empty" %self.name |
|
115 | 122 | |
|
116 | 123 | if format == 'list': |
|
117 | 124 | strList = value.split(',') |
|
118 | 125 | |
|
119 | 126 | self.__formated_value = strList |
|
120 | 127 | |
|
121 | 128 | return self.__formated_value |
|
122 | 129 | |
|
123 | 130 | if format == 'intlist': |
|
124 | 131 | """ |
|
125 | 132 | Example: |
|
126 | 133 | value = (0,1,2) |
|
127 | 134 | """ |
|
128 | 135 | |
|
129 | 136 | new_value = ast.literal_eval(value) |
|
130 | 137 | |
|
131 | 138 | if type(new_value) not in (tuple, list): |
|
132 | 139 | new_value = [int(new_value)] |
|
133 | 140 | |
|
134 | 141 | self.__formated_value = new_value |
|
135 | 142 | |
|
136 | 143 | return self.__formated_value |
|
137 | 144 | |
|
138 | 145 | if format == 'floatlist': |
|
139 | 146 | """ |
|
140 | 147 | Example: |
|
141 | 148 | value = (0.5, 1.4, 2.7) |
|
142 | 149 | """ |
|
143 | 150 | |
|
144 | 151 | new_value = ast.literal_eval(value) |
|
145 | 152 | |
|
146 | 153 | if type(new_value) not in (tuple, list): |
|
147 | 154 | new_value = [float(new_value)] |
|
148 | 155 | |
|
149 | 156 | self.__formated_value = new_value |
|
150 | 157 | |
|
151 | 158 | return self.__formated_value |
|
152 | 159 | |
|
153 | 160 | if format == 'date': |
|
154 | 161 | strList = value.split('/') |
|
155 | 162 | intList = [int(x) for x in strList] |
|
156 | 163 | date = datetime.date(intList[0], intList[1], intList[2]) |
|
157 | 164 | |
|
158 | 165 | self.__formated_value = date |
|
159 | 166 | |
|
160 | 167 | return self.__formated_value |
|
161 | 168 | |
|
162 | 169 | if format == 'time': |
|
163 | 170 | strList = value.split(':') |
|
164 | 171 | intList = [int(x) for x in strList] |
|
165 | 172 | time = datetime.time(intList[0], intList[1], intList[2]) |
|
166 | 173 | |
|
167 | 174 | self.__formated_value = time |
|
168 | 175 | |
|
169 | 176 | return self.__formated_value |
|
170 | 177 | |
|
171 | 178 | if format == 'pairslist': |
|
172 | 179 | """ |
|
173 | 180 | Example: |
|
174 | 181 | value = (0,1),(1,2) |
|
175 | 182 | """ |
|
176 | 183 | |
|
177 | 184 | new_value = ast.literal_eval(value) |
|
178 | 185 | |
|
179 | 186 | if type(new_value) not in (tuple, list): |
|
180 | 187 | raise ValueError, "%s has to be a tuple or list of pairs" %value |
|
181 | 188 | |
|
182 | 189 | if type(new_value[0]) not in (tuple, list): |
|
183 | 190 | if len(new_value) != 2: |
|
184 | 191 | raise ValueError, "%s has to be a tuple or list of pairs" %value |
|
185 | 192 | new_value = [new_value] |
|
186 | 193 | |
|
187 | 194 | for thisPair in new_value: |
|
188 | 195 | if len(thisPair) != 2: |
|
189 | 196 | raise ValueError, "%s has to be a tuple or list of pairs" %value |
|
190 | 197 | |
|
191 | 198 | self.__formated_value = new_value |
|
192 | 199 | |
|
193 | 200 | return self.__formated_value |
|
194 | 201 | |
|
195 | 202 | if format == 'multilist': |
|
196 | 203 | """ |
|
197 | 204 | Example: |
|
198 | 205 | value = (0,1,2),(3,4,5) |
|
199 | 206 | """ |
|
200 | 207 | multiList = ast.literal_eval(value) |
|
201 | 208 | |
|
202 | 209 | if type(multiList[0]) == int: |
|
203 | 210 | multiList = ast.literal_eval("(" + value + ")") |
|
204 | 211 | |
|
205 | 212 | self.__formated_value = multiList |
|
206 | 213 | |
|
207 | 214 | return self.__formated_value |
|
208 | 215 | |
|
209 | 216 | if format == 'bool': |
|
210 | 217 | value = int(value) |
|
211 | 218 | |
|
212 | 219 | if format == 'int': |
|
213 | 220 | value = float(value) |
|
214 | 221 | |
|
215 | 222 | format_func = eval(format) |
|
216 | 223 | |
|
217 | 224 | self.__formated_value = format_func(value) |
|
218 | 225 | |
|
219 | 226 | return self.__formated_value |
|
220 | 227 | |
|
221 | 228 | def updateId(self, new_id): |
|
222 | 229 | |
|
223 | 230 | self.id = str(new_id) |
|
224 | 231 | |
|
225 | 232 | def setup(self, id, name, value, format='str'): |
|
226 | 233 | self.id = str(id) |
|
227 | 234 | self.name = name |
|
228 | 235 | if format == 'obj': |
|
229 | 236 | self.value = value |
|
230 | 237 | else: |
|
231 | 238 | self.value = str(value) |
|
232 | 239 | self.format = str.lower(format) |
|
233 | 240 | |
|
234 | 241 | self.getValue() |
|
235 | 242 | |
|
236 | 243 | return 1 |
|
237 | 244 | |
|
238 | 245 | def update(self, name, value, format='str'): |
|
239 | 246 | |
|
240 | 247 | self.name = name |
|
241 | 248 | self.value = str(value) |
|
242 | 249 | self.format = format |
|
243 | 250 | |
|
244 | 251 | def makeXml(self, opElement): |
|
245 | 252 | if self.name not in ('queue',): |
|
246 | 253 | parmElement = SubElement(opElement, self.ELEMENTNAME) |
|
247 | 254 | parmElement.set('id', str(self.id)) |
|
248 | 255 | parmElement.set('name', self.name) |
|
249 | 256 | parmElement.set('value', self.value) |
|
250 | 257 | parmElement.set('format', self.format) |
|
251 | 258 | |
|
252 | 259 | def readXml(self, parmElement): |
|
253 | 260 | |
|
254 | 261 | self.id = parmElement.get('id') |
|
255 | 262 | self.name = parmElement.get('name') |
|
256 | 263 | self.value = parmElement.get('value') |
|
257 | 264 | self.format = str.lower(parmElement.get('format')) |
|
258 | 265 | |
|
259 | 266 | #Compatible with old signal chain version |
|
260 | 267 | if self.format == 'int' and self.name == 'idfigure': |
|
261 | 268 | self.name = 'id' |
|
262 | 269 | |
|
263 | 270 | def printattr(self): |
|
264 | 271 | |
|
265 | 272 | print "Parameter[%s]: name = %s, value = %s, format = %s" %(self.id, self.name, self.value, self.format) |
|
266 | 273 | |
|
267 | 274 | class OperationConf(): |
|
268 | 275 | |
|
269 | 276 | id = None |
|
270 | 277 | name = None |
|
271 | 278 | priority = None |
|
272 | 279 | type = None |
|
273 | 280 | |
|
274 | 281 | parmConfObjList = [] |
|
275 | 282 | |
|
276 | 283 | ELEMENTNAME = 'Operation' |
|
277 | 284 | |
|
278 | 285 | def __init__(self): |
|
279 | 286 | |
|
280 | 287 | self.id = '0' |
|
281 | 288 | self.name = None |
|
282 | 289 | self.priority = None |
|
283 | 290 | self.type = 'self' |
|
284 | 291 | |
|
285 | 292 | |
|
286 | 293 | def __getNewId(self): |
|
287 | 294 | |
|
288 | 295 | return int(self.id)*10 + len(self.parmConfObjList) + 1 |
|
289 | 296 | |
|
290 | 297 | def updateId(self, new_id): |
|
291 | 298 | |
|
292 | 299 | self.id = str(new_id) |
|
293 | 300 | |
|
294 | 301 | n = 1 |
|
295 | 302 | for parmObj in self.parmConfObjList: |
|
296 | 303 | |
|
297 | 304 | idParm = str(int(new_id)*10 + n) |
|
298 | 305 | parmObj.updateId(idParm) |
|
299 | 306 | |
|
300 | 307 | n += 1 |
|
301 | 308 | |
|
302 | 309 | def getElementName(self): |
|
303 | 310 | |
|
304 | 311 | return self.ELEMENTNAME |
|
305 | 312 | |
|
306 | 313 | def getParameterObjList(self): |
|
307 | 314 | |
|
308 | 315 | return self.parmConfObjList |
|
309 | 316 | |
|
310 | 317 | def getParameterObj(self, parameterName): |
|
311 | 318 | |
|
312 | 319 | for parmConfObj in self.parmConfObjList: |
|
313 | 320 | |
|
314 | 321 | if parmConfObj.name != parameterName: |
|
315 | 322 | continue |
|
316 | 323 | |
|
317 | 324 | return parmConfObj |
|
318 | 325 | |
|
319 | 326 | return None |
|
320 | 327 | |
|
321 | 328 | def getParameterObjfromValue(self, parameterValue): |
|
322 | 329 | |
|
323 | 330 | for parmConfObj in self.parmConfObjList: |
|
324 | 331 | |
|
325 | 332 | if parmConfObj.getValue() != parameterValue: |
|
326 | 333 | continue |
|
327 | 334 | |
|
328 | 335 | return parmConfObj.getValue() |
|
329 | 336 | |
|
330 | 337 | return None |
|
331 | 338 | |
|
332 | 339 | def getParameterValue(self, parameterName): |
|
333 | 340 | |
|
334 | 341 | parameterObj = self.getParameterObj(parameterName) |
|
335 | 342 | |
|
336 | 343 | # if not parameterObj: |
|
337 | 344 | # return None |
|
338 | 345 | |
|
339 | 346 | value = parameterObj.getValue() |
|
340 | 347 | |
|
341 | 348 | return value |
|
342 | 349 | |
|
343 | 350 | |
|
344 | 351 | def getKwargs(self): |
|
345 | 352 | |
|
346 | 353 | kwargs = {} |
|
347 | 354 | |
|
348 | 355 | for parmConfObj in self.parmConfObjList: |
|
349 | 356 | if self.name == 'run' and parmConfObj.name == 'datatype': |
|
350 | 357 | continue |
|
351 | 358 | |
|
352 | 359 | kwargs[parmConfObj.name] = parmConfObj.getValue() |
|
353 | 360 | |
|
354 | 361 | return kwargs |
|
355 | 362 | |
|
356 | 363 | def setup(self, id, name, priority, type): |
|
357 | 364 | |
|
358 | 365 | self.id = str(id) |
|
359 | 366 | self.name = name |
|
360 | 367 | self.type = type |
|
361 | 368 | self.priority = priority |
|
362 | 369 | |
|
363 | 370 | self.parmConfObjList = [] |
|
364 | 371 | |
|
365 | 372 | def removeParameters(self): |
|
366 | 373 | |
|
367 | 374 | for obj in self.parmConfObjList: |
|
368 | 375 | del obj |
|
369 | 376 | |
|
370 | 377 | self.parmConfObjList = [] |
|
371 | 378 | |
|
372 | 379 | def addParameter(self, name, value, format='str'): |
|
373 | 380 | |
|
374 | 381 | id = self.__getNewId() |
|
375 | 382 | |
|
376 | 383 | parmConfObj = ParameterConf() |
|
377 | 384 | if not parmConfObj.setup(id, name, value, format): |
|
378 | 385 | return None |
|
379 | 386 | |
|
380 | 387 | self.parmConfObjList.append(parmConfObj) |
|
381 | 388 | |
|
382 | 389 | return parmConfObj |
|
383 | 390 | |
|
384 | 391 | def changeParameter(self, name, value, format='str'): |
|
385 | 392 | |
|
386 | 393 | parmConfObj = self.getParameterObj(name) |
|
387 | 394 | parmConfObj.update(name, value, format) |
|
388 | 395 | |
|
389 | 396 | return parmConfObj |
|
390 | 397 | |
|
391 | 398 | def makeXml(self, procUnitElement): |
|
392 | 399 | |
|
393 | 400 | opElement = SubElement(procUnitElement, self.ELEMENTNAME) |
|
394 | 401 | opElement.set('id', str(self.id)) |
|
395 | 402 | opElement.set('name', self.name) |
|
396 | 403 | opElement.set('type', self.type) |
|
397 | 404 | opElement.set('priority', str(self.priority)) |
|
398 | 405 | |
|
399 | 406 | for parmConfObj in self.parmConfObjList: |
|
400 | 407 | parmConfObj.makeXml(opElement) |
|
401 | 408 | |
|
402 | 409 | def readXml(self, opElement): |
|
403 | 410 | |
|
404 | 411 | self.id = opElement.get('id') |
|
405 | 412 | self.name = opElement.get('name') |
|
406 | 413 | self.type = opElement.get('type') |
|
407 | 414 | self.priority = opElement.get('priority') |
|
408 | 415 | |
|
409 | 416 | #Compatible with old signal chain version |
|
410 | 417 | #Use of 'run' method instead 'init' |
|
411 | 418 | if self.type == 'self' and self.name == 'init': |
|
412 | 419 | self.name = 'run' |
|
413 | 420 | |
|
414 | 421 | self.parmConfObjList = [] |
|
415 | 422 | |
|
416 | 423 | parmElementList = opElement.iter(ParameterConf().getElementName()) |
|
417 | 424 | |
|
418 | 425 | for parmElement in parmElementList: |
|
419 | 426 | parmConfObj = ParameterConf() |
|
420 | 427 | parmConfObj.readXml(parmElement) |
|
421 | 428 | |
|
422 | 429 | #Compatible with old signal chain version |
|
423 | 430 | #If an 'plot' OPERATION is found, changes name operation by the value of its type PARAMETER |
|
424 | 431 | if self.type != 'self' and self.name == 'Plot': |
|
425 | 432 | if parmConfObj.format == 'str' and parmConfObj.name == 'type': |
|
426 | 433 | self.name = parmConfObj.value |
|
427 | 434 | continue |
|
428 | 435 | |
|
429 | 436 | self.parmConfObjList.append(parmConfObj) |
|
430 | 437 | |
|
431 | 438 | def printattr(self): |
|
432 | 439 | |
|
433 | 440 | print "%s[%s]: name = %s, type = %s, priority = %s" %(self.ELEMENTNAME, |
|
434 | 441 | self.id, |
|
435 | 442 | self.name, |
|
436 | 443 | self.type, |
|
437 | 444 | self.priority) |
|
438 | 445 | |
|
439 | 446 | for parmConfObj in self.parmConfObjList: |
|
440 | 447 | parmConfObj.printattr() |
|
441 | 448 | |
|
442 | 449 | def createObject(self, plotter_queue=None): |
|
443 | 450 | |
|
444 | 451 | |
|
445 | 452 | if self.type == 'self': |
|
446 | 453 | raise ValueError, "This operation type cannot be created" |
|
447 | 454 | |
|
448 | 455 | if self.type == 'plotter': |
|
449 | 456 | #Plotter(plotter_name) |
|
450 | 457 | if not plotter_queue: |
|
451 | 458 | raise ValueError, "plotter_queue is not defined. Use:\nmyProject = Project()\nmyProject.setPlotterQueue(plotter_queue)" |
|
452 | 459 | |
|
453 | 460 | opObj = Plotter(self.name, plotter_queue) |
|
454 | 461 | |
|
455 | 462 | if self.type == 'external' or self.type == 'other': |
|
456 | 463 | |
|
457 | 464 | className = eval(self.name) |
|
458 | 465 | kwargs = self.getKwargs() |
|
459 | 466 | |
|
460 | 467 | opObj = className(**kwargs) |
|
461 | 468 | |
|
462 | 469 | return opObj |
|
463 | 470 | |
|
464 | 471 | |
|
465 | 472 | class ProcUnitConf(): |
|
466 | 473 | |
|
467 | 474 | id = None |
|
468 | 475 | name = None |
|
469 | 476 | datatype = None |
|
470 | 477 | inputId = None |
|
471 | 478 | parentId = None |
|
472 | 479 | |
|
473 | 480 | opConfObjList = [] |
|
474 | 481 | |
|
475 | 482 | procUnitObj = None |
|
476 | 483 | opObjList = [] |
|
477 | 484 | |
|
478 | 485 | ELEMENTNAME = 'ProcUnit' |
|
479 | 486 | |
|
480 | 487 | def __init__(self): |
|
481 | 488 | |
|
482 | 489 | self.id = None |
|
483 | 490 | self.datatype = None |
|
484 | 491 | self.name = None |
|
485 | 492 | self.inputId = None |
|
486 | 493 | |
|
487 | 494 | self.opConfObjList = [] |
|
488 | 495 | |
|
489 | 496 | self.procUnitObj = None |
|
490 | 497 | self.opObjDict = {} |
|
491 | 498 | |
|
492 | 499 | def __getPriority(self): |
|
493 | 500 | |
|
494 | 501 | return len(self.opConfObjList)+1 |
|
495 | 502 | |
|
496 | 503 | def __getNewId(self): |
|
497 | 504 | |
|
498 | 505 | return int(self.id)*10 + len(self.opConfObjList) + 1 |
|
499 | 506 | |
|
500 | 507 | def getElementName(self): |
|
501 | 508 | |
|
502 | 509 | return self.ELEMENTNAME |
|
503 | 510 | |
|
504 | 511 | def getId(self): |
|
505 | 512 | |
|
506 | 513 | return self.id |
|
507 | 514 | |
|
508 | 515 | def updateId(self, new_id, parentId=parentId): |
|
509 | 516 | |
|
510 | 517 | |
|
511 | 518 | new_id = int(parentId)*10 + (int(self.id) % 10) |
|
512 | 519 | new_inputId = int(parentId)*10 + (int(self.inputId) % 10) |
|
513 | 520 | |
|
514 | 521 | #If this proc unit has not inputs |
|
515 | 522 | if self.inputId == '0': |
|
516 | 523 | new_inputId = 0 |
|
517 | 524 | |
|
518 | 525 | n = 1 |
|
519 | 526 | for opConfObj in self.opConfObjList: |
|
520 | 527 | |
|
521 | 528 | idOp = str(int(new_id)*10 + n) |
|
522 | 529 | opConfObj.updateId(idOp) |
|
523 | 530 | |
|
524 | 531 | n += 1 |
|
525 | 532 | |
|
526 | 533 | self.parentId = str(parentId) |
|
527 | 534 | self.id = str(new_id) |
|
528 | 535 | self.inputId = str(new_inputId) |
|
529 | 536 | |
|
530 | 537 | |
|
531 | 538 | def getInputId(self): |
|
532 | 539 | |
|
533 | 540 | return self.inputId |
|
534 | 541 | |
|
535 | 542 | def getOperationObjList(self): |
|
536 | 543 | |
|
537 | 544 | return self.opConfObjList |
|
538 | 545 | |
|
539 | 546 | def getOperationObj(self, name=None): |
|
540 | 547 | |
|
541 | 548 | for opConfObj in self.opConfObjList: |
|
542 | 549 | |
|
543 | 550 | if opConfObj.name != name: |
|
544 | 551 | continue |
|
545 | 552 | |
|
546 | 553 | return opConfObj |
|
547 | 554 | |
|
548 | 555 | return None |
|
549 | 556 | |
|
550 | 557 | def getOpObjfromParamValue(self, value=None): |
|
551 | 558 | |
|
552 | 559 | for opConfObj in self.opConfObjList: |
|
553 | 560 | if opConfObj.getParameterObjfromValue(parameterValue=value) != value: |
|
554 | 561 | continue |
|
555 | 562 | return opConfObj |
|
556 | 563 | return None |
|
557 | 564 | |
|
558 | 565 | def getProcUnitObj(self): |
|
559 | 566 | |
|
560 | 567 | return self.procUnitObj |
|
561 | 568 | |
|
562 | 569 | def setup(self, id, name, datatype, inputId, parentId=None): |
|
563 | 570 | |
|
564 | 571 | #Compatible with old signal chain version |
|
565 | 572 | if datatype==None and name==None: |
|
566 | 573 | raise ValueError, "datatype or name should be defined" |
|
567 | 574 | |
|
568 | 575 | if name==None: |
|
569 | 576 | if 'Proc' in datatype: |
|
570 | 577 | name = datatype |
|
571 | 578 | else: |
|
572 | 579 | name = '%sProc' %(datatype) |
|
573 | 580 | |
|
574 | 581 | if datatype==None: |
|
575 | 582 | datatype = name.replace('Proc','') |
|
576 | 583 | |
|
577 | 584 | self.id = str(id) |
|
578 | 585 | self.name = name |
|
579 | 586 | self.datatype = datatype |
|
580 | 587 | self.inputId = inputId |
|
581 | 588 | self.parentId = parentId |
|
582 | 589 | |
|
583 | 590 | self.opConfObjList = [] |
|
584 | 591 | |
|
585 | 592 | self.addOperation(name='run', optype='self') |
|
586 | 593 | |
|
587 | 594 | def removeOperations(self): |
|
588 | 595 | |
|
589 | 596 | for obj in self.opConfObjList: |
|
590 | 597 | del obj |
|
591 | 598 | |
|
592 | 599 | self.opConfObjList = [] |
|
593 | 600 | self.addOperation(name='run') |
|
594 | 601 | |
|
595 | 602 | def addParameter(self, **kwargs): |
|
596 | 603 | ''' |
|
597 | 604 | Add parameters to "run" operation |
|
598 | 605 | ''' |
|
599 | 606 | opObj = self.opConfObjList[0] |
|
600 | 607 | |
|
601 | 608 | opObj.addParameter(**kwargs) |
|
602 | 609 | |
|
603 | 610 | return opObj |
|
604 | 611 | |
|
605 | 612 | def addOperation(self, name, optype='self'): |
|
606 | 613 | |
|
607 | 614 | id = self.__getNewId() |
|
608 | 615 | priority = self.__getPriority() |
|
609 | 616 | |
|
610 | 617 | opConfObj = OperationConf() |
|
611 | 618 | opConfObj.setup(id, name=name, priority=priority, type=optype) |
|
612 | 619 | |
|
613 | 620 | self.opConfObjList.append(opConfObj) |
|
614 | 621 | |
|
615 | 622 | return opConfObj |
|
616 | 623 | |
|
617 | 624 | def makeXml(self, projectElement): |
|
618 | 625 | |
|
619 | 626 | procUnitElement = SubElement(projectElement, self.ELEMENTNAME) |
|
620 | 627 | procUnitElement.set('id', str(self.id)) |
|
621 | 628 | procUnitElement.set('name', self.name) |
|
622 | 629 | procUnitElement.set('datatype', self.datatype) |
|
623 | 630 | procUnitElement.set('inputId', str(self.inputId)) |
|
624 | 631 | |
|
625 | 632 | for opConfObj in self.opConfObjList: |
|
626 | 633 | opConfObj.makeXml(procUnitElement) |
|
627 | 634 | |
|
628 | 635 | def readXml(self, upElement): |
|
629 | 636 | |
|
630 | 637 | self.id = upElement.get('id') |
|
631 | 638 | self.name = upElement.get('name') |
|
632 | 639 | self.datatype = upElement.get('datatype') |
|
633 | 640 | self.inputId = upElement.get('inputId') |
|
634 | 641 | |
|
635 | 642 | if self.ELEMENTNAME == "ReadUnit": |
|
636 | 643 | self.datatype = self.datatype.replace("Reader", "") |
|
637 | 644 | |
|
638 | 645 | if self.ELEMENTNAME == "ProcUnit": |
|
639 | 646 | self.datatype = self.datatype.replace("Proc", "") |
|
640 | 647 | |
|
641 | 648 | if self.inputId == 'None': |
|
642 | 649 | self.inputId = '0' |
|
643 | 650 | |
|
644 | 651 | self.opConfObjList = [] |
|
645 | 652 | |
|
646 | 653 | opElementList = upElement.iter(OperationConf().getElementName()) |
|
647 | 654 | |
|
648 | 655 | for opElement in opElementList: |
|
649 | 656 | opConfObj = OperationConf() |
|
650 | 657 | opConfObj.readXml(opElement) |
|
651 | 658 | self.opConfObjList.append(opConfObj) |
|
652 | 659 | |
|
653 | 660 | def printattr(self): |
|
654 | 661 | |
|
655 | 662 | print "%s[%s]: name = %s, datatype = %s, inputId = %s" %(self.ELEMENTNAME, |
|
656 | 663 | self.id, |
|
657 | 664 | self.name, |
|
658 | 665 | self.datatype, |
|
659 | 666 | self.inputId) |
|
660 | 667 | |
|
661 | 668 | for opConfObj in self.opConfObjList: |
|
662 | 669 | opConfObj.printattr() |
|
663 | 670 | |
|
664 | 671 | |
|
665 | 672 | def getKwargs(self): |
|
666 | 673 | |
|
667 | 674 | opObj = self.opConfObjList[0] |
|
668 | 675 | kwargs = opObj.getKwargs() |
|
669 | 676 | |
|
670 | 677 | return kwargs |
|
671 | 678 | |
|
672 | 679 | def createObjects(self, plotter_queue=None): |
|
673 | 680 | |
|
674 | 681 | className = eval(self.name) |
|
675 | 682 | kwargs = self.getKwargs() |
|
676 | 683 | procUnitObj = className(**kwargs) |
|
677 | 684 | |
|
678 | 685 | for opConfObj in self.opConfObjList: |
|
679 | 686 | |
|
680 | 687 | if opConfObj.type=='self' and self.name=='run': |
|
681 | 688 | continue |
|
682 | 689 | elif opConfObj.type=='self': |
|
683 | 690 | procUnitObj.addOperationKwargs(opConfObj.id, **opConfObj.getKwargs()) |
|
684 | 691 | continue |
|
685 | 692 | |
|
686 | 693 | opObj = opConfObj.createObject(plotter_queue) |
|
687 | 694 | |
|
688 | 695 | self.opObjDict[opConfObj.id] = opObj |
|
689 | 696 | |
|
690 | 697 | procUnitObj.addOperation(opObj, opConfObj.id) |
|
691 | 698 | |
|
692 | 699 | self.procUnitObj = procUnitObj |
|
693 | 700 | |
|
694 | 701 | return procUnitObj |
|
695 | 702 | |
|
696 | 703 | def run(self): |
|
697 | 704 | |
|
698 | 705 | is_ok = False |
|
699 | 706 | |
|
700 | 707 | for opConfObj in self.opConfObjList: |
|
701 | 708 | |
|
702 | 709 | kwargs = {} |
|
703 | 710 | for parmConfObj in opConfObj.getParameterObjList(): |
|
704 | 711 | if opConfObj.name == 'run' and parmConfObj.name == 'datatype': |
|
705 | 712 | continue |
|
706 | 713 | |
|
707 | 714 | kwargs[parmConfObj.name] = parmConfObj.getValue() |
|
708 | 715 | |
|
709 | 716 | #ini = time.time() |
|
710 | 717 | |
|
711 | 718 | #print "\tRunning the '%s' operation with %s" %(opConfObj.name, opConfObj.id) |
|
712 | 719 | sts = self.procUnitObj.call(opType = opConfObj.type, |
|
713 | 720 | opName = opConfObj.name, |
|
714 | 721 | opId = opConfObj.id) |
|
715 | 722 | |
|
716 | 723 | # total_time = time.time() - ini |
|
717 | 724 | # |
|
718 | 725 | # if total_time > 0.002: |
|
719 | 726 | # print "%s::%s took %f seconds" %(self.name, opConfObj.name, total_time) |
|
720 | 727 | |
|
721 | 728 | is_ok = is_ok or sts |
|
722 | 729 | |
|
723 | 730 | return is_ok |
|
724 | 731 | |
|
725 | 732 | def close(self): |
|
726 | 733 | |
|
727 | 734 | for opConfObj in self.opConfObjList: |
|
728 | 735 | if opConfObj.type == 'self': |
|
729 | 736 | continue |
|
730 | 737 | |
|
731 | 738 | opObj = self.procUnitObj.getOperationObj(opConfObj.id) |
|
732 | 739 | opObj.close() |
|
733 | 740 | |
|
734 | 741 | self.procUnitObj.close() |
|
735 | 742 | |
|
736 | 743 | return |
|
737 | 744 | |
|
738 | 745 | class ReadUnitConf(ProcUnitConf): |
|
739 | 746 | |
|
740 | 747 | path = None |
|
741 | 748 | startDate = None |
|
742 | 749 | endDate = None |
|
743 | 750 | startTime = None |
|
744 | 751 | endTime = None |
|
745 | 752 | |
|
746 | 753 | ELEMENTNAME = 'ReadUnit' |
|
747 | 754 | |
|
748 | 755 | def __init__(self): |
|
749 | 756 | |
|
750 | 757 | self.id = None |
|
751 | 758 | self.datatype = None |
|
752 | 759 | self.name = None |
|
753 | 760 | self.inputId = None |
|
754 | 761 | |
|
755 | 762 | self.parentId = None |
|
756 | 763 | |
|
757 | 764 | self.opConfObjList = [] |
|
758 | 765 | self.opObjList = [] |
|
759 | 766 | |
|
760 | 767 | def getElementName(self): |
|
761 | 768 | |
|
762 | 769 | return self.ELEMENTNAME |
|
763 | 770 | |
|
764 | 771 | def setup(self, id, name, datatype, path='', startDate="", endDate="", startTime="", |
|
765 | 772 | endTime="", parentId=None, queue=None, server=None, **kwargs): |
|
766 | 773 | #Compatible with old signal chain version |
|
767 | 774 | if datatype==None and name==None: |
|
768 | 775 | raise ValueError, "datatype or name should be defined" |
|
769 | 776 | |
|
770 | 777 | if name==None: |
|
771 | 778 | if 'Reader' in datatype: |
|
772 | 779 | name = datatype |
|
773 | 780 | else: |
|
774 | 781 | name = '%sReader' %(datatype) |
|
775 | 782 | if datatype==None: |
|
776 | 783 | datatype = name.replace('Reader','') |
|
777 | 784 | |
|
778 | 785 | self.id = id |
|
779 | 786 | self.name = name |
|
780 | 787 | self.datatype = datatype |
|
781 | 788 | if path != '': |
|
782 | 789 | self.path = os.path.abspath(path) |
|
783 | 790 | self.startDate = startDate |
|
784 | 791 | self.endDate = endDate |
|
785 | 792 | self.startTime = startTime |
|
786 | 793 | self.endTime = endTime |
|
787 | 794 | |
|
788 | 795 | self.inputId = '0' |
|
789 | 796 | self.parentId = parentId |
|
790 | 797 | self.queue = queue |
|
791 | 798 | self.server = server |
|
792 | 799 | self.addRunOperation(**kwargs) |
|
793 | 800 | |
|
794 | 801 | def update(self, datatype, path, startDate, endDate, startTime, endTime, parentId=None, name=None, **kwargs): |
|
795 | 802 | |
|
796 | 803 | #Compatible with old signal chain version |
|
797 | 804 | if datatype==None and name==None: |
|
798 | 805 | raise ValueError, "datatype or name should be defined" |
|
799 | 806 | |
|
800 | 807 | if name==None: |
|
801 | 808 | if 'Reader' in datatype: |
|
802 | 809 | name = datatype |
|
803 | 810 | else: |
|
804 | 811 | name = '%sReader' %(datatype) |
|
805 | 812 | |
|
806 | 813 | if datatype==None: |
|
807 | 814 | datatype = name.replace('Reader','') |
|
808 | 815 | |
|
809 | 816 | self.datatype = datatype |
|
810 | 817 | self.name = name |
|
811 | 818 | self.path = path |
|
812 | 819 | self.startDate = startDate |
|
813 | 820 | self.endDate = endDate |
|
814 | 821 | self.startTime = startTime |
|
815 | 822 | self.endTime = endTime |
|
816 | 823 | |
|
817 | 824 | self.inputId = '0' |
|
818 | 825 | self.parentId = parentId |
|
819 | 826 | |
|
820 | 827 | self.updateRunOperation(**kwargs) |
|
821 | 828 | |
|
822 | 829 | def removeOperations(self): |
|
823 | 830 | |
|
824 | 831 | for obj in self.opConfObjList: |
|
825 | 832 | del obj |
|
826 | 833 | |
|
827 | 834 | self.opConfObjList = [] |
|
828 | 835 | |
|
829 | 836 | def addRunOperation(self, **kwargs): |
|
830 | 837 | |
|
831 | 838 | opObj = self.addOperation(name = 'run', optype = 'self') |
|
832 | 839 | |
|
833 | 840 | if self.server is None: |
|
834 | 841 | opObj.addParameter(name='datatype' , value=self.datatype, format='str') |
|
835 | 842 | opObj.addParameter(name='path' , value=self.path, format='str') |
|
836 | 843 | opObj.addParameter(name='startDate' , value=self.startDate, format='date') |
|
837 | 844 | opObj.addParameter(name='endDate' , value=self.endDate, format='date') |
|
838 | 845 | opObj.addParameter(name='startTime' , value=self.startTime, format='time') |
|
839 | 846 | opObj.addParameter(name='endTime' , value=self.endTime, format='time') |
|
840 | 847 | opObj.addParameter(name='queue' , value=self.queue, format='obj') |
|
841 | 848 | for key, value in kwargs.items(): |
|
842 | 849 | opObj.addParameter(name=key, value=value, format=type(value).__name__) |
|
843 | 850 | else: |
|
844 | 851 | opObj.addParameter(name='server' , value=self.server, format='str') |
|
845 | 852 | |
|
846 | 853 | |
|
847 | 854 | return opObj |
|
848 | 855 | |
|
849 | 856 | def updateRunOperation(self, **kwargs): |
|
850 | 857 | |
|
851 | 858 | opObj = self.getOperationObj(name = 'run') |
|
852 | 859 | opObj.removeParameters() |
|
853 | 860 | |
|
854 | 861 | opObj.addParameter(name='datatype' , value=self.datatype, format='str') |
|
855 | 862 | opObj.addParameter(name='path' , value=self.path, format='str') |
|
856 | 863 | opObj.addParameter(name='startDate' , value=self.startDate, format='date') |
|
857 | 864 | opObj.addParameter(name='endDate' , value=self.endDate, format='date') |
|
858 | 865 | opObj.addParameter(name='startTime' , value=self.startTime, format='time') |
|
859 | 866 | opObj.addParameter(name='endTime' , value=self.endTime, format='time') |
|
860 | 867 | |
|
861 | 868 | for key, value in kwargs.items(): |
|
862 | 869 | opObj.addParameter(name=key, value=value, format=type(value).__name__) |
|
863 | 870 | |
|
864 | 871 | return opObj |
|
865 | 872 | |
|
866 | 873 | # def makeXml(self, projectElement): |
|
867 | 874 | # |
|
868 | 875 | # procUnitElement = SubElement(projectElement, self.ELEMENTNAME) |
|
869 | 876 | # procUnitElement.set('id', str(self.id)) |
|
870 | 877 | # procUnitElement.set('name', self.name) |
|
871 | 878 | # procUnitElement.set('datatype', self.datatype) |
|
872 | 879 | # procUnitElement.set('inputId', str(self.inputId)) |
|
873 | 880 | # |
|
874 | 881 | # for opConfObj in self.opConfObjList: |
|
875 | 882 | # opConfObj.makeXml(procUnitElement) |
|
876 | 883 | |
|
877 | 884 | def readXml(self, upElement): |
|
878 | 885 | |
|
879 | 886 | self.id = upElement.get('id') |
|
880 | 887 | self.name = upElement.get('name') |
|
881 | 888 | self.datatype = upElement.get('datatype') |
|
882 | 889 | self.inputId = upElement.get('inputId') |
|
883 | 890 | |
|
884 | 891 | if self.ELEMENTNAME == "ReadUnit": |
|
885 | 892 | self.datatype = self.datatype.replace("Reader", "") |
|
886 | 893 | |
|
887 | 894 | if self.inputId == 'None': |
|
888 | 895 | self.inputId = '0' |
|
889 | 896 | |
|
890 | 897 | self.opConfObjList = [] |
|
891 | 898 | |
|
892 | 899 | opElementList = upElement.iter(OperationConf().getElementName()) |
|
893 | 900 | |
|
894 | 901 | for opElement in opElementList: |
|
895 | 902 | opConfObj = OperationConf() |
|
896 | 903 | opConfObj.readXml(opElement) |
|
897 | 904 | self.opConfObjList.append(opConfObj) |
|
898 | 905 | |
|
899 | 906 | if opConfObj.name == 'run': |
|
900 | 907 | self.path = opConfObj.getParameterValue('path') |
|
901 | 908 | self.startDate = opConfObj.getParameterValue('startDate') |
|
902 | 909 | self.endDate = opConfObj.getParameterValue('endDate') |
|
903 | 910 | self.startTime = opConfObj.getParameterValue('startTime') |
|
904 | 911 | self.endTime = opConfObj.getParameterValue('endTime') |
|
905 | 912 | |
|
906 | 913 | class Project(Process): |
|
907 | ||
|
908 | 914 | id = None |
|
909 | 915 | name = None |
|
910 | 916 | description = None |
|
911 | 917 | filename = None |
|
912 | 918 | |
|
913 | 919 | procUnitConfObjDict = None |
|
914 | 920 | |
|
915 | 921 | ELEMENTNAME = 'Project' |
|
916 | 922 | |
|
917 | 923 | plotterQueue = None |
|
918 | 924 | |
|
919 | def __init__(self, plotter_queue=None): | |
|
925 | def __init__(self, plotter_queue=None, logfile=None): | |
|
920 | 926 | Process.__init__(self) |
|
921 | 927 | self.id = None |
|
922 | 928 | self.name = None |
|
923 | 929 | self.description = None |
|
924 | ||
|
930 | if logfile is not None: | |
|
931 | logToFile(logfile) | |
|
925 | 932 | self.plotterQueue = plotter_queue |
|
926 | 933 | |
|
927 | 934 | self.procUnitConfObjDict = {} |
|
928 | ||
|
935 | ||
|
929 | 936 | def __getNewId(self): |
|
930 | 937 | |
|
931 | 938 | idList = self.procUnitConfObjDict.keys() |
|
932 | 939 | |
|
933 | 940 | id = int(self.id)*10 |
|
934 | 941 | |
|
935 | 942 | while True: |
|
936 | 943 | id += 1 |
|
937 | 944 | |
|
938 | 945 | if str(id) in idList: |
|
939 | 946 | continue |
|
940 | 947 | |
|
941 | 948 | break |
|
942 | 949 | |
|
943 | 950 | return str(id) |
|
944 | 951 | |
|
945 | 952 | def getElementName(self): |
|
946 | 953 | |
|
947 | 954 | return self.ELEMENTNAME |
|
948 | 955 | |
|
949 | 956 | def getId(self): |
|
950 | 957 | |
|
951 | 958 | return self.id |
|
952 | 959 | |
|
953 | 960 | def updateId(self, new_id): |
|
954 | 961 | |
|
955 | 962 | self.id = str(new_id) |
|
956 | 963 | |
|
957 | 964 | keyList = self.procUnitConfObjDict.keys() |
|
958 | 965 | keyList.sort() |
|
959 | 966 | |
|
960 | 967 | n = 1 |
|
961 | 968 | newProcUnitConfObjDict = {} |
|
962 | 969 | |
|
963 | 970 | for procKey in keyList: |
|
964 | 971 | |
|
965 | 972 | procUnitConfObj = self.procUnitConfObjDict[procKey] |
|
966 | 973 | idProcUnit = str(int(self.id)*10 + n) |
|
967 | 974 | procUnitConfObj.updateId(idProcUnit, parentId = self.id) |
|
968 | 975 | |
|
969 | 976 | newProcUnitConfObjDict[idProcUnit] = procUnitConfObj |
|
970 | 977 | n += 1 |
|
971 | 978 | |
|
972 | 979 | self.procUnitConfObjDict = newProcUnitConfObjDict |
|
973 | 980 | |
|
974 | 981 | def setup(self, id, name, description): |
|
975 | 982 | |
|
976 | 983 | self.id = str(id) |
|
977 | 984 | self.name = name |
|
978 | 985 | self.description = description |
|
979 | 986 | |
|
980 | 987 | def update(self, name, description): |
|
981 | 988 | |
|
982 | 989 | self.name = name |
|
983 | 990 | self.description = description |
|
984 | 991 | |
|
985 | 992 | def addReadUnit(self, id=None, datatype=None, name=None, **kwargs): |
|
986 | 993 | if id is None: |
|
987 | 994 | idReadUnit = self.__getNewId() |
|
988 | 995 | else: |
|
989 | 996 | idReadUnit = str(id) |
|
990 | 997 | |
|
991 | 998 | readUnitConfObj = ReadUnitConf() |
|
992 | 999 | readUnitConfObj.setup(idReadUnit, name, datatype, parentId=self.id, **kwargs) |
|
993 | 1000 | |
|
994 | 1001 | self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj |
|
995 | 1002 | |
|
996 | 1003 | return readUnitConfObj |
|
997 | 1004 | |
|
998 | 1005 | def addProcUnit(self, inputId='0', datatype=None, name=None): |
|
999 | 1006 | |
|
1000 | 1007 | idProcUnit = self.__getNewId() |
|
1001 | 1008 | |
|
1002 | 1009 | procUnitConfObj = ProcUnitConf() |
|
1003 | 1010 | procUnitConfObj.setup(idProcUnit, name, datatype, inputId, parentId=self.id) |
|
1004 | 1011 | |
|
1005 | 1012 | self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj |
|
1006 | 1013 | |
|
1007 | 1014 | return procUnitConfObj |
|
1008 | 1015 | |
|
1009 | 1016 | def removeProcUnit(self, id): |
|
1010 | 1017 | |
|
1011 | 1018 | if id in self.procUnitConfObjDict.keys(): |
|
1012 | 1019 | self.procUnitConfObjDict.pop(id) |
|
1013 | 1020 | |
|
1014 | 1021 | def getReadUnitId(self): |
|
1015 | 1022 | |
|
1016 | 1023 | readUnitConfObj = self.getReadUnitObj() |
|
1017 | 1024 | |
|
1018 | 1025 | return readUnitConfObj.id |
|
1019 | 1026 | |
|
1020 | 1027 | def getReadUnitObj(self): |
|
1021 | 1028 | |
|
1022 | 1029 | for obj in self.procUnitConfObjDict.values(): |
|
1023 | 1030 | if obj.getElementName() == "ReadUnit": |
|
1024 | 1031 | return obj |
|
1025 | 1032 | |
|
1026 | 1033 | return None |
|
1027 | 1034 | |
|
1028 | 1035 | def getProcUnitObj(self, id=None, name=None): |
|
1029 | 1036 | |
|
1030 | 1037 | if id != None: |
|
1031 | 1038 | return self.procUnitConfObjDict[id] |
|
1032 | 1039 | |
|
1033 | 1040 | if name != None: |
|
1034 | 1041 | return self.getProcUnitObjByName(name) |
|
1035 | 1042 | |
|
1036 | 1043 | return None |
|
1037 | 1044 | |
|
1038 | 1045 | def getProcUnitObjByName(self, name): |
|
1039 | 1046 | |
|
1040 | 1047 | for obj in self.procUnitConfObjDict.values(): |
|
1041 | 1048 | if obj.name == name: |
|
1042 | 1049 | return obj |
|
1043 | 1050 | |
|
1044 | 1051 | return None |
|
1045 | 1052 | |
|
1046 | 1053 | def procUnitItems(self): |
|
1047 | 1054 | |
|
1048 | 1055 | return self.procUnitConfObjDict.items() |
|
1049 | 1056 | |
|
1050 | 1057 | def makeXml(self): |
|
1051 | 1058 | |
|
1052 | 1059 | projectElement = Element('Project') |
|
1053 | 1060 | projectElement.set('id', str(self.id)) |
|
1054 | 1061 | projectElement.set('name', self.name) |
|
1055 | 1062 | projectElement.set('description', self.description) |
|
1056 | 1063 | |
|
1057 | 1064 | for procUnitConfObj in self.procUnitConfObjDict.values(): |
|
1058 | 1065 | procUnitConfObj.makeXml(projectElement) |
|
1059 | 1066 | |
|
1060 | 1067 | self.projectElement = projectElement |
|
1061 | 1068 | |
|
1062 | 1069 | def writeXml(self, filename=None): |
|
1063 | 1070 | |
|
1064 | 1071 | if filename == None: |
|
1065 | 1072 | if self.filename: |
|
1066 | 1073 | filename = self.filename |
|
1067 | 1074 | else: |
|
1068 | 1075 | filename = "schain.xml" |
|
1069 | 1076 | |
|
1070 | 1077 | if not filename: |
|
1071 | 1078 | print "filename has not been defined. Use setFilename(filename) for do it." |
|
1072 | 1079 | return 0 |
|
1073 | 1080 | |
|
1074 | 1081 | abs_file = os.path.abspath(filename) |
|
1075 | 1082 | |
|
1076 | 1083 | if not os.access(os.path.dirname(abs_file), os.W_OK): |
|
1077 | 1084 | print "No write permission on %s" %os.path.dirname(abs_file) |
|
1078 | 1085 | return 0 |
|
1079 | 1086 | |
|
1080 | 1087 | if os.path.isfile(abs_file) and not(os.access(abs_file, os.W_OK)): |
|
1081 | 1088 | print "File %s already exists and it could not be overwriten" %abs_file |
|
1082 | 1089 | return 0 |
|
1083 | 1090 | |
|
1084 | 1091 | self.makeXml() |
|
1085 | 1092 | |
|
1086 | 1093 | ElementTree(self.projectElement).write(abs_file, method='xml') |
|
1087 | 1094 | |
|
1088 | 1095 | self.filename = abs_file |
|
1089 | 1096 | |
|
1090 | 1097 | return 1 |
|
1091 | 1098 | |
|
1092 | 1099 | def readXml(self, filename = None): |
|
1093 | 1100 | |
|
1094 | 1101 | if not filename: |
|
1095 | 1102 | print "filename is not defined" |
|
1096 | 1103 | return 0 |
|
1097 | 1104 | |
|
1098 | 1105 | abs_file = os.path.abspath(filename) |
|
1099 | 1106 | |
|
1100 | 1107 | if not os.path.isfile(abs_file): |
|
1101 | 1108 | print "%s file does not exist" %abs_file |
|
1102 | 1109 | return 0 |
|
1103 | 1110 | |
|
1104 | 1111 | self.projectElement = None |
|
1105 | 1112 | self.procUnitConfObjDict = {} |
|
1106 | 1113 | |
|
1107 | 1114 | try: |
|
1108 | 1115 | self.projectElement = ElementTree().parse(abs_file) |
|
1109 | 1116 | except: |
|
1110 | 1117 | print "Error reading %s, verify file format" %filename |
|
1111 | 1118 | return 0 |
|
1112 | 1119 | |
|
1113 | 1120 | self.project = self.projectElement.tag |
|
1114 | 1121 | |
|
1115 | 1122 | self.id = self.projectElement.get('id') |
|
1116 | 1123 | self.name = self.projectElement.get('name') |
|
1117 | 1124 | self.description = self.projectElement.get('description') |
|
1118 | 1125 | |
|
1119 | 1126 | readUnitElementList = self.projectElement.iter(ReadUnitConf().getElementName()) |
|
1120 | 1127 | |
|
1121 | 1128 | for readUnitElement in readUnitElementList: |
|
1122 | 1129 | readUnitConfObj = ReadUnitConf() |
|
1123 | 1130 | readUnitConfObj.readXml(readUnitElement) |
|
1124 | 1131 | |
|
1125 | 1132 | if readUnitConfObj.parentId == None: |
|
1126 | 1133 | readUnitConfObj.parentId = self.id |
|
1127 | 1134 | |
|
1128 | 1135 | self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj |
|
1129 | 1136 | |
|
1130 | 1137 | procUnitElementList = self.projectElement.iter(ProcUnitConf().getElementName()) |
|
1131 | 1138 | |
|
1132 | 1139 | for procUnitElement in procUnitElementList: |
|
1133 | 1140 | procUnitConfObj = ProcUnitConf() |
|
1134 | 1141 | procUnitConfObj.readXml(procUnitElement) |
|
1135 | 1142 | |
|
1136 | 1143 | if procUnitConfObj.parentId == None: |
|
1137 | 1144 | procUnitConfObj.parentId = self.id |
|
1138 | 1145 | |
|
1139 | 1146 | self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj |
|
1140 | 1147 | |
|
1141 | 1148 | self.filename = abs_file |
|
1142 | 1149 | |
|
1143 | 1150 | return 1 |
|
1144 | 1151 | |
|
1145 | 1152 | def printattr(self): |
|
1146 | 1153 | |
|
1147 | 1154 | print "Project[%s]: name = %s, description = %s" %(self.id, |
|
1148 | 1155 | self.name, |
|
1149 | 1156 | self.description) |
|
1150 | 1157 | |
|
1151 | 1158 | for procUnitConfObj in self.procUnitConfObjDict.values(): |
|
1152 | 1159 | procUnitConfObj.printattr() |
|
1153 | 1160 | |
|
1154 | 1161 | def createObjects(self): |
|
1155 | 1162 | |
|
1156 | 1163 | for procUnitConfObj in self.procUnitConfObjDict.values(): |
|
1157 | 1164 | procUnitConfObj.createObjects(self.plotterQueue) |
|
1158 | 1165 | |
|
1159 | 1166 | def __connect(self, objIN, thisObj): |
|
1160 | 1167 | |
|
1161 | 1168 | thisObj.setInput(objIN.getOutputObj()) |
|
1162 | 1169 | |
|
1163 | 1170 | def connectObjects(self): |
|
1164 | 1171 | |
|
1165 | 1172 | for thisPUConfObj in self.procUnitConfObjDict.values(): |
|
1166 | 1173 | |
|
1167 | 1174 | inputId = thisPUConfObj.getInputId() |
|
1168 | 1175 | |
|
1169 | 1176 | if int(inputId) == 0: |
|
1170 | 1177 | continue |
|
1171 | 1178 | |
|
1172 | 1179 | #Get input object |
|
1173 | 1180 | puConfINObj = self.procUnitConfObjDict[inputId] |
|
1174 | 1181 | puObjIN = puConfINObj.getProcUnitObj() |
|
1175 | 1182 | |
|
1176 | 1183 | #Get current object |
|
1177 | 1184 | thisPUObj = thisPUConfObj.getProcUnitObj() |
|
1178 | 1185 | |
|
1179 | 1186 | self.__connect(puObjIN, thisPUObj) |
|
1180 | 1187 | |
|
1181 | 1188 | def __handleError(self, procUnitConfObj, send_email=True): |
|
1182 | 1189 | |
|
1183 | 1190 | import socket |
|
1184 | 1191 | |
|
1185 | 1192 | err = traceback.format_exception(sys.exc_info()[0], |
|
1186 | 1193 | sys.exc_info()[1], |
|
1187 | 1194 | sys.exc_info()[2]) |
|
1188 | 1195 | |
|
1189 | 1196 | print "***** Error occurred in %s *****" %(procUnitConfObj.name) |
|
1190 | 1197 | print "***** %s" %err[-1] |
|
1191 | 1198 | |
|
1192 | 1199 | message = "".join(err) |
|
1193 | 1200 | |
|
1194 | 1201 | sys.stderr.write(message) |
|
1195 | 1202 | |
|
1196 | 1203 | if not send_email: |
|
1197 | 1204 | return |
|
1198 | 1205 | |
|
1199 | 1206 | subject = "SChain v%s: Error running %s\n" %(schainpy.__version__, procUnitConfObj.name) |
|
1200 | 1207 | |
|
1201 | 1208 | subtitle = "%s: %s\n" %(procUnitConfObj.getElementName() ,procUnitConfObj.name) |
|
1202 | 1209 | subtitle += "Hostname: %s\n" %socket.gethostbyname(socket.gethostname()) |
|
1203 | 1210 | subtitle += "Working directory: %s\n" %os.path.abspath("./") |
|
1204 | 1211 | subtitle += "Configuration file: %s\n" %self.filename |
|
1205 | 1212 | subtitle += "Time: %s\n" %str(datetime.datetime.now()) |
|
1206 | 1213 | |
|
1207 | 1214 | readUnitConfObj = self.getReadUnitObj() |
|
1208 | 1215 | if readUnitConfObj: |
|
1209 | 1216 | subtitle += "\nInput parameters:\n" |
|
1210 | 1217 | subtitle += "[Data path = %s]\n" %readUnitConfObj.path |
|
1211 | 1218 | subtitle += "[Data type = %s]\n" %readUnitConfObj.datatype |
|
1212 | 1219 | subtitle += "[Start date = %s]\n" %readUnitConfObj.startDate |
|
1213 | 1220 | subtitle += "[End date = %s]\n" %readUnitConfObj.endDate |
|
1214 | 1221 | subtitle += "[Start time = %s]\n" %readUnitConfObj.startTime |
|
1215 | 1222 | subtitle += "[End time = %s]\n" %readUnitConfObj.endTime |
|
1216 | 1223 | |
|
1217 | 1224 | adminObj = schainpy.admin.SchainNotify() |
|
1218 | 1225 | adminObj.sendAlert(message=message, |
|
1219 | 1226 | subject=subject, |
|
1220 | 1227 | subtitle=subtitle, |
|
1221 | 1228 | filename=self.filename) |
|
1222 | 1229 | |
|
1223 | 1230 | def isPaused(self): |
|
1224 | 1231 | return 0 |
|
1225 | 1232 | |
|
1226 | 1233 | def isStopped(self): |
|
1227 | 1234 | return 0 |
|
1228 | 1235 | |
|
1229 | 1236 | def runController(self): |
|
1230 | 1237 | """ |
|
1231 | 1238 | returns 0 when this process has been stopped, 1 otherwise |
|
1232 | 1239 | """ |
|
1233 | 1240 | |
|
1234 | 1241 | if self.isPaused(): |
|
1235 | 1242 | print "Process suspended" |
|
1236 | 1243 | |
|
1237 | 1244 | while True: |
|
1238 | 1245 | sleep(0.1) |
|
1239 | 1246 | |
|
1240 | 1247 | if not self.isPaused(): |
|
1241 | 1248 | break |
|
1242 | 1249 | |
|
1243 | 1250 | if self.isStopped(): |
|
1244 | 1251 | break |
|
1245 | 1252 | |
|
1246 | 1253 | print "Process reinitialized" |
|
1247 | 1254 | |
|
1248 | 1255 | if self.isStopped(): |
|
1249 | 1256 | print "Process stopped" |
|
1250 | 1257 | return 0 |
|
1251 | 1258 | |
|
1252 | 1259 | return 1 |
|
1253 | 1260 | |
|
1254 | 1261 | def setFilename(self, filename): |
|
1255 | 1262 | |
|
1256 | 1263 | self.filename = filename |
|
1257 | 1264 | |
|
1258 | 1265 | def setPlotterQueue(self, plotter_queue): |
|
1259 | 1266 | |
|
1260 | 1267 | raise NotImplementedError, "Use schainpy.controller_api.ControllerThread instead Project class" |
|
1261 | 1268 | |
|
1262 | 1269 | def getPlotterQueue(self): |
|
1263 | 1270 | |
|
1264 | 1271 | raise NotImplementedError, "Use schainpy.controller_api.ControllerThread instead Project class" |
|
1265 | 1272 | |
|
1266 | 1273 | def useExternalPlotter(self): |
|
1267 | 1274 | |
|
1268 | 1275 | raise NotImplementedError, "Use schainpy.controller_api.ControllerThread instead Project class" |
|
1269 | 1276 | |
|
1270 | 1277 | |
|
1271 | 1278 | def run(self, filename=None): |
|
1272 | 1279 | |
|
1273 | 1280 | # self.writeXml(filename) |
|
1274 | 1281 | self.createObjects() |
|
1275 | 1282 | self.connectObjects() |
|
1276 | 1283 | |
|
1277 | 1284 | |
|
1278 | 1285 | print "*"*60 |
|
1279 | 1286 | print " Starting SIGNAL CHAIN PROCESSING v%s " %schainpy.__version__ |
|
1280 | 1287 | print "*"*60 |
|
1281 | 1288 | |
|
1282 | 1289 | |
|
1283 | 1290 | keyList = self.procUnitConfObjDict.keys() |
|
1284 | 1291 | keyList.sort() |
|
1285 | 1292 | |
|
1286 | 1293 | while(True): |
|
1287 | 1294 | |
|
1288 | 1295 | is_ok = False |
|
1289 | 1296 | |
|
1290 | 1297 | for procKey in keyList: |
|
1291 | 1298 | # print "Running the '%s' process with %s" %(procUnitConfObj.name, procUnitConfObj.id) |
|
1292 | 1299 | |
|
1293 | 1300 | procUnitConfObj = self.procUnitConfObjDict[procKey] |
|
1294 | 1301 | |
|
1295 | 1302 | try: |
|
1296 | 1303 | sts = procUnitConfObj.run() |
|
1297 | 1304 | is_ok = is_ok or sts |
|
1298 | 1305 | except KeyboardInterrupt: |
|
1299 | 1306 | is_ok = False |
|
1300 | 1307 | break |
|
1301 | 1308 | except ValueError, e: |
|
1302 | 1309 | sleep(0.5) |
|
1303 | 1310 | self.__handleError(procUnitConfObj, send_email=True) |
|
1304 | 1311 | is_ok = False |
|
1305 | 1312 | break |
|
1306 | 1313 | except: |
|
1307 | 1314 | sleep(0.5) |
|
1308 | 1315 | self.__handleError(procUnitConfObj) |
|
1309 | 1316 | is_ok = False |
|
1310 | 1317 | break |
|
1311 | 1318 | |
|
1312 | 1319 | #If every process unit finished so end process |
|
1313 | 1320 | if not(is_ok): |
|
1314 | 1321 | # print "Every process unit have finished" |
|
1315 | 1322 | break |
|
1316 | 1323 | |
|
1317 | 1324 | if not self.runController(): |
|
1318 | 1325 | break |
|
1319 | 1326 | |
|
1320 | 1327 | #Closing every process |
|
1321 | 1328 | for procKey in keyList: |
|
1322 | 1329 | procUnitConfObj = self.procUnitConfObjDict[procKey] |
|
1323 | 1330 | procUnitConfObj.close() |
|
1324 | ||
|
1325 | print "Process finished" |
@@ -1,39 +1,59 | |||
|
1 |
""" |
|
|
1 | """ | |
|
2 | 2 | SCHAINPY - LOG |
|
3 | 3 | Simple helper for log standarization |
|
4 | 4 | Usage: |
|
5 | 5 | from schainpy.utils import log |
|
6 | 6 | log.error('A kitten died beacuse of you') |
|
7 | 7 | log.warning('You are doing it wrong but what the heck, I'll allow it) |
|
8 | 8 | log.succes('YOU ROCK!') |
|
9 | 9 | To create your own logger inside your class do it like this: |
|
10 | 10 | from schainpy.utils import log |
|
11 | 11 | awesomeLogger = log.makelogger("never gonna", bg="red", fg="white") |
|
12 | 12 | awesomeLogger('give you up') |
|
13 | 13 | which will look like this: |
|
14 | 14 | [NEVER GONNA] - give you up |
|
15 | 15 | with color red as background and white as foreground. |
|
16 | 16 | """ |
|
17 | ||
|
17 | import os | |
|
18 | import sys | |
|
18 | 19 | import click |
|
19 | 20 | |
|
20 | 21 | def warning(message): |
|
21 | 22 | click.echo(click.style('[WARNING] - ' + message, fg='yellow')) |
|
22 | 23 | |
|
23 | 24 | |
|
24 | 25 | def error(message): |
|
25 | 26 | click.echo(click.style('[ERROR] - ' + message, fg='red', bg='black')) |
|
26 | 27 | |
|
27 | 28 | |
|
28 | 29 | def success(message): |
|
29 | 30 | click.echo(click.style(message, fg='green')) |
|
30 | 31 | |
|
31 | 32 | |
|
32 | 33 | def log(message, topic='LOG'): |
|
33 | 34 | click.echo('[{}] - {}'.format(topic, message)) |
|
34 | 35 | |
|
35 | 36 | def makelogger(topic, bg='reset', fg='reset'): |
|
36 | 37 | def func(message): |
|
37 | 38 | click.echo(click.style('[{}] - '.format(topic.upper()) + message, |
|
38 | 39 | bg=bg, fg=fg)) |
|
39 | 40 | return func |
|
41 | ||
|
42 | class LoggerForFile(): | |
|
43 | def __init__(self, filename): | |
|
44 | self.old_stdout=sys.stdout | |
|
45 | cwd = os.getcwd() | |
|
46 | self.log_file = open(os.path.join(cwd, filename), 'w+') | |
|
47 | def write(self, text): | |
|
48 | text = text.rstrip() | |
|
49 | if not text: | |
|
50 | return | |
|
51 | self.log_file.write(text + '\n') | |
|
52 | self.old_stdout.write(text + '\n') | |
|
53 | def flush(self): | |
|
54 | self.old_stdout.flush() | |
|
55 | ||
|
56 | def logToFile(filename='log.log'): | |
|
57 | logger = LoggerForFile(filename) | |
|
58 | sys.stdout = logger | |
|
59 |
General Comments 0
You need to be logged in to leave comments.
Login now