import repository from arizona
[raven.git] / apps / gacks / gacksaccount.py
1 import fcntl
2 import os
3 import time
4
5 from gacksexcep import *
6 from gackshandle import *
7
8 class GacksAccount:
9     def __init__(self, name=None, kind=None, manager=None, dict=None):
10         self.id = None                  # unique id
11         self.name = name                # name of account holder (slicename, etc)
12         self.kind = kind
13         self.level = "default"          # "gold", "silver", etc
14         self.balance = 0.0
15         self.minBalance = 0             # expect this to always be 0
16         self.maxBalance = -1            # balance cannot exceed this value (-1 == no maximum limit)
17         self.manager = manager
18         self.multiplier = 1             # service level multiplier
19
20         self.goodStanding = 0           # for gold, "paperwork filed"; for
21                                         # others, also "paid up"
22         self.inRate = 0                 # units per hour
23         self.outRate = -1               # units per hour
24         self.lastInTime = time.time()   # timestamp of last input
25         self.lastOutTime = time.time()  # timestamp of last output
26         self.lastOutAmount = 0
27
28         if dict!=None:
29             self.load_from_dict(dict)
30
31         # so we can tell what is dirty...
32         self.lastLevel = None
33         self.lastBalance = None
34         self.lastMultiplier = None
35         self.lastGoodStanding = None
36         self.lastMinBalance = None
37         self.lastMaxBalance = None
38         self.lastInRate = None
39         self.lastLastInTime = None
40
41         self.created = False            # true if this is a blank account, freshly created
42
43     def is_dirty(self):
44         return (self.level != self.lastLevel) or \
45                (self.balance != self.lastBalance) or \
46                (self.minBalance != self.lastMinBalance) or \
47                (self.maxBalance != self.lastMaxBalance) or \
48                (self.inRate != self.lastInRate) or \
49                (self.lastInTime != self.lastLastInTime)
50
51     def get_balance(self):
52         return self.balance
53
54     def set_balance(self, x):
55         self.balance = x
56         if (self.minBalance>=0) and (self.balance < self.minBalance):
57             self.balance = self.minBalance
58         if (self.maxBalance>=0) and (self.balance > self.maxBalance):
59             self.balance = self.maxBalance
60
61     def get_standing(self):
62         return self.goodStanding
63
64     def set_standing(self, x):
65         self.goodStanding = x
66
67     def adjust_balance(self, delta):
68         self.set_balance(self.balance+delta)
69
70     def refresh(self):
71         self.manager.fill_account(acct=self)
72
73     def commit(self):
74         self.manager.write_account(acct=self)
75
76     def apply_inRate(self, tNow=None):
77         if self.inRate > 0:
78             if tNow==None:
79                 tNow = time.time()
80             delta = (tNow - self.lastInTime) / 3600.0        # number of hours elapsed
81             delta = delta * self.inRate                      # number of units collected over the time period
82             if (delta > 0.001):
83                 self.adjust_balance(delta)
84                 self.lastInTime = tNow
85
86     def dump(self):
87         print "id:", self.id, "name:", self.name, "kind:", self.kind, "level:", self.level
88         print "balance:", self.balance, "min:", self.minBalance, "max:", self.maxBalance
89         print "in: rate:", self.inRate, "lastTime:", self.lastInTime
90
91     def as_dict(self):
92         d = {"id": self.id,
93              "name": self.name,
94              "kind": self.kind,
95              "level": self.level,
96              "balance": self.balance,
97              "minBalance": self.minBalance,
98              "maxBalance": self.maxBalance,
99              "goodStanding": self.goodStanding,
100              "multiplier": self.multiplier,
101              "inRate": self.inRate,
102              "outRate": self.outRate}
103         return d
104
105     def load_from_dict(self, d):
106         self.id = d["id"]
107         self.name = d["name"]
108         self.kind = d["kind"]
109         self.level = d["level"]
110         self.balance = d["balance"]
111         self.minBalance = d["minBalance"]
112         self.maxBalance = d["maxBalance"]
113         self.goodStanding = d["goodStanding"]
114         self.multiplier = d["multiplier"]
115         self.inRate = d["inRate"]
116         self.outRate = d["outRate"]
117