iro/telnumber.py
author Sandro Knauß <knauss@netzguerilla.net>
Fri, 30 Mar 2012 11:23:22 +0200
branchdevel
changeset 267 ef2df3f23cb1
parent 139 65117fd28400
child 291 84eb5a7a715a
permissions -rw-r--r--
adding docstring: iro

# -*- coding: utf-8 -*-
#Copyright (C) 2009  Sandro Knauß <bugs@sandroknauss.de>

#This program is free software; you can redistribute it and/or modify it under the terms
#of the GNU General Public License as published by the Free Software Foundation;
#either version 3 of the License, or any later version.
#This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
#without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#See the GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with this program; if not, see <http://www.gnu.org/licenses/>.

import re

from .error import InvalidTel

class Telnumber:
    """A telefonnumer, with splitted country part"""
    re_telnum=re.compile(r'^\s*(\+)?([0-9\s\-/\(\)]){5,}\s*$')
    """Regex for a complete telefon number"""

    re_land=re.compile(r'^\s*(\+|00)(?P<land>[1-9]{2})')
    """Regex for country part"""

    re_number=re.compile(r'[^0-9]')
    """Regex for numbers"""

    std_land="49"
    """Standard country part"""

    def __init__(self,number=None):
        """
        :param string number: a telefonnumber
        
        .. automethod:: __eq__
        .. automethod:: __neq__
        .. automethod:: __hash__
        .. automethod:: __str__
        .. automethod:: __repr__

        """
        self.land = None
        """Country part of a telefonnumber"""
        
        self.number = None
        """Localpart of the telefonnumber"""
        
        if not(number is  None):
            self.createNumber(number)

    def createNumber(self, number):
        """Split string into two parts: one country part and the rest.
        
        For a local number :attr:`std_land` will be used for country part.

        :param string number: a telefonnumber
        """
        if not self.re_telnum.match(number):
            raise InvalidTel(number)
        
        
        self.land=self.std_land

        land=self.re_land.match(number)
        
        if land:
            self.land=land.group("land")
            number=number[land.end("land"):]
            
        number=self.re_number.sub('',number)
        
        if number[0]=="0":
            number=number[1:]

        self.number = number
        
    def __eq__(self, y):
        """Return ``True``, if y hase the same telefonnumber"""
        return ((self.number == y.number) and ( self.land == y.land))
    
    def __neq__(self, y):
        """Return ``True``, if y is not equal (see :meth:`__eq__`)"""
        return not self.__eq__(y)

    def __hash__(self):
        """Return the Hash for telefonnumbers.

        Yust use the hash of the string representation of the Number
        """
        return str(self).__hash__()

    def __str__(self):
        """String representation of the telefonnumber.

        :return: the international telefonnumber with leading zeros
        """
        return "00%s%s"%(self.land,self.number)

    def __repr__(self):
        """debug representation of the class.
        
        :return: `<Telnumber 0012345667>`
        """
        return "<Telnumber %s>"%str(self)