Loading [MathJax]/extensions/tex2jax.js
ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
meta-reader.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 # Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
4 # This script reads metadata from a given file
5 
6 import sys
7 import json
8 import argparse
9 import time
10 import logging
11 import os
12 
13 # escape sequence [?1034h which appear on several runs due to smm capability (Meta Mode On) for xterm.
14 if 'TERM' in os.environ:
15  del os.environ['TERM']
16 
17 msg = logging.getLogger('MetaReader')
18 
19 from PyUtils.MetaReader import read_metadata, trigger_keys
20 
21 
22 def _tree_print(content, indent=2, pad=0, list_max_items=-1, dict_sort=None, ascii=False, hide_content=False, hide_trigger=False):
23  s = ''
24 
25  if isinstance(content, dict):
26 
27  if dict_sort == 'key':
28  items = list(sorted(content.items(), key = lambda t: t[0]))
29  elif dict_sort == 'value':
30  items = list(sorted(content.items(), key = lambda t: t[1]))
31  else:
32  items = list(content.items())
33 
34  items_count = len(items)
35  for i in range(items_count):
36 
37  key, value = items[i]
38 
39  last = i == items_count - 1
40 
41  if hide_content and (key in ['itemList', 'metadata_items'] or 'EventFormatStream' in key):
42  continue
43 
44  if hide_trigger and key in trigger_keys:
45  continue
46 
47  if pad == 0:
48  skey = str(key)
49  elif pad < 0:
50  skey = str(key).rjust(-pad)
51  else:
52  skey = str(key).ljust(pad)
53 
54  if not ascii:
55  s += ('├' if not last else '└') + '─' * indent + ' ' + skey + ': '
56  else:
57  s += ('|' if not last else '`') + '-' * indent + ' ' + skey + ': '
58 
59  lines = _tree_print(value, indent=indent, pad=pad, dict_sort=dict_sort, list_max_items=list_max_items, ascii=ascii,
60  hide_content=hide_content, hide_trigger=hide_trigger).split('\n')
61 
62  if len(lines) == 1:
63  s += lines[0] + '\n'
64  else:
65  for line in lines:
66  if line.strip():
67  if not ascii:
68  s += '\n' + ('│' if not last else ' ') + ' ' * indent + ' ' + str(line)
69  else:
70  s += '\n' + ('|' if not last else ' ') + ' ' * indent + ' ' + str(line)
71 
72  s += '\n'
73  elif isinstance(content, (list, tuple)) and list_max_items >= 0 and len(content) > list_max_items:
74 
75  items = list(content)
76  items_count = len(items)
77 
78  for i in range(items_count):
79  value = items[i]
80 
81  last = i == items_count - 1
82 
83  if not ascii:
84  s += ('├' if not last else '└') + '─' * indent + ' ' + str(value) + '\n'
85  else:
86  s += ('|' if not last else '`') + '-' * indent + ' ' + str(value) + '\n'
87  else:
88  s += str(content)
89 
90  return s
91 
92 
93 def _main():
94  # Parsing the arguments provided by user
95  parser = argparse.ArgumentParser(description='This script reads metadata from a given file')
96  parser.add_argument('filenames',
97  nargs = '+',
98  help= 'The filenames to read. User can provide a single file or a list of files.')
99  parser.add_argument('-v',
100  '--verbose',
101  action='store_true',
102  help='print detailed output on screen')
103  parser.add_argument('-o',
104  '--output',
105  metavar='FILE',
106  default=None,
107  help="Saves the output in a file. By default, the output is written on the screen (stdout) in a prettier format for better readabiilty.")
108  parser.add_argument('--json',
109  action='store_true',
110  help="Sets the output file format as json.")
111  parser.add_argument('--indent',
112  metavar='N',
113  type=int,
114  default=2,
115  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.")
116  parser.add_argument('-m',
117  '--mode',
118  default= 'lite',
119  metavar='MODE',
120  type=str,
121  choices=['tiny', 'lite', 'full', 'peeker', 'iov'],
122  help="This flag provides the user capability to select the amount of metadata retrieved. There are five options: "
123  "tiny (only those values used in PyJobTransforms), "
124  "lite, "
125  "peeker, "
126  "full (all available data found), "
127  "and iov (full+iov details)" )
128 
129  parser.add_argument('-t',
130  '--type',
131  default= None,
132  metavar='TYPE',
133  type=str,
134  choices=['POOL', 'BS'],
135  help="The file type of the input filename. By default, it tries to determine itself the file type of the input.")
136  parser.add_argument('-f',
137  '--filter',
138  default= [],
139  metavar='FILTER',
140  nargs = '+',
141  type=str,
142  help="The metadata keys to filter. ")
143  parser.add_argument('--promote',
144  default=None,
145  type=bool,
146  help="Force promotion or not of the metadata keys ")
147  parser.add_argument('--hideContentList',
148  action='store_true',
149  help="Hide content lists (event-level and metadata containers)")
150  parser.add_argument('--hideTrigger',
151  action='store_true',
152  help="Hide trigger metadata")
153  args = parser.parse_args()
154 
155  verbose = args.verbose
156  filenames = args.filenames
157  output = args.output
158  is_json = args.json
159  indent = args.indent
160  mode = args.mode
161  file_type = args.type
162  meta_key_filter = args.filter
163 
164  msg.setLevel(logging.INFO if verbose else logging.WARNING)
165  # create a stream handler
166  handler = logging.StreamHandler()
167  handler.setLevel(logging.INFO if verbose else logging.WARNING)
168  # create a logging format
169  formatter = logging.Formatter('%(name)s %(levelname)s %(message)s')
170  handler.setFormatter(formatter)
171  # add the handlers to the logger
172  msg.addHandler(handler)
173 
174  startTime = time.time()
175  msg.info('Imported headers in: {0} miliseconds'.format((time.time() - startTime) * 1e3))
176  msg.info('The output file is: {0}'.format(output))
177  metadata = read_metadata(filenames, file_type, mode= mode, meta_key_filter= meta_key_filter, promote=args.promote)
178 
179  if output is None:
180  if is_json:
181  print(json.dumps(metadata, indent=indent))
182  else:
183  enc = sys.stdout.encoding
184  ascii = not sys.stdout.isatty() or not enc or enc.lower().find('ansi') >= 0 or enc.lower().find('ascii') >= 0
185  print(_tree_print(metadata, indent=indent, pad=18, dict_sort='key', list_max_items=8, ascii=ascii,
186  hide_content=args.hideContentList, hide_trigger=args.hideTrigger))
187 
188  else:
189  if is_json:
190  with open(output, 'w') as fd:
191  print(json.dumps(metadata, indent=indent), file=fd)
192  else:
193  with open(output, 'w') as fd:
194  print(_tree_print(metadata, indent=indent, pad=18, dict_sort='key', list_max_items=8, ascii=True,
195  hide_content=args.hideContentList, hide_trigger=args.hideTrigger),
196  file=fd)
197 
198  msg.info('Done!')
199 
200 if __name__ == '__main__':
201  _main()
meta-reader._main
def _main()
Definition: meta-reader.py:93
DerivationFramework::TriggerMatchingUtils::sorted
std::vector< typename R::value_type > sorted(const R &r, PROJ proj={})
Helper function to create a sorted vector from an unsorted range.
vtune_athena.format
format
Definition: vtune_athena.py:14
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
python.MetaReader.read_metadata
def read_metadata(filenames, file_type=None, mode='lite', promote=None, meta_key_filter=None, unique_tag_info_values=True, ignoreNonExistingLocalFiles=False)
Definition: MetaReader.py:53
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
print
void print(char *figname, TCanvas *c1)
Definition: TRTCalib_StrawStatusPlots.cxx:25
meta-reader._tree_print
def _tree_print(content, indent=2, pad=0, list_max_items=-1, dict_sort=None, ascii=False, hide_content=False, hide_trigger=False)
Definition: meta-reader.py:22
Trk::open
@ open
Definition: BinningType.h:40
str
Definition: BTagTrackIpAccessor.cxx:11
Trk::split
@ split
Definition: LayerMaterialProperties.h:38