1# isil.py - functions for handling identifiers for libraries and related 2# organizations 3# 4# Copyright (C) 2011-2017 Arthur de Jong 5# 6# This library is free software; you can redistribute it and/or 7# modify it under the terms of the GNU Lesser General Public 8# License as published by the Free Software Foundation; either 9# version 2.1 of the License, or (at your option) any later version. 10# 11# This library is distributed in the hope that it will be useful, 12# but WITHOUT ANY WARRANTY; without even the implied warranty of 13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14# Lesser General Public License for more details. 15# 16# You should have received a copy of the GNU Lesser General Public 17# License along with this library; if not, write to the Free Software 18# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19# 02110-1301 USA 20 21"""ISIL (International Standard Identifier for Libraries). 22 23The ISIL is the International Standard Identifier for Libraries and Related 24Organizations (ISO 15511) used to uniquely identify libraries, archives, 25museums, and similar organisations. 26 27The identifier can be up to 15 characters that may use digits, 28letters (case insensitive) hyphens, colons and slashes. The non-alphanumeric 29characters are part of the identifier and are not just for readability. 30 31The identifier consists of two parts separated by a hyphen. The first part is 32either a two-letter ISO 3166 country code or a (not two-letter) non-national 33prefix that identifies the agency that issued the ISIL. The second part is 34the is the identifier issued by that agency. 35 36Only the first part can be validated since it is registered globally. There 37may be some validation possible with the second parts (some agencies provide 38web services for validation) but there is no common format to these services. 39 40More information: 41 42* https://en.wikipedia.org/wiki/ISBT_128 43* http://biblstandard.dk/isil/ 44* https://www.iso.org/standard/57332.html 45 46>>> validate('IT-RM0267') 47'IT-RM0267' 48>>> validate('OCLC-DLC') 49'OCLC-DLC' 50>>> validate('WW-RM0267') # unregistered country code 51Traceback (most recent call last): 52 ... 53InvalidComponent: ... 54>>> format('it-RM0267') 55'IT-RM0267' 56""" 57 58from stdnum.exceptions import * 59from stdnum.util import clean 60 61 62# the valid characters in an ISIL 63_alphabet = set( 64 '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-:/') 65 66 67def compact(number): 68 """Convert the ISIL to the minimal representation. This strips 69 surrounding whitespace.""" 70 return clean(number, '').strip() 71 72 73def _is_known_agency(agency): 74 """Check whether the specified agency is valid.""" 75 # look it up in the db 76 from stdnum import numdb 77 results = numdb.get('isil').info(agency.upper() + '$') 78 # there should be only one part and it should have properties 79 return len(results) == 1 and bool(results[0][1]) 80 81 82def validate(number): 83 """Check if the number provided is a valid ISIL.""" 84 number = compact(number) 85 if not all(x in _alphabet for x in number): 86 raise InvalidFormat() 87 if len(number) > 15: 88 raise InvalidLength() 89 if not _is_known_agency(number.split('-')[0]): 90 raise InvalidComponent() 91 return number 92 93 94def is_valid(number): 95 """Check if the number provided is a valid ISIL.""" 96 try: 97 return bool(validate(number)) 98 except ValidationError: 99 return False 100 101 102def format(number): 103 """Reformat the number to the standard presentation format.""" 104 number = compact(number) 105 parts = number.split('-') 106 if len(parts) > 1 and _is_known_agency(parts[0]): 107 parts[0] = parts[0].upper() 108 return '-'.join(parts) 109