Exemplo n.º 1
0
def compact(number):
    """Convert the number to the minimal representation. This strips the
    number of any valid separators and removes surrounding whitespace."""
    number = clean(number, ' ').strip()
    if number[:3].lower() == 'bc1':
        number = number.lower()
    return number
Exemplo n.º 2
0
def compact(number):
    """Convert the number to the minimal representation. This strips the
    number of any valid separators and removes surrounding whitespace."""
    number = clean(number, ' .-').strip()
    if number.startswith('0000'):
        number = number[4:]  # strip leading 0000 postgiro bank code
    return number
Exemplo n.º 3
0
def compact(number):
    """Convert the number to the minimal representation. This strips the
    number of any valid separators and removes surrounding whitespace."""
    number = clean(number, ' -').upper().strip()
    if number.startswith('MT'):
        number = number[2:]
    return number
Exemplo n.º 4
0
def compact(number):
    """Convert the number to the minimal representation. This strips the
    number of any valid separators and removes surrounding whitespace."""
    number = clean(number, '').upper().strip()
    module = _get_cc_module(number[:2])
    if not module:
        raise InvalidComponent()
    return number[:2] + module.compact(number[2:])
Exemplo n.º 5
0
def validate(number):
    """Checks to see if the number provided is a valid VAT number. This
    performs the country-specific check for the number."""
    number = clean(number, '').upper().strip()
    module = _get_cc_module(number[:2])
    if not module:
        raise InvalidComponent()
    return number[:2] + module.validate(number[2:])
Exemplo n.º 6
0
def validate(number):
    """Checks to see if the number provided is a valid EIN. This checks
    the length, groups and formatting if it is present."""
    match = _ein_re.search(clean(number, '').strip())
    if not match:
        raise InvalidFormat()
    get_campus(number)  # raises exception for unknown campus
    return compact(number)
Exemplo n.º 7
0
def validate(number):
    """Checks to see if the number provided is a valid ATIN. This checks
    the length and formatting if it is present."""
    match = _atin_re.search(clean(number, '').strip())
    if not match:
        raise InvalidFormat()
    # sadly, no more information on ATIN number validation was found
    return compact(number)
def _split(number):
    """Split the number into a court, registry, register number and
    optionally qualifier."""
    number = clean(number).strip()
    for fmt in _formats:
        m = re.match(fmt, number, flags=re.I | re.U)
        if m:
            return m.group('court'), m.group('registry'), m.group('nr'), m.group('x')
    raise InvalidFormat()
Exemplo n.º 9
0
def compact(number):
    """Convert the number to the minimal representation. This strips the
    number of any valid separators and removes surrounding whitespace."""
    number = clean(number, ' -./:').upper().strip()
    if number.startswith('EL') or number.startswith('GR'):
        number = number[2:]
    if len(number) == 8:
        number = '0' + number  # old format had 8 digits
    return number
Exemplo n.º 10
0
def compact(number, convert=False):
    """Convert the ISBN to the minimal representation. This strips the number
    of any valid ISBN separators and removes surrounding whitespace. If the
    covert parameter is True the number is also converted to ISBN-13
    format."""
    number = clean(number, ' -').strip().upper()
    if len(number) == 9:
        number = '0' + number
    if convert:
        return to_isbn13(number)
    return number
Exemplo n.º 11
0
def validate(number):
    """Check if the number is a valid VAT number.

    This performs the country-specific check for the number.
    """
    number = clean(number, '').strip()
    module = _get_cc_module(number[:2])
    try:
        return number[:2].upper() + module.validate(number[2:])
    except ValidationError:
        return module.validate(number)
Exemplo n.º 12
0
def validate(number):
    """Checks to see if the number provided is a valid ITIN. This checks
    the length, groups and formatting if it is present."""
    match = _itin_re.search(clean(number, '').strip())
    if not match:
        raise InvalidFormat()
    area = match.group('area')
    group = match.group('group')
    if area[0] != '9' or group not in _allowed_groups:
        raise InvalidComponent()
    return compact(number)
Exemplo n.º 13
0
def compact(number, convert=False):
    """Convert the ISBN to the minimal representation. This strips the number
    of any valid ISBN separators and removes surrounding whitespace. If the
    covert parameter is True the number is also converted to ISBN-13
    format."""
    number = clean(number, ' -').strip().upper()
    if len(number) == 9:
        number = '0' + number
    if convert:
        return to_isbn13(number)
    return number
