示例#1
0
    def _get_translated_keys(self, ref_key, sub_ref):
        """Get translated keys."""
        key_configs = {}
        for mapping_key, from_key in self.from_keys_.items():
            if from_key in sub_ref:
                key_configs[mapping_key] = sub_ref[from_key]
            else:
                logging.error('%s from_key %s missing in %s',
                              self, from_key, sub_ref)

        translated_keys = []
        for translated_key in self.translated_keys_:
            if callable(translated_key):
                translated_key = translated_key(
                    sub_ref, ref_key, **key_configs)

            if not translated_key:
                logging.debug('%s ignore empty translated key', self)
                continue

            if not util.is_instance(translated_key, [str, unicode]):
                logging.error(
                    '%s translated key %s should be [str, unicode]',
                    self, translated_key)
                continue

            translated_keys.append(translated_key)

        return translated_keys
示例#2
0
    def __init__(self,
                 pattern,
                 progress=None,
                 message_template='',
                 severity=None,
                 unmatch_sameline_next_matcher_name='',
                 unmatch_nextline_next_matcher_name='',
                 match_sameline_next_matcher_name='',
                 match_nextline_next_matcher_name=''):
        self.regex_ = re.compile(pattern)
        if not progress:
            self.progress_ = SameProgress()
        elif isinstance(progress, ProgressCalculator):
            self.progress_ = progress
        elif util.is_instance(progress, [int, float]):
            self.progress_ = RelativeProgress(progress)
        else:
            raise TypeError('progress unsupport type %s: %s' %
                            (type(progress), progress))

        self.message_template_ = message_template
        self.severity_ = severity
        self.unmatch_sameline_ = unmatch_sameline_next_matcher_name
        self.unmatch_nextline_ = unmatch_nextline_next_matcher_name
        self.match_sameline_ = match_sameline_next_matcher_name
        self.match_nextline_ = match_nextline_next_matcher_name
    def __init__(self, config, parent=None, parent_key=None):
        """Construct ConfigReference from configuration.

        :param config: configuration to build the ConfigRerence instance.
        :type config: dict
        :param parent: parent ConfigReference instance.
        :param parent_key: the key refers to the config in parent.
        :type parent_key: str

        :raises: TypeError
        """
        if parent and not isinstance(parent, self.__class__):
            raise TypeError('parent %s type should be %s'
                            % (parent, self.__class__.__name__))\

        if parent_key and not util.is_instance(parent_key, [str, unicode]):
            raise TypeError('parent_key %s type should be [str, unicode]'
                            % parent_key)

        self.config = config
        self.refs_ = {'.': self}
        self.parent_ = parent
        self.parent_key_ = parent_key
        if parent is not None:
            self.refs_['..'] = parent
            self.refs_['/'] = parent.refs_['/']
            parent.refs_[parent_key] = self
            if parent.config is None or not isinstance(parent.config, dict):
                parent.__init__({}, parent=parent.parent_,
                                parent_key=parent.parent_key_)

            parent.config[parent_key] = self.config
        else:
            self.refs_['..'] = self
            self.refs_['/'] = self

        if config and isinstance(config, dict):
            for key, value in config.items():
                if not util.is_instance(key, [str, unicode]):
                    msg = 'key type is %s while expected is [str, unicode]: %s'
                    raise TypeError(msg % (type(key), key))
                ConfigReference(value, self, key)
示例#4
0
    def _is_valid_from_values(self):
        """Check from values are valid."""
        for mapping_key, from_value in self.from_values_.items():
            if not util.is_instance(from_value, [str, unicode]):
                raise TypeError(
                    'from_values[%s] type is %s while '
                    'expected type is [str, unicode]: %s' % (
                        mapping_key, type(from_value), from_value))

            if '*' in from_value:
                raise KeyError(
                    'from_values[%s] %s contains *' % (
                        mapping_key, from_value))
示例#5
0
 def _is_valid_translated_keys(self):
     """Check translated keys are valid."""
     for i, translated_key in enumerate(self.translated_keys_):
         if util.is_instance(translated_key, [str, unicode]):
             if '*' in translated_key:
                 raise KeyError(
                     'transalted_keys[%d] %s should not contain *' % (
                         i, translated_key))
         elif not callable(translated_key):
             raise TypeError(
                 'translated_keys[%d] type is %s while expected '
                  'types are str or callable: %s' % (
                      i, type(translated_key), translated_key))
示例#6
0
    def _is_valid_override_conditions(self):
        """Check override conditions are valid."""
        override_items = self.override_conditions_.items()
        for mapping_key, override_condition in override_items:
            if not util.is_instance(override_condition, [str, unicode]):
                raise TypeError('override_conditions[%s] type is %s '
                                'while expected type is [str, unicode]: %s' %
                                (mapping_key, type(override_condition),
                                 override_condition))

            if '*' in override_condition:
                raise KeyError('override_conditions[%s] %s contains *' %
                               (mapping_key, override_condition))
    def ref_items(self, path):
        """Return the refs matching the path glob.

        :param path: glob pattern to match the path to the ref.
        :type path: str

        :returns: dict of key to :class:`ConfigReference` instance.
        :raises: KeyError
        """
        if not path:
            raise KeyError('key %s is empty' % path)

        parts = []

        if util.is_instance(path, [str, unicode]):
            parts = path.split('/')
        else:
            parts = path

        if not parts[0]:
            parts = parts[1:]
            refs = [('/', self.refs_['/'])]
        else:
            refs = [('', self)]

        for part in parts:
            if not part:
                continue

            next_refs = []
            for prefix, ref in refs:
                if self._special_path(part):
                    sub_prefix = os.path.join(prefix, part)
                    next_refs.append((sub_prefix, ref.refs_[part]))
                    continue

                for sub_key, sub_ref in ref.refs_.items():
                    if self._special_path(sub_key):
                        continue

                    matched = fnmatch.fnmatch(sub_key, part)
                    if not matched:
                        continue

                    sub_prefix = os.path.join(prefix, sub_key)
                    next_refs.append((sub_prefix, sub_ref))

            refs = next_refs

        return refs
