Subversion Repositories SmartDukaan

Rev

Rev 3130 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
94 ashish 1
'''
2
Created on 23-Feb-2010
3
 
4
@author: ashish
5
'''
6
from shop2020.thriftpy.config.ttypes import ConfigException
7
import os
8
from ConfigParser import ConfigParser, NoSectionError, NoOptionError
9
 
10
 
11
 
12
class ConfigServerHandler:
13
 
14
    """
15
        Implements Configserver stub from thrift.
16
    """
17
 
18
 
3130 rajveer 19
    def __init__(self, path_to_cfg, environment):
94 ashish 20
        """
21
        Constructor takes file path info as parameter
22
        """
23
        if os.path.exists(path_to_cfg) == False:
24
            self.cfg_path = '/tmp/shop2020.cfg'
25
        else:
26
            self.cfg_path = path_to_cfg
27
 
28
        #now load the configuration 
29
        self.config_parser = ConfigParser()
3130 rajveer 30
        self.section_name = environment
94 ashish 31
        self.initialized = False;
32
        try:
33
            read_ok = self.config_parser.read([self.cfg_path])
34
        except Exception, e:
35
            print e;
36
 
37
        #check if files have been read properly
38
        for filename in read_ok:
39
            if filename != self.cfg_path:
40
                raise IOError("File could not be read")
41
            else:
42
                self.initialized = True
43
 
44
 
45
    def getPropetry(self, propertyName):
46
        """
47
        Parameters:
48
         - propertyName
49
        """
50
        if self.initialized:
51
            try:
52
                return self.config_parser.get(self.section_name, propertyName)
53
            except NoSectionError:
54
                raise ConfigException(402, "config file is missing section 'Config'")
55
            except NoOptionError:
56
                raise ConfigException(402, "config file is missing option " + propertyName)
57
        else:
58
            raise ConfigException(401, "The config server is not properly initialized. Please try again later")
59
 
60
 
61
    def loadProperty(self, propertyName, propertyValue):
62
        """
63
        Parameters:
64
         - propertyName
65
         - propertyValue
66
        """
67
        if self.initialized:
68
            try:
69
                if propertyName == "":
70
                    raise ConfigException(403, "property name is absent")
71
                self.config_parser.set(self.section_name, propertyName, propertyValue)
72
            except NoSectionError:
73
                raise ConfigException(402, "config file is missing section 'Config'")
74
        else:
75
            raise ConfigException(401, "The config server is not properly initialized. Please try again later")
2926 rajveer 76
 
77
    def reloadProperties(self):
78
        self.initialized = False;
3241 rajveer 79
        temp_config_parser = ConfigParser()
2926 rajveer 80
        try:
3241 rajveer 81
            read_ok = temp_config_parser.read([self.cfg_path])
2926 rajveer 82
        except Exception, e:
83
            print e;
84
 
85
        #check if files have been read properly
86
        for filename in read_ok:
87
            if filename != self.cfg_path:
88
                raise IOError("File could not be read")
89
            else:
3241 rajveer 90
                self.config_parser = temp_config_parser
91
                self.initialized = True