Exemplo n.º 14
0
def validate(number):
    """Checks to see if the number provided is a valid ITIN. This checks
    the length, groups and formatting if it is present."""
    match = _itin_re.search(clean(number, '').strip())
    if not match:
        raise InvalidFormat()
    area = match.group('area')
    group = match.group('group')
    if area[0] != '9' or group not in _allowed_groups:
        raise InvalidComponent()
    return compact(number)
Exemplo n.º 15
0
def split(number):
    """Splits the number into a root, an episode or part, a check digit a
    version and another check digit. If any of the parts are missing an
    empty string is returned."""
    number = clean(number, ' -').strip().upper()
    if len(number) == 17 or len(number) == 26:
        return number[0:12], number[12:16], number[16], number[17:25], number[25:]
    elif len(number) > 16:
        return number[0:12], number[12:16], '', number[16:24], number[24:]
    else:
        return number[0:12], number[12:16], number[16:], '', ''
Exemplo n.º 16
0
def compact(number):
    """Convert the number to the minimal representation. This strips the
    number of any valid separators and removes surrounding whitespace."""
    number = clean(number, ' -./').upper().strip()
    if number.startswith('BE'):
        number = number[2:]
    if number.startswith('(0)'):
        number = '0' + number[3:]
    if len(number) == 9:
        number = '0' + number  # old format had 9 digits
    return number
Exemplo n.º 17
0
def compact(number):
    """Convert the number to the minimal representation. This strips the
    number of any valid separators and removes surrounding whitespace."""
    number = clean(number).strip().replace(' ', '-').split('-')
    if len(number) == 4:
        # zero pad the different sections if they are found
        lengths = (2, 4, 7, 3)
        return ''.join(n.zfill(l) for n, l in zip(number, lengths))
    else:
        # otherwise zero pad the account type
        number = ''.join(number)
        return number[:13] + number[13:].zfill(3)
Exemplo n.º 18
0
def split(number):
    """Split the number into a root, an episode or part, a check digit a
    version and another check digit. If any of the parts are missing an empty
    string is returned."""
    number = clean(number, ' -').strip().upper()
    if len(number) == 17 or len(number) == 26:
        return number[0:12], number[12:16], number[16], number[17:25], number[
            25:]
    elif len(number) > 16:
        return number[0:12], number[12:16], '', number[16:24], number[24:]
    else:
        return number[0:12], number[12:16], number[16:], '', ''
Exemplo n.º 19
0
def validate(number):
    """Check if the number is a valid VAT number. This performs the
    country-specific check for the number."""
    number = clean(number, '').upper().strip()
    cc = number[:2]
    module = _get_cc_module(cc)
    if not module:
        raise InvalidComponent()
    number = module.validate(number)
    if not number.startswith(cc):
        number = cc + number
    return number
Exemplo n.º 20
0
def compact(number):
    """Convert the number to the minimal representation. This strips the
    number of any valid separators and removes surrounding whitespace."""
    number = clean(number, '').upper().strip()
    cc = number[:2]
    module = _get_cc_module(cc)
    if not module:
        raise InvalidComponent()
    number = module.compact(number)
    if not number.startswith(cc):
        number = cc + number
    return number
Exemplo n.º 21
0
def compact(number):
    """Convert the number to the minimal representation. This strips the
    number of any valid separators and removes surrounding whitespace."""
    number = clean(number).strip().replace(' ', '-').split('-')
    if len(number) == 4:
        # zero pad the different sections if they are found
        lengths = (2, 4, 7, 3)
        return ''.join(n.zfill(l) for n, l in zip(number, lengths))
    else:
        # otherwise zero pad the account type
        number = ''.join(number)
        return number[:13] + number[13:].zfill(3)
def compact(number):
    """Convert the number to the minimal representation. This strips the
    number of any valid separators and removes surrounding whitespace."""
    number = clean(number, ' ').upper().strip()
    for prefix in ('УНП', u'УНП', 'UNP', u'UNP'):
        if type(number) == type(prefix) and number.startswith(prefix):
            number = number[len(prefix):]
    # Replace Cyrillic letters with Latin letters
    cleaned = ''.join(_cyrillic_to_latin.get(x, x) for x in to_unicode(number))
    if type(cleaned) != type(number):  # pragma: no cover (Python2 only)
        cleaned = cleaned.encode('utf-8')
    return cleaned
