|
| 1 | +# __init__.py - functions for handling ISMNs |
| 2 | +# |
| 3 | +# Copyright (C) 2010 Arthur de Jong |
| 4 | +# |
| 5 | +# This library is free software; you can redistribute it and/or |
| 6 | +# modify it under the terms of the GNU Lesser General Public |
| 7 | +# License as published by the Free Software Foundation; either |
| 8 | +# version 2.1 of the License, or (at your option) any later version. |
| 9 | +# |
| 10 | +# This library is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | +# Lesser General Public License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU Lesser General Public |
| 16 | +# License along with this library; if not, write to the Free Software |
| 17 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 18 | +# 02110-1301 USA |
| 19 | + |
| 20 | +"""Module for handling ISMNs (International Standard Music Number). This |
| 21 | +module handles both numbers in the 10-digit 13-digit format. |
| 22 | +
|
| 23 | +>>> is_valid('979-0-3452-4680-5') |
| 24 | +True |
| 25 | +>>> is_valid('9790060115615') |
| 26 | +True |
| 27 | +>>> is_valid(' M-2306-7118-7') |
| 28 | +True |
| 29 | +>>> is_valid('9790060115614') # incorrect check digit |
| 30 | +False |
| 31 | +>>> compact(' 979-0-3452-4680-5') |
| 32 | +'9790345246805' |
| 33 | +>>> format('9790060115615') |
| 34 | +'979-0-060-11561-5' |
| 35 | +>>> format('M230671187') |
| 36 | +'979-0-2306-7118-7' |
| 37 | +>>> to_ismn13('M230671187') |
| 38 | +'9790230671187' |
| 39 | +""" |
| 40 | + |
| 41 | + |
| 42 | +def compact(number): |
| 43 | + """Convert the ISMN to the minimal representation. This strips the number |
| 44 | + of any valid ISMN separators and removes surrounding whitespace.""" |
| 45 | + number = number.replace(' '
10000
,'').replace('-','').replace('.','').strip().upper() |
| 46 | + return number |
| 47 | + |
| 48 | +def _calc_check_digit(number): |
| 49 | + """Calculate the ISMN check digit. The number passed should not have |
| 50 | + the check bit included and should be in the 13-digit form.""" |
| 51 | + return str((10 - sum( (2 * (i % 2) + 1) * int(number[i]) for i in range(len(number)))) % 10) |
| 52 | + |
| 53 | +def is_valid(number): |
| 54 | + """Checks to see if the number provided is a valid ISMN (either a legacy |
| 55 | + 10-digit one or a 13-digit one). This checks the length and the check |
| 56 | + bit but does not check if the publisher is valid.""" |
| 57 | + try: |
| 58 | + number = compact(number) |
| 59 | + except: |
| 60 | + return False |
| 61 | + if len(number) == 10 and number[0] == 'M' and number[1:].isdigit(): |
| 62 | + if _calc_check_digit('9790' + number[1:-1]) == number[-1]: |
| 63 | + return True |
| 64 | + elif len(number) == 13 and number.isdigit(): |
| 65 | + if _calc_check_digit(number[:-1]) == number[-1]: |
| 66 | + return True |
| 67 | + return False |
| 68 | + |
| 69 | +def to_ismn13(number): |
| 70 | + """Convert the number to ISMN13 format.""" |
| 71 | + number = number.strip() |
| 72 | + min_number = compact(number) |
| 73 | + if len(min_number) == 13: |
| 74 | + return number # nothing to do, already 13 digit format |
| 75 | + # add prefix and strip the M |
| 76 | + if ' ' in number: |
| 77 | + return '979 0' + number[1:] |
| 78 | + elif '-' in number: |
| 79 | + return '979-0' + number[1:] |
| 80 | + else: |
| 81 | + return '9790' + number[1:] |
| 82 | + |
| 83 | +# these are the ranges allocated to publisher codes |
| 84 | +_ranges = ( (3, '000', '099'), (4, '1000', '3999'), (5, '40000', '69999'), |
| 85 | + (6, '700000', '899999'), (7, '9000000', '9999999') ) |
| 86 | + |
| 87 | +def split(number): |
| 88 | + """Split the specified ISMN into a bookland prefix (979), an ISMN |
| 89 | + prefix (0), a publisher element (3 to 7 digits), an item element (2 to |
| 90 | + 6 digits) and a check digit.""" |
| 91 | + # clean up number |
| 92 | + number = to_ismn13(compact(number)) |
| 93 | + # rind the correct range and split the number |
| 94 | + for length, low, high in _ranges: |
| 95 | + if low <= number[4:4+length] <= high: |
| 96 | + return number[:3], number[3], number[4:4+length], number[4+length:-1], number[-1] |
| 97 | + |
| 98 | +def format(number, separator='-'): |
| 99 | + """Reformat the passed number to the standard format with the |
| 100 | + prefixes, the publisher element, the item element and the check-digit |
| 101 | + separated by the specified separator. The number is converted to the |
| 102 | + 13-digit format silently.""" |
| 103 | + return separator.join(x for x in split(number) if x) |
0 commit comments