5 from __future__
import print_function
14 log = logging.getLogger( __name__ )
15 log.setLevel( logging.INFO )
17 handler = logging.StreamHandler()
18 format =
"%(levelname)s:%(name)s: %(message)s"
19 handler.setFormatter( logging.Formatter( format ) )
20 log.addHandler( handler )
23 historyFile =
'.coolconsole_hist'
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.
34 filename = "<console>",
35 histfile = os.path.join( os.environ[
"HOME"], historyFile) ):
36 code.InteractiveConsole.__init__( self )
40 readline.parse_and_bind(
"tab: complete" )
41 readline.set_history_length( 100 )
42 if hasattr( readline,
"read_history_file" ):
44 readline.read_history_file( histfile )
50 readline.write_history_file( histfile )
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'
88 banner =
"Welcome to AtlCoolConsole. Type 'help' for instructions."
91 HistoryConsole.__init__( self )
96 self.
push(
'from CoolConvUtilities import AtlCoolTool' )
97 if connectString
is not None:
99 self.
push(
'print (this)' )
101 print (
"Not connected. Use the 'open' command to connect to "
103 HistoryConsole.interact( self, self.
banner )
109 res=re.search(
'^' + command +
r'($|\s+(?P<remainder>.*))', line )
111 return command, res.group(
'remainder')
or ""
116 if command ==
'less':
118 elif command ==
'more':
120 elif command ==
'ls':
121 return self.
command_ls( argumentString,
False )
122 elif command ==
'll':
124 elif command ==
'help':
126 elif command ==
'exit':
128 elif command ==
'quit':
130 elif command ==
'open':
132 elif command ==
'listtags':
134 elif command ==
'filtertags':
136 elif command ==
'listchans':
138 elif command ==
'listinfo':
140 elif command ==
'rmdir':
142 elif command ==
'usetag':
144 elif command ==
'usechan':
146 elif command ==
'settag':
148 elif command ==
'settginfo':
150 elif command ==
'setchan':
152 elif command ==
'userunlumi':
154 elif command ==
'usetimes':
156 elif command ==
'clonetag':
158 elif command ==
'rmtag':
160 elif command ==
'locktag':
162 elif command ==
'headtag':
164 elif command ==
'tracetags':
166 elif command ==
'setdesc':
170 elif command ==
'pwd':
172 elif command ==
'pws':
179 if argumentString
is None:
180 raise Exception(
"usage: less <folder>" )
181 argumentString.strip()
182 nodes = argumentString.split()
185 cmds.append(
'this.less("%s", header=True)' % node )
186 return ';'.
join( cmds )
189 if argumentString
is None:
190 raise Exception(
"usage: more <folder>" )
191 argumentString.strip()
192 nodes = argumentString.split()
195 cmds.append(
'this.more("%s", header=True)' % node )
196 return ';'.
join( cmds )
200 if argumentString
is None: argumentString =
'.'
201 argumentString.strip()
202 if argumentString ==
'': argumentString =
'.'
203 nodes = argumentString.split()
206 if (doCount): tof=
'True'
208 cmds.append(
'this.ls("%s", header=True, doCount=%s)' % (node,tof) )
209 return ';'.
join( cmds )
212 return ';'.
join([
'this.pwd()'])
215 return ';'.
join([
'this.pws()'])
218 if argumentString
is None: argumentString =
'.'
219 argumentString.strip()
220 if argumentString
is None: argumentString =
'.'
221 nodes = argumentString.split()
224 cmds.append(
'this.lstags("%s")' % node )
225 return ';'.
join( cmds )
228 if argumentString
is None or argumentString==
"": argumentString =
'.'
229 argumentString.strip()
230 if argumentString
is None: argumentString =
'.'
231 args = argumentString.split()
237 cmds.append(
'this.lstags("%s",pattern="%s")' % (node,pattern))
238 return ';'.
join(cmds)
242 if argumentString
is None: argumentString =
'.'
243 argumentString.strip()
244 if argumentString
is None: argumentString =
'.'
245 nodes = argumentString.split()
248 cmds.append(
'this.listchans("%s")' % node )
249 return ';'.
join( cmds )
252 if argumentString
is None: argumentString =
'.'
253 argumentString.strip()
254 if argumentString
is None: argumentString =
'.'
255 nodes = argumentString.split()
258 cmds.append(
'this.listinfo("%s")' % node )
259 return ';'.
join( cmds )
262 argumentString.strip()
264 cmds.append(
'this.rmdir("%s")' % argumentString)
265 return ';'.
join( cmds )
268 argumentString.strip()
270 cmds.append(
'this.usetag("%s")' % argumentString)
271 return ';'.
join( cmds )
274 argumentString.strip()
276 cmds.append(
'this.usechan("%s")' % argumentString)
277 return ';'.
join( cmds )
280 if argumentString
is None: argumentString =
'/'
281 argumentString.strip()
282 if argumentString ==
'': argumentString =
'/'
284 cmds.append(
'this.cd("%s")' % argumentString)
285 return ';'.
join( cmds )
288 args=argumentString.split()
290 print (
'Usage: settag <folder> <foldertag> <parenttag>')
293 cmds.append(
'this.settag("%s")' % argumentString)
294 return ';'.
join(cmds)
297 args=argumentString.split()
299 print (
'Usage: settginfo <folder> <tag> <description>')
302 cmds.append(
'this.settginfo("%s")' % argumentString)
303 return ';'.
join(cmds)
306 args=argumentString.split()
308 print (
'Usage: setchan <folder> <chanID> <chanName> {<chanDescr>}')
311 cmds.append(
'this.setchan("%s")' % argumentString)
312 return ';'.
join(cmds)
315 args=argumentString.split()
316 if len(args)!=1
and len(args)<4:
317 print (
'Usage: userunlumi <run1> {<LB1> <run2> <LB2>}')
320 cmds.append(
'this.userunlumi("%s")' % argumentString)
321 return ';'.
join(cmds)
324 args=argumentString.split()
326 print (
'Usage: usetimes <time1> <time2>')
329 cmds.append(
'this.usetimes("%s")' % argumentString)
330 return ';'.
join(cmds)
333 args=argumentString.split()
335 print (
'Usage: clonetag <folder> <sourcetag> <desttag>')
338 cmds.append(
'this.clonetag("%s")' % argumentString)
339 return ';'.
join(cmds)
343 args=argumentString.split()
345 print (
'Usage: rmtag <folder> <leaf or parent tag>')
348 cmds.append(
'this.rmtag("%s")' % argumentString)
349 return ';'.
join(cmds)
352 args=argumentString.split()
354 print (
'Usage: locktag <folder> <tag> {action=l|p|u|r}')
357 cmds.append(
'this.locktag("%s")' % argumentString)
358 return ';'.
join(cmds)
361 args=argumentString.split()
363 print (
'Usage: headtag <folder> <tag>')
366 cmds.append(
'this.headtag("%s")' % argumentString)
367 return ';'.
join(cmds)
371 args=argumentString.split()
373 print (
'Usage: tracetags <folder> <foldertag>')
376 cmds.append(
'this.tracetags("%s")' % argumentString)
377 return ';'.
join(cmds)
381 args=argumentString.split()
383 print (
'Usage: setdesc <folder> {<newdesc>}')
386 cmds.append(
'this.setdesc("%s")' % argumentString)
387 return ';'.
join(cmds)
391 if argumentString
is not None and argumentString !=
'':
394 return 'help ' + argumentString
395 print (
'Available commands:')
399 print (
' %(key)-8s : %(value)s' % {
'key' : 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( '/' )")
410 return 'import sys ; sys.exit(0)'
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
423 command, remainder = self.
parseLine( line )
424 log.debug(
'command: %s' % command )
425 log.debug(
'remainder: %s' % remainder )
429 log.debug(
'command: "%s"' % res )
430 interpreterCommand = res
432 interpreterCommand = line
435 return HistoryConsole.push( self, interpreterCommand )
440 if __name__ ==
'__main__':
443 usage = (
'Usage: %s <connect string>\n'
444 '\t<connect string>: a RAL connect string\n'
448 if len( sys.argv ) == 2:
449 connectString = sys.argv[1]