Exemplo n.º 23
0
    def check_vat_nl(self, vat):
        """
        Temporary Netherlands VAT validation to support the new format introduced in January 2020,
        until upstream is fixed.

        Algorithm detail: http://kleineondernemer.nl/index.php/nieuw-btw-identificatienummer-vanaf-1-januari-2020-voor-eenmanszaken

        TODO: remove when fixed upstream
        """

        try:
            from stdnum.util import clean
            from stdnum.nl.bsn import checksum
        except ImportError:
            return True

        vat = clean(vat, ' -.').upper().strip()

        # Remove the prefix
        if vat.startswith("NL"):
            vat = vat[2:]

        if not len(vat) == 12:
            return False

        # Check the format
        match = self.__check_vat_nl_re.match(vat)
        if not match:
            return False

        # Match letters to integers
        char_to_int = {k: str(ord(k) - 55) for k in string.ascii_uppercase}
        char_to_int['+'] = '36'
        char_to_int['*'] = '37'

        # 2 possible checks:
        # - For natural persons
        # - For non-natural persons and combinations of natural persons (company)

        # Natural person => mod97 full checksum
        check_val_natural = '2321'
        for x in vat:
            check_val_natural += x if x.isdigit() else char_to_int[x]
        if int(check_val_natural) % 97 == 1:
            return True

        # Company => weighted(9->2) mod11 on bsn
        vat = vat[:-3]
        if vat.isdigit() and checksum(vat) == 0:
            return True

        return False
Exemplo n.º 24
0
def validate(number):
    """Check if the number is a valid identity number."""
    number = compact(number)
    if len(number) not in (11, 13):
        raise InvalidLength()
    if number[-5] not in '-+':
        raise InvalidFormat()
    digits = clean(number, '-+')
    if not isdigits(digits):
        raise InvalidFormat()
    get_birth_date(number)
    luhn.validate(digits[-10:])
    return number
Exemplo n.º 25
0
    def check_vat_ec(self, vat):
        try:
            from stdnum.util import clean
            from stdnum.ec import ci, ruc
        except ImportError:
            return True

        if self.l10n_latam_identification_type_id.is_vat:
            vat = clean(vat, ' -.').upper().strip()
            if self.l10n_latam_identification_type_id.name == 'Ced':
                return ci.is_valid(vat)
            elif self.l10n_latam_identification_type_id.name == 'RUC' and vat != '9999999999999':
                return ruc.is_valid(vat)
        return True
Exemplo n.º 26
0
def compact(number):
    """Convert the number to the minimal representation. This strips the
    number of any valid separators and removes surrounding whitespace."""
    number = _cleanup_re.sub('/', clean(number).upper().strip())
    # remove optional slash between first letter and county digits
    if number[1:2] == '/':
        number = number[:1] + number[2:]
    # normalise county number to two digits
    if number[2:3] == '/':
        number = number[:1] + '0' + number[1:]
    # convert trailing full date to year only
    m = _onrc_fulldate_re.match(number)
    if m:
        number = ''.join(m.groups())
    return number
Exemplo n.º 27
0
def to_isbn13(number):
    """Convert the number to ISBN-13 format."""
    number = number.strip()
    min_number = clean(number, ' -')
    if len(min_number) == 13:
        return number  # nothing to do, already ISBN-13
    if len(min_number) == 9:
        number = '0' + number  # convert from 9 to 10 digits
    # put new check digit in place
    number = number[:-1] + ean.calc_check_digit('978' + min_number[:-1])
    # add prefix
    if ' ' in number:
        return '978 ' + number
    elif '-' in number:
        return '978-' + number
    else:
        return '978' + number
Exemplo n.º 28
0
def compact(number):
    """Convert the number to the minimal representation.

    This strips the number of any valid separators and removes surrounding
    whitespace. Also adds padding zeroes if necessary.
    """
    number = clean(number, ' ').upper().strip()
    parts = number.split('-')
    if len(parts) == 3:
        # Pad each group with zeroes
        parts[0] = '0' * (2 - len(parts[0])) + parts[0]
        parts[1] = '0' * (4 - len(parts[1])) + parts[1]
        parts[2] = '0' * (4 - len(parts[2])) + parts[2]
    number = ''.join(parts)
    if len(number) == 9:
        number = '0' + number  # Add leading zero
    return number
