Subversion Repositories SmartDukaan

Rev

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

'''
Created on 23-Feb-2010

@author: ashish
'''
from shop2020.thriftpy.config.ttypes import ConfigException
import os
from ConfigParser import ConfigParser, NoSectionError, NoOptionError



class ConfigServerHandler:
    
    """
        Implements Configserver stub from thrift.
    """
    
    
    def __init__(self, path_to_cfg, environment):
        """
        Constructor takes file path info as parameter
        """
        if os.path.exists(path_to_cfg) == False:
            self.cfg_path = '/tmp/shop2020.cfg'
        else:
            self.cfg_path = path_to_cfg
        
        #now load the configuration 
        self.config_parser = ConfigParser()
        self.section_name = environment
        self.initialized = False;
        try:
            read_ok = self.config_parser.read([self.cfg_path])
        except Exception, e:
            print e;
        
        #check if files have been read properly
        for filename in read_ok:
            if filename != self.cfg_path:
                raise IOError("File could not be read")
            else:
                self.initialized = True
                
                
    def getPropetry(self, propertyName):
        """
        Parameters:
         - propertyName
        """
        if self.initialized:
            try:
                return self.config_parser.get(self.section_name, propertyName)
            except NoSectionError:
                raise ConfigException(402, "config file is missing section 'Config'")
            except NoOptionError:
                raise ConfigException(402, "config file is missing option " + propertyName)
        else:
            raise ConfigException(401, "The config server is not properly initialized. Please try again later")
        

    def loadProperty(self, propertyName, propertyValue):
        """
        Parameters:
         - propertyName
         - propertyValue
        """
        if self.initialized:
            try:
                if propertyName == "":
                    raise ConfigException(403, "property name is absent")
                self.config_parser.set(self.section_name, propertyName, propertyValue)
            except NoSectionError:
                raise ConfigException(402, "config file is missing section 'Config'")
        else:
            raise ConfigException(401, "The config server is not properly initialized. Please try again later")

    def reloadProperties(self):
        self.initialized = False;
        temp_config_parser = ConfigParser()
        try:
            read_ok = temp_config_parser.read([self.cfg_path])
        except Exception, e:
            print e;
        
        #check if files have been read properly
        for filename in read_ok:
            if filename != self.cfg_path:
                raise IOError("File could not be read")
            else:
                self.config_parser = temp_config_parser
                self.initialized = True