Subversion Repositories SmartDukaan

Rev

Rev 5401 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5291 varun.gupt 1
'''
2
Created on 24-May-2012
3
 
4
@author: Varun Gupta
5
'''
6
import json
7
 
8
class WatchListManager:
9
 
10
    def __init__(self):
11
        self.watchlistFilePath = '/tmp/price-comp-dashboard/watchlist.json'
12
 
13
    def getWatchlist(self):
14
        try:
15
            f = open(self.watchlistFilePath, 'r')
16
            jsonStr = f.read() 
17
            watchlist = json.loads(jsonStr)
18
            f.close()
19
        except TypeError as e:
20
            watchlist = []
21
 
22
        return watchlist
23
 
24
    def save(self, entity):
25
        try:
26
            f = open(self.watchlistFilePath, 'r')
27
            jsonStr = f.read() 
28
            watchlist = json.loads(jsonStr)
29
            f.close()
30
        except TypeError as e:
31
            print 'TypeError: ', e
32
            watchlist = []
33
 
34
        print 'Watchlist: ', watchlist
35
 
36
        if entity not in watchlist:
37
            watchlist.append(entity)
38
 
39
            f = open(self.watchlistFilePath, 'w')
40
            json.dump(watchlist, f, indent = 4)
41
            f.close()
42
        print 'New Watchlist: ', watchlist
43
 
44
    def remove(self, entity):
45
        try:
46
            f = open(self.watchlistFilePath, 'r')
47
            jsonStr = f.read() 
48
            watchlist = json.loads(jsonStr)
49
            f.close()
50
        except TypeError as e:
51
            watchlist = []
52
 
53
        if entity in watchlist:
54
            watchlist.remove(entity)
55
 
56
            f = open(self.watchlistFilePath, 'w')
57
            json.dump(watchlist, f, indent = 4)
58
            f.close()
59
        print 'New Watchlist: ', watchlist