|
| 1 | +# imei.py - functions for handling International Mobile Equipment Identity |
| 2 | +# (IMEI) numbers |
| 3 | +# |
| 4 | +# Copyright (C) 2010 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 | +"""Module for handling IMEI (International Mobile Equipment Identity) |
| 22 | +numbers, used to identify mobile phones. |
| 23 | +
|
| 24 | +>>> is_valid('35686800-004141-20') |
| 25 | +True |
| 26 | +>>> is_valid('35-417803-685978-1') # incorrect check digit |
| 27 | +False |
| 28 | +>>> compact('35686800-004141-20') |
| 29 | +'3568680000414120' |
| 30 | +>>> format('354178036859789') |
| 31 | +'35-417803-685978-9' |
| 32 | +>>> imei_type('35686800-004141-20') |
| 33 | +'IMEISV' |
| 34 | +""" |
| 35 | + |
| 36 | + |
| 37 | +def compact(number): |
| 38 | + """Convert the IMEI number to the minimal representation. This strips the |
| 39 | + number of any valid separators and removes surrounding whitespace.""" |
| 40 | + number = number.replace(' ','').replace('-','').strip().upper() |
| 41 | + return number |
| 42 | + |
| 43 | +def imei_type(number): |
| 44 | + """Check the passed number and returns 'IMEI', 'IMEISV' or None (for |
| 45 | + invalid) for checking the type of number passed.""" |
| 46 | + try: |
| 47 | + number = compact(number) |
| 48 | + except: |
| 49 | + return None |
| 50 | + if len(number) == 14: # IMEI without check digit |
| 51 | + return 'IMEI' if number.isdigit() else None |
| 52 | + if len(number) == 15: # IMEI with check digit |
| 53 | + from stdnum import luhn |
| 54 | + return 'IMEI' if luhn.is_valid(number) else None |
| 55 | + elif len(number) == 16: |
| 56 | + return 'IMEISV' if number.isdigit() else None |
| 57 | + else: |
| 58 | + return None |
| 59 | + |
| 60 | +def is_valid(number): |
| 61 | + """Checks to see if the number provided is a valid IMEI (or IMEISV) |
| 62 | + number.""" |
| 63 | + return imei_type(number) is not None |
| 64 | + |
| 65 | +def format(number, separator='-'): |
| 66 | + """Reformat the passed number to the standard format.""" |
| 67 | + number = compact(number) |
| 68 | + number = ( number[:2], number[2:8], number[8:14], number[14:] ) |
| 69 | + return separator.join(x for x in number if x) |
0 commit comments