Subversion Repositories SmartDukaan

Rev

Rev 5401 | Blame | Compare with Previous | Last modification | View Log | RSS feed

'''
Created on 24-May-2012

@author: Varun Gupta
'''
import json

class WatchListManager:

    def __init__(self):
        self.watchlistFilePath = '/usr/price-comp-dashboard/watchlist.json'
    
    def getWatchlist(self):
        try:
            f = open(self.watchlistFilePath, 'r')
            jsonStr = f.read() 
            watchlist = json.loads(jsonStr)
            f.close()
        except TypeError as e:
            watchlist = []
            
        return watchlist
    
    def save(self, entity):
        try:
            f = open(self.watchlistFilePath, 'r')
            jsonStr = f.read()
            try:
                watchlist = json.loads(jsonStr)
            except ValueError as e:
                print e
                watchlist = []
            f.close()
        except TypeError as e:
            print 'TypeError: ', e
            watchlist = []
        
        print 'Watchlist: ', watchlist
        
        if entity not in watchlist:
            watchlist.append(entity)
            
            f = open(self.watchlistFilePath, 'w')
            json.dump(watchlist, f, indent = 4)
            f.close()
        print 'New Watchlist: ', watchlist
    
    def remove(self, entity):
        try:
            f = open(self.watchlistFilePath, 'r')
            jsonStr = f.read() 
            watchlist = json.loads(jsonStr)
            f.close()
        except TypeError as e:
            watchlist = []
        
        if entity in watchlist:
            watchlist.remove(entity)
            
            f = open(self.watchlistFilePath, 'w')
            json.dump(watchlist, f, indent = 4)
            f.close()
        print 'New Watchlist: ', watchlist