CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_9/src/FWCore/GuiBrowsers/python/JSONExport.py

Go to the documentation of this file.
00001 import sys
00002 import os.path
00003 import logging
00004 import random
00005 
00006 import FWCore.ParameterSet.SequenceTypes as sqt
00007 import FWCore.ParameterSet.Config as cms
00008 import FWCore.ParameterSet.Modules as mod
00009 import FWCore.ParameterSet.Types as typ
00010 import FWCore.ParameterSet.Mixins as mix
00011 
00012 from Vispa.Plugins.ConfigEditor.ConfigDataAccessor import ConfigDataAccessor
00013 from FWCore.GuiBrowsers.FileExportPlugin import FileExportPlugin
00014 
00015 class JsonExport(FileExportPlugin):
00016   option_types={}
00017   plugin_name='JSON Export'
00018   file_types=('html','json')
00019   def __init__(self):
00020     FileExportPlugin.__init__(self)
00021 
00022   def produce(self,data):
00023     
00024     #pset = lambda pdict: [[k,repr(v).split('(',1)[0],(repr(v).split('(',1)[1][:-1])] for k,v in pdict.items()]
00025     def pset(pdict):
00026       result = []
00027       for k,v in pdict.items():
00028         if v.pythonTypeName()=='cms.PSet' or v.pythonTypeName()=='cms.untracked.PSet':
00029           result.append([k,v.pythonTypeName(),'pset',pset(v.parameters_())])
00030         elif v.pythonTypeName()=='cms.VPSet' or v.pythonTypeName()=='cms.untracked.VPSet':
00031           result.append([k,v.pythonTypeName(),'vpset',[pset(a.parameters_()) for a in v]])
00032         elif v.pythonTypeName().lower().startswith('cms.v') or v.pythonTypeName().lower().startswith('cms.untracked.v'):
00033           result.append([k,v.pythonTypeName(),'list',[repr(a) for a in v]])
00034         else:
00035           result.append([k,v.pythonTypeName(),'single',repr(v.pythonValue())])
00036       return result
00037           
00038     #allObjects = [d for d in data._allObjects if (data.type(d) in ("EDProducer","EDFilter","EDAnalyzer","OutputModule"))]
00039     #data.readConnections(allObjects)
00040         
00041     def moduledict(mod,prefix,links=False):
00042       result={}
00043       result['label']=data.label(mod)
00044       result['class']=data.classname(mod)
00045       result['file']=data.pypath(mod)
00046       result['line']=data.lineNumber(mod)
00047       result['package']=data.pypackage(mod)
00048       result['pset']=pset(mod.parameters_())
00049       result['type']=data.type(mod)
00050       if links:
00051         result['uses']=[data.uses(mod)]
00052         result['usedby']=[data.usedBy(mod)]
00053       result['id']='%s_%s'%(prefix,data.label(mod))
00054       return result
00055       
00056     all={}
00057     for tlo in data.topLevelObjects():
00058       children=data.children(tlo)
00059       if children:
00060         all[tlo._label]=children
00061       
00062     process = {'name':data.process().name_(),'src':data._filename}
00063     
00064     #now unavailable  
00065     #schedule = []
00066     #if 'Schedule' in all:
00067     #  for s in all['Schedule']:
00068     #    schedule.append(data.label(s))
00069       
00070     source={}
00071     if 'source' in all:
00072       s = all['source'][0]
00073       source['class']=data.classname(s)
00074       source['pset']=pset(s.parameters_())
00075       
00076     essources=[]
00077     if 'essources' in all:
00078       for e in all['essources']:
00079         essources.append(moduledict(e,'essource'))
00080     esproducers=[]
00081     if 'esproducers' in all:
00082       for e in all['esproducers']:
00083         essources.append(moduledict(e,'esproducer'))
00084     esprefers=[]
00085     if 'esprefers' in all:
00086       for e in all['esprefers']:
00087         essources.append(moduledict(e,'esprefers'))
00088     services=[]
00089     if 'services' in all:
00090       for s in all['services']:
00091         services.append({'class':data.classname(s),'pset':pset(s.parameters_())})    
00092       
00093       
00094     def jsonPathRecursive(p,prefix):
00095       #print "At:",self.label(p),self.type(p)
00096       children = data.children(p)
00097       if children:
00098         children = [jsonPathRecursive(c,prefix) for c in children]
00099         return {'type':'Sequence','label':'Sequence %s'%(data.label(p)),'id':'seq_%s' % data.label(p),'children':children}
00100       else:
00101         return moduledict(p,prefix,True)
00102         
00103           
00104     paths=[]
00105     if 'paths' in all:
00106       for p in all['paths']:
00107         path=jsonPathRecursive(p,data.label(p))
00108         if path:
00109           if not type(path)==type([]):
00110             if path['type']=='Sequence':
00111               path = path['children']
00112             else:
00113               path = [path]
00114         else:
00115           path=[]
00116         paths.append({'label':data.label(p),'path':path})
00117     endpaths=[]
00118     if 'endpaths' in all:
00119       for p in all['endpaths']:
00120         path=jsonPathRecursive(p,data.label(p))
00121         if path:
00122           if not type(path)==type([]):
00123             if path['type']=='Sequence':
00124               path = path['children']
00125             else:
00126               path = [path]
00127         else:
00128           path=[]
00129         endpaths.append({'label':data.label(p),'path':path})
00130       
00131     #json={'process':process,'schedule':schedule,'source':source,'essources':essources,'esproducers':esproducers,'esprefers':esprefers,'services':services,'paths':paths,'endpaths':endpaths}
00132     json={'process':process,'source':source,'essources':essources,'esproducers':esproducers,'esprefers':esprefers,'services':services,'paths':paths,'endpaths':endpaths}
00133       
00134     return repr(json)
00135     
00136   def export(self,data,filename,filetype):
00137     if not data.process():
00138       raise "JSONExport requires a cms.Process object"
00139       
00140     json = self.produce(data)
00141     
00142     if filetype=='json':
00143       jsonfile = open(filename,'w')
00144       jsonfile.write(json)
00145       jsonfile.close()
00146     if filetype=='html':
00147       #open the HTML template and inject the JSON...
00148       pass
00149