示例#1
0
def config(filename='database.ini', section='postgresql'):
    # create a parser
    parser = ConfigParser()
    # read config file
    parser.read(filename)

    # get section, default to postgresql
    db = {}
    if parser.has_section(section):
        params = parser.items(section)
        for param in params:
            db[param[0]] = param[1]
    else:
        raise Exception('Section {0} not found in the {1} file'.format(
            section, filename))

    return db
示例#2
0
class GUIPrefsReader(object):
    '''
    Reader for GUI prefs. To save re inputting every time 
    '''

    PREFS_SEC = 'prefs'
    GUI_PREFS = '../conf/gui.prefs'

    def __init__(self):
        '''
        Constructor
        '''
        self.dvalue = None
        self.dselect = 'dest'
        #v:x111|MYGROUP, myconf.conf, 2193, 2013-01-01, 2013-01-02
        self.plist = ('lgvalue', 'uconf', 'epsg', 'fd', 'td')

        self.cp = ConfigParser()
        self.fn = os.path.join(os.path.dirname(__file__), self.GUI_PREFS)
        with codecs.open(self.fn, 'r', 'utf-8') as cf:
            self.cp.readfp(cf)

    def read(self):
        '''Read stored DS value and return this and its matching params'''
        try:
            with codecs.open(self.fn, 'r', 'utf-8') as cf:
                self.cp.readfp(cf)
            self.dvalue = self.cp.get(self.PREFS_SEC, self.dselect)
            if LU.assessNone(self.dvalue) is None:
                return (None, ) * (len(self.plist) + 1)
        except NoSectionError as nse:
            #if no sec init sec and opt and ret nones
            ldslog.warn('Error getting GUI prefs section :: ' + str(nse))
            if not self._initSection(self.PREFS_SEC):
                raise
            self._initOption(self.PREFS_SEC, self.dselect)
            return (None, ) * (len(self.plist) + 1)
        except NoOptionError as noe:
            #if no opt init opt and ret nones
            ldslog.warn('Error getting GUI prefs :: ' + str(noe))
            if not self._initOption(self.PREFS_SEC, self.dselect):
                raise
            return (None, ) * (len(self.plist) + 1)
        #if dval is okay ret it and res of a read of that sec
        return (self.dvalue, ) + self.readsec(self.dvalue)

    def readall(self):
        '''Reads entire grp into dict'''
        gpra = {}
        secs = self.cp.sections()
        secs.remove(self.PREFS_SEC)
        for sec in secs:
            gpra[sec] = self.readsec(sec)
        return gpra

    def getDestinations(self):
        return self.cp.sections()

    def readsec(self, section):
        #options per DS type
        rlist = ()

        for p in self.plist:
            try:
                rlist += (self.cp.get(section, p), )
            except NoSectionError as nse:
                #if not ds sec init sec then init opt
                ldslog.warn('Error getting GUI ' + section + ' :: ' + str(nse))
                if not self._initSection(section):
                    raise
                self._initOption(section, p)
                rlist += (None, )
            except NoOptionError as noe:
                #if no opt init the opt
                ldslog.warn('Error getting GUI ' + section + ' pref, ' + p +
                            ' :: ' + str(noe))
                if not self._initOption(section, p):
                    raise
                rlist += (None, )
        return rlist

    def writeline(self, field, value):
        #not the best solution since depends on a current gpr and a recent read/write.
        if self.dvalue:
            self.writesecline(self.dvalue, field, value)

    def writesecline(self, section, field, value):
        try:
            self.cp.set(section, field, value if LU.assessNone(value) else '')
            with codecs.open(self.fn, 'w', 'utf-8') as configfile:
                self.cp.write(configfile)
            ldslog.debug(str(section) + ':' + str(field) + '=' + str(value))
        except Exception as e:
            ldslog.warn('Problem writing GUI prefs. {} - sfv={}'.format(
                e, (section, field, value)))

    def write(self, rlist):
        self.dvalue = rlist[0]

        if self.cp.has_section(self.PREFS_SEC):
            self.cp.set(self.PREFS_SEC, self.dselect, self.dvalue)
        else:
            self.cp.add_section(self.PREFS_SEC)
            self.cp.set(self.PREFS_SEC, self.dselect, self.dvalue)

        for pr in zip(self.plist, rlist[1:]):
            if not self.cp.has_section(self.dvalue):
                self.cp.add_section(self.dvalue)
            try:
                if LU.assessNone(pr[1]):
                    self.cp.set(self.dvalue, pr[0], pr[1])
                    ldslog.debug(self.dvalue + ':' + pr[0] + '=' + pr[1])
            except Exception as e:
                ldslog.warn('Problem writing GUI prefs. ' + str(e))
        with codecs.open(self.fn, 'w', 'utf-8') as configfile:
            self.cp.write(configfile)

    def _initSection(self, section):
        checksec = LU.standardiseDriverNames(section)
        if checksec:
            self.cp.add_section(checksec)
            return True
        elif section == self.PREFS_SEC:
            self.cp.add_section(section)
            return True
        return False

    def _initOption(self, section, option):
        if option in self.plist + (self.dselect, ):
            self.writesecline(section, option, None)
            return True
        return False

    @classmethod
    def validate():
        '''Make sure a guipref file is valid, check pref points to alt least one valid DST'''
        filename = os.path.join(os.path.dirname(__file__),
                                GUIPrefsReader.GUI_PREFS)
        gp = GUIPrefsReader(filename)
        #validate UC check it has a valid dest named and configured
        p = gp.cp.get('prefs', 'dest')
        return gp.cp.has_section(p)
