CMS 3D CMS Logo

dataformats.py
Go to the documentation of this file.
1 from __future__ import print_function
2 
3 import cStringIO,operator
4 from functools import reduce
5 
6 def indent(rows, hasHeader=False, headerChar='-', delim=' | ', justify='left',
7  separateRows=False, prefix='', postfix='', wrapfunc=lambda x:x):
8  """Indents a table by column.
9  - rows: A sequence of sequences of items, one sequence per row.
10  - hasHeader: True if the first row consists of the columns' names.
11  - headerChar: Character to be used for the row separator line
12  (if hasHeader==True or separateRows==True).
13  - delim: The column delimiter.
14  - justify: Determines how are data justified in their column.
15  Valid values are 'left','right' and 'center'.
16  - separateRows: True if rows are to be separated by a line
17  of 'headerChar's.
18  - prefix: A string prepended to each printed row.
19  - postfix: A string appended to each printed row.
20  - wrapfunc: A function f(text) for wrapping text; each element in
21  the table is first wrapped by this function."""
22  # closure for breaking logical rows to physical, using wrapfunc
23  def rowWrapper(row):
24  newRows = [wrapfunc(item).split('\n') for item in row]
25  return [[substr or '' for substr in item] for item in map(None,*newRows)]
26  # break each logical row into one or more physical ones
27  logicalRows = [rowWrapper(row) for row in rows]
28  # columns of physical rows
29  columns = map(None,*reduce(operator.add,logicalRows))
30  # get the maximum of each column by the string length of its items
31  maxWidths = [max([len(str(item)) for item in column]) for column in columns]
32  rowSeparator = headerChar * (len(prefix) + len(postfix) + sum(maxWidths) + \
33  len(delim)*(len(maxWidths)-1))
34  # select the appropriate justify method
35  justify = {'center':str.center, 'right':str.rjust, 'left':str.ljust}[justify.lower()]
36  output=cStringIO.StringIO()
37  if separateRows: print(rowSeparator, file=output)
38  for physicalRows in logicalRows:
39  for row in physicalRows:
40  print(prefix \
41  + delim.join([justify(str(item),width) for (item,width) in zip(row,maxWidths)]) \
42  + postfix, file=output)
43  if separateRows or hasHeader: print(rowSeparator, file=output); hasHeader=False
44  return output.getvalue()
45 
46 # written by Mike Brown
47 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/148061
48 def wrap_onspace(text, width):
49  """
50  A word-wrap function that preserves existing line breaks
51  and most spaces in the text. Expects that existing line
52  breaks are posix newlines (\n).
53  """
54  return reduce(lambda line, word, width=width: '%s%s%s' %
55  (line,
56  ' \n'[(len(line[line.rfind('\n')+1:])
57  + len(word.split('\n',1)[0]
58  ) >= width)],
59  word),
60  text.split(' ')
61  )
62 
63 import re
64 def wrap_onspace_strict(text, width):
65  """Similar to wrap_onspace, but enforces the width constraint:
66  words longer than width are split."""
67  wordRegex = re.compile(r'\S{'+str(width)+r',}')
68  return wrap_onspace(wordRegex.sub(lambda m: wrap_always(m.group(),width),text),width)
69 
70 import math
71 def wrap_always(text, width):
72  """A simple word-wrap function that wraps text on exactly width characters.
73  It doesn't split the text in words."""
74  return '\n'.join([ text[width*i:width*(i+1)] \
75  for i in xrange(int(math.ceil(1.*len(text)/width))) ])
76 
77 
78 
79 # END OF TABLE FORMATING
80 
81 # START of import
82 import sys
83 imported_modules = []
84 
85 def importDF(path):
86 
87  modules_to_import = "RecoTracker RecoLocalTracker RecoLocalCalo RecoEcal RecoEgamma RecoLocalMuon RecoMuon RecoJets RecoMET RecoBTag RecoTauTag RecoVertex RecoPixelVertexing HLTrigger RecoParticleFlow".split()
88  modules_to_import = "RecoLocalTracker RecoLocalMuon RecoLocalCalo RecoEcal TrackingTools RecoTracker RecoJets RecoMET RecoMuon RecoBTau RecoBTag RecoTauTag RecoVertex RecoPixelVertexing RecoEgamma RecoParticleFlow L1Trigger".split()
89 
90 
91  for module in modules_to_import:
92  m = module + "_dataformats"
93  try:
94  sys.path.append(path+"/src/Documentation/DataFormats/python/")
95 # sys.path.append(".")
96  globals()[m] = __import__(m)
97  imported_modules.append(m)
98  print("Searching in "+ module)
99  except ImportError:
100  print("skipping", module)
101 
102 # END of import
103 
104 
105 def search(query):
106  labels = ('Where(Package)', 'Instance', 'Container', 'Description')
107  width = 20
108  data = ""
109 
110  for module in imported_modules:
111  dict = vars(globals()[module])["json"]
112  for type in ["full", "reco", "aod"]:
113  for data_items in dict[type]['data']:
114  if query.lower() in data_items.__str__().lower() and not (("No documentation".lower()) in data_items.__str__().lower()):
115  data+= module.replace("_json", "")+" ("+ type.replace("full", "FEVT") + ")||" + "||".join(data_items.values())+"\n"
116 
117  if (data != ""):
118  rows = [row.strip().split('||') for row in data.splitlines()]
119  print(indent([labels]+rows, hasHeader=True, separateRows=True, prefix='| ', postfix=' |', wrapfunc=lambda x: wrap_always(x,width)))
120  else:
121  print("No documentation found")
122 
123 def help():
124  print("usage: dataformats pattern_to_search")
125  print("example: dataformats muon")
126  print("Note! multiple patterns separated by space are not supported")
127 
128 if __name__ == "__main__":
129 
130  if ("help" in sys.argv):
131  help()
132  sys.exit(0)
133 
134  if (len(sys.argv) > 2):
135  importDF(sys.argv[1])
136  print("\nSearching for: "+sys.argv[2]+"\n")
137  search(sys.argv[2])
138 
139  else:
140  help()
141 
142 
dataformats.wrap_onspace_strict
def wrap_onspace_strict(text, width)
Definition: dataformats.py:64
dataformats.indent
def indent(rows, hasHeader=False, headerChar='-', delim='|', justify='left', separateRows=False, prefix='', postfix='', wrapfunc=lambda x:x)
Definition: dataformats.py:6
dataformats.importDF
def importDF(path)
Definition: dataformats.py:85
dataformats.wrap_onspace
def wrap_onspace(text, width)
Definition: dataformats.py:48
join
static std::string join(char **cmd)
Definition: RemoteFile.cc:17
dataformats.help
def help()
Definition: dataformats.py:123
submitPVValidationJobs.split
def split(sequence, size)
Definition: submitPVValidationJobs.py:352
dataformats.wrap_always
def wrap_always(text, width)
Definition: dataformats.py:71
str
#define str(s)
Definition: TestProcessor.cc:53
dataformats.search
def search(query)
Definition: dataformats.py:105
print
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:46
SiStripPI::max
Definition: SiStripPayloadInspectorHelper.h:169
createfilelist.int
int
Definition: createfilelist.py:10
ComparisonHelper::zip
OutputIterator zip(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
Definition: L1TStage2CaloLayer1.h:41
genParticles_cff.map
map
Definition: genParticles_cff.py:11