Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/stdnum/isbn/__init__.py
blob: 9efc73a60ebcad23796e46ce6be8d8c802a76ea0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# __init__.py - functions for handling ISBNs
#
# Copyright (C) 2010 Arthur de Jong
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA

"""Module for handling ISBNs. This module handles both numbers in ISBN10
(10-digit) and ISB13 (13-digit) format.

>>> validate('978-9024538270')
True
>>> validate('978-9024538271') # incorrect check digit
False
>>> compact('1-85798-218-5')
'1857982185'
>>> format('9780471117094')
'978-0-471-11709-4'
>>> isbn_type('1-85798-218-5')
'ISBN10'
>>> isbn_type('978-0-471-11709-4')
'ISBN13'
>>> to_isbn13('1-85798-218-5')
'978-1-85798-218-3'
"""


def compact(number):
    """Convert the ISBN to the minimal representation. This strips the number
    of any valid ISBN separators and removes surrounding whitespace."""
    number = number.replace(' ','').replace('-','').strip().upper()
    if len(number) == 9:
        number = '0' + number
    return number

def _calc_isbn10_check_digit(number):
    """Calculate the ISBN check digit for 10-digit numbers. The number passed
    should not have the check bit included."""
    check = sum( (i + 1) * int(number[i]) for i in range(len(number)) ) % 11
    return 'X' if check == 10 else str(check)

def _calc_isbn13_check_digit(number):
    """Calculate the ISBN check digit for 13-digit numbers. The number passed
    should not have the check bit included."""
    return str((10 - sum( (2 * (i % 2) + 1) * int(number[i]) for i in range(len(number)))) % 10)

def isbn_type(number):
    """Check the passed number and returns 'ISBN13', 'ISBN10' or None (for
    invalid) for checking the type of number passed."""
    number = compact(number)
    if len(number) == 10:
        if not number[:-1].isdigit():
            return None
        if _calc_isbn10_check_digit(number[:-1]) != number[-1]:
            return None
        return 'ISBN10'
    elif len(number) == 13:
        if not number.isdigit():
            return None
        if _calc_isbn13_check_digit(number[:-1]) != number[-1]:
            return None
        return 'ISBN13'
    else:
        return None

def validate(number):
    """Checks to see if the number provided is a valid ISBN (either a legacy
    10-digit one or a 13-digit one). This checks the length and the check
    bit but does not check if the group and publisher are valid (use split()
    for that)."""
    return isbn_type(number) is not None

def to_isbn13(number):
    """Convert the number to ISBN13 format."""
    number = number.strip()
    min_number = compact(number)
    if len(min_number) == 13:
        return number # nothing to do, already ISBN13
    # put new check digit in place
    number = number[:-1] + _calc_isbn13_check_digit('978' + min_number[:-1])
    # add prefix
    if ' ' in number:
        return '978 ' + number
    elif '-' in number:
        return '978-' + number
    else:
        return '978' + number

def split(number):
    """Split the specified ISBN into an EAN.UCC prefix, a group prefix, a
    registrant, an item number and a check-digit. If the number is in ISNB10
    format the returned EAN.UCC prefix is '978'."""
    import ranges
    # clean up number
    number = compact(number)
    # get Bookland prefix if any
    if len(number) == 10:
        oprefix = ''
        prefix = '978'
    else:
        oprefix = prefix = number[:3]
        number = number[3:]
    # get group
    group, number = ranges.lookup(prefix, number)
    publisher, number = ranges.lookup('%s-%s' % (prefix, group), number)
    itemnr = number[:-1]
    check = number[-1]
    return ( oprefix, group, publisher, itemnr, check )

def format(number, separator='-'):
    """Reformat the passed number to the standard format with the EAN.UCC
    prefix (if any), the group prefix, the registrant, the item number and
    the check-digit separated (if possible) by the specified separator.
    Passing an empty separator should equal compact() though this is less
    efficient."""
    return separator.join(x for x in split(number) if x)