def helper(match: re.match): alias = match.groupdict()["alias"] if alias.upper() in self.reserved_keywords or alias.upper() in ( "ASC", "DESC"): # List of Presto reserved keywords return match.group( ) # Not an alias but the continuation of the SQL logic else: return match.group()[:-len(alias)] + "as " + alias
def match_to_free(line: int, m: re.match) -> Free: """Given a match that has groups: ('77633', '__init__.py', '422', 'validate', '0x7fca858e4200') this returns a Malloc() object.""" return Free( line, int(m.group(1)), m.group(2), int(m.group(3)), m.group(4), int(m.group(5), 16), )
def match_to_malloc(line: int, m: re.match) -> Malloc: """Given a match that has groups: ('77633', 'cmn_cmd_opts.py', '141', 'set_log_level', '560', '0x7fca83ef4240') this returns a Malloc() object.""" return Malloc( line, int(m.group(1)), m.group(2), int(m.group(3)), m.group(4), int(m.group(5)), int(m.group(6), 16), )
def _makeRange(m: re.match, s: int, e: int, i: int) -> List[float]: # For a pythonesque ranging: #list(arange(float(m.group(s)),float(m.group(e)),float(m.group(i)))) # BUT probably more expected ranging includes end point... # Check if there is no interval and ensure minimal return is start tol = 1e-3 # Attempt to allow for just missing the end sign = lambda x: copysign(1, x) (vs, ve, vi) = (float(m.group(s)), float(m.group(e)), float(m.group(i))) # Tolerate the interval being the wrong way round for the increment if vi == 0.0 or sign(vi) * (ve - vs) <= 0: # arange will now be ok return [vs] return list(arange(vs, ve + (ve - vs) * tol, vi))
def literal_string_decode(match: re.match) -> str: """ url_decode, but as a response for re.sub. Intended to be used with pattern['string'], as it expects \\1 to be the quote kind and \\2 its contents. It produces single-quote strings when possible, but when the string contains a \n character, is converted to triple quotes. It scapes all quotes and activates all escape sequences. >>> import re >>> re.sub(r"(')([\s\S]*)\\1",literal_string_decode,"asdasd'YXNkCmFzZA=='asdasd") "asdasd'''asd\\nasd'''asdasd" """ string = special_activate(quote_escape(url_decode(match.group(2)))) kind = "'''" if "\n" in string else match.group(1) return kind + string + kind
def is_message_id(self, match: re.match, msg: str) -> bool: start = match.start() email = match.group() # Note that our regex will match thigs like "message-id=Issue1649523226559@postfix-mail.mail-system.svc.cluster.local" # so we need to filter / check for these first if email.startswith(self.MESSAGE_ID_LINE): return True if start >= self.MESSAGE_ID_LINE_LEN: pos = start - 1 while True: char = msg[pos] if char == '=': break elif char in '{<["\'': pos = pos - 1 continue return False check = msg[pos - self.MESSAGE_ID_LINE_LEN + 1:pos + 1] if check == self.MESSAGE_ID_LINE: return True return False
def replace(self, match: re.match, msg: str) -> str: email = match.group() # Return the details unchanged if they look like Postfix message ID if self.is_message_id(match, msg): return email if not self.case_sensitive: email = email.lower() if self.split: # The "@" can show up in the local part, but shouldn't appear in the # domain part (at least not that we know). local, domain = email.rsplit("@", 1) local = hmac.new(self.salt_encoded, local.encode(), hashlib.sha256).hexdigest() domain = hmac.new(self.salt_encoded, domain.encode(), hashlib.sha256).hexdigest() if self.short_sha: local = local[:8] domain = domain[:8] email = local + "@" + domain else: email = hmac.new(self.salt_encoded, email.encode(), hashlib.sha256).hexdigest() if self.short_sha: email = email[:8] return self.prefix + email + self.suffix
def get_group_str(self, match: re.match = None, group_name: str = ''): group_str = '' try: group_str = match.group(group_name) except IndexError: pass return group_str
def escape_char(m: re.match) -> str: c = m.group() if c == '/': return '--' elif c == '-': return '-m' else: return '-' + hex(ord(c))[2:]
def free_key_quote(match: re.match) -> str: ''' Response to re.sub, returns a quoted free-key if \\2 is a valid python identifier. \\1 is assumed to be the indentation level. String key is encoded. >>> import re >>> re.sub(r"(\\t*)([^' \\n,\{]+)(?= *:)",free_key_quote,"\\ta:4") "\\t'YQ==':4" >>> re.sub(r"(\\t*)([^' \\n,\{]+)(?= *:)",free_key_quote,"\\t'a':4") "\\t'a':4" ''' level = match.group(1) key = match.group(2) if is_raw_key(key): return level + key elif key.isidentifier(): return f"{level}'{url_encode(key)}'"
def change(match: re.match): num = match.group() if num[0] == '.': return '0' + num if num[-1] == '.': return num + '0' return num
def lowercasesmoothsub(match: re.match, g=1) -> str: val = match.group(g) substitutions = { 'A': u'ᾀ', 'H': u'ᾐ', 'W': u'ᾠ', } substitute = substitutions[val] return substitute
def _match_obj_to_date(m: re.match) -> Optional[date]: """ Convert a successful regex match to a datetime.date object Called by: lines_in_weeks_out() """ if m: # group(3) is the year, group(1) is the month, group(2) is the day dt = [int(m.group(x)) for x in (3, 1, 2)] return datetime.date(dt[0], dt[1], dt[2]) # year, month, day else: return None
def lowercaseroughsub(match: re.match, g=1) -> str: val = match.group(g) substitutions = { 'A': u'ᾁ', 'H': u'ᾑ', 'W': u'ᾡ', } substitute = substitutions[val] return substitute
def lowercasesircumflexsub(match: re.match, g=1) -> str: val = match.group(g) substitutions = { 'A': u'ᾷ', 'H': u'ῇ', 'W': u'ῷ', } substitute = substitutions[val] return substitute
def lowercasegravesub(match: re.match, g=1) -> str: val = match.group(g) substitutions = { 'A': u'ᾲ', 'H': u'ῂ', 'W': u'ῲ', } substitute = substitutions[val] return substitute
def lowercaseacutedsub(match: re.match, g=1) -> str: val = match.group(g) substitutions = { 'A': u'ᾴ', 'H': u'ῄ', 'W': u'ῴ', } substitute = substitutions[val] return substitute
def replace(self, match: re.match, msg: str) -> str: email = match.group() # Return the details unchanged if they look like Postfix message ID if self.is_message_id(match, msg): return email # The "@" can show up in the local part, but shouldn't appear in the # domain part (at least not that we know). local, domain = email.rsplit("@", 1) local = self.mask_local(local) domain = self.mask_domain(domain) return local + '@' + domain
def lowercaseroughacute(match: re.match, g=1) -> str: val = match.group(g) substitutions = { 'A': u'ἅ', 'E': u'ἕ', 'I': u'ἵ', 'O': u'ὅ', 'U': u'ὕ', 'H': u'ἥ', 'W': u'ὥ', } substitute = substitutions[val] return substitute
def lowercaseroughcircumflex(match: re.match, g=1) -> str: val = match.group(g) substitutions = { 'A': u'ἇ', 'E': u'ἑ͂', # IG: TE=S BOLE=S E(=I 'I': u'ἷ', 'O': u'ὁ͂', # IG: PE]RI\ DE\ O(=[N !]DIK 'U': u'ὗ', 'H': u'ἧ', 'W': u'ὧ', } substitute = substitutions[val] return substitute
def lowercasesmoothacute(match: re.match, g=1) -> str: val = match.group(g) substitutions = { 'A': u'ἄ', 'E': u'ἔ', 'I': u'ἴ', 'O': u'ὄ', 'U': u'ὔ', 'H': u'ἤ', 'W': u'ὤ', } substitute = substitutions[val] return substitute
def lowercaseroughgrave(match: re.match, g=1) -> str: val = match.group(g) substitutions = { 'A': u'ἃ', 'E': u'ἓ', 'I': u'ἳ', 'O': u'ὃ', 'U': u'ὓ', 'H': u'ἣ', 'W': u'ὣ', } substitute = substitutions[val] return substitute
def lowercasesmoothgrave(match: re.match, g=1) -> str: val = match.group(g) substitutions = { 'A': u'ἂ', 'E': u'ἒ', 'I': u'ἲ', 'O': u'ὂ', 'U': u'ὒ', 'H': u'ἢ', 'W': u'ὢ', } substitute = substitutions[val] return substitute
def lowercaseroughcircumflexsubscript(match: re.match, g=1) -> str: val = match.group(g) substitutions = { 'A': u'ᾇ', 'E': u'', 'I': u'', 'O': u'', 'U': u'', 'H': u'ᾗ', 'W': u'ᾧ', } substitute = substitutions[val] return substitute
def lowercasesmoothacutesubscript(match: re.match, g=1) -> str: val = match.group(g) substitutions = { 'A': u'ᾄ', 'E': u'', 'I': u'', 'O': u'', 'U': u'', 'H': u'ᾔ', 'W': u'ᾤ', } substitute = substitutions[val] return substitute
def lowercaseacute(match: re.match, g=1) -> str: val = match.group(g) substitutions = { 'A': u'ά', 'E': u'έ', 'I': u'ί', 'O': u'ό', 'U': u'ύ', 'H': u'ή', 'W': u'ώ', } substitute = substitutions[val] return substitute
def lowercaseroughacutesubscript(match: re.match, g=1) -> str: val = match.group(g) substitutions = { 'A': u'ᾅ', 'E': u'', 'I': u'', 'O': u'', 'U': u'', 'H': u'ᾕ', 'W': u'ᾥ', } substitute = substitutions[val] return substitute
def lowercasegrave(match: re.match, g=1) -> str: val = match.group(g) substitutions = { 'A': u'ὰ', 'E': u'ὲ', 'I': u'ὶ', 'O': u'ὸ', 'U': u'ὺ', 'H': u'ὴ', 'W': u'ὼ', } substitute = substitutions[val] return substitute
def lowercasesircumflexdiaresis(match: re.match, g=1) -> str: val = match.group(g) substitutions = { 'A': u'', 'E': u'', 'I': u'', 'O': u'', 'U': u'ῧ', 'H': u'', 'W': u'', } substitute = substitutions[val] return substitute
def capitalsmoothgraveadscript(match: re.match, g=1) -> str: val = match.group(g) substitutions = { 'A': u'ᾊ', 'E': u'', 'I': u'', 'O': u'', 'U': u'', 'H': u'ᾚ', 'W': u'ᾪ', } substitute = substitutions[val] return substitute
def from_gcov_match(cls, match: re.match): count = int(match.group('count') or '0') id_ = int(match.group('id')) type_ = match.group('type') info = match.group('info') return cls(count, id_, type_, info)