def track_call(self, m_open: regex.Match, m_close: regex.Match, tag_contents: List[str]): """Set up tracking for "{% call ... %}".""" unique_alternate_id = self.next_slice_id() open_ = m_open.group(1) close_ = m_close.group(1) # Here, we still need to evaluate the original tag contents, e.g. in # case it has intentional side effects, but also return a slice ID # for tracking. alternate_code = (f"\0{unique_alternate_id} {open_} " f"{''.join(tag_contents)} {close_}") return self.make_raw_slice_info(unique_alternate_id, alternate_code)
def track_templated(self, m_open: regex.Match, m_close: regex.Match, tag_contents: List[str]) -> RawSliceInfo: """Compute tracking info for Jinja templated region, e.g. {{ foo }}.""" unique_alternate_id = self.next_slice_id() open_ = m_open.group(1) close_ = m_close.group(1) # Here, we still need to evaluate the original tag contents, e.g. in # case it has intentional side effects, but also return a slice ID # for tracking. alternate_code = (f"\0{unique_alternate_id} {open_} " f"{''.join(tag_contents)} {close_}") return self.make_raw_slice_info(unique_alternate_id, alternate_code)
def process(self, builder: BlockLevelBuilder, match: re.Match): para = Node(NodeType.block, builder.location) para.class_ = match.group(1) args = builder.parse_args('}') para.add_args(args) builder.append_block(para)
def btc_base58_check(match: regex.Match): text = match.groupdict().get('valu') try: base58.b58decode_check(text) except ValueError: return None, {} return ('btc', text), {}
def cardano_shelly_check(match: regex.Match): text = match.groupdict().get('valu') # Bech32 decoding ret = bech32.bech32_decode(text) if ret == (None, None): return None, {} return ('ada', text), {}
def process(self, builder: BlockLevelBuilder, match: re.Match): if match.group(1) == '//': para = builder.parse_para() builder.append_annotation(Annotation('comment', node=para)) else: line = Node(NodeType.block, builder.location) line.add_subs(builder.parse_line()) builder.append_annotation(Annotation('comment', node=line))
def fqdn_prefix_check(match: regex.Match): mnfo = match.groupdict() valu = mnfo.get('valu') prefix = mnfo.get('prefix') cbfo = {} if prefix is not None: new_valu = valu.rstrip(inverse_prefixs.get(prefix)) if new_valu != valu: valu = new_valu cbfo['match'] = valu return valu, cbfo
def bch_check(match: regex.Match): text = match.groupdict().get('valu') # Checksum if we're mixed case or not prefix, body = text.split(':', 1) if not body.isupper() and not body.islower(): return None, {} try: cashaddr_convert.Address._cash_string(text) except: return None, {} text = text.lower() return ('bch', text), {}
def eth_check(match: regex.Match): text = match.groupdict().get('valu') prefix, body = text.split('x') # type: str, str # Checksum if we're mixed case or not if not body.isupper() and not body.islower(): ret = ether_eip55(body) if ret is None or ret != text: return None, {} return ('eth', text), {} # any valid 0x<40 character> hex string is possibly a ETH address. return ('eth', text.lower()), {}
def extract_tag_contents( str_parts: List[str], m_close: regex.Match, m_open: regex.Match, str_buff: str, ) -> List[str]: """Given Jinja tag info, return the stuff inside the braces. I.e. Trim off the brackets and the whitespace. """ if len(str_parts) >= 3: # Handle a tag received as individual parts. trimmed_parts = str_parts[1:-1] if trimmed_parts[0].isspace(): del trimmed_parts[0] if trimmed_parts[-1].isspace(): del trimmed_parts[-1] else: # Handle a tag received in one go. trimmed_content = str_buff[len(m_open.group(0) ):-len(m_close.group(0))] trimmed_parts = trimmed_content.split() return trimmed_parts
def cardano_byron_check(match: regex.Match): text = match.groupdict().get('valu') # Try a base58 / cbor decoding try: decoded_text = base58.b58decode(text) message = cbor2.loads(decoded_text) if len(message) != 2: return None, {} csum = message[1] computed_checksum = binascii.crc32(message[0].value) if csum == computed_checksum: return ('ada', text), {} except (ValueError, cbor2.CBORError): pass return None, {}
def substrate_check(match: regex.Match): text = match.groupdict().get('valu') prefix = text[0] # str if prefix == '1' and substrate_ss58.is_valid_ss58_address( text, valid_ss58_format=0): # polkadot return ('dot', text), {} elif prefix.isupper() and substrate_ss58.is_valid_ss58_address( text, valid_ss58_format=2): # kusuma return ('ksm', text), {} else: # Do nothing with generic substrate matches # Generic substrate addresses are checked with valid_ss58_format=42 return None, {}
def _refang2_func(match: regex.Match, offsets: dict, fangs: dict): # This callback exploits the fact that known de-fanging strategies either # do in-place transforms, or transforms which increase the target string # size. By re-fanging, we are compressing the old string into a new string # of potentially a smaller size. We record the offset where any transform # affects the contents of the string. This means, downstream, we can avoid # have to go back to the source text if there were **no** transforms done. # This relies on the prior assertions of refang sizing. group = match.group(0) ret = fangs[group.lower()] rlen = len(ret) mlen = len(group) span = match.span(0) consumed = offsets.get('_consumed', 0) offs = span[0] - consumed nv = mlen - rlen # For offsets, we record the nv + 1 since the now-compressed string # has one character represented by mlen - rlen + 1 characters in the # original string. offsets[offs] = nv + 1 offsets['_consumed'] = consumed + nv return ret
def fqdn_check(match: regex.Match): mnfo = match.groupdict() valu = mnfo.get('valu') nval = unicodedata.normalize('NFKC', valu) nval = regex.sub(udots, '.', nval) nval = nval.strip().strip('.') try: idna.encode(nval, uts46=True).decode('utf8') except idna.IDNAError: try: nval.encode('idna').decode('utf8').lower() except UnicodeError: return None, {} return valu, {}
def btc_bech32_check(match: regex.Match): text = match.groupdict().get('valu') prefix, _ = text.split('1', 1) prefix = prefix.lower() if prefix == 'bc': bitcoin.SelectParams('mainnet') elif prefix == 'tb': bitcoin.SelectParams('testnet') elif prefix == 'bcrt': bitcoin.SelectParams('regtest') else: # pragma: no cover raise ValueError(f'Unknown prefix {text}') try: _ = bitcoin_b32.CBech32Data(text) except bitcoin_b32.Bech32Error: return None, {} # The proper form of a bech32 address is lowercased. We do not want to verify # a mixed case form, so lowercase it prior to returning. return ('btc', text.lower()), {}
def xrp_check(match: regex.Match): text = match.groupdict().get('valu') if xrp_addresscodec.is_valid_classic_address( text) or xrp_addresscodec.is_valid_xaddress(text): return ('xrp', text), {} return None, {}
def process(self, builder: BlockLevelBuilder, match: re.Match): para = builder.parse_para() para.class_ = '^' para.text = match.group(1) builder.append_block(para)
def process(self, builder: BlockLevelBuilder, match: re.Match): class_ = match.group(1) line = Node(NodeType.block, builder.location) line.class_ = class_ line.add_subs(builder.parse_line()) builder.append_block(line)
def process(self, builder: BlockLevelBuilder, match: re.Match): args_group = match.group(3) args: List[str] = [] if args_group is not None: args = [x.strip() for x in args_group.split(',')] builder.append_annotation(Annotation(match.group(1), args=args))