Revision a464ce71 lib/ht.py
b/lib/ht.py | ||
---|---|---|
292 | 292 |
compat.all(val_type(v) for v in container.values())) |
293 | 293 |
|
294 | 294 |
return desc(TAnd(TDict, fn)) |
295 |
|
|
296 |
|
|
297 |
def _TStrictDictCheck(require_all, exclusive, items, val): |
|
298 |
"""Helper function for L{TStrictDict}. |
|
299 |
|
|
300 |
""" |
|
301 |
notfound_fn = lambda _: not exclusive |
|
302 |
|
|
303 |
if require_all and not frozenset(val.keys()).issuperset(items.keys()): |
|
304 |
# Requires items not found in value |
|
305 |
return False |
|
306 |
|
|
307 |
return compat.all(items.get(key, notfound_fn)(value) |
|
308 |
for (key, value) in val.items()) |
|
309 |
|
|
310 |
|
|
311 |
def TStrictDict(require_all, exclusive, items): |
|
312 |
"""Strict dictionary check with specific keys. |
|
313 |
|
|
314 |
@type require_all: boolean |
|
315 |
@param require_all: Whether all keys in L{items} are required |
|
316 |
@type exclusive: boolean |
|
317 |
@param exclusive: Whether only keys listed in L{items} should be accepted |
|
318 |
@type items: dictionary |
|
319 |
@param items: Mapping from key (string) to verification function |
|
320 |
|
|
321 |
""" |
|
322 |
descparts = ["Dictionary containing"] |
|
323 |
|
|
324 |
if exclusive: |
|
325 |
descparts.append(" none but the") |
|
326 |
|
|
327 |
if require_all: |
|
328 |
descparts.append(" required") |
|
329 |
|
|
330 |
if len(items) == 1: |
|
331 |
descparts.append(" key ") |
|
332 |
else: |
|
333 |
descparts.append(" keys ") |
|
334 |
|
|
335 |
descparts.append(utils.CommaJoin("\"%s\" (value %s)" % (key, value) |
|
336 |
for (key, value) in items.items())) |
|
337 |
|
|
338 |
desc = WithDesc("".join(descparts)) |
|
339 |
|
|
340 |
return desc(TAnd(TDict, |
|
341 |
compat.partial(_TStrictDictCheck, require_all, exclusive, |
|
342 |
items))) |
Also available in: Unified diff