<commit_before># -*- test-case-name: openid.test.test_xri -*-
"""Utility functions for handling XRIs.

@see: XRI Syntax v2.0 at the U{OASIS XRI Technical Committee<http://www.oasis-open.org/committees/tc_home.php?wg_abbrev=xri>}
"""

import re
from functools import reduce

from openid import codecutil  # registers 'oid_percent_escape' encoding handler


XRI_AUTHORITIES = ['!', '=', '@', '+', '$', '(']
XREF_RE = re.compile(r'\((.*?)\)')


def is_iname(identifier):
    return identifier.startswith(tuple(['xri://'] + XRI_AUTHORITIES))


def _escape_xref(match):
    '''
    Escape things that need to be escaped if they're in a cross-reference.
    '''
    return match.group(0).replace('/', '%2F') .replace('?', '%3F') .replace('#', '%23')


def urlescape(xri):
    '''
    Escapes an unprefixed xri to be used as part of a URL.
    '''
    xri = xri.replace('%', '%25')
    xri = XREF_RE.sub(_escape_xref, xri)
    return xri.encode('ascii', errors='oid_percent_escape').decode()


def providerIsAuthoritative(providerID, canonicalID):
    """Is this provider ID authoritative for this XRI?<commit_msg>Replace use of codecutil with urllib.parse.quote in xri.py<commit_after># -*- test-case-name: openid.test.test_xri -*-
"""Utility functions for handling XRIs.

@see: XRI Syntax v2.0 at the U{OASIS XRI Technical Committee<http://www.oasis-open.org/committees/tc_home.php?wg_abbrev=xri>}
"""

import re
import urllib.parse
from functools import reduce


XRI_AUTHORITIES = ['!', '=', '@', '+', '$', '(']
XREF_RE = re.compile(r'\((.*?)\)')

def is_iname(identifier):
    return identifier.startswith(tuple(['xri://'] + XRI_AUTHORITIES))


def _escape_xref(match):
    '''
    Escape things that need to be escaped if they're in a cross-reference.
    '''
    return match.group(0).replace('/', '%2F') .replace('?', '%3F') .replace('#', '%23')


def urlescape(xri):
    '''
    Escapes an unprefixed xri to be used as part of a URL.
    '''
    xri = urllib.parse.quote(xri, safe=''.join(XRI_AUTHORITIES + [')', '/', '?', '#', '*']))
    xri = XREF_RE.sub(_escape_xref, xri)
    return xri


def providerIsAuthoritative(providerID, canonicalID):
    """Is this provider ID authoritative for this XRI?
{'fraction_non_alphanumeric': 0.17892824704813806, 'fraction_numerical': 0.007266121707538601, 'mean_word_length': 4.099537037037037, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '860748', 'n_tokens_mistral': 816, 'n_tokens_neox': 791, 'n_words': 189}