/
rplotsh.py
executable file
·373 lines (320 loc) · 13.1 KB
/
rplotsh.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/usr/bin/env python3
# coding=utf-8
# argument parsing
from argparse import ArgumentParser
optparser = ArgumentParser(description=__doc__)
optparser.add_argument('filenames', nargs='+', help='ROOT files')
options = optparser.parse_args()
from fixes import ROOT
from ROOT import gROOT, gDirectory
import cmd
import shlex
from rdir import Rdir, savepwd
from utils import is_dir, root_str, NoExitArgParse
from textwrap import dedent
class empty(cmd.Cmd):
def emptyline(self):
pass
def do_EOF(self, line):
"""Exit on EOF (C-d)"""
return True
class rshell(cmd.Cmd):
"""Shell-like navigation commands for ROOT files"""
ls_parser = NoExitArgParse(description='List objects in directory/file',
epilog='See also: pathspec', add_help=False)
ls_parser.add_argument('-l', action='store_true', dest='showtype',
help='Long form listing, include object type and '
'size (compressed in parantheses).')
ls_parser.add_argument('paths', nargs='*', help='Object names.')
pwd = gROOT
prompt = '{}> '.format(pwd.GetName())
objs = {}
@classmethod
def _bytes2kb(cls, Bytes):
unit = 1
while Bytes >= 1024:
Bytes /= 1024.0
unit += 1
if unit == 1:
return Bytes
elif unit == 2:
unit = 'KB'
elif unit == 3:
unit = 'MB'
elif unit == 4:
unit = 'GB'
return '{:.1f}{}'.format(Bytes, unit)
def add_files(self, files):
self.rdir_helper = Rdir(files)
def completion_helper(self, text, line, begidx, endidx, comp_type=None):
if line.rfind(':') > 0:
pathstr = line.split()[-1]
else:
pathstr = text
self.comp_f = [f.GetName() + ':' for f in self.rdir_helper.files]
if self.pwd == gROOT and pathstr.find(':') < 0:
completions = self.comp_f
else:
path = os.path.dirname(pathstr)
completions = self.rdir_helper.ls_names(path, comp_type)
# NB: Strip trailing slash, and get path without filename.
# This is necessary since Cmd for some reason splits at
# the colon separator.
path = path.rstrip('/').split(':')[-1]
if path or text.rfind('/') == 0:
completions = ['/'.join((path, i)) for i in completions]
completions += self.comp_f
if not text:
return completions
else:
return filter(lambda i: str.startswith(i, text), completions)
def precmd(self, line):
return cmd.Cmd.precmd(self, line)
def postcmd(self, stop, line):
self.oldpwd = self.pwd.GetDirectory('')
self.pwd = gDirectory.GetDirectory('')
dirn = self.pwd.GetName()
if len(dirn) > 20:
dirn = '{}..{}'.format(dirn[0:9], dirn[-9:])
self.prompt = '{}> '.format(dirn)
return cmd.Cmd.postcmd(self, stop, line)
def get_ls_fmt(self, showtype=False, indent=''):
if showtype:
return indent + '{cls:<20}{fs:>8}({us:>8}) {nm}{m}'
else:
return indent + '{nm}{m}'
def print_key(self, key, fmt):
name = key.GetName()
if isinstance(key, ROOT.TKey):
cname = key.GetClassName()
fsize = self._bytes2kb(key.GetNbytes())
usize = self._bytes2kb(key.GetObjlen())
else: # NB: special case, a TFile
cname = key.ClassName()
cls = ROOT.TClass.GetClass(cname)
if cls.InheritsFrom(ROOT.TFile.Class()):
res = fmt.format(cls=cname, nm=name, m=':', fs='-', us='-')
elif cls.InheritsFrom(ROOT.TDirectoryFile.Class()):
res = fmt.format(cls=cname, nm=name, m='/', fs=fsize, us=usize)
else:
res = fmt.format(cls=cname, nm=name, m='', fs=fsize, us=usize)
print(res)
def ls_objs(self, keys, showtype=False, indent=''):
# handle invalid keys
if keys:
valid = reduce(lambda i, j: i and j, keys)
else:
valid = False
if valid:
fmt = self.get_ls_fmt(showtype, indent)
for key in keys:
self.print_key(key, fmt)
else:
raise ValueError('{}: cannot access {}: No such object')
def print_memobjs(self, objs):
"""Print memory objects"""
for name, obj in objs.iteritems():
print '{}:\n {}'.format(name, root_str(obj))
def do_lsmem(self, args):
"""List objects read in memory"""
if args:
tokens = shlex.split(args)
try:
tmp = dict([(tok, self.objs[tok]) for tok in tokens])
except KeyError:
from fnmatch import fnmatchcase
tmp = {}
for tok in tokens:
tmp.update(dict([(key, self.objs[key]) for key in self.objs
if fnmatchcase(key, tok)]))
self.print_memobjs(tmp)
else:
self.print_memobjs(self.objs)
def complete_lsmem(self, text, line, begidx, endidx):
return filter(lambda key: key.startswith(text), self.objs)
def help_ls(self):
self.ls_parser.print_help()
def do_ls(self, args=''):
"""List contents of a directory/file"""
opts = self.ls_parser.parse_args(args.split())
if opts.paths: # w/ args
for path in opts.paths:
isdir = self.rdir_helper.get_dir(path)
indent = ''
if isdir:
if not isinstance(isdir, ROOT.TFile):
# convert to TKey when TDirectoryFile
dirname = isdir.GetName()
with savepwd():
isdir.cd('..')
# read the latest cycle
isdir = filter(lambda k: k.GetName() == dirname,
gDirectory.GetListOfKeys())[0]
self.print_key(isdir, self.get_ls_fmt(opts.showtype))
indent = ' '
keys = self.rdir_helper.ls(path)
try:
self.ls_objs(keys, opts.showtype, indent)
except ValueError as err:
print(str(err).format('ls', path))
else: # no args
if gROOT == self.pwd:
# can't access files trivially when in root
for f in gROOT.GetListOfFiles():
self.print_key(f, self.get_ls_fmt(opts.showtype))
else: # in a file
try:
self.ls_objs(self.rdir_helper.ls(), opts.showtype)
except ValueError as err:
print(str(err).format('ls', ''))
print('Warning: this shouldn\'t happen, something went '
'terribly wrong!')
def complete_ls(self, text, line, begidx, endidx):
return self.completion_helper(text, line, begidx, endidx)
def do_pwd(self, args=None):
"""Print the name of the current working directory"""
thisdir = self.pwd.GetDirectory('')
pwdname = thisdir.GetName()
while not (isinstance(thisdir, ROOT.TFile) or self.pwd == gROOT):
thisdir = thisdir.GetDirectory('../')
if isinstance(thisdir, ROOT.TFile):
break
pwdname = '/'.join((thisdir.GetName(), pwdname))
if isinstance(self.pwd, ROOT.TFile):
print('{}:'.format(pwdname))
elif self.pwd == gROOT:
print(pwdname)
else:
print('{}:/{}'.format(thisdir.GetName(), pwdname))
def do_cd(self, args=''):
"""Change directory to specified directory. See also: pathspec."""
if args.strip() == '-':
success = self.oldpwd.cd()
else:
success = self.pwd.cd(args)
if not success:
print('cd: {}: No such file or directory'.format(args))
else:
if not args.strip():
gROOT.cd()
def complete_cd(self, text, line, begidx, endidx):
return self.completion_helper(text, line, begidx, endidx,
ROOT.TDirectoryFile)
def save_obj(self, args):
"""Read objects into shell"""
self.objs.update(args)
def help_read(self):
msg = '''\
Syntax: read <objname> [as <newobjname>]
If <objname> is a directory, all objects in that directory are
read in to memory. In this case, if `as <newobjname>\' is present,
objects are stored in a list of that name. <objname> can also be a
globbing pattern or a regular expression, `as <newobjname>\'
semantics are similar to directory.
Note: Since `as\' is a keyword, an object named `as\' cannot be read
simply. Use a regex for that: e.g. a[s].'''
print(dedent(msg))
def do_read(self, args):
"""Read objects into memory."""
if args:
tokens = shlex.split(args)
path = tokens[0]
if 'as' in tokens: # destination var specified or not
# or 3 == len(tokens)
try:
assert(tokens[1] == 'as')
try:
newobj = tokens[2]
except IndexError:
newobj = None
# raise ValueError('Missing destination variable')
except AssertionError:
print 'Unknown command token: {}'.format(tokens[1])
print 'Will do regular read'
else:
newobj = None
# find and read objects
objs = self.rdir_helper.read(path, metainfo=True)
if not objs: # nothing found, try glob
pattern = path.rsplit('/', 1)
if len(pattern) > 1:
path, pattern = pattern[:-1], pattern[-1]
else:
path, pattern = None, pattern[0]
from fnmatch import fnmatchcase
match = lambda name: fnmatchcase(name, pattern)
notdir = lambda key: not is_dir(key) and match(key.GetName())
objs = self.rdir_helper.read(path, robj_p=notdir, metainfo=True)
if not objs: # nothing found, try regex
import re
match = re.compile(pattern).match
notdir = lambda key: not is_dir(key) and match(key.GetName())
objs = self.rdir_helper.read(path, robj_p=notdir, metainfo=True)
# save read objects
if newobj:
if len(objs) > 1:
objs = {newobj: objs}
else:
objs = {newobj: objs[0]} # only one element
else:
objs = [(obj.GetName(), obj) for obj in objs]
self.save_obj(objs)
else:
print('Nothing to read!')
def complete_read(self, text, line, begidx, endidx):
return self.completion_helper(text, line, begidx, endidx)
def do_python(self, args=None):
"""Start an interactive Python console"""
import code
import readline
import rlcompleter
# save, switch, and restore history files
readline.write_history_file(__histfile__)
readline.clear_history() # remove rplotsh history from Python
if os.path.exists(__pyhistfile__):
readline.read_history_file(__pyhistfile__)
# save and restore old completer
rplotsh_completer = readline.get_completer()
readline.set_completer(rlcompleter.Completer(self.objs).complete)
readline.parse_and_bind("tab: complete")
shell = code.InteractiveConsole(self.objs)
shell.interact()
readline.set_completer(rplotsh_completer)
readline.write_history_file(__pyhistfile__)
if os.path.exists(__histfile__):
readline.read_history_file(__histfile__)
def help_pathspec(self):
msg = '''\
Paths inside the current file can be specified using the normal syntax:
- full path: /dir1/dir2
- relative path: ../dir1
Paths in other root files have to be preceded by the file name and a
colon:
- file path: myfile.root:/dir1/dir2
See also: TDirectoryFile::cd(..) in ROOT docs, `help pathspec\'.'''
print(dedent(msg))
class rplotsh(rshell, empty):
"""Interactive plotting interface for ROOT files"""
def postloop(self):
print
if __name__ == '__main__':
# history file for interactive use
import atexit
import readline
import os
import sys
# history variables
__histfile__ = '.rplotsh'
__pyhistfile__ = '{}.py'.format(__histfile__)
if os.path.exists(__histfile__):
readline.read_history_file(__histfile__)
atexit.register(readline.write_history_file, __histfile__)
del atexit, readline
# command loop
try:
rplotsh_inst = rplotsh()
rplotsh_inst.add_files(options.filenames)
rplotsh_inst.cmdloop()
except KeyboardInterrupt:
rplotsh_inst.postloop()
sys.exit(1)