Cat
shell.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 
3 import os
4 import sys
5 import time
6 import code
7 import platform
8 import progressbar
9 
10 from libCatKernel import *
11 
12 from arguments import *
13 from wrapper import *
14 
15 #----------------------------------------------------------------------------
16 # options
17 
18 
19 def opts():
20  return cat.options()
21 
22 #----------------------------------------------------------------------------
23 # tree/object navigation
24 
25 
26 def obj():
27  return cat.current()
28 
29 
30 def get(path):
31  return cat.current().child(path)
32 
33 
34 def cd(path=''):
35  cat.cd(path)
36  prompt()
37 
38 
39 def load(filename):
40  if (os.path.exists(filename) == False):
41  filename = filename + ".py"
42  if (os.path.exists(filename)):
43  execfile(filename)
44  prompt()
45  cat.options().setDefFile(filename)
46  else:
47  cat.warning("File " + filename + " does not exist.", "CAT")
48 
49 
50 def reload():
51  filename = cat.options().defFile()
52  cat.computer().clear()
53  load(filename)
54 
55 
56 def ls(path=''):
57  if path == '':
58  element = cat.current()
59  else:
60  element = cat.current().child(path)
61  if type(element) != None:
62  l = element.children()
63  for iter in range(len(l)):
64  cat.info(" " + l[iter].name() +
65  " [" + l[iter].type() + "]", "CAT")
66  cat.info(element.name() + " : " + str(len(l)) +
67  " child(ren) found.", "CAT")
68 
69 
70 def ll(path=''):
71  if path == '':
72  element = cat.current()
73  else:
74  element = cat.current().child(path)
75  if type(element) != None:
76  lproc = cat.procDb().list(element)
77  nproc = len(lproc)
78  if (nproc) > 0:
79  cat.info("- Processus: ", "CAT")
80  for p in range(nproc):
81  cat.info(" " + lproc[p].name() +
82  " : " + lproc[p].title(), "CAT")
83  l = element.children()
84  if (len(l)) > 0:
85  cat.info("- Element(s): ", "CAT")
86  for iter in range(len(l)):
87  cat.info(" " + l[iter].name() +
88  " [" + l[iter].type() + "]", "CAT")
89  lproc = cat.procDb().list(l[iter])
90  for p in range(len(lproc)):
91  cat.info(" . " + lproc[p].name() +
92  " : " + lproc[p].title(), "CAT")
93  cat.info(element.name() + " : " + str(len(l)) + " child(ren) found [" +
94  str(nproc) + " processus].", "CAT")
95 
96 
97 def pwd():
98  cat.info(cat.current().name() + " [" + cat.current().type() + "]", "CAT")
99 
100 
101 def tree(path=''):
102  if path == '':
103  element = cat.current()
104  else:
105  element = cat.current().child(path)
106  if type(element) != None:
107  element.tree()
108 
109 
110 def view(path='', indent=' '):
111  if path == '':
112  element = cat.current()
113  else:
114  element = cat.current().child(path)
115  line = indent + ' -> ' + \
116  element.name() + '[' + element.type() + \
117  ' - ' + element.attributs() + ']'
118  print line
119  l = element.children()
120  for iter in range(len(l)):
121  lproc = cat.procDb().list(l[iter])
122  for p in range(len(lproc)):
123  print indent + " ." + lproc[p].name() + "(" + lproc[p].title() + ") - " + lproc[p].attributs()
124  view(l[iter].path(''), indent + ' ')
125 
126  # def com(objecgt):
127  # while connection!=None:
128  # print connection()
129 
130 
131 def create(object, type):
132  if cat.create(object, type).isFailure():
133  cat.fatal("Could not make object " +
134  object + " of type " + type, "CAT")
135 
136 #----------------------------------------------------------------------------
137 
138 
139 def process(object):
140  ok = False
141  list = cat.procDb().list()
142  for p in range(len(list)):
143  if (list[p].name() == object):
144  return list[p]
145  if (ok == False):
146  cat.warning("Could not find processus " + object + ".", "CAT")
147  return None
148 
149 
150 def data(object, stream=None):
151  ok = False
152  list = cat.procDb().list()
153  for p in range(len(list)):
154  if (list[p].name() == object):
155  ok = True
156  if (stream == None):
157  list[p].data().info()
158  else:
159  return list[p].data().vector(stream)
160  if (ok == False):
161  cat.warning("Could not find processus " + object + ".", "CAT")
162  return
163 
164 #----------------------------------------------------------------------------
165 # application settings
166 
167 
168 def app():
169  return cat
170 
171 
172 def clear(path=''):
173  if path == '':
174  element = cat.current()
175  else:
176  element = cat.current().child(path)
177  if type(element) != None:
178  element.clear()
179 
180 
181 def delete(path=''):
182  if path == '*':
183  cat.current().clear()
184  else:
185  cat.current().delChild(path)
186 
187 
188 def outputLevel(level='INFO'):
189  levels = {'VERBOSE': MsgLevel.VERBOSE,
190  'DEBUG': MsgLevel.DEBUG,
191  'INFO': MsgLevel.INFO,
192  'WARNING': MsgLevel.WARNING,
193  'FATAL': MsgLevel.FATAL}
194  outLevel = levels.get(level)
195  if outLevel != None:
196  cat.options().setLogOutputLevel(outLevel)
197  else:
198  cat.warning("Output Level should be 'VERBOSE', 'DEBUG', 'INFO', 'WARNING' or 'FATAL'",
199  "CAT")
200  cat.warning("'" + level + "' level output not understood.", "CAT")
201 
202 
203 def init():
204  cat.computer().recursiveInitCommunications()
205  cat.computer().recursiveInitElement()
206 
207 
208 def network():
209  cat.fatal("Not implemented.", "CAT")
210 
211 
212 def reset(path=''):
213  if path == '':
214  element = cat.current()
215  else:
216  element = cat.current().child(path)
217  if type(element) != None:
218  element.reset()
219 
220 
221 def prompt():
222  current = cat.current()
223  if current != None:
224  sys.ps1 = "\033[92m" + current.name() + "\033[0m[\033[93m" + \
225  current.type() + "\033[0m] > "
226  else:
227  sys.ps1 = "\033[91mInvalid\033[0m[\033[95mNone\033[0m] > "
228 
229 
230 def loadDll(name):
231  cat.dllMgr().load(name)
232 
233 #----------------------------------------------------------------------------
234 # processing
235 
236 
237 def run(processus, element, nevt):
238  if (type(element) == str):
239  element = get(element)
240  if (element == None):
241  return Code.FAILURE
242  ok = False
243  proc = None
244  list = cat.procDb().list(element)
245  for p in range(len(list)):
246  if (list[p].name() == processus):
247  proc = list[p]
248  ok = True
249  if (ok == False):
250  cat.warning("Could not find processus " + processus + ".", "CAT")
251  return Code.FAILURE
252  nrun = opts().runNumber()
253  if (cat.prepare(processus, element, nevt).isFailure()):
254  cat.warning("Could not prepare the processus " +
255  processus + ".", "CAT")
256  return Code.FAILURE
257  evt = nevt
258  width = 50
259 
260  plts = None
261  module = None
262  wrap = wrapper(cat, proc, "proc")
263  online = cat.options().plotFreq()
264  if (wrap.ok):
265  module = wrap.GetActive()
266  proc.setElement(element)
267  if module and (online != 0):
268  plts = module.createPlots(cat, proc, nrun)
269 
271  processus, end=abs(evt), width=width, fill='▣', blank='-')
272  timing = -1.
273  ievt = 0
274  if (evt<0): timing = time.time()
275  evt = abs(evt)
276  stop = False
277 
278  try:
279  while ( not stop ):
280  ievt+=1
281  if timing>=0:
282  elapsed = time.time() - timing
283  p.set(elapsed)
284  if elapsed > evt : stop = True
285  else :
286  p + 1
287  if ievt>=evt : stop = True
288  p.show_progress()
289  if (cat.loop().isFailure()):
290  cat.warning("Error detected during processing.", "CAT")
291  if online>0 and plt:
292  if ievt%online==0:
293  if module :
294  module.updatePlots(plts, proc)
295  except KeyboardInterrupt:
296  pass
297 
298  cat.terminate()
299  if module and online==0:
300  plts = module.createPlots(cat, proc, nrun)
301  module.updatePlots(plts, proc)
302 
303  # Plot(processus, nrun)
304 
305 # def plot(processus, nrun):
306 # p = proc(processus)
307 # if (p != None):
308 # wrap = wrapper(cat, p, "proc")
309 # if (wrap.ok):
310 # module = wrap.GetActive()
311 # plot = module.Plot(cat, p, None, nrun)
312 # if plot:
313 # plot.current().show()
314 # return Code.SUCCESS
315 
316 # def finalplot(processus, nrun):
317 # p = proc(processus)
318 # if (p != None):
319 # wrap = wrapper(cat, p, "proc")
320 # if (wrap.ok):
321 # module = wrap.GetActive()
322 # plot = module.FinalPlot(cat, p, None, nrun)
323 # if plot:
324 # plot.current().show()
325 # return Code.SUCCESS
326 
327 
328 def shell(app, args):
329  import version
330  global cat
331  global figid
332  figid = 0
333  if (app == None):
334  cat = Application()
335  else:
336  cat = app
337  outputLevel(args.output)
338  vers = version.version()
339  vers.info()
340  cat.initialize()
341  if ("" <> args.file):
342  if (os.path.exists(args.file)):
343  execfile(args.file)
344  cat.options().setDefFile(args.file)
345  else:
346  cat.fatal("Cannot execute init file " +
347  args.file + ". Abort.", "CAT")
348  return -1
349  prompt()
350 
351 # main
352 
353 if __name__ == "__main__":
354  args = arguments()
355  args.shellMode = True
356  shell(Application(), args)
def create(object, type)
Definition: shell.py:131
def prompt()
Definition: shell.py:221
def network()
Definition: shell.py:208
def ll(path='')
Definition: shell.py:70
def get(path)
Definition: shell.py:30
def run(processus, element, nevt)
Definition: shell.py:237
def outputLevel(level='INFO')
Definition: shell.py:188
def shell(app, args)
Definition: shell.py:328
def data(object, stream=None)
Definition: shell.py:150
def app()
Definition: shell.py:168
def loadDll(name)
Definition: shell.py:230
def ls(path='')
Definition: shell.py:56
def pwd()
Definition: shell.py:97
def opts()
Definition: shell.py:19
def delete(path='')
Definition: shell.py:181
def view(path='', indent=' ')
Definition: shell.py:110
def tree(path='')
Definition: shell.py:101
def clear(path='')
Definition: shell.py:172
def reset(path='')
Definition: shell.py:212
def process(object)
Definition: shell.py:139
def cd(path='')
Definition: shell.py:34
Definition: shell.py:1
def reload()
Definition: shell.py:50
def obj()
Definition: shell.py:26
def load(filename)
Definition: shell.py:39
def init()
Definition: shell.py:203