CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
JSONExport.py
Go to the documentation of this file.
1 import sys
2 import os.path
3 import logging
4 import random
5 
7 import FWCore.ParameterSet.Config as cms
8 import FWCore.ParameterSet.Modules as mod
9 import FWCore.ParameterSet.Types as typ
10 import FWCore.ParameterSet.Mixins as mix
11 
12 from Vispa.Plugins.ConfigEditor.ConfigDataAccessor import ConfigDataAccessor
13 from FWCore.GuiBrowsers.FileExportPlugin import FileExportPlugin
14 
16  option_types={}
17  plugin_name='JSON Export'
18  file_types=('html','json')
19  def __init__(self):
20  FileExportPlugin.__init__(self)
21 
22  def produce(self,data):
23 
24  #pset = lambda pdict: [[k,repr(v).split('(',1)[0],(repr(v).split('(',1)[1][:-1])] for k,v in pdict.items()]
25  def pset(pdict):
26  result = []
27  for k,v in pdict.items():
28  if v.pythonTypeName()=='cms.PSet' or v.pythonTypeName()=='cms.untracked.PSet':
29  result.append([k,v.pythonTypeName(),'pset',pset(v.parameters_())])
30  elif v.pythonTypeName()=='cms.VPSet' or v.pythonTypeName()=='cms.untracked.VPSet':
31  result.append([k,v.pythonTypeName(),'vpset',[pset(a.parameters_()) for a in v]])
32  elif v.pythonTypeName().lower().startswith('cms.v') or v.pythonTypeName().lower().startswith('cms.untracked.v'):
33  result.append([k,v.pythonTypeName(),'list',[repr(a) for a in v]])
34  else:
35  result.append([k,v.pythonTypeName(),'single',repr(v.pythonValue())])
36  return result
37 
38  #allObjects = [d for d in data._allObjects if (data.type(d) in ("EDProducer","EDFilter","EDAnalyzer","OutputModule"))]
39  #data.readConnections(allObjects)
40 
41  def moduledict(mod,prefix,links=False):
42  result={}
43  result['label']=data.label(mod)
44  result['class']=data.classname(mod)
45  result['file']=data.pypath(mod)
46  result['line']=data.lineNumber(mod)
47  result['package']=data.pypackage(mod)
48  result['pset']=pset(mod.parameters_())
49  result['type']=data.type(mod)
50  if links:
51  result['uses']=[data.uses(mod)]
52  result['usedby']=[data.usedBy(mod)]
53  result['id']='%s_%s'%(prefix,data.label(mod))
54  return result
55 
56  all={}
57  for tlo in data.topLevelObjects():
58  children=data.children(tlo)
59  if children:
60  all[tlo._label]=children
61 
62  process = {'name':data.process().name_(),'src':data._filename}
63 
64  #now unavailable
65  #schedule = []
66  #if 'Schedule' in all:
67  # for s in all['Schedule']:
68  # schedule.append(data.label(s))
69 
70  source={}
71  if 'source' in all:
72  s = all['source'][0]
73  source['class']=data.classname(s)
74  source['pset']=pset(s.parameters_())
75 
76  essources=[]
77  if 'essources' in all:
78  for e in all['essources']:
79  essources.append(moduledict(e,'essource'))
80  esproducers=[]
81  if 'esproducers' in all:
82  for e in all['esproducers']:
83  essources.append(moduledict(e,'esproducer'))
84  esprefers=[]
85  if 'esprefers' in all:
86  for e in all['esprefers']:
87  essources.append(moduledict(e,'esprefers'))
88  services=[]
89  if 'services' in all:
90  for s in all['services']:
91  services.append({'class':data.classname(s),'pset':pset(s.parameters_())})
92 
93 
94  def jsonPathRecursive(p,prefix):
95  #print "At:",self.label(p),self.type(p)
96  children = data.children(p)
97  if children:
98  children = [jsonPathRecursive(c,prefix) for c in children]
99  return {'type':'Sequence','label':'Sequence %s'%(data.label(p)),'id':'seq_%s' % data.label(p),'children':children}
100  else:
101  return moduledict(p,prefix,True)
102 
103 
104  paths=[]
105  if 'paths' in all:
106  for p in all['paths']:
107  path=jsonPathRecursive(p,data.label(p))
108  if path:
109  if not type(path)==type([]):
110  if path['type']=='Sequence':
111  path = path['children']
112  else:
113  path = [path]
114  else:
115  path=[]
116  paths.append({'label':data.label(p),'path':path})
117  endpaths=[]
118  if 'endpaths' in all:
119  for p in all['endpaths']:
120  path=jsonPathRecursive(p,data.label(p))
121  if path:
122  if not type(path)==type([]):
123  if path['type']=='Sequence':
124  path = path['children']
125  else:
126  path = [path]
127  else:
128  path=[]
129  endpaths.append({'label':data.label(p),'path':path})
130 
131  #json={'process':process,'schedule':schedule,'source':source,'essources':essources,'esproducers':esproducers,'esprefers':esprefers,'services':services,'paths':paths,'endpaths':endpaths}
132  json={'process':process,'source':source,'essources':essources,'esproducers':esproducers,'esprefers':esprefers,'services':services,'paths':paths,'endpaths':endpaths}
133 
134  return repr(json)
135 
136  def export(self,data,filename,filetype):
137  if not data.process():
138  raise "JSONExport requires a cms.Process object"
139 
140  json = self.produce(data)
141 
142  if filetype=='json':
143  jsonfile = open(filename,'w')
144  jsonfile.write(json)
145  jsonfile.close()
146  if filetype=='html':
147  #open the HTML template and inject the JSON...
148  pass
149