Skip to content

Commit e65a5fe

Browse files
authored
Merge pull request #47374 from Dr15Jones/addPSetTemplateToVPSetInCfi
Have fillDescriptions generate a template for VPSet in cfi files
2 parents 4358f18 + f4700bc commit e65a5fe

File tree

7 files changed

+117
-16
lines changed

7 files changed

+117
-16
lines changed

FWCore/Integration/test/unit_test_outputs/testProducerWithPsetDesc_cfi.py

+53-2
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,25 @@
187187
ouDrinks = cms.untracked.uint32(5)
188188
),
189189
test104 = cms.untracked.VPSet(
190-
cms.PSet()
190+
cms.PSet(),
191+
template = cms.PSetTemplate(
192+
Drinks = cms.uint32(5),
193+
ndoDrinks = cms.optional.uint32,
194+
ndouDrinks = cms.optional.untracked.uint32,
195+
oDrinks = cms.uint32(5),
196+
ouDrinks = cms.untracked.uint32(5),
197+
uDrinks = cms.untracked.uint32(5)
198+
)
191199
),
192200
test105 = cms.untracked.VPSet(
193-
),
201+
template = cms.PSetTemplate(
202+
Drinks = cms.uint32(5),
203+
ndoDrinks = cms.optional.uint32,
204+
ndouDrinks = cms.optional.untracked.uint32,
205+
oDrinks = cms.uint32(5),
206+
ouDrinks = cms.untracked.uint32(5),
207+
uDrinks = cms.untracked.uint32(5)
208+
) ),
194209
test1 = cms.double(0.1),
195210
test2 = cms.double(0.2),
196211
testA = cms.string('fooA'),
@@ -228,6 +243,35 @@
228243
xvalue = cms.int32(17)
229244
)
230245
)
246+
),
247+
template = cms.PSetTemplate(
248+
Drinks = cms.uint32(5),
249+
anotherVPSet = cms.VPSet(
250+
cms.PSet(),
251+
cms.PSet(
252+
xvalue = cms.int32(100)
253+
),
254+
template = cms.PSetTemplate(
255+
xvalue = cms.int32(7)
256+
)
257+
),
258+
ndoDrinks = cms.optional.uint32,
259+
ndouDrinks = cms.optional.untracked.uint32,
260+
oDrinks = cms.uint32(5),
261+
ouDrinks = cms.untracked.uint32(5),
262+
testDeeplyNested = cms.PSet(
263+
bswitch = cms.bool(False),
264+
bvalue1 = cms.double(101),
265+
bvalue2 = cms.double(101),
266+
iswitch = cms.int32(1),
267+
ivalue1 = cms.double(101),
268+
ivalue2 = cms.untracked.double(101),
269+
sswitch = cms.string('1'),
270+
svalue1 = cms.double(101),
271+
svalue2 = cms.double(101),
272+
testint = cms.int32(1000)
273+
),
274+
uDrinks = cms.untracked.uint32(5)
231275
)
232276
),
233277
subpset = cms.PSet(
@@ -369,6 +413,13 @@
369413
cms.PSet(
370414
type = cms.string('edmtestAnotherMakerWithRecursivePlugin'),
371415
value = cms.int32(11)
416+
),
417+
template = cms.PSetTemplate(
418+
pluginRecursive = cms.PSet(
419+
420+
),
421+
type = cms.string('edmtestAnotherMakerWithRecursivePlugin'),
422+
value = cms.int32(5)
372423
)
373424
),
374425
mightGet = cms.optional.untracked.vstring

FWCore/ParameterSet/interface/ParameterSetDescription.h

