iro/telnumber.py
branchdevel
changeset 126 1ac2439a68b5
parent 0 a3b6e531f0d2
child 129 d6704178a18f
equal deleted inserted replaced
125:19b3f383c9ce 126:1ac2439a68b5
       
     1 # -*- coding: utf-8 -*-
       
     2 #Copyright (C) 2009  Sandro Knauß <bugs@sandroknauss.de>
       
     3 
       
     4 #This program is free software; you can redistribute it and/or modify it under the terms
       
     5 #of the GNU General Public License as published by the Free Software Foundation;
       
     6 #either version 3 of the License, or any later version.
       
     7 #This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
       
     8 #without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
       
     9 #See the GNU General Public License for more details.
       
    10 
       
    11 #You should have received a copy of the GNU General Public License
       
    12 #along with this program; if not, see <http://www.gnu.org/licenses/>.
       
    13 
       
    14 import re
       
    15 
       
    16 from .error import NotATelNumber
       
    17 
       
    18 class Telnumber:
       
    19     re_telnum=re.compile(r'^\s*(\+)?([0-9\s\-/\(\)]){5,}\s*$')
       
    20     re_land=re.compile(r'^\s*(\+|00)(?P<land>[1-9]{2})')
       
    21     re_number=re.compile(r'[^0-9]')
       
    22     std_land="49"
       
    23 
       
    24     def __init__(self,number=None):
       
    25         if not(number is  None):
       
    26             self.createNumber(number)
       
    27 
       
    28     def createNumber(self, number):
       
    29         
       
    30         if not self.re_telnum.match(number):
       
    31             raise NotATelNumber(number)
       
    32         
       
    33         
       
    34         self.land=self.std_land
       
    35         land=self.re_land.match(number)
       
    36         
       
    37         if land:
       
    38             self.land=land.group("land")
       
    39             number=number[land.end("land"):]
       
    40             
       
    41         number=self.re_number.sub('',number)
       
    42         
       
    43         if number[0]=="0":
       
    44             number=number[1:]
       
    45 
       
    46         self.number = number
       
    47         
       
    48     def __eq__(self, y):
       
    49         return ((self.number == y.number) and ( self.land == y.land))
       
    50     
       
    51     def __ne__(self, y):
       
    52         return not self.__eq__(y)