Changeset 3296

Show
Ignore:
Timestamp:
01/21/08 22:43:54 (16 years ago)
Author:
jerome
Message:

Introduces the 'config_charset' directive to specify the character
set to use when decoding the configuration files' contents.
All directives are now decoded to unicode strings for internal use.

Location:
pykota/trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/conf/pykota.conf.sample

    r3285 r3296  
    5050[global] 
    5151 
    52  
     52# This directive configures the charset to use when reading PyKota's  
     53# configuration files. All directives values will be decoded and 
     54# interpreted using the character set defined here. 
     55# This value can only be present in the [global] section. 
     56# 
     57# When not set, the default value 'UTF-8' is used. 
     58config_charset : UTF-8 
    5359 
    5460#################################################################### 
  • pykota/trunk/pykota/config.py

    r3288 r3296  
    4545        self.config = ConfigParser.ConfigParser() 
    4646        self.config.read([self.filename]) 
     47        try : 
     48            self.config_charset = self.config.get("global", \ 
     49                                                  "config_charset", \ 
     50                                                   raw=1) 
     51        except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) : 
     52            self.config_charset = "UTF-8" 
    4753             
    4854    def isTrue(self, option) :         
     
    6470        return [pname for pname in self.config.sections() if pname != "global"] 
    6571         
    66     def getGlobalOption(self, option, ignore=0) :     
     72    def getGlobalOption(self, option, ignore=False) :     
    6773        """Returns an option from the global section, or raises a PyKotaConfigError if ignore is not set, else returns None.""" 
    6874        try : 
    69             return self.config.get("global", option, raw=1) 
     75            return self.config.get("global", option, raw=1).decode(self.config_charset) 
    7076        except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) :     
    7177            if ignore : 
     
    7682    def getPrinterOption(self, printername, option) :     
    7783        """Returns an option from the printer section, or the global section, or raises a PyKotaConfigError.""" 
    78         globaloption = self.getGlobalOption(option, ignore=1) 
    79         try : 
    80             return self.config.get(printername, option, raw=1) 
     84        globaloption = self.getGlobalOption(option, ignore=True) 
     85        try : 
     86            return self.config.get(printername, option, raw=1).decode(self.config_charset) 
    8187        except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) :     
    8288            if globaloption is not None : 
     
    99105            for option in ["storageserver", "storagename", "storageuser"] : 
    100106                backendinfo[option] = self.getGlobalOption(option) 
    101             backendinfo["storageuserpw"] = self.getGlobalOption("storageuserpw", ignore=1)  # password is optional 
     107            backendinfo["storageuserpw"] = self.getGlobalOption("storageuserpw", ignore=True)  # password is optional 
    102108             
    103109        backendinfo["storageadmin"] = None 
     
    108114            if adminconf.sections() : # were we able to read the file ? 
    109115                try : 
    110                     backendinfo["storageadmin"] = adminconf.get("global", "storageadmin", raw=1) 
     116                    backendinfo["storageadmin"] = adminconf.get("global", "storageadmin", raw=1).decode(self.config_charset) 
    111117                except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) :     
    112118                    if not issqlite : 
    113119                        raise PyKotaConfigError, _("Option %s not found in section global of %s") % ("storageadmin", self.adminfilename) 
    114120                try : 
    115                     backendinfo["storageadminpw"] = adminconf.get("global", "storageadminpw", raw=1) 
     121                    backendinfo["storageadminpw"] = adminconf.get("global", "storageadminpw", raw=1).decode(self.config_charset) 
    116122                except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) :     
    117123                    pass # Password is optional 
     
    120126                # server directly and users to use the replicas transparently. 
    121127                try : 
    122                     backendinfo["storagebackend"] = adminconf.get("global", "storagebackend", raw=1) 
     128                    backendinfo["storagebackend"] = adminconf.get("global", "storagebackend", raw=1).decode(self.config_charset) 
    123129                except ConfigParser.NoOptionError : 
    124130                    pass 
    125131                try : 
    126                     backendinfo["storageserver"] = adminconf.get("global", "storageserver", raw=1) 
     132                    backendinfo["storageserver"] = adminconf.get("global", "storageserver", raw=1).decode(self.config_charset) 
    127133                except ConfigParser.NoOptionError : 
    128134                    pass 
    129135                try : 
    130                     backendinfo["storagename"] = adminconf.get("global", "storagename", raw=1) 
     136                    backendinfo["storagename"] = adminconf.get("global", "storagename", raw=1).decode(self.config_charset) 
    131137                except ConfigParser.NoOptionError : 
    132138                    pass 
     
    151157                 
    152158        # should we use TLS, by default (if unset) value is NO         
    153         ldapinfo["ldaptls"] = self.isTrue(self.getGlobalOption("ldaptls", ignore=1)) 
    154         ldapinfo["cacert"] = self.getGlobalOption("cacert", ignore=1) 
     159        ldapinfo["ldaptls"] = self.isTrue(self.getGlobalOption("ldaptls", ignore=True)) 
     160        ldapinfo["cacert"] = self.getGlobalOption("cacert", ignore=True) 
    155161        if ldapinfo["cacert"] : 
    156162            ldapinfo["cacert"] = ldapinfo["cacert"].strip() 
     
    173179    def getLogoURL(self) : 
    174180        """Returns the URL to use for the logo in the CGI scripts.""" 
    175         url = self.getGlobalOption("logourl", ignore=1) or \ 
     181        url = self.getGlobalOption("logourl", ignore=True) or \ 
    176182                   "http://www.pykota.com/pykota.png" 
    177183        return url.strip()            
     
    179185    def getLogoLink(self) : 
    180186        """Returns the URL to go to when the user clicks on the logo in the CGI scripts.""" 
    181         url = self.getGlobalOption("logolink", ignore=1) or \ 
     187        url = self.getGlobalOption("logolink", ignore=True) or \ 
    182188                   "http://www.pykota.com/" 
    183189        return url.strip()            
     
    496502    def getPrivacy(self) :         
    497503        """Returns True if privacy is activated, else False.""" 
    498         return self.isTrue(self.getGlobalOption("privacy", ignore=1)) 
     504        return self.isTrue(self.getGlobalOption("privacy", ignore=True)) 
    499505         
    500506    def getDebug(self) :           
    501507        """Returns True if debugging is activated, else False.""" 
    502         return self.isTrue(self.getGlobalOption("debug", ignore=1)) 
     508        return self.isTrue(self.getGlobalOption("debug", ignore=True)) 
    503509             
    504510    def getCaching(self) :           
    505511        """Returns True if database caching is enabled, else False.""" 
    506         return self.isTrue(self.getGlobalOption("storagecaching", ignore=1)) 
     512        return self.isTrue(self.getGlobalOption("storagecaching", ignore=True)) 
    507513             
    508514    def getLDAPCache(self) :           
    509515        """Returns True if low-level LDAP caching is enabled, else False.""" 
    510         return self.isTrue(self.getGlobalOption("ldapcache", ignore=1)) 
     516        return self.isTrue(self.getGlobalOption("ldapcache", ignore=True)) 
    511517             
    512518    def getDisableHistory(self) :           
    513519        """Returns True if we want to disable history, else False.""" 
    514         return self.isTrue(self.getGlobalOption("disablehistory", ignore=1)) 
     520        return self.isTrue(self.getGlobalOption("disablehistory", ignore=True)) 
    515521             
    516522    def getUserNameToLower(self) :           
    517523        """Deprecated.""" 
    518         return self.getGlobalOption("utolower", ignore=1) 
     524        return self.getGlobalOption("utolower", ignore=True) 
    519525         
    520526    def getUserNameCase(self) : 
     
    522528        validvalues = [ "upper", "lower", "native" ] 
    523529        try : 
    524             value = self.getGlobalOption("usernamecase", ignore=1).strip().lower() 
     530            value = self.getGlobalOption("usernamecase", ignore=True).strip().lower() 
    525531        except AttributeError :     
    526532            value = "native" 
     
    531537    def getRejectUnknown(self) :           
    532538        """Returns True if we want to reject the creation of unknown users or groups, else False.""" 
    533         return self.isTrue(self.getGlobalOption("reject_unknown", ignore=1)) 
     539        return self.isTrue(self.getGlobalOption("reject_unknown", ignore=True)) 
    534540         
    535541    def getPrinterKeepFiles(self, printername) :           
     
    645651    def getWinbindSeparator(self) :           
    646652        """Returns the winbind separator's value if it is set, else None.""" 
    647         return self.getGlobalOption("winbind_separator", ignore=1) 
     653        return self.getGlobalOption("winbind_separator", ignore=True) 
    648654 
    649655    def getAccountBanner(self, printername) : 
     
    721727        branchbasename = "coefficient_" 
    722728        try : 
    723             globalbranches = [ (k, self.config.get("global", k)) for k in self.config.options("global") if k.startswith(branchbasename) ] 
     729            globalbranches = [ (k, self.config.get("global", k).decode(self.config_charset)) for k in self.config.options("global") if k.startswith(branchbasename) ] 
    724730        except ConfigParser.NoSectionError, msg : 
    725731            raise PyKotaConfigError, "Invalid configuration file : %s" % msg 
    726732        try : 
    727             sectionbranches = [ (k, self.config.get(printername, k)) for k in self.config.options(printername) if k.startswith(branchbasename) ] 
     733            sectionbranches = [ (k, self.config.get(printername, k).decode(self.config_charset)) for k in self.config.options(printername) if k.startswith(branchbasename) ] 
    728734        except ConfigParser.NoSectionError, msg : 
    729735            sectionbranches = []