def resolve_pattern_prompt(self, match): """ Resolve adaptive pattern prompt """ old_pattern_prompt = self.patterns["prompt"].pattern pattern_prompt = old_pattern_prompt sl = self.profile.can_strip_hostname_to for k, v in six.iteritems(match.groupdict()): if v: if k == "hostname" and sl and len(v) > sl: ss = list(reversed(v[sl:])) v = re.escape(v[:sl]) + reduce( lambda x, y: "(?:%s%s)?" % (re.escape(y), x), ss[1:], "(?:%s)?" % re.escape(ss[0]), ) else: v = re.escape(v) pattern_prompt = replace_re_group(pattern_prompt, "(?P<%s>" % k, v) pattern_prompt = replace_re_group(pattern_prompt, "(?P=%s" % k, v) else: self.logger.error("Invalid prompt pattern") if old_pattern_prompt != pattern_prompt: self.logger.debug("Refining pattern prompt to %r", pattern_prompt) self.patterns["prompt"] = re.compile(pattern_prompt, re.DOTALL | re.MULTILINE)
def test_replace_re_group(config, expected): assert replace_re_group(config, "(?P<groupname>", "groupvalue") == expected