+1
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ namespace edm {
296296

297297
const_iterator end() const { return entries_.end(); }
298298

299+
bool empty() const noexcept { return entries_.empty(); }
299300
// Better performance if space is reserved for the number of
300301
// top level parameters before any are added.
301302
void reserve(SetDescriptionEntries::size_type n) { entries_.reserve(n); }

FWCore/ParameterSet/python/Config.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1059,9 +1059,9 @@ def dumpPython(self, options=PrintOptions()) -> str:
10591059
specialImportRegistry._reset()
10601060
header = "import FWCore.ParameterSet.Config as cms"
10611061
result = "process = cms.Process(\""+self.__name+"\")\n\n"
1062-
if self.source_():
1062+
if not self.source_() is None:
10631063
result += "process.source = "+self.source_().dumpPython(options)
1064-
if self.looper_():
1064+
if not self.looper_() is None:
10651065
result += "process.looper = "+self.looper_().dumpPython()
10661066
result+=self._dumpPythonList(self.psets, options)
10671067
result+=self._dumpPythonList(self.vpsets, options)
@@ -1103,10 +1103,10 @@ def splitPython(self, options:PrintOptions = PrintOptions()) -> dict:
11031103

11041104
result = 'process = cms.Process("' + self.__name + '")\n\n'
11051105

1106-
if self.source_():
1106+
if not self.source_() is None:
11071107
parts['source'] = (None, 'source = ' + self.source_().dumpPython(options))
11081108

1109-
if self.looper_():
1109+
if not self.looper_() is None:
11101110
parts['looper'] = (None, 'looper = ' + self.looper_().dumpPython())
11111111

11121112
parts.update(self._splitPythonList('psets', self.psets, options))

FWCore/ParameterSet/python/Mixins.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ def __setParameters(self,parameters):
268268
self.__addParameter(name, value)
269269
if v is not None:
270270
self.__validator=v
271+
def hasNoParameters(self) -> bool:
272+
return len(self.__parameterNames) == 0
271273
def __setattr__(self,name:str,value):
272274
#since labels are not supposed to have underscores at the beginning
273275
# I will assume that if we have such then we are setting an internal variable
@@ -697,7 +699,9 @@ def dumpPython(self, options:PrintOptions=PrintOptions()) -> str:
697699
if n>=256:
698700
#wrap in a tuple since they don't have a size constraint
699701
result+=" ("
702+
wroteAtLeastOne = False
700703
for i, v in enumerate(self):
704+
wroteAtLeastOne = True
701705
if i == 0:
702706
if n>nPerLine: result += '\n'+options.indentation()
703707
else:
@@ -710,7 +714,7 @@ def dumpPython(self, options:PrintOptions=PrintOptions()) -> str:
710714
result +=' ) '
711715
moreArgs = self._additionalInitArguments(options)
712716
if moreArgs:
713-
if i > 0:
717+
if wroteAtLeastOne:
714718
result += ', \n' + options.indentation()
715719
result += moreArgs
716720
if n>nPerLine:

FWCore/ParameterSet/python/Types.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ def __init__(self, *args, **kargs):
197197
def __call__(self, value):
198198
self.__dict__
199199
return self._pset.clone(**value)
200+
def _isEmpty(self) -> bool:
201+
return self._pset.hasNoParameters()
200202
def _isValid(self, value) -> bool:
201203
return isinstance(value,dict) or isinstance(value, PSet)
202204
def dumpPython(self, options:PrintOptions=PrintOptions()) -> str:
@@ -1459,7 +1461,7 @@ def template(self):
14591461
def copy(self):
14601462
return copy.copy(self)
14611463
def _additionalInitArguments(self, options):
1462-
if self._template:
1464+
if self._template and not self._template._isEmpty():
14631465
#NOTE: PSetTemplate.dumpPython does not include the 'cms.' part
14641466
return 'template = cms.'+self._template.dumpPython(options)
14651467
return None
@@ -2420,6 +2422,10 @@ def testVPSetWithTemplate(self):
24202422
ptest = VPSet(PSet(b=int32(3)), template = PSetTemplate(a=required.int32))
24212423
self.assertEqual(len(ptest), 1)
24222424
self.assertEqual(ptest[0].b.value(), 3)
2425+
self.assertEqual(ptest.dumpPython(),"cms.VPSet(cms.PSet(\n b = cms.int32(3)\n), \ntemplate = cms.PSetTemplate(\n a = cms.required.int32\n))"
2426+
)
2427+
ptest = VPSet(template=PSetTemplate())
2428+
self.assertEqual(ptest.dumpPython(),"cms.VPSet()")
24232429
#will inject `a=required.int32` into the PSet when starting from a dict()
24242430
ptest = VPSet(dict(), template = PSetTemplate(a=required.int32))
24252431
self.assertEqual(len(ptest), 1)

FWCore/ParameterSet/src/ParameterDescription.cc

+17-8
Original file line numberDiff line numberDiff line change
@@ -365,14 +365,23 @@ namespace edm {
365365
int indentation,
366366
CfiOptions& options) const {
367367
bool nextOneStartsWithAComma = false;
368-
for_all(vPset_,
369-
std::bind(&writeOneElementToCfi,
370-
std::placeholders::_1,
371-
std::ref(os),
372-
indentation,
373-
options,
374-
std::ref(nextOneStartsWithAComma)));
375-
os << "\n";
368+
for (auto const& p : vPset_) {
369+
writeOneElementToCfi(p, os, indentation, options, nextOneStartsWithAComma);
370+
}
371+
if (cfi::shouldWriteUntyped(options) or psetDesc_->empty() or psetDesc_->anythingAllowed() or
372+
psetDesc_->isUnknown()) {
373+
os << "\n";
374+
} else {
375+
if (not vPset_.empty()) {
376+
os << ",";
377+
}
378+
os << "\n";
379+
printSpaces(os, indentation + 2);
380+
CfiOptions fullOp = cfi::Typed{};
381+
os << "template = cms.PSetTemplate(";
382+
psetDesc_->writeCfi(os, false, indentation + 4, fullOp);
383+
os << ")\n";
384+
}
376385
printSpaces(os, indentation);
377386
}
378387

FWCore/ParameterSet/test/test_catch_ParameterSetDescription.cc

+30
Original file line numberDiff line numberDiff line change
@@ -1398,6 +1398,36 @@ vp = cms.VPSet(
13981398
p = cms.PSet(),
13991399
vp = cms.VPSet(
14001400
)
1401+
),
1402+
template = cms.PSetTemplate(
1403+
i = cms.required.int32,
1404+
vi = cms.required.vint32,
1405+
ui = cms.required.uint32,
1406+
vui = cms.required.vuint32,
1407+
l = cms.required.int64,
1408+
vl = cms.required.vint64,
1409+
ul = cms.required.uint64,
1410+
vul = cms.required.vuint64,
1411+
b = cms.required.bool,
1412+
d = cms.required.double,
1413+
vd = cms.required.vdouble,
1414+
s = cms.required.string,
1415+
vs = cms.required.vstring,
1416+
t = cms.required.InputTag,
1417+
vt = cms.required.VInputTag,
1418+
et = cms.required.ESInputTag,
1419+
vet = cms.required.VESInputTag,
1420+
f = cms.required.FileInPath,
1421+
e = cms.required.EventID,
1422+
ve = cms.required.VEventID,
1423+
L = cms.required.LuminosityBlockID,
1424+
vL = cms.required.VLuminosityBlockID,
1425+
er = cms.required.EventRange,
1426+
ver = cms.required.VEventRange,
1427+
Lr = cms.required.LuminosityBlockRange,
1428+
vLr = cms.required.VLuminosityBlockRange,
1429+
p = cms.PSet(),
1430+
vp = cms.required.VPSet
14011431
)
14021432
)
14031433
)-";

0 commit comments

Comments
 (0)