Subversion Repositories SmartDukaan

Rev

Rev 2926 | Go to most recent revision | Details | 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
 
19
    def __init__(self, path_to_cfg):
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()
30
        self.section_name = "Config"
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")