Subversion Repositories SmartDukaan

Rev

Rev 94 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

'''
Created on 25-Mar-2010

@author: ashish
'''
import urlparse
from shop2020.thriftpy.config.ttypes import ConfigException
from thrift.transport import TSocket
from thrift.transport.TTransport import TFramedTransport
from thrift.protocol.TBinaryProtocol import TBinaryProtocol
from shop2020.thriftpy.config import Configuration

class ConfigClient:
    file_path = '/tmp/config.properties'
    
    def __init__(self):
        file = open(self.file_path)
        host = 'localhost'
        port = 9999
        
        if not file:
            self.host = 'localhost'
            self.port = 9999
        else:
            url = file.readline()
            file.close()
            if not url:
                self.host = 'localhost'
                self.port = 9999
                
            else :
                parsed_url = urlparse.urlparse(url)
                if parsed_url.hostname:
                    self.host = parsed_url.hostname
                if parsed_url.port:
                    self.port = parsed_url.port
        self.start_client()
    
    
    def start_client(self):
        try:
            self.transport = TSocket.TSocket(self.host, self.port)
            self.transport = TFramedTransport(self.transport)
            self.protocol = TBinaryProtocol(self.transport)
            self.client = Configuration.Client(self.protocol)
            self.transport.open()
        except:
            raise ConfigException(101, 'unable to load the client')
        
    def get_property(self, key):
        return self.client.getPropetry(key)
    
    def set_property(self, key, value):
        return self.client.loadProperty(key, value)
    
    def reloadProperties(self):
        return self.client.reloadProperties()
    
    
if __name__ == '__main__':
    #test config client 
    try:
        config_client = ConfigClient()
        print config_client.get_property("export_entities_path")
        config_client.set_property('export_entities_path', 'abc')
        print config_client.get_property("export_entities_path")
        config_client.reloadProperties()
        print config_client.get_property('export_entities_path')   
    except:
        print "error while putting up config client"