Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compiling with python3 #23989

Merged
merged 1 commit into from Jul 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 18 additions & 18 deletions FWCore/GuiBrowsers/python/ConfigToolBase.py
Expand Up @@ -7,7 +7,7 @@
#### patches needed for deepcopy of sorted dicts ####

import FWCore.ParameterSet.DictTypes as dicttypes

def new_SortedKeysDict__copy__(self):
return self.__class__(self)
dicttypes.SortedKeysDict.__copy__ = new_SortedKeysDict__copy__
Expand All @@ -29,7 +29,7 @@ class parameter:
pass

### Base class for object oriented designed tools

class ConfigToolBase(object) :

""" Base class for PAT tools
Expand All @@ -55,30 +55,30 @@ def __call__(self,process):
""" Call the instance
"""
raise NotImplementedError

def apply(self,process):

if hasattr(process, "addAction"):
process.disableRecording()

try:
comment=inspect.stack(2)[2][4][0].rstrip("\n")
if comment.startswith("#"):
self.setComment(comment.lstrip("#"))
except:
pass

self.toolCode(process)

if hasattr(process, "addAction"):
process.enableRecording()
action=self.__copy__()
process.addAction(action)

def toolCode(self, process):
raise NotImplementedError


### __copy__(self) returns a copy of the tool
def __copy__(self):
c=type(self)()
Expand All @@ -95,7 +95,7 @@ def description(self):
""" Return a string with a detailed description of the action.
"""
return self._description

### use addParameter method in the redefinition of tool constructor in order to add parameters to the tools
### each tool is defined by its label, default value, description, type and allowedValues (the last two attribute can be ignored
### if the user gives a valid default values and if there is not a list of allowed values)
Expand Down Expand Up @@ -140,16 +140,16 @@ def dumpPython(self):
dumpPython = '#'+self._comment
dumpPython += "\n"+self._label+"(process "
for key in self._parameters.keys():
if repr(self._parameters[key].value)!=repr(self._defaultParameters[key].value):
dumpPython+= ", "+str(key)+" = "
if self._parameters[key].type is str:
string = "'"+str(self.getvalue(key))+"'"
else:
string = str(self.getvalue(key))
dumpPython+= string
if repr(self._parameters[key].value)!=repr(self._defaultParameters[key].value):
dumpPython+= ", "+str(key)+" = "
if self._parameters[key].type is str:
string = "'"+str(self.getvalue(key))+"'"
else:
string = str(self.getvalue(key))
dumpPython+= string
dumpPython+=")"+'\n'
return (dumpPythonImport,dumpPython)

def setComment(self, comment):
""" Write a comment in the configuration file
"""
Expand Down
98 changes: 49 additions & 49 deletions FWCore/GuiBrowsers/python/EnablePSetHistory.py
Expand Up @@ -14,14 +14,14 @@ def auto_inspect():
while len(stack)>=1 and len(stack[0])>=2 and ('FWCore/ParameterSet' in stack[0][1] or 'FWCore/GuiBrowsers' in stack[0][1]):
stack = stack[1:]
if len(stack)>=1 and len(stack[0])>=3:
return stack
return stack
else:
return [("unknown","unknown","unknown")]
return [("unknown","unknown","unknown")]

#### patches needed for deepcopy of process ####

import FWCore.ParameterSet.DictTypes as typ

def new_SortedKeysDict__copy__(self):
return self.__class__(self)
typ.SortedKeysDict.__copy__ = new_SortedKeysDict__copy__
Expand Down Expand Up @@ -113,15 +113,15 @@ def new_dumpHistory(self,withImports=True):
if isinstance(item,(str,unicode)):
dumpHistory.append(item +"\n")
else: # isTool
print(item)
print(item)
dump=item.dumpPython()
if isinstance(dump,tuple):
if withImports and dump[0] not in dumpHistory:
dumpHistory.append(dump[0])
dumpHistory.append(dump[1] +"\n")
else:
dumpHistory.append(dump +"\n")

return ''.join(dumpHistory)
cms.Process.dumpHistory=new_dumpHistory

Expand Down Expand Up @@ -218,7 +218,7 @@ def new_recurseDumpModifications_(self, name, o):
'line': mod['line'],
'dump': o.dumpPython({}),
'type': 'seq'})

if isinstance(o, cms._Parameterizable):
for mod in o._modifications:
paramname = mod['name']
Expand All @@ -238,12 +238,12 @@ def new_recurseDumpModifications_(self, name, o):
'action': mod['action'],
'dump': dump,
'type': 'param'})

# Loop over any child elements
for key in o.parameterNames_():
value = getattr(o,key)
modifications += self.recurseDumpModifications_("%s.%s" % (name, key), value)

if isinstance(o, cms._ValidatingListBase):
for index, item in enumerate(o):
modifications += self.recurseDumpModifications_("%s[%s]" % (name, index), item)
Expand Down Expand Up @@ -396,25 +396,25 @@ def new_items_(self):
#### parameterizable history ####

def new_Parameterizable_init(self,*a,**k):
self.__dict__['_modifications'] = []
self.old__init__(*a,**k)
self._modifications = []
self.__dict__['_modifications'] = []
self.old__init__(*a,**k)
self._modifications = []
cms._Parameterizable.old__init__ = cms._Parameterizable.__init__
cms._Parameterizable.__init__ = new_Parameterizable_init

def new_Parameterizable_addParameter(self, name, value):
self.old__addParameter(name,value)
stack = auto_inspect()
self._modifications.append({'file':stack[0][1],'line':stack[0][2],'name':name,'old':None,'new':deepcopy(value),'action':'add'})
self.old__addParameter(name,value)
stack = auto_inspect()
self._modifications.append({'file':stack[0][1],'line':stack[0][2],'name':name,'old':None,'new':deepcopy(value),'action':'add'})
cms._Parameterizable.old__addParameter = cms._Parameterizable._Parameterizable__addParameter
cms._Parameterizable._Parameterizable__addParameter = new_Parameterizable_addParameter

def new_Parameterizable_setattr(self, name, value):
if (not self.isFrozen()) and (not name.startswith('_')) and (name in self.__dict__):
stack = auto_inspect()
self._modifications.append({'file':stack[0][1],'line':stack[0][2],'name':name,'old':deepcopy(self.__dict__[name]),'new':deepcopy(value),'action':'replace'})
self._isModified = True
self.old__setattr__(name,value)
if (not self.isFrozen()) and (not name.startswith('_')) and (name in self.__dict__):
stack = auto_inspect()
self._modifications.append({'file':stack[0][1],'line':stack[0][2],'name':name,'old':deepcopy(self.__dict__[name]),'new':deepcopy(value),'action':'replace'})
self._isModified = True
self.old__setattr__(name,value)
cms._Parameterizable.old__setattr__ = cms._Parameterizable.__setattr__
cms._Parameterizable.__setattr__ = new_Parameterizable_setattr

Expand Down Expand Up @@ -485,19 +485,19 @@ def new_Sequence_name(self):
cms.Sequence._name_ = new_Sequence_name

def new__Module_name(self):
if hasattr(self,'_Labelable__label'):
return getattr(self,'_Labelable__label')
elif hasattr(self,'_TypedParameterizable__type'):
return 'unnamed(%s)'%getattr(self,'_TypedParameterizable__type')
return type(self).__name__
if hasattr(self,'_Labelable__label'):
return getattr(self,'_Labelable__label')
elif hasattr(self,'_TypedParameterizable__type'):
return 'unnamed(%s)'%getattr(self,'_TypedParameterizable__type')
return type(self).__name__
cms._Module._name_ = new__Module_name

def new__ModuleSequenceType__init__(self,*arg,**argv):
self._modifications = []
self.old__init__(*arg,**argv)
cms._ModuleSequenceType.old__init__ = cms._ModuleSequenceType.__init__
cms._ModuleSequenceType.__init__ = new__ModuleSequenceType__init__

def new__ModuleSequenceType_resetModified(self):
self._isModified=False
self._modifications = []
Expand Down Expand Up @@ -552,7 +552,7 @@ def new__ModuleSequenceType__iadd__(self,other):

from FWCore.ParameterSet.Modules import Source
from FWCore.GuiBrowsers.editorTools import changeSource

if __name__=='__main__':
import unittest
class TestModificationTracking(unittest.TestCase):
Expand Down Expand Up @@ -604,7 +604,7 @@ def testPSet(self):
#self.assertEqual(ex._modifications[-1]['name'],'nine')
ex.newvpset = cms.VPSet()
self.assertEqual(ex._modifications[-1]['name'],'newvpset')

process = cms.Process('unittest')
process.ex = ex
mods = process.dumpModifications()
Expand All @@ -619,63 +619,63 @@ def testPSet(self):
#self.assert_('process.ex.eight' in mods)
self.assert_('process.ex.nine' in mods)
self.assert_('process.ex.newvpset' in mods)



def testSeq(self):
process = cms.Process('unittest')
for i in range(10):
setattr(process,'f%s'%i,cms.EDFilter('f%s'%i))
setattr(process,'f%s'%i,cms.EDFilter('f%s'%i))
process.seq1 = cms.Sequence(process.f1*process.f2*process.f3)
self.assertEqual(process.seq1._modifications,[])
process.seq2 = cms.Sequence(process.f4+process.f5+process.f6)
self.assertEqual(process.seq2._modifications,[])

process.seq1.replace(process.f1,process.f0*process.f1)
self.assertEqual(process.seq1._modifications[-1]['action'],'replace')

process.seq2.remove(process.f5)
self.assertEqual(process.seq2._modifications[-1]['action'],'remove')

process.path = cms.Path(process.seq1*process.f7)
self.assertEqual(process.path._modifications,[])

process.path *= process.seq2
self.assertEqual(process.path._modifications[-1]['action'],'append')
process.path.remove(process.f6)
self.assertEqual(process.path._modifications[-1]['action'],'remove')
process.path.replace(process.f2,~process.f2)
self.assertEqual(process.path._modifications[-1]['action'],'replace')

mods = process.dumpModifications()
self.assert_('process.seq1' in mods)
self.assert_('process.seq2' in mods)
self.assert_('process.path' in mods)

def testdumpHistory(self):
process = cms.Process('unittest')
process.source=Source("PoolSource",fileNames = cms.untracked.string("file:file.root"))

changeSource(process,"file:filename.root")
self.assertEqual(changeSource._parameters['source'].value,"file:filename.root")

changeSource(process,"file:filename2.root")
self.assertEqual(changeSource._parameters['source'].value,"file:filename2.root")

changeSource(process,"file:filename3.root")
self.assertEqual(changeSource._parameters['source'].value,"file:filename3.root")

self.assertEqual(process.dumpHistory(),"\nfrom FWCore.GuiBrowsers.editorTools import *\n\nchangeSource(process , 'file:filename.root')\n\n\nchangeSource(process , 'file:filename2.root')\n\n\nchangeSource(process , 'file:filename3.root')\n\n")

process.source.fileNames=cms.untracked.vstring("file:replacedfile.root")
self.assertEqual(process.dumpHistory(),"\nfrom FWCore.GuiBrowsers.editorTools import *\n\nchangeSource(process , 'file:filename.root')\n\n\nchangeSource(process , 'file:filename2.root')\n\n\nchangeSource(process , 'file:filename3.root')\n\nprocess.source.fileNames = cms.untracked.vstring('file:replacedfile.root')\n")

process.disableRecording()
changeSource.setParameter('source',"file:filename4.root")
action=changeSource.__copy__()
process.addAction(action)
self.assertEqual(process.dumpHistory(),"\nfrom FWCore.GuiBrowsers.editorTools import *\n\nchangeSource(process , 'file:filename.root')\n\n\nchangeSource(process , 'file:filename2.root')\n\n\nchangeSource(process , 'file:filename3.root')\n\nprocess.source.fileNames = cms.untracked.vstring('file:replacedfile.root')\n")

process.enableRecording()
changeSource.setParameter('source',"file:filename5.root")
action=changeSource.__copy__()
Expand All @@ -685,14 +685,14 @@ def testdumpHistory(self):

process.deleteAction(0)
self.assertEqual(process.dumpHistory(),"\nfrom FWCore.GuiBrowsers.editorTools import *\n\nchangeSource(process , 'file:filename2.root')\n\n\nchangeSource(process , 'file:filename3.root')\n\n\nchangeSource(process , 'file:filename5.root')\n\n")

def testModifiedObjectsHistory(self):
process = cms.Process('unittest')
process.source=Source("PoolSource",fileNames = cms.untracked.string("file:file.root"))

changeSource(process,"file:filename.root")
self.assertEqual(len(process.history()[0][1]),1)

process.source.fileNames=cms.untracked.vstring("file:replacedfile.root")
self.assertEqual(len(process.history()[0][1]),1)
self.assertEqual(len(process.history()[1][1]),1)
Expand All @@ -705,12 +705,12 @@ def testModifiedObjectsHistory(self):
self.assertEqual(len(process.history()[0][1]),1)
self.assertEqual(len(process.history()[1][1]),1)
self.assertEqual(len(process.history()[2][1]),1)

process.source.fileNames=cms.untracked.vstring("file:replacedfile2.root")
self.assertEqual(len(process.history()[0][1]),1)
self.assertEqual(len(process.history()[1][1]),1)
self.assertEqual(len(process.history()[2][1]),1)
self.assertEqual(len(process.history()[3][1]),1)

unittest.main()