示例#3
0
class GUIPrefsReader(object):
    '''
    Reader for GUI prefs. To save re inputting every time 
    '''

    PREFS_SEC = 'prefs'
    GUI_PREFS = '../conf/gui.prefs'
    
    def __init__(self):
        '''
        Constructor
        '''
        self.dvalue = None
        self.dselect = 'dest'
        #v:x111|MYGROUP, myconf.conf, 2193, 2013-01-01, 2013-01-02
        self.plist = ('lgvalue','uconf','epsg','fd','td')
        
        self.cp = ConfigParser()
        self.fn = os.path.join(os.path.dirname(__file__),self.GUI_PREFS)
        with codecs.open(self.fn,'r','utf-8') as cf:
            self.cp.readfp(cf)
        
    def read(self):
        '''Read stored DS value and return this and its matching params'''
        try:
            with codecs.open(self.fn, 'r','utf-8') as cf:
                self.cp.readfp(cf)
            self.dvalue = self.cp.get(self.PREFS_SEC, self.dselect) 
            if LU.assessNone(self.dvalue) is None:
                return (None,)*(len(self.plist)+1)
        except NoSectionError as nse:
            #if no sec init sec and opt and ret nones
            ldslog.warn('Error getting GUI prefs section :: '+str(nse))
            if not self._initSection(self.PREFS_SEC):
                raise
            self._initOption(self.PREFS_SEC,self.dselect)
            return (None,)*(len(self.plist)+1)
        except NoOptionError as noe:
            #if no opt init opt and ret nones
            ldslog.warn('Error getting GUI prefs :: '+str(noe))
            if not self._initOption(self.PREFS_SEC,self.dselect):
                raise
            return (None,)*(len(self.plist)+1)
        #if dval is okay ret it and res of a read of that sec
        return (self.dvalue,)+self.readsec(self.dvalue)
    
    
    def readall(self):
        '''Reads entire grp into dict'''
        gpra = {}
        secs = self.cp.sections()
        secs.remove(self.PREFS_SEC)
        for sec in secs:
            gpra[sec] = self.readsec(sec)
        return gpra
        
    def getDestinations(self):
        return self.cp.sections()
    
    def readsec(self,section):
        #options per DS type
        rlist = ()
        
        for p in self.plist:
            try:
                rlist += (self.cp.get(section, p),)
            except NoSectionError as nse:
                #if not ds sec init sec then init opt
                ldslog.warn('Error getting GUI '+section+' :: '+str(nse))
                if not self._initSection(section):
                    raise
                self._initOption(section, p)
                rlist += (None,)
            except NoOptionError as noe:
                #if no opt init the opt
                ldslog.warn('Error getting GUI '+section+' pref, '+p+' :: '+str(noe))
                if not self._initOption(section,p):
                    raise
                rlist += (None,)
        return rlist
    
    def writeline(self,field,value):
        #not the best solution since depends on a current gpr and a recent read/write. 
        if self.dvalue:
            self.writesecline(self.dvalue,field,value)
        
    def writesecline(self,section,field,value):
        try:            
            self.cp.set(section,field,value if LU.assessNone(value) else '')
            with codecs.open(self.fn, 'w','utf-8') as configfile:
                self.cp.write(configfile)
            ldslog.debug(str(section)+':'+str(field)+'='+str(value))                                                                                        
        except Exception as e:
            ldslog.warn('Problem writing GUI prefs. {} - sfv={}'.format(e,(section,field,value)))
            
            
    def write(self,rlist):
        self.dvalue = rlist[0]
        
        if self.cp.has_section(self.PREFS_SEC):
            self.cp.set(self.PREFS_SEC,self.dselect,self.dvalue)
        else:
            self.cp.add_section(self.PREFS_SEC)
            self.cp.set(self.PREFS_SEC,self.dselect,self.dvalue)
            
        for pr in zip(self.plist,rlist[1:]):
            if not self.cp.has_section(self.dvalue):
                self.cp.add_section(self.dvalue)
            try:
                if LU.assessNone(pr[1]):         
                    self.cp.set(self.dvalue,pr[0],pr[1])
                    ldslog.debug(self.dvalue+':'+pr[0]+'='+pr[1])                                                                                        
            except Exception as e:
                ldslog.warn('Problem writing GUI prefs. '+str(e))
        with codecs.open(self.fn, 'w','utf-8') as configfile:
            self.cp.write(configfile)
                      
    
    def _initSection(self,section):
        checksec = LU.standardiseDriverNames(section)
        if checksec:
            self.cp.add_section(checksec)
            return True
        elif section == self.PREFS_SEC:
            self.cp.add_section(section)
            return True
        return False
            
    def _initOption(self,section,option):
        if option in self.plist+(self.dselect,):
            self.writesecline(section,option,None)
            return True
        return False
    
    @classmethod
    def validate():
        '''Make sure a guipref file is valid, check pref points to alt least one valid DST'''
        filename = os.path.join(os.path.dirname(__file__),GUIPrefsReader.GUI_PREFS)
        gp = GUIPrefsReader(filename)
        #validate UC check it has a valid dest named and configured
        p = gp.cp.get('prefs', 'dest')
        return gp.cp.has_section(p)