示例#8
0
    def _is_valid_override_conditions(self):
        """Check override conditions are valid."""
        override_items = self.override_conditions_.items()
        for mapping_key, override_condition in override_items:
            if not util.is_instance(override_condition, [str, unicode]):
                raise TypeError(
                    'override_conditions[%s] type is %s '
                    'while expected type is [str, unicode]: %s' % (
                        mapping_key, type(override_condition),
                        override_condition))

            if '*' in override_condition:
                raise KeyError(
                    'override_conditions[%s] %s contains *' % (
                        mapping_key, override_condition))
示例#9
0
    def __init__(self, pattern, progress=None,
                 message_template='', severity=None,
                 unmatch_sameline_next_matcher_name='',
                 unmatch_nextline_next_matcher_name='',
                 match_sameline_next_matcher_name='',
                 match_nextline_next_matcher_name=''):
        self.regex_ = re.compile(pattern)
        if not progress:
            self.progress_ = SameProgress()
        elif isinstance(progress, ProgressCalculator):
            self.progress_ = progress
        elif util.is_instance(progress, [int, float]):
            self.progress_ = RelativeProgress(progress)
        else:
            raise TypeError(
                'progress unsupport type %s: %s' % (
                    type(progress), progress))

        self.message_template_ = message_template
        self.severity_ = severity
        self.unmatch_sameline_ = unmatch_sameline_next_matcher_name
        self.unmatch_nextline_ = unmatch_nextline_next_matcher_name
        self.match_sameline_ = match_sameline_next_matcher_name
        self.match_nextline_ = match_nextline_next_matcher_name
示例#10
0
 def test_isinstance(self):
     """test isinstance."""
     self.assertTrue(util.is_instance({}, [dict, list]))
     self.assertFalse(util.is_instance({}, [str, list]))
     self.assertFalse(util.is_instance({}, []))
示例#11
0
 def test_isinstance(self):
     self.assertTrue(util.is_instance({}, [dict, list]))
     self.assertFalse(util.is_instance({}, [str, list]))
     self.assertFalse(util.is_instance({}, []))
示例#12
0
def _model_condition(col_attr, value):
    if isinstance(value, list):
        basetype_values = []
        composite_values = []
        for item in value:
            if util.is_instance(item, [list, dict]):
                composite_values.append(item)
            else:
                basetype_values.append(item)
        conditions = []
        if basetype_values:
            if len(basetype_values) == 1:
                condition = (col_attr == basetype_values[0])
            else:
                condition = col_attr.in_(basetype_values)
            conditions.append(condition)
        for composite_value in composite_values:
            condition = _model_condition(col_attr, composite_value)
            if condition is not None:
                conditions.append(condition)
        if not conditions:
            return None
        if len(conditions) == 1:
            return conditions[0]
        return or_(*conditions)
    elif isinstance(value, dict):
        conditions = []
        if 'eq' in value:
            conditions.append(_model_condition_func(
                col_attr, value['eq'],
                lambda attr, data: attr == data,
                lambda attr, data, item_condition_func: attr.in_(data)
            ))
        if 'lt' in value:
            conditions.append(_model_condition_func(
                col_attr, value['lt'],
                lambda attr, data: attr < data,
                _one_item_list_condition_func
            ))
        if 'gt' in value:
            conditions.append(_model_condition_func(
                col_attr, value['gt'],
                lambda attr, data: attr > data,
                _one_item_list_condition_func
            ))
        if 'le' in value:
            conditions.append(_model_condition_func(
                col_attr, value['le'],
                lambda attr, data: attr <= data,
                _one_item_list_condition_func
            ))
        if 'ge' in value:
            conditions.append(_model_condition_func(
                col_attr, value['ge'],
                lambda attr, data: attr >= data,
                _one_item_list_condition_func
            ))
        if 'ne' in value:
            conditions.append(_model_condition_func(
                col_attr, value['ne'],
                lambda attr, data: attr != data,
                lambda attr, data, item_condition_func: attr.notin_(data)
            ))
        if 'in' in value:
            conditions.append(col_attr.in_(value['in']))
        if 'notin' in value:
            conditions.append(col_attr.notin_(value['notin']))
        if 'startswith' in value:
            conditions.append(_model_condition_func(
                col_attr, value['startswith'],
                lambda attr, data: attr.like('%s%%' % data)
            ))
        if 'endswith' in value:
            conditions.append(_model_condition_func(
                col_attr, value['endswith'],
                lambda attr, data: attr.like('%%%s' % data)
            ))
        if 'like' in value:
            conditions.append(_model_condition_func(
                col_attr, value['like'],
                lambda attr, data: attr.like('%%%s%%' % data)
            ))
        if 'between' in value:
            conditions.append(_model_condition_func(
                col_attr, value['between'],
                _between_condition
            ))
        conditions = [
            condition
            for condition in conditions
            if condition is not None
        ]
        if not conditions:
            return None
        if len(conditions) == 1:
            return conditions[0]
        return and_(conditions)
    else:
        condition = (col_attr == value)
        return condition