Subversion Repositories SmartDukaan

Rev

Rev 5401 | Details | Compare with Previous | 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):
5761 amar.kumar 11
        self.watchlistFilePath = '/usr/price-comp-dashboard/watchlist.json'
5291 varun.gupt 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')
5401 varun.gupt 27
            jsonStr = f.read()
28
            try:
29
                watchlist = json.loads(jsonStr)
30
            except ValueError as e:
31
                print e
32
                watchlist = []
5291 varun.gupt 33
            f.close()
34
        except TypeError as e:
35
            print 'TypeError: ', e
36
            watchlist = []
37
 
38
        print 'Watchlist: ', watchlist
39
 
40
        if entity not in watchlist:
41
            watchlist.append(entity)
42
 
43
            f = open(self.watchlistFilePath, 'w')
44
            json.dump(watchlist, f, indent = 4)
45
            f.close()
46
        print 'New Watchlist: ', watchlist
47
 
48
    def remove(self, entity):
49
        try:
50
            f = open(self.watchlistFilePath, 'r')
51
            jsonStr = f.read() 
52
            watchlist = json.loads(jsonStr)
53
            f.close()
54
        except TypeError as e:
55
            watchlist = []
56
 
57
        if entity in watchlist:
58
            watchlist.remove(entity)
59
 
60
            f = open(self.watchlistFilePath, 'w')
61
            json.dump(watchlist, f, indent = 4)
62
            f.close()
63
        print 'New Watchlist: ', watchlist