def __init__(self, config, parent=None, parent_key=None): 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.isInstanceOf(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: 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] = config else: self._refs['..'] = self self._refs['/'] = self if config and isinstance(config, dict): for key, value in config.items(): if not util.isInstanceOf(key, [str, unicode]): msg = 'key type is %s while expected is [str, unicode]: %s' raise TypeError(msg % (type(key), key)) ConfigReference(value, self, key)
def getTranslatedKeys(self, ref_key, sub_ref): '''Get translated keys from ref_key and ref to ref_key.''' 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('from_key %s missing in %s', from_key, sub_ref) translated_keys = [] for translated_key in self.translated_keys: if callable(translated_key): try: translated_key = translated_key( sub_ref, ref_key, **key_configs) except Exception as error: msg = '%s fails to get translated key by %s[%s](**%s)' logging.error(msg, translated_key, ref_key, key_configs) logging.exception(error) continue if not translated_key: continue if not util.isInstanceOf(translated_key, [str, unicode]): logging.error('translated key %s should be [str, unicode]', translated_key) continue translated_keys.append(translated_key) return translated_keys
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.isInstanceOf(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_next_matcher_name = \ unmatch_sameline_next_matcher_name self.unmatch_nextline_next_matcher_name = \ unmatch_nextline_next_matcher_name self.match_sameline_next_matcher_name = \ match_sameline_next_matcher_name self.match_nextline_next_matcher_name = \ match_nextline_next_matcher_name
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.isInstanceOf(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_next_matcher_name = \ unmatch_sameline_next_matcher_name self.unmatch_nextline_next_matcher_name = \ unmatch_nextline_next_matcher_name self.match_sameline_next_matcher_name = \ match_sameline_next_matcher_name self.match_nextline_next_matcher_name = \ match_nextline_next_matcher_name
def refItems(self, path): '''Return the refs of the glob path. Args: path: str, glob pattern. like '/networking/*/ip', Returns: dict of {key: ConfigReference instance}. Example: config = {'network': {'management': {'ip': '1.2.3.4'}, 'public': {'ip': '2.3.4.5'}}} ref = ConfigReference(config) refs = ref.refItems('/networking/*/ip') refs['/networking/management/ip'].config = '1.2.3.4' refs['/networking/public/ip'].config = '2.3.4.5' ''' if not path: raise KeyError('key %s is empty' % path) parts = [] if util.isInstanceOf(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.specialPath(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.specialPath(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
def _isValidTranslatedKeys(self): '''Check translated keys are valid.''' for i, translated_key in enumerate(self.translated_keys): if util.isInstanceOf(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))
def _isValidFromValues(self): '''Check from values are valid.''' for mapping_key, from_value in self.from_values.items(): if not util.isInstanceOf(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))
def _isValidOverrideConditions(self): '''Check override conditions are valid.''' override_items = self.override_conditions.items() for mapping_key, override_condition in override_items: if not util.isInstanceOf(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 test_isInstanceOf(self): self.assertTrue(util.isInstanceOf({}, [dict, list])) self.assertFalse(util.isInstanceOf({}, [str, list])) self.assertFalse(util.isInstanceOf({}, []))