aboutsummaryrefslogtreecommitdiff
path: root/src/ppm/provider.py
blob: 484270b85d3ccbeafb24f88d1d46d1db3c2af94c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# vim: set fileencoding=utf-8 :
#
# (C) 2010 Guido Guenther <agx@sigxcpu.org>
#    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
#    (at your option) 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, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

import logging

class ProviderError(Exception):
    def __init__(self, msg):
        self.msg = msg


class Provider(object):
    """Keeps the information on howto top up the ballance and how to query the
    current ballance"""

    def __init__(self, country, name):
        self.country = country
        self.name = name
        self.fetch_balance_cmds = {}
        self.top_up_cmds = {}
        self.balance = None
        logging.debug("New provider: %s, %s", country, name)

    def add_fetch_balance_cmd(self, cmd):
        self.fetch_balance_cmds.update(cmd)
        logging.debug("Adding balance check %s" % cmd)

    def add_top_up_cmd(self, cmd):
        self.top_up_cmds.update(cmd)
        logging.debug("Adding top up %s" % cmd)

    def has_fetch_balance_cmd(self):
        # Only USSD for now
        if self.fetch_balance_cmds.has_key('ussd'):
            return True
        else:
            return False

    def has_top_up_cmd(self):
        # Only USSD for now
        if self.top_up_cmds.has_key('ussd'):
            return True
        else:
            return False

    def fetch_balance(self, mm, reply_func=None, error_func=None):
        if self.has_fetch_balance_cmd():
            mm.ussd_initiate (self.fetch_balance_cmds['ussd'],
                              reply_func=reply_func,
                              error_func=error_func)
            return True
        else:
            return False

    def top_up(self, mm, code, reply_func=None, error_func=None):
        if self.has_top_up_cmd():
            cmd = self.top_up_cmds['ussd'][0].replace(
                                                  self.top_up_cmds['ussd'][1],
                                                  code)
            logging.debug("Top up cmd: %s", cmd)
            mm.ussd_initiate (cmd, reply_func=reply_func, error_func=error_func)
            return True
        else:
            return False