ATLAS Offline Software
AtlCoolConsole.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # version modified by Richard Hawkings from COOl1.3.2 distribution
3 # original by Sven Schmitt (Abstracture/Mainz)
4 
5 from __future__ import print_function
6 import sys
7 import code
8 import readline
9 import atexit
10 import os
11 import re
12 import logging
13 
14 log = logging.getLogger( __name__ )
15 log.setLevel( logging.INFO )
16 
17 handler = logging.StreamHandler()
18 format = "%(levelname)s:%(name)s: %(message)s"
19 handler.setFormatter( logging.Formatter( format ) )
20 log.addHandler( handler )
21 
22 
23 historyFile = '.coolconsole_hist'
24 
25 class HistoryConsole(code.InteractiveConsole):
26  """
27  This class extends InteractiveConsole with command line history. The
28  history can be accessed with the usual CURSOR UP keystroke. It is also
29  stored to a file '%s' in the user's home directory.
30  """ % historyFile
31 
32  def __init__( self,
33  locals = None,
34  filename = "<console>",
35  histfile = os.path.join( os.environ["HOME"], historyFile) ):
36  code.InteractiveConsole.__init__( self )
37  self.init_history( histfile )
38 
39  def init_history( self, histfile ):
40  readline.parse_and_bind( "tab: complete" )
41  readline.set_history_length( 100 )
42  if hasattr( readline, "read_history_file" ):
43  try:
44  readline.read_history_file( histfile )
45  except IOError:
46  pass
47  atexit.register( self.save_history, histfile )
48 
49  def save_history( self, histfile ):
50  readline.write_history_file( histfile )
51 
52 
53 
55 
56  commands = { 'help' : 'help overview',
57  'less' : 'list contents of folders, e.g. less "/a"',
58  'more' : 'list contents of folders (ATLAS specific)',
59  'ls' : 'list contents of foldersets, e.g. ls "/"',
60  'll' : 'list contents of foldersets with entry count',
61  'exit' : 'quit the interpreter session',
62  'quit' : 'alias for exit',
63  'open' : "open the specified database, "
64  "e.g. open 'sqlite://...'",
65  'cd' : 'change directory',
66  'pwd' : 'print current directory',
67  'rmdir' : 'remove a folder or folderset',
68  'usetag' : 'set subsequent list operations (less) to use given COOL tag',
69  'usechan' : 'set subsequent list operations (less) to display given COOL channel number only',
70  'userunlumi' : 'set limits for run/lumi blocks in more/less',
71  'usetimes' : 'set limits for timestamps in more/less',
72  'pws' : 'print current tag and channel selections used by less',
73  'listtags' : 'list tags in folder / folder set',
74  'listchans' : 'list channels in folder',
75  'listinfo' : 'list info about folder (type, columns)',
76  'filtertags' : 'list tags in folder / folder set with filtering',
77  'settag' : 'set hierarchical tag for a folder+parent',
78  'settginfo' : 'set tag description string',
79  'setchan' : 'set channel name/description',
80  'clonetag' : 'clone data from one tag to another',
81  'setdesc' : 'set folder description (meta-data) string',
82  'rmtag' : 'remove tag or hierarchical tag relation to a parent',
83  'locktag' : 'lock a tag so the contents cannot be changed',
84  'headtag' : 'apply HEAD-style tag to a folder',
85  'tracetags': 'list tags defined in subfolders for parent'
86  }
87 
88  banner = "Welcome to AtlCoolConsole. Type 'help' for instructions."
89 
90  def __init__( self, connectString = None ):
91  HistoryConsole.__init__( self )
92  self.connectString = connectString
93 
94 
95  def interact( self ):
96  self.push( 'from CoolConvUtilities import AtlCoolTool' )
97  if connectString is not None:
98  self.push( 'this = AtlCoolTool.AtlCoolTool("%s")' % self.connectString )
99  self.push( 'print (this)' )
100  else:
101  print ( "Not connected. Use the 'open' command to connect to "
102  "a database." )
103  HistoryConsole.interact( self, self.banner )
104 
105 
106  def parseLine( self, line ):
107  for command in self.commands.keys():
108  #res = re.search( '^' + command + '\s*(?P<remainder>.*)', line )
109  res=re.search( '^' + command + r'($|\s+(?P<remainder>.*))', line )
110  if res is not None:
111  return command, res.group('remainder') or ""
112  return None, line
113 
114 
115  def createInterpreterCommand( self, command, argumentString ):
116  if command == 'less':
117  return self.command_less( argumentString )
118  elif command == 'more':
119  return self.command_more( argumentString )
120  elif command == 'ls':
121  return self.command_ls( argumentString,False )
122  elif command == 'll':
123  return self.command_ls( argumentString,True )
124  elif command == 'help':
125  return self.command_help( argumentString )
126  elif command == 'exit':
127  return self.command_exit()
128  elif command == 'quit':
129  return self.command_exit()
130  elif command == 'open':
131  return self.command_open( argumentString )
132  elif command =='listtags':
133  return self.command_lstags( argumentString )
134  elif command =='filtertags':
135  return self.command_filtertags( argumentString )
136  elif command =='listchans':
137  return self.command_listchans( argumentString )
138  elif command =='listinfo':
139  return self.command_listinfo( argumentString )
140  elif command =='rmdir':
141  return self.command_rmdir( argumentString )
142  elif command =='usetag':
143  return self.command_usetag( argumentString )
144  elif command =='usechan':
145  return self.command_usechan( argumentString )
146  elif command =='settag':
147  return self.command_settag( argumentString )
148  elif command =='settginfo':
149  return self.command_settginfo( argumentString )
150  elif command =='setchan':
151  return self.command_setchan( argumentString )
152  elif command =='userunlumi':
153  return self.command_userunlumi( argumentString )
154  elif command =='usetimes':
155  return self.command_usetimes( argumentString )
156  elif command =='clonetag':
157  return self.command_clonetag( argumentString )
158  elif command =='rmtag':
159  return self.command_rmtag( argumentString )
160  elif command =='locktag':
161  return self.command_locktag( argumentString )
162  elif command =='headtag':
163  return self.command_headtag( argumentString )
164  elif command =='tracetags':
165  return self.command_tracetags( argumentString )
166  elif command == 'setdesc':
167  return self.command_setdesc( argumentString )
168  elif command =='cd':
169  return self.command_cd( argumentString )
170  elif command =='pwd':
171  return self.command_pwd()
172  elif command =='pws':
173  return self.command_pws()
174  else:
175  return None
176 
177 
178  def command_less( self, argumentString ):
179  if argumentString is None:
180  raise Exception( "usage: less <folder>" )
181  argumentString.strip()
182  nodes = argumentString.split()
183  cmds = []
184  for node in nodes:
185  cmds.append( 'this.less("%s", header=True)' % node )
186  return ';'.join( cmds )
187 
188  def command_more( self, argumentString ):
189  if argumentString is None:
190  raise Exception( "usage: more <folder>" )
191  argumentString.strip()
192  nodes = argumentString.split()
193  cmds = []
194  for node in nodes:
195  cmds.append( 'this.more("%s", header=True)' % node )
196  return ';'.join( cmds )
197 
198 
199  def command_ls( self, argumentString, doCount=False ):
200  if argumentString is None: argumentString = '.'
201  argumentString.strip()
202  if argumentString == '': argumentString = '.'
203  nodes = argumentString.split()
204  cmds = []
205  tof='False'
206  if (doCount): tof='True'
207  for node in nodes:
208  cmds.append( 'this.ls("%s", header=True, doCount=%s)' % (node,tof) )
209  return ';'.join( cmds )
210 
211  def command_pwd(self):
212  return ';'.join(['this.pwd()'])
213 
214  def command_pws(self):
215  return ';'.join(['this.pws()'])
216 
217  def command_lstags( self, argumentString ):
218  if argumentString is None: argumentString = '.'
219  argumentString.strip()
220  if argumentString is None: argumentString = '.'
221  nodes = argumentString.split()
222  cmds = []
223  for node in nodes:
224  cmds.append( 'this.lstags("%s")' % node )
225  return ';'.join( cmds )
226 
227  def command_filtertags(self,argumentString):
228  if argumentString is None or argumentString=="": argumentString = '.'
229  argumentString.strip()
230  if argumentString is None: argumentString = '.'
231  args = argumentString.split()
232  node=args[0]
233  pattern=""
234  if len(args)>1:
235  pattern=args[1]
236  cmds = []
237  cmds.append('this.lstags("%s",pattern="%s")' % (node,pattern))
238  return ';'.join(cmds)
239 
240 
241  def command_listchans( self, argumentString ):
242  if argumentString is None: argumentString = '.'
243  argumentString.strip()
244  if argumentString is None: argumentString = '.'
245  nodes = argumentString.split()
246  cmds = []
247  for node in nodes:
248  cmds.append( 'this.listchans("%s")' % node )
249  return ';'.join( cmds )
250 
251  def command_listinfo( self, argumentString ):
252  if argumentString is None: argumentString = '.'
253  argumentString.strip()
254  if argumentString is None: argumentString = '.'
255  nodes = argumentString.split()
256  cmds = []
257  for node in nodes:
258  cmds.append( 'this.listinfo("%s")' % node )
259  return ';'.join( cmds )
260 
261  def command_rmdir(self, argumentString ):
262  argumentString.strip()
263  cmds=[]
264  cmds.append('this.rmdir("%s")' % argumentString)
265  return ';'.join( cmds )
266 
267  def command_usetag(self, argumentString ):
268  argumentString.strip()
269  cmds=[]
270  cmds.append('this.usetag("%s")' % argumentString)
271  return ';'.join( cmds )
272 
273  def command_usechan(self, argumentString ):
274  argumentString.strip()
275  cmds=[]
276  cmds.append('this.usechan("%s")' % argumentString)
277  return ';'.join( cmds )
278 
279  def command_cd( self, argumentString ):
280  if argumentString is None: argumentString ='/'
281  argumentString.strip()
282  if argumentString == '': argumentString = '/'
283  cmds=[]
284  cmds.append('this.cd("%s")' % argumentString)
285  return ';'.join( cmds )
286 
287  def command_settag(self,argumentString):
288  args=argumentString.split()
289  if len(args)<3:
290  print ('Usage: settag <folder> <foldertag> <parenttag>')
291  return ''
292  cmds=[]
293  cmds.append('this.settag("%s")' % argumentString)
294  return ';'.join(cmds)
295 
296  def command_settginfo(self,argumentString):
297  args=argumentString.split()
298  if len(args)<2:
299  print ('Usage: settginfo <folder> <tag> <description>')
300  return ''
301  cmds=[]
302  cmds.append('this.settginfo("%s")' % argumentString)
303  return ';'.join(cmds)
304 
305  def command_setchan(self,argumentString):
306  args=argumentString.split()
307  if len(args)<3:
308  print ('Usage: setchan <folder> <chanID> <chanName> {<chanDescr>}')
309  return ''
310  cmds=[]
311  cmds.append('this.setchan("%s")' % argumentString)
312  return ';'.join(cmds)
313 
314  def command_userunlumi(self,argumentString):
315  args=argumentString.split()
316  if len(args)!=1 and len(args)<4:
317  print ('Usage: userunlumi <run1> {<LB1> <run2> <LB2>}')
318  return ''
319  cmds=[]
320  cmds.append('this.userunlumi("%s")' % argumentString)
321  return ';'.join(cmds)
322 
323  def command_usetimes(self,argumentString):
324  args=argumentString.split()
325  if len(args)<2:
326  print ('Usage: usetimes <time1> <time2>')
327  return ''
328  cmds=[]
329  cmds.append('this.usetimes("%s")' % argumentString)
330  return ';'.join(cmds)
331 
332  def command_clonetag(self,argumentString):
333  args=argumentString.split()
334  if len(args)<3:
335  print ('Usage: clonetag <folder> <sourcetag> <desttag>')
336  return ''
337  cmds=[]
338  cmds.append('this.clonetag("%s")' % argumentString)
339  return ';'.join(cmds)
340 
341 
342  def command_rmtag(self,argumentString):
343  args=argumentString.split()
344  if len(args)<2:
345  print ('Usage: rmtag <folder> <leaf or parent tag>')
346  return ''
347  cmds=[]
348  cmds.append('this.rmtag("%s")' % argumentString)
349  return ';'.join(cmds)
350 
351  def command_locktag(self,argumentString):
352  args=argumentString.split()
353  if len(args)<2:
354  print ('Usage: locktag <folder> <tag> {action=l|p|u|r}')
355  return ''
356  cmds=[]
357  cmds.append('this.locktag("%s")' % argumentString)
358  return ';'.join(cmds)
359 
360  def command_headtag(self,argumentString):
361  args=argumentString.split()
362  if len(args)<2:
363  print ('Usage: headtag <folder> <tag>')
364  return ''
365  cmds=[]
366  cmds.append('this.headtag("%s")' % argumentString)
367  return ';'.join(cmds)
368 
369 
370  def command_tracetags(self,argumentString):
371  args=argumentString.split()
372  if len(args)<2:
373  print ('Usage: tracetags <folder> <foldertag>')
374  return ''
375  cmds=[]
376  cmds.append('this.tracetags("%s")' % argumentString)
377  return ';'.join(cmds)
378 
379 
380  def command_setdesc( self, argumentString ):
381  args=argumentString.split()
382  if len(args)<1:
383  print ('Usage: setdesc <folder> {<newdesc>}')
384  return ''
385  cmds=[]
386  cmds.append('this.setdesc("%s")' % argumentString)
387  return ';'.join(cmds)
388 
389 
390  def command_help( self, argumentString ):
391  if argumentString is not None and argumentString != '':
392  # we have a python "help(...)" command most likely
393  # we only want to intercept "help", therefore return the original
394  return 'help ' + argumentString
395  print ('Available commands:')
396  keys = list(self.commands.keys())
397  keys.sort()
398  for key in keys:
399  print (' %(key)-8s : %(value)s' % { 'key' : key,
400  'value' : self.commands[key] })
401  print ("These commands are shortcuts that are forwarded to a CoolTool")
402  print ("instance 'this', referring to the currently connected database.")
403  print ("Since this environment is a fully functional python shell,")
404  print ("'this' can be used like any python object, e.g.:")
405  print (" this.ls( '/' )")
406  return ''
407 
408 
409  def command_exit( self ):
410  return 'import sys ; sys.exit(0)'
411 
412 
413  def command_open( self, argumentString ):
414  log.debug( 'argumentString: ' + argumentString )
415  if ( not argumentString.startswith('"') and
416  not argumentString.startswith("'") ):
417  argumentString = "'" + argumentString + "'"
418  log.debug( 'argumentString: ' + argumentString )
419  return 'this = AtlCoolTool.AtlCoolTool(%s)' % argumentString
420 
421 
422  def push(self, line):
423  command, remainder = self.parseLine( line )
424  log.debug( 'command: %s' % command )
425  log.debug( 'remainder: %s' % remainder )
426 
427  res = self.createInterpreterCommand( command, remainder )
428  if res is not None:
429  log.debug( 'command: "%s"' % res )
430  interpreterCommand = res
431  else:
432  interpreterCommand = line
433 
434  #HistoryConsole.push( self, 'print "cmd: %s"' % interpreterCommand )
435  return HistoryConsole.push( self, interpreterCommand )
436 
437 
438 
439 
440 if __name__ == '__main__':
441 
442 
443  usage = ( 'Usage: %s <connect string>\n'
444  '\t<connect string>: a RAL connect string\n'
445  ) % sys.argv[0]
446 
447 
448  if len( sys.argv ) == 2:
449  connectString = sys.argv[1]
450  else:
451  connectString = None
452 
453  tool = AtlCoolConsole( connectString )
454  tool.interact()
455 
456 
AtlCoolConsole.AtlCoolConsole.command_settag
def command_settag(self, argumentString)
Definition: AtlCoolConsole.py:287
AtlCoolConsole.HistoryConsole.__init__
def __init__(self, locals=None, filename="<console>", histfile=os.path.join(os.environ["HOME"], historyFile))
Definition: AtlCoolConsole.py:32
AtlCoolConsole.AtlCoolConsole.command_usetimes
def command_usetimes(self, argumentString)
Definition: AtlCoolConsole.py:323
AtlCoolConsole.AtlCoolConsole.command_locktag
def command_locktag(self, argumentString)
Definition: AtlCoolConsole.py:351
AtlCoolConsole.AtlCoolConsole.command_cd
def command_cd(self, argumentString)
Definition: AtlCoolConsole.py:279
AtlCoolConsole.AtlCoolConsole.command_more
def command_more(self, argumentString)
Definition: AtlCoolConsole.py:188
AtlCoolConsole.AtlCoolConsole.command_clonetag
def command_clonetag(self, argumentString)
Definition: AtlCoolConsole.py:332
AtlCoolConsole.HistoryConsole
Definition: AtlCoolConsole.py:25
AtlCoolConsole.AtlCoolConsole.command_filtertags
def command_filtertags(self, argumentString)
Definition: AtlCoolConsole.py:227
AtlCoolConsole.AtlCoolConsole.command_usetag
def command_usetag(self, argumentString)
Definition: AtlCoolConsole.py:267
AtlCoolConsole.AtlCoolConsole
Definition: AtlCoolConsole.py:54
AtlCoolConsole.AtlCoolConsole.command_setdesc
def command_setdesc(self, argumentString)
Definition: AtlCoolConsole.py:380
AtlCoolConsole.AtlCoolConsole.command_tracetags
def command_tracetags(self, argumentString)
Definition: AtlCoolConsole.py:370
AtlCoolConsole.AtlCoolConsole.command_ls
def command_ls(self, argumentString, doCount=False)
Definition: AtlCoolConsole.py:199
AtlCoolConsole.AtlCoolConsole.command_usechan
def command_usechan(self, argumentString)
Definition: AtlCoolConsole.py:273
AtlCoolConsole.HistoryConsole.save_history
def save_history(self, histfile)
Definition: AtlCoolConsole.py:49
AtlCoolConsole.AtlCoolConsole.banner
string banner
Definition: AtlCoolConsole.py:88
AtlCoolConsole.AtlCoolConsole.createInterpreterCommand
def createInterpreterCommand(self, command, argumentString)
Definition: AtlCoolConsole.py:115
AtlCoolConsole.AtlCoolConsole.command_userunlumi
def command_userunlumi(self, argumentString)
Definition: AtlCoolConsole.py:314
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
AtlCoolConsole.AtlCoolConsole.command_listchans
def command_listchans(self, argumentString)
Definition: AtlCoolConsole.py:241
AtlCoolConsole.AtlCoolConsole.command_pws
def command_pws(self)
Definition: AtlCoolConsole.py:214
AtlCoolConsole.AtlCoolConsole.push
def push(self, line)
Definition: AtlCoolConsole.py:422
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
AtlCoolConsole.AtlCoolConsole.command_exit
def command_exit(self)
Definition: AtlCoolConsole.py:409
AtlCoolConsole.AtlCoolConsole.command_listinfo
def command_listinfo(self, argumentString)
Definition: AtlCoolConsole.py:251
AtlCoolConsole.AtlCoolConsole.command_headtag
def command_headtag(self, argumentString)
Definition: AtlCoolConsole.py:360
AtlCoolConsole.AtlCoolConsole.commands
dictionary commands
Definition: AtlCoolConsole.py:56
AtlCoolConsole.AtlCoolConsole.__init__
def __init__(self, connectString=None)
Definition: AtlCoolConsole.py:90
AtlCoolConsole.AtlCoolConsole.command_help
def command_help(self, argumentString)
Definition: AtlCoolConsole.py:390
AtlCoolConsole.AtlCoolConsole.command_open
def command_open(self, argumentString)
Definition: AtlCoolConsole.py:413
AtlCoolConsole.AtlCoolConsole.interact
def interact(self)
Definition: AtlCoolConsole.py:95
AtlCoolConsole.AtlCoolConsole.parseLine
def parseLine(self, line)
Definition: AtlCoolConsole.py:106
python.Bindings.keys
keys
Definition: Control/AthenaPython/python/Bindings.py:798
AtlCoolConsole.AtlCoolConsole.command_less
def command_less(self, argumentString)
Definition: AtlCoolConsole.py:178
AtlCoolConsole.AtlCoolConsole.command_setchan
def command_setchan(self, argumentString)
Definition: AtlCoolConsole.py:305
AtlCoolConsole.AtlCoolConsole.command_rmdir
def command_rmdir(self, argumentString)
Definition: AtlCoolConsole.py:261
AtlCoolConsole.AtlCoolConsole.command_rmtag
def command_rmtag(self, argumentString)
Definition: AtlCoolConsole.py:342
AtlCoolConsole.AtlCoolConsole.connectString
connectString
Definition: AtlCoolConsole.py:92
AtlCoolConsole.HistoryConsole.init_history
def init_history(self, histfile)
Definition: AtlCoolConsole.py:39
AtlCoolConsole.AtlCoolConsole.command_pwd
def command_pwd(self)
Definition: AtlCoolConsole.py:211
AtlCoolConsole.AtlCoolConsole.command_settginfo
def command_settginfo(self, argumentString)
Definition: AtlCoolConsole.py:296
AtlCoolConsole.AtlCoolConsole.command_lstags
def command_lstags(self, argumentString)
Definition: AtlCoolConsole.py:217