CMS 3D CMS Logo

Classes | Functions

comments Namespace Reference

Classes

class  Comment

Functions

def identifyComments
def loopfile
def modifyPythonVersion
def prepareReplaceDict

Function Documentation

def comments::identifyComments (   configString)

Definition at line 69 of file comments.py.

00070                                   :
00071     
00072     replaceDict = {}
00073 
00074     replaceDict["@beginning"] = "# The following comments couldn't be translated into the new config version:\n"
00075     allKeywords = ['module','int32','vint32','uint32', 'vuint32','double','vdouble','InputTag', 'VInputTag', 'PSet', 'VPSet', 'string', 'bool', 'vbool', 'path', 'sequence', 'schedule', 'endpath', 'es_source', 'es_module', 'block', 'FileInPath']
00076     unnamedKeywords = ['es_source', 'es_module']
00077     # the ugly parsing part
00078     inComment = False # are we currently in a comment?
00079     inSlashStarComment = False
00080 
00081     # TODO - include inline comments
00082     # for now only modules work
00083     for line in configString.splitlines():
00084         if line.lstrip().startswith("/*"):
00085             comment = Comment()
00086             comment.type = "slashstar"
00087             splitStarSlash = line.lstrip().lstrip("/*").split("*/")
00088             comment.value = "# "+splitStarSlash[0]+"\n"
00089             # has it ended yet?  Might ignore legitimate commands after the end.
00090             inSlashStarComment = (line.find('*/') == -1)
00091             inComment = True
00092         elif inSlashStarComment:
00093             splitStarSlash = line.lstrip().lstrip("/*").split("*/")
00094             comment.value += "# "+splitStarSlash[0]+"\n"
00095             inSlashStarComment = (line.find('*/') == -1)
00096         elif line.lstrip().startswith("#") or line.lstrip().startswith("//"):
00097           if inComment:
00098             comment.value += "#"+line.lstrip().lstrip("//").lstrip("#") + "\n"
00099           else:
00100             comment = Comment()
00101             comment.type = "trailing"
00102             comment.value = "#"+line.lstrip().lstrip("//").lstrip("#") + "\n"
00103             inComment = True
00104         elif inComment:  # we are now in a line just below a comment
00105           words = line.lstrip().split()
00106           # at least <keyword> <label> = "
00107           if len(words) > 1:
00108              prepareReplaceDict(line,comment,replaceDict)             
00109           if len(words) > 0:
00110              inComment = False
00111         else:
00112              # now to comments in the same line
00113              if len(line.split("#")) > 1 or len(line.split("//")) > 1:
00114                comment = Comment()
00115                if len(line.split("#")) > 1:
00116                  comment.value = '#'+line.split("#")[1]
00117                else:
00118                  comment.value = '#'+line.split("//")[1]
00119                comment.type = "inline"
00120                # prepare the replaceDict
00121                prepareReplaceDict(line, comment, replaceDict)
00122       
00123 
00124     return replaceDict
00125           
00126         
00127 

def comments::loopfile (   cfgFileName)

Definition at line 160 of file comments.py.

00161                          :
00162    cfgFile = file(cfgFileName)
00163    cfgString = cfgFile.read()
00164    
00165    pyFileName = cfgName2py.cfgName2py(cfgFileName)
00166 
00167    try: 
00168      pyFile = file(pyFileName)
00169      pyString = pyFile.read()
00170    except IOError:
00171      print pyFileName, "does not exist"
00172      return
00173    comments = identifyComments(cfgString)
00174    print "Opening", pyFileName
00175    newPyString = modifyPythonVersion(pyString, comments)
00176    pyFile.close()
00177 
00178    pyOutFile = file(pyFileName,"w")
00179    pyOutFile.write(newPyString)
00180    print "Wrote", pyFileName
00181    pyOutFile.close()
00182  
00183 #out = file("foo_cfi.py","w")
00184 #out.write(configString)
00185 #print configString
00186 
00187 
00188 
# 
def comments::modifyPythonVersion (   configString,
  replaceDict 
)

Definition at line 128 of file comments.py.

