6 from __future__
import print_function
16 if 'TERM' in os.environ:
17 del os.environ[
'TERM']
19 msg = logging.getLogger(
'MetaReader')
21 from PyUtils.MetaReader
import read_metadata, trigger_keys
24 def _tree_print(content, indent=2, pad=0, list_max_items=-1, dict_sort=None, ascii=False, hide_content=False, hide_trigger=False):
27 if isinstance(content, dict):
29 if dict_sort ==
'key':
30 items =
list(
sorted(content.items(), key =
lambda t: t[0]))
31 elif dict_sort ==
'value':
32 items =
list(
sorted(content.items(), key =
lambda t: t[1]))
34 items =
list(content.items())
36 items_count = len(items)
37 for i
in range(items_count):
41 last = i == items_count - 1
43 if hide_content
and (key
in [
'itemList',
'metadata_items']
or 'EventFormatStream' in key):
46 if hide_trigger
and key
in trigger_keys:
52 skey =
str(key).rjust(-pad)
54 skey =
str(key).ljust(pad)
57 s += (
'├' if not last
else '└') +
'─' * indent +
' ' + skey +
': '
59 s += (
'|' if not last
else '`') +
'-' * indent +
' ' + skey +
': '
61 lines =
_tree_print(value, indent=indent, pad=pad, dict_sort=dict_sort, list_max_items=list_max_items, ascii=ascii,
62 hide_content=hide_content, hide_trigger=hide_trigger).
split(
'\n')
70 s +=
'\n' + (
'│' if not last
else ' ') +
' ' * indent +
' ' +
str(line)
72 s +=
'\n' + (
'|' if not last
else ' ') +
' ' * indent +
' ' +
str(line)
75 elif isinstance(content, (list, tuple))
and list_max_items >= 0
and len(content) > list_max_items:
78 items_count = len(items)
80 for i
in range(items_count):
83 last = i == items_count - 1
86 s += (
'├' if not last
else '└') +
'─' * indent +
' ' +
str(value) +
'\n'
88 s += (
'|' if not last
else '`') +
'-' * indent +
' ' +
str(value) +
'\n'
97 parser = argparse.ArgumentParser(description=
'This script reads metadata from a given file')
98 parser.add_argument(
'filenames',
100 help=
'The filenames to read. User can provide a single file or a list of files.')
101 parser.add_argument(
'-v',
104 help=
'print detailed output on screen')
105 parser.add_argument(
'-o',
109 help=
"Saves the output in a file. By default, the output is written on the screen (stdout) in a prettier format for better readabiilty.")
110 parser.add_argument(
'--json',
112 help=
"Sets the output file format as json.")
113 parser.add_argument(
'--indent',
117 help=
"Sets the indent spaces in the output either on screen (without -o flag) either on file (with -o flag). By default, uses two spaces as indent.")
118 parser.add_argument(
'-m',
123 choices=[
'tiny',
'lite',
'full',
'peeker',
'iov'],
124 help=
"This flag provides the user capability to select the amount of metadata retrieved. There are five options: "
125 "tiny (only those values used in PyJobTransforms), "
128 "full (all available data found), "
129 "and iov (full+iov details)" )
131 parser.add_argument(
'-t',
136 choices=[
'POOL',
'BS'],
137 help=
"The file type of the input filename. By default, it tries to determine itself the file type of the input.")
138 parser.add_argument(
'-f',
144 help=
"The metadata keys to filter. ")
145 parser.add_argument(
'--promote',
148 help=
"Force promotion or not of the metadata keys ")
149 parser.add_argument(
'--hideContentList',
151 help=
"Hide content lists (event-level and metadata containers)")
152 parser.add_argument(
'--hideTrigger',
154 help=
"Hide trigger metadata")
155 args = parser.parse_args()
157 verbose = args.verbose
158 filenames = args.filenames
163 file_type = args.type
164 meta_key_filter = args.filter
166 msg.setLevel(logging.INFO
if verbose
else logging.WARNING)
168 handler = logging.StreamHandler()
169 handler.setLevel(logging.INFO
if verbose
else logging.WARNING)
171 formatter = logging.Formatter(
'%(name)s %(levelname)s %(message)s')
172 handler.setFormatter(formatter)
174 msg.addHandler(handler)
176 startTime = time.time()
177 msg.info(
'Imported headers in: {0} miliseconds'.
format((time.time() - startTime) * 1e3))
178 msg.info(
'The output file is: {0}'.
format(output))
179 metadata =
read_metadata(filenames, file_type, mode= mode, meta_key_filter= meta_key_filter, promote=args.promote)
183 print(json.dumps(metadata, indent=indent))
185 enc = sys.stdout.encoding
186 ascii =
not sys.stdout.isatty()
or not enc
or enc.lower().
find(
'ansi') >= 0
or enc.lower().
find(
'ascii') >= 0
187 print(
_tree_print(metadata, indent=indent, pad=18, dict_sort=
'key', list_max_items=8, ascii=ascii,
188 hide_content=args.hideContentList, hide_trigger=args.hideTrigger))
192 with open(output,
'w')
as fd:
193 print(json.dumps(metadata, indent=indent), file=fd)
195 with open(output,
'w')
as fd:
196 print(
_tree_print(metadata, indent=indent, pad=18, dict_sort=
'key', list_max_items=8, ascii=
True,
197 hide_content=args.hideContentList, hide_trigger=args.hideTrigger),
202 if __name__ ==
'__main__':