blob: d74574c3e4fca68af1c2bfd9c290d886a64af623 [file] [log] [blame]
# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import factory_common # pylint: disable=W0611
from cros.factory.rule import GetLogger, RuleFunction, Value, RuleException
def Assert(expr):
"""A wrapper method to test assertion.
expr: An expression that should evalute to True or False.
HWIDException if the given expr does not evaluate to True."""
if not expr:
GetLogger().Error('Assertion failed.')
def Re(expr):
"""A wrapper method to create regular expression Value object.
expr: A string indicating the regular expression.
A regular expression-enabled Value object.
return Value(expr, is_re=True)
def LookupMap(key, mapping):
"""A utility method for looking up value in a map.
key: The key to look up value of.
mapping: A map object to look up value from. This must be of dict type.
A value retrieved from the given map object with the given key.
RuleException: If 'mapping' is not of dict tyep.
KeyError: If 'key' is not found in 'mapping'.
if not isinstance(mapping, dict):
raise RuleException('%r is not a dict' % mapping)
return mapping[key]
def CallIf(cond, func, *args, **kwargs):
"""A utility function to conditionally call a function.
cond: The conditional variable.
func: The function to call if cond evaluates to True.
*args: Positional arguments to pass to func.
**kwargs: Keyword arguments to pass to func.
if cond:
func(*args, **kwargs)