00129                                                   :
00130 
00131     # first put all comments at the beginning of the file which could not be assigned to any other token
00132     if replaceDict["@beginning"] != "# The following comments couldn't be translated into the new config version:\n":
00133       configString = replaceDict["@beginning"]+"\n"+configString 
00134 
00135     replaceDict.pop("@beginning")
00136 
00137     # identify the lines to replace and prepare the replacing line
00138     actualReplacements = {}
00139     for keyword, comment in replaceDict.iteritems():
00140         for line in configString.splitlines():
00141             if line.lstrip().startswith(keyword):
00142                 indentation = line[0:line.find(keyword)]
00143                 if len([1 for l in configString.splitlines() if l.lstrip().startswith(keyword)]) !=1:
00144                     print "WARNING. Following keyword not unique:", keyword
00145                     continue 
00146                 if comment.type == "inline":
00147                   newLine = line + " #"+comment.value+"\n"
00148                 else:
00149                   newLine = line.replace(line,comment.value+line)  # lacking the trailing whitespace support
00150                   newLine = newLine.replace('#', indentation+'#')
00151                 actualReplacements[line] = newLine
00152 
00153 
00154     # now do the actual replacement
00155     for original, new in actualReplacements.iteritems():        
00156         configString = configString.replace(original, new)
00157 
00158     return configString
00159 

def comments::prepareReplaceDict (   line,
  comment,
  replaceDict 
)
take a line and a corresponding comment and prepare the replaceDict such that it can be found again in the python version 

Definition at line 21 of file comments.py.

00022                                                   :
00023     """take a line and a corresponding comment and prepare the replaceDict such that it can be found again in the python version """
00024     allKeywords = ['module','int32','vint32','uint32', 'vuint32','double','vdouble','InputTag', 'VInputTag', 'PSet', 'VPSet', 'string', 'vstring', 'bool', 'vbool', 'path', 'sequence', 'schedule', 'endpath', 'es_source', 'es_module', 'block', 'FileInPath']
00025     unnamedKeywords = ['es_source', 'es_module']
00026 
00027 
00028     words = line.lstrip().split()
00029     # at least <keyword> <label> = "
00030     if len(words) > 1:
00031         firstWord = words[0]
00032         if firstWord in allKeywords and len(words) > 2 and words[2] == '=':
00033            tokenInPython = words[1] + " = "
00034            replaceDict[tokenInPython] = comment
00035         elif firstWord == 'untracked' and len(words) > 3 and words[1] in allKeywords and words[3] == '=':
00036            tokenInPython = words[2] + " = cms.untracked"
00037            replaceDict[tokenInPython] = comment
00038         elif firstWord.startswith('include'): # handling of include statements
00039            pythonModule = cfgName2py.cfgName2py(line.split('"')[1])
00040            pythonModule = pythonModule.replace("/",".").replace("python.","").replace(".py","")
00041            tokenInPython = "from "+pythonModule
00042            tokenInPython = tokenInPython.replace("/",".").replace("python.","").replace(".py","")
00043            replaceDict[tokenInPython] = comment
00044            # if a cfg
00045            tokenInPython = "process.load(\""+pythonModule
00046            replaceDict[tokenInPython] = comment
00047         elif firstWord == 'source' and len(words) > 1 and words[1] == '=':
00048            replaceDict['source = '] = comment
00049         elif firstWord in unnamedKeywords and len(words) > 2 and words[1] == '=':
00050            tokenInPython = words[2] + ' = cms.ES'
00051            replaceDict[tokenInPython] = comment
00052         elif firstWord == 'replace' and len(words) > 2 and words[2] == '=':
00053            tokenInPython= words[1] + " = "
00054            replaceDict[tokenInPython] = comment
00055         elif firstWord == 'replace' and len(words) > 2 and words[2] == '=':
00056            tokenInPython= words[1] + " = "
00057            replaceDict[tokenInPython] = comment
00058            # if it's a cfg
00059            tokenInPython = 'process.'+tokenInPython
00060            replaceDict[tokenInPython] = comment
00061         elif firstWord == 'using' and len(words) == 2:
00062            tokenInPython= words[1]
00063            replaceDict[tokenInPython] = comment
00064        # if it's a significant line, we're not in a comment any more
00065         else:
00066           replaceDict["@beginning"] +="\n"+comment.value
00067     
00068