CMS 3D CMS Logo

List of all members | Public Member Functions
cfg-viewer.server Class Reference

Public Member Functions

def __init__ (self, name)
 
def printServer (self, name)
 

Detailed Description

Definition at line 1270 of file cfg-viewer.py.

Constructor & Destructor Documentation

def cfg-viewer.server.__init__ (   self,
  name 
)

Definition at line 1271 of file cfg-viewer.py.

References cfg-viewer.server.printServer().

1271  def __init__(self, name):
1272  self.printServer(name)
1273 
def __init__(self, name)
Definition: cfg-viewer.py:1271
def printServer(self, name)
Definition: cfg-viewer.py:1274

Member Function Documentation

def cfg-viewer.server.printServer (   self,
  name 
)

Definition at line 1274 of file cfg-viewer.py.

Referenced by cfg-viewer.server.__init__().

1274  def printServer(self, name):
1275  with open(name, 'w') as f:
1276  f.write("""
1277 #!/usr/bin/env python\nimport SimpleHTTPServer\nimport SocketServer
1278 import shutil\nimport os\nimport cgi\nimport socket\nimport errno\n
1279 #Right now cannot deal with SequenceTypes\nclass CfgConvert:
1280  def __init__(self, obj):\n self._pName = obj["processName"]
1281  self._pFileN = obj["processFileName"]\n obj.__delitem__("processName")
1282  obj.__delitem__("processFileName")\n self._obj = obj
1283  self._func="cms.%s"
1284  self.header=\"""import FWCore.ParameterSet.Config as cms
1285 process = cms.Process('%(n)s')\nfrom %(fileN)s import *\n%(changes)s\n\n\"""
1286  def _doConversion(self):\n return self.header%(
1287  {"changes":self._doWork(self._obj),
1288  "n":self._pName,"fileN":self._pFileN})\n\n def _doWork(self, obj):
1289  result =""\n for key, value in six.iteritems(obj):
1290  _type = value["Type"]\n _file = value["File"]
1291  _params = value["Parameters"]\n _oType = value["oType"]
1292  if(_oType):\n _oType= "'%s',"%(value["oType"])
1293  #f = eval(func%(_type))\n if(type(_params)== list):
1294  if(len(_params)==0):
1295  result +="process.%s= cms.%s(%s)\\n"%(key,_type,_oType[:-1])
1296  else:\n params = self._convert(_params)
1297  result+="process.%s= cms.%s(%s%s)\\n"%(key,_type,_oType, params)
1298  #elif(type(_params)== dict):
1299  # return "Sorry path and endpaths can not currently be translated."
1300  #params = self._doWork(_params)\n
1301  #obj = f("actualType", *parameters)\n return result\n
1302  def _convert(self,params):
1303  # NOTE: params do not take names inside functions
1304  # It is: name = cms.type(value)\n # okay we have a list like
1305  #[name,params, untracked,type]||[name,params,type]
1306  # 2 format options in the params\n # 1. [["name", "value", "type"]]
1307  # 2. [["PsetName",[[as above or this]], "Pset"]]\n
1308  # At position 0 is name. list[0]==name or list[0]==list
1309  # then that will be all
1310  # At position 1 is value or list. list[1]==value||list.
1311  # At position 2 is type. list[2] == type || untracked.
1312  # If there is a position 3 its type. list[3]== type.
1313  if(all(type(x) == list for x in params)):
1314  return ",".join([self._convert(x) for x in params])
1315  length = len(params)\n if(length==1):
1316  if(type(params[0]) == list):\n # wont have params[1]etc
1317  # do inners\n return self._convert(params[0])
1318  #wrong format\n print "Error 01 %s"%(str(params))\n return ""
1319  if(length !=3 and length !=4):\n #wrong format
1320  print "Error 02 len is %s"%(str(params))\n return ""
1321  # okay get on with it.\n name = params[0]
1322  if(type(params[1])==list):\n # do listy things
1323  value = self._convert(params[1])\n else:\n value=params[1]
1324  ty = params[2]\n if(name==ty):\n name=""\n if(length==4):
1325  ty+="."+params[3]\n if(name== params[3]):\n name=""
1326  if("vstring" in ty):\n # split the value up by commas
1327  value = ",".join(["'%s'"%x for x in value.split(",")])
1328  elif("string" in ty):\n #surround string with quotation marks
1329  value = "%s"%(value)\n # okay now done with everything
1330  call= self._func%(ty)\n if(name):
1331  return"%s=%s(%s)"%(name,call,value)\n return"%s(%s)"%(call,value)\n
1332 class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):\n
1333  def do_GET(self):\n if(self.path == "/index.html"):
1334  # find out what cfg html folders we have below
1335  # and add each main html page to the index\n li = os.listdir(".")
1336  dEnd = "-cfghtml"
1337  dirs = [x for x in li if os.path.isdir(x) and x.endswith(dEnd)]
1338  name = "main.html"
1339  names = [os.path.join(x,name) for x in dirs if name in os.listdir(x)]
1340  tmpte = '<li><a href ="%(n)s">%(s)s</a></li>'
1341  lis = [tmpte%{"n":x,"s":os.path.split(x)[0].replace(dEnd, "")}
1342  for x in names]\n with open("index.html", 'w') as f:
1343  f.write(\"""\n<!DOCTYPE html>\n<html>\n <head>
1344  <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
1345  <title>cfg-browser</title>\n </head>\n <body>
1346  <h4>Configuration files:</h4>\n <ul>\n %s\n </ul>\n </body>
1347 </html>\n \"""%("".join(lis)))\n
1348  return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)\n
1349  def do_POST(self):
1350  ctype,pdict= cgi.parse_header(self.headers.getheader('content-type'))
1351  bdy = cgi.parse_multipart(self.rfile,pdict)
1352  ch= " ".join(bdy[bdy.keys()[0]])\n self.conversion(eval(ch))
1353  self.writeFile()\n print "finished writing"\n
1354  def conversion(self, json):\n # Need to convert my json into config
1355  result = CfgConvert(json)._doConversion()
1356  with open("changed_cfg.py", 'w')as f:\n f.write(result)\n
1357  def writeFile(self):\n with open("changed_cfg.py", 'rb') as f:
1358  self.send_response(200)
1359  self.send_header("Content-Type", 'text/html')
1360  self.send_header("Content-Disposition",
1361  'attachment; filename="changed_cfg.py"')
1362  fs = os.fstat(f.fileno())
1363  self.send_header("Content-Length", str(fs.st_size))
1364  self.end_headers()\n shutil.copyfileobj(f, self.wfile)\n
1365 def main(port=8000, reattempts=5):\n if(port <1024):
1366  print \"""This port number may be refused permission.
1367 It is better to use a port number > 1023.\"""\n try:
1368  Handler = ServerHandler
1369  httpd = SocketServer.TCPServer(("", port), Handler)
1370  print "using port", port
1371  print "Open http://localhost:%s/index.html in your browser."%(port)
1372  httpd.serve_forever()\n except socket.error as e:
1373  if(reattempts >0 and e[0] == errno.EACCES):
1374  print "Permission was denied."
1375  elif(reattempts >0 and e[0]== errno.EADDRINUSE):
1376  print "Address %s is in use."%(port)\n else:\n raise
1377  print "Trying again with a new port number."\n if(port < 1024):
1378  port = 1024\n else:\n port = port +1
1379  main(port, reattempts-1)\n\nif __name__ == "__main__":\n import sys
1380  if(len(sys.argv)>1):\n try:\n port = int(sys.argv[1])
1381  main(port)\n sys.exit() \n except ValueError:
1382  print "Integer not valid, using default port number."\n main()
1383 """)
1384 
def printServer(self, name)
Definition: cfg-viewer.py:1274