CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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.

References prepareReplaceDict(), and split.

Referenced by loopfile().

69 
70 def identifyComments(configString):
71 
72  replaceDict = {}
73 
74  replaceDict["@beginning"] = "# The following comments couldn't be translated into the new config version:\n"
75  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']
76  unnamedKeywords = ['es_source', 'es_module']
77  # the ugly parsing part
78  inComment = False # are we currently in a comment?
79  inSlashStarComment = False
80 
81  # TODO - include inline comments
82  # for now only modules work
83  for line in configString.splitlines():
84  if line.lstrip().startswith("/*"):
85  comment = Comment()
86  comment.type = "slashstar"
87  splitStarSlash = line.lstrip().lstrip("/*").split("*/")
88  comment.value = "# "+splitStarSlash[0]+"\n"
89  # has it ended yet? Might ignore legitimate commands after the end.
90  inSlashStarComment = (line.find('*/') == -1)
91  inComment = True
92  elif inSlashStarComment:
93  splitStarSlash = line.lstrip().lstrip("/*").split("*/")
94  comment.value += "# "+splitStarSlash[0]+"\n"
95  inSlashStarComment = (line.find('*/') == -1)
96  elif line.lstrip().startswith("#") or line.lstrip().startswith("//"):
97  if inComment:
98  comment.value += "#"+line.lstrip().lstrip("//").lstrip("#") + "\n"
99  else:
100  comment = Comment()
101  comment.type = "trailing"
102  comment.value = "#"+line.lstrip().lstrip("//").lstrip("#") + "\n"
103  inComment = True
104  elif inComment: # we are now in a line just below a comment
105  words = line.lstrip().split()
106  # at least <keyword> <label> = "
107  if len(words) > 1:
108  prepareReplaceDict(line,comment,replaceDict)
109  if len(words) > 0:
110  inComment = False
111  else:
112  # now to comments in the same line
113  if len(line.split("#")) > 1 or len(line.split("//")) > 1:
114  comment = Comment()
115  if len(line.split("#")) > 1:
116  comment.value = '#'+line.split("#")[1]
117  else:
118  comment.value = '#'+line.split("//")[1]
119  comment.type = "inline"
120  # prepare the replaceDict
121  prepareReplaceDict(line, comment, replaceDict)
122 
123 
124  return replaceDict
125 
126 
127 
def prepareReplaceDict
Definition: comments.py:21
double split
Definition: MVATrainer.cc:139
def identifyComments
Definition: comments.py:69
def comments.loopfile (   cfgFileName)

Definition at line 160 of file comments.py.

References cfgName2py.cfgName2py(), mergeVDriftHistosByStation.file, identifyComments(), and modifyPythonVersion().

161 def loopfile(cfgFileName):
162  cfgFile = file(cfgFileName)
163  cfgString = cfgFile.read()
164 
165  pyFileName = cfgName2py.cfgName2py(cfgFileName)
166 
167  try:
168  pyFile = file(pyFileName)
169  pyString = pyFile.read()
170  except IOError:
171  print pyFileName, "does not exist"
172  return
173  comments = identifyComments(cfgString)
174  print "Opening", pyFileName
175  newPyString = modifyPythonVersion(pyString, comments)
176  pyFile.close()
177 
178  pyOutFile = file(pyFileName,"w")
179  pyOutFile.write(newPyString)
180  print "Wrote", pyFileName
181  pyOutFile.close()
182 
183 #out = file("foo_cfi.py","w")
184 #out.write(configString)
185 #print configString
186 
187 
188 
#
def modifyPythonVersion
Definition: comments.py:128
def cfgName2py
Definition: cfgName2py.py:4
def loopfile
Definition: comments.py:160
def identifyComments
Definition: comments.py:69
def comments.modifyPythonVersion (   configString,
  replaceDict 
)

Definition at line 128 of file comments.py.

Referenced by loopfile().

129 def modifyPythonVersion(configString, replaceDict):
130 
131  # first put all comments at the beginning of the file which could not be assigned to any other token
132  if replaceDict["@beginning"] != "# The following comments couldn't be translated into the new config version:\n":
133  configString = replaceDict["@beginning"]+"\n"+configString
134 
135  replaceDict.pop("@beginning")
136 
137  # identify the lines to replace and prepare the replacing line
138  actualReplacements = {}
139  for keyword, comment in replaceDict.iteritems():
140  for line in configString.splitlines():
141  if line.lstrip().startswith(keyword):
142  indentation = line[0:line.find(keyword)]
143  if len([1 for l in configString.splitlines() if l.lstrip().startswith(keyword)]) !=1:
144  print "WARNING. Following keyword not unique:", keyword
145  continue
146  if comment.type == "inline":
147  newLine = line + " #"+comment.value+"\n"
148  else:
149  newLine = line.replace(line,comment.value+line) # lacking the trailing whitespace support
150  newLine = newLine.replace('#', indentation+'#')
151  actualReplacements[line] = newLine
152 
153 
154  # now do the actual replacement
155  for original, new in actualReplacements.iteritems():
156  configString = configString.replace(original, new)
157 
158  return configString
159 
def modifyPythonVersion
Definition: comments.py:128
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.

Referenced by identifyComments().

21 
22 def prepareReplaceDict(line, comment, replaceDict):
23  """take a line and a corresponding comment and prepare the replaceDict such that it can be found again in the python version """
24  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']
25  unnamedKeywords = ['es_source', 'es_module']
26 
27 
28  words = line.lstrip().split()
29  # at least <keyword> <label> = "
30  if len(words) > 1:
31  firstWord = words[0]
32  if firstWord in allKeywords and len(words) > 2 and words[2] == '=':
33  tokenInPython = words[1] + " = "
34  replaceDict[tokenInPython] = comment
35  elif firstWord == 'untracked' and len(words) > 3 and words[1] in allKeywords and words[3] == '=':
36  tokenInPython = words[2] + " = cms.untracked"
37  replaceDict[tokenInPython] = comment
38  elif firstWord.startswith('include'): # handling of include statements
39  pythonModule = cfgName2py.cfgName2py(line.split('"')[1])
40  pythonModule = pythonModule.replace("/",".").replace("python.","").replace(".py","")
41  tokenInPython = "from "+pythonModule
42  tokenInPython = tokenInPython.replace("/",".").replace("python.","").replace(".py","")
43  replaceDict[tokenInPython] = comment
44  # if a cfg
45  tokenInPython = "process.load(\""+pythonModule
46  replaceDict[tokenInPython] = comment
47  elif firstWord == 'source' and len(words) > 1 and words[1] == '=':
48  replaceDict['source = '] = comment
49  elif firstWord in unnamedKeywords and len(words) > 2 and words[1] == '=':
50  tokenInPython = words[2] + ' = cms.ES'
51  replaceDict[tokenInPython] = comment
52  elif firstWord == 'replace' and len(words) > 2 and words[2] == '=':
53  tokenInPython= words[1] + " = "
54  replaceDict[tokenInPython] = comment
55  elif firstWord == 'replace' and len(words) > 2 and words[2] == '=':
56  tokenInPython= words[1] + " = "
57  replaceDict[tokenInPython] = comment
58  # if it's a cfg
59  tokenInPython = 'process.'+tokenInPython
60  replaceDict[tokenInPython] = comment
61  elif firstWord == 'using' and len(words) == 2:
62  tokenInPython= words[1]
63  replaceDict[tokenInPython] = comment
64  # if it's a significant line, we're not in a comment any more
65  else:
66  replaceDict["@beginning"] +="\n"+comment.value
67 
68 
def cfgName2py
Definition: cfgName2py.py:4
def prepareReplaceDict
Definition: comments.py:21
double split
Definition: MVATrainer.cc:139