iro/telnumber.py
changeset 302 3f4bdea2abbf
parent 294 0e75bd39767d
equal deleted inserted replaced
90:eb04ac3a8327 302:3f4bdea2abbf
       
     1 # Copyright (c) 2012 netzguerilla.net <iro@netzguerilla.net>
       
     2 # 
       
     3 # This file is part of Iro.
       
     4 # 
       
     5 # Permission is hereby granted, free of charge, to any person obtaining a copy of
       
     6 # this software and associated documentation files (the "Software"), to deal in
       
     7 # the Software without restriction, including without limitation the rights to use,
       
     8 # copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
       
     9 # #Software, and to permit persons to whom the Software is furnished to do so,
       
    10 # subject to the following conditions:
       
    11 # 
       
    12 # The above copyright notice and this permission notice shall be included in
       
    13 # all copies or substantial portions of the Software.
       
    14 # 
       
    15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
       
    16 # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
       
    17 # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
       
    18 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
       
    19 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
       
    20 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
       
    21 
       
    22 # -*- coding: utf-8 -*-
       
    23 import re
       
    24 
       
    25 from .error import InvalidTel
       
    26 
       
    27 class Telnumber:
       
    28     """A telefonnumer, with splitted country part"""
       
    29     re_telnum=re.compile(r'^\s*(\+)?([0-9\s\-/\(\)]){5,}\s*$')
       
    30     """Regex for a complete telefon number"""
       
    31 
       
    32     re_land=re.compile(r'^\s*(\+|00)(?P<land>[1-9]{2})')
       
    33     """Regex for country part"""
       
    34 
       
    35     re_number=re.compile(r'[^0-9]')
       
    36     """Regex for numbers"""
       
    37 
       
    38     std_land="49"
       
    39     """Standard country part"""
       
    40 
       
    41     def __init__(self,number=None):
       
    42         """
       
    43         :param string number: a telefonnumber
       
    44         
       
    45         .. automethod:: __eq__
       
    46         .. automethod:: __neq__
       
    47         .. automethod:: __hash__
       
    48         .. automethod:: __str__
       
    49         .. automethod:: __repr__
       
    50 
       
    51         """
       
    52         self.land = None
       
    53         """Country part of a telefonnumber"""
       
    54         
       
    55         self.number = None
       
    56         """Localpart of the telefonnumber"""
       
    57         
       
    58         if not(number is  None):
       
    59             self.createNumber(number)
       
    60 
       
    61     def createNumber(self, number):
       
    62         """Split string into two parts: one country part and the rest.
       
    63         
       
    64         For a local number :attr:`std_land` will be used for country part.
       
    65 
       
    66         :param string number: a telefonnumber
       
    67         """
       
    68         if not self.re_telnum.match(number):
       
    69             raise InvalidTel(number)
       
    70         
       
    71         
       
    72         self.land=self.std_land
       
    73 
       
    74         land=self.re_land.match(number)
       
    75         
       
    76         if land:
       
    77             self.land=land.group("land")
       
    78             number=number[land.end("land"):]
       
    79             
       
    80         number=self.re_number.sub('',number)
       
    81         
       
    82         if number[0]=="0":
       
    83             number=number[1:]
       
    84 
       
    85         self.number = number
       
    86         
       
    87     def __eq__(self, y):
       
    88         """Return ``True``, if y hase the same telefonnumber"""
       
    89         return ((self.number == y.number) and ( self.land == y.land))
       
    90     
       
    91     def __neq__(self, y):
       
    92         """Return ``True``, if y is not equal (see :meth:`__eq__`)"""
       
    93         return not self.__eq__(y)
       
    94 
       
    95     def __hash__(self):
       
    96         """Return the Hash for telefonnumbers.
       
    97 
       
    98         Yust use the hash of the string representation of the Number
       
    99         """
       
   100         return str(self).__hash__()
       
   101 
       
   102     def __str__(self):
       
   103         """String representation of the telefonnumber.
       
   104 
       
   105         :return: the international telefonnumber with leading zeros
       
   106         """
       
   107         return "00%s%s"%(self.land,self.number)
       
   108 
       
   109     def __repr__(self):
       
   110         """debug representation of the class.
       
   111         
       
   112         :return: `<Telnumber 0012345667>`
       
   113         """
       
   114         return "<Telnumber %s>"%str(self)