Exemplo n.º 29
0
def to_isbn13(number):
    """Convert the number to ISBN-13 format."""
    number = number.strip()
    min_number = clean(number, ' -')
    if len(min_number) == 13:
        return number  # nothing to do, already ISBN-13
    if len(min_number) == 9:
        number = '0' + number  # convert from 9 to 10 digits
    # put new check digit in place
    number = number[:-1] + ean.calc_check_digit('978' + min_number[:-1])
    # add prefix
    if ' ' in number:
        return '978 ' + number
    elif '-' in number:
        return '978-' + number
    else:
        return '978' + number
Exemplo n.º 30
0
def validate(number):
    """Checks to see if the number provided is a valid SSN. This checks
    the length, groups and formatting if it is present."""
    match = _ssn_re.search(clean(number, "").strip())
    if not match:
        raise InvalidFormat()
    area = match.group("area")
    group = match.group("group")
    serial = match.group("serial")
    # check for all-0 or some unused areas
    # (9xx also won't be issued which includes the advertising range)
    if area == "000" or area == "666" or area[0] == "9" or group == "00" or serial == "0000":
        raise InvalidComponent()
    # check blacklists
    if format(number) in _ssn_blacklist:
        raise InvalidComponent()
    return compact(number)
Exemplo n.º 31
0
def validate(number):
    """Check if the number is a valid SSN. This checks the length, groups and
    formatting if it is present."""
    match = _ssn_re.search(clean(number, '').strip())
    if not match:
        raise InvalidFormat()
    area = match.group('area')
    group = match.group('group')
    serial = match.group('serial')
    # check for all-0 or some unused areas
    # (9xx also won't be issued which includes the advertising range)
    if area == '000' or area == '666' or area[0] == '9' or \
       group == '00' or serial == '0000':
        raise InvalidComponent()
    # check blacklists
    if format(number) in _ssn_blacklist:
        raise InvalidComponent()
    return compact(number)
Exemplo n.º 32
0
def validate(number):
    """Checks to see if the number provided is a valid SSN. This checks
    the length, groups and formatting if it is present."""
    match = _ssn_re.search(clean(number, '').strip())
    if not match:
        raise InvalidFormat()
    area = match.group('area')
    group = match.group('group')
    serial = match.group('serial')
    # check for all-0 or some unused areas
    # (9xx also won't be issued which includes the advertising range)
    if area == '000' or area == '666' or area[0] == '9' or \
       group == '00' or serial == '0000':
        raise InvalidComponent()
    # check blacklists
    if format(number) in _ssn_blacklist:
        raise InvalidComponent()
    return compact(number)
Exemplo n.º 33
0
    def __init__(self, plmnid):

        plmnid = clean(plmnid, ' -').strip().upper()

        if not isdigits(plmnid):
            raise ValueError("Invalid PLMNID %s" % plmnid)

        if len(plmnid) not in (4, 5):
            raise ValueError("Invalid PLMNID length %s" % plmnid)

        if len(tuple(numdb.get('imsi').split(plmnid))) < 2:
            raise ValueError("Invalid PLMNID format %s" % plmnid)

        self.info = dict(plmnid=plmnid)

        mcc_info, mnc_info = numdb.get('imsi').info(plmnid)

        self.info['mcc'] = mcc_info[0]
        self.info.update(mcc_info[1])
        self.info['mnc'] = mnc_info[0]
        self.info.update(mnc_info[1])
Exemplo n.º 34
0
    def __init__(self, imsi):

        imsi = clean(imsi, ' -').strip().upper()

        if not isdigits(imsi):
            raise ValueError("Invalid IMSI %s" % imsi)

        if len(imsi) not in (14, 15):
            raise ValueError("Invalid IMSI length %s" % imsi)

        if len(tuple(numdb.get('imsi').split(imsi))) < 2:
            raise ValueError("Invalid IMSI length %s" % imsi)

        self.info = dict(imsi=imsi)

        mcc_info, mnc_info, msin_info = numdb.get('imsi').info(imsi)

        self.info['mcc'] = mcc_info[0]
        self.info.update(mcc_info[1])
        self.info['mnc'] = mnc_info[0]
        self.info.update(mnc_info[1])
        self.info['msin'] = msin_info[0]
        self.info.update(msin_info[1])
Exemplo n.º 35
0
def compact(number):
    """Convert the HETU to the minimal representation. This strips
    surrounding whitespace and converts it to upper case."""
    return clean(number, '').upper().strip()
Exemplo n.º 36
0
def _cleanup(number):
    """Remove any grouping information from the number and removes surrounding
    whitespace."""
    return clean(str(number), " -").strip().upper()
Exemplo n.º 37
0
 def check_vat_ec(self, vat):
     vat = clean(vat, ' -.').upper().strip()
     return self.is_valid_ruc_ec(vat)
Exemplo n.º 38
0
def compact(number):
    """Convert the ISIL to the minimal representation. This strips
    surrounding whitespace."""
    return clean(number, '').strip()
Exemplo n.º 39
0
def compact(number):
    """Convert the Veronumero to the minimal representation. This strips
    surrounding whitespace and removes separators."""
    return clean(number, ' ').strip()
Exemplo n.º 40
0
def _cleanup(number):
    """Remove any grouping information from the number and removes surrounding
    whitespace."""
    return clean(number, ' -').strip().upper()
Exemplo n.º 41
0
def compact(number):
    """Convert the number to the minimal representation. This strips
    surrounding whitespace and separation dash."""
    return clean(number, ' ').upper().strip()
Exemplo n.º 42
0
def compact(number):
    """Convert the number to the minimal representation."""
    number = clean(number, ' ').strip()
    if '-' not in number:
        number = '-'.join((number[:-3], number[-3:-1], number[-1:]))
    return number
Exemplo n.º 43
0
def compact(number):
    """Convert the number to the minimal representation."""
    return clean(number, ' ').upper().strip()
Exemplo n.º 44
0
def compact(number):
    """Convert the number to the minimal representation. This strips the
    number of any surrounding whitespace."""
    return clean(number, ' -').strip().upper()
Exemplo n.º 45
0
def compact(number):
    """Convert the ISIL to the minimal representation. This strips
    surrounding whitespace."""
    return clean(number, '').strip()
Exemplo n.º 46
0
def compact(number):
    """Convert the AT-02 number to the minimal representation. This strips
    the number of any valid separators and removes invalid characters."""
    return clean(number, ' -/?:().m\'+"').strip().upper()
Exemplo n.º 47
0
def compact(number):
    """Convert the number to the minimal representation. This strips the
    number of any valid separators and removes surrounding whitespace."""
    number = clean(number, ' -.').strip()
    # pad with leading zeroes
    return (9 - len(number)) * '0' + number
Exemplo n.º 48
0
def compact(number):
    """Convert the number to the minimal representation. This strips the
    number of any surrounding whitespace."""
    number = clean(number).strip()
    return number
Exemplo n.º 49
0
def compact(number):
    """Convert the kennitala to the minimal representation. This
    strips surrounding whitespace and separation dash, and converts it
    to upper case."""
    return clean(number, '-').upper().strip()
Exemplo n.º 50
0
def compact(number):
    """Convert the iban number to the minimal representation. This strips the
    number of any valid separators and removes surrounding whitespace."""
    return clean(number, ' -.').strip().upper()
Exemplo n.º 51
0
def compact(number):
    """Convert the number to the minimal representation. This strips the
    number of any valid separators and removes surrounding whitespace."""
    number = clean(number, ' -').strip()
    # pad with leading zeroes
    return (9 - len(number)) * '0' + number
Exemplo n.º 52
0
def compact(number):
    """Convert the number to the minimal representation."""
    number = clean(number, ' ').strip()
    if '-' not in number:
        number = '-'.join((number[:-3], number[-3:-1], number[-1:]))
    return number
Exemplo n.º 53
0
def compact(number):
    """Convert the number to the minimal representation. This strips the
    number of any valid separators and removes surrounding whitespace."""
    return clean(number, '-').strip()
Exemplo n.º 54
0
def compact(number):
    """Convert the HETU to the minimal representation. This strips
    surrounding whitespace and converts it to upper case."""
    return clean(number, '').upper().strip()
Exemplo n.º 55
0
def compact(number):
    """Convert the number to the minimal representation."""
    number = clean(number, ' -').upper().strip()
    if number.startswith('NZ'):
        return number[2:]
    return number
Exemplo n.º 56
0
def compact(number):
    """Convert the AT-02 number to the minimal representation. This strips the
    number of any valid separators and removes invalid characters."""
    return clean(number, ' -/?:().m\'+"').strip().upper()
def compact(number):
    """Convert the number to the minimal representation."""
    number = clean(number).strip()
    module = _get_cc_module(number[:2])
    return number[:2] + module.compact(number[2:])
Exemplo n.º 58
0
def compact(number):
    """Convert the MAC address to the minimal, consistent representation."""
    number = clean(number, ' ').strip().lower().replace('-', ':')
    # zero-pad single-digit elements
    return ':'.join('0' + n if len(n) == 1 else n for n in number.split(':'))