| 94 |
ashish |
1 |
'''
|
|
|
2 |
Created on 25-Mar-2010
|
|
|
3 |
|
|
|
4 |
@author: ashish
|
|
|
5 |
'''
|
|
|
6 |
import urlparse
|
|
|
7 |
from shop2020.thriftpy.config.ttypes import ConfigException
|
|
|
8 |
from thrift.transport import TSocket
|
|
|
9 |
from thrift.transport.TTransport import TFramedTransport
|
|
|
10 |
from thrift.protocol.TBinaryProtocol import TBinaryProtocol
|
|
|
11 |
from shop2020.thriftpy.config import Configuration
|
|
|
12 |
|
|
|
13 |
class ConfigClient:
|
|
|
14 |
file_path = '/tmp/config.properties'
|
|
|
15 |
|
|
|
16 |
def __init__(self):
|
|
|
17 |
file = open(self.file_path)
|
|
|
18 |
host = 'localhost'
|
|
|
19 |
port = 9999
|
|
|
20 |
|
|
|
21 |
if not file:
|
|
|
22 |
self.host = 'localhost'
|
|
|
23 |
self.port = 9999
|
|
|
24 |
else:
|
|
|
25 |
url = file.readline()
|
|
|
26 |
file.close()
|
|
|
27 |
if not url:
|
|
|
28 |
self.host = 'localhost'
|
|
|
29 |
self.port = 9999
|
|
|
30 |
|
|
|
31 |
else :
|
|
|
32 |
parsed_url = urlparse.urlparse(url)
|
|
|
33 |
if parsed_url.hostname:
|
|
|
34 |
self.host = parsed_url.hostname
|
|
|
35 |
if parsed_url.port:
|
|
|
36 |
self.port = parsed_url.port
|
|
|
37 |
self.start_client()
|
|
|
38 |
|
|
|
39 |
|
|
|
40 |
def start_client(self):
|
|
|
41 |
try:
|
|
|
42 |
self.transport = TSocket.TSocket(self.host, self.port)
|
|
|
43 |
self.transport = TFramedTransport(self.transport)
|
|
|
44 |
self.protocol = TBinaryProtocol(self.transport)
|
|
|
45 |
self.client = Configuration.Client(self.protocol)
|
|
|
46 |
self.transport.open()
|
|
|
47 |
except:
|
|
|
48 |
raise ConfigException(101, 'unable to load the client')
|
|
|
49 |
|
|
|
50 |
def get_property(self, key):
|
|
|
51 |
return self.client.getPropetry(key)
|
|
|
52 |
|
|
|
53 |
def set_property(self, key, value):
|
|
|
54 |
return self.client.loadProperty(key, value)
|
|
|
55 |
|
|
|
56 |
|
|
|
57 |
|
|
|
58 |
if __name__ == '__main__':
|
|
|
59 |
#test config client
|
|
|
60 |
try:
|
|
|
61 |
config_client = ConfigClient()
|
|
|
62 |
print config_client.get_property("errorcode_0_msg")
|
|
|
63 |
config_client.set_property('errorcode_0_msg', 'abc')
|
|
|
64 |
print config_client.get_property('errorcode_0_msg')
|
|
|
65 |
except:
|
|
|
66 |
print "error while putting up config client"
|
|
|
67 |
|