31 | | |
| 31 | import ConfigParser |
| 32 | |
| 33 | class TeeError(Exception): |
| 34 | """Base exception for CupsOfTee related stuff.""" |
| 35 | def __init__(self, message = ""): |
| 36 | self.message = message |
| 37 | Exception.__init__(self, message) |
| 38 | def __repr__(self): |
| 39 | return self.message |
| 40 | __str__ = __repr__ |
| 41 | |
| 42 | class ConfigError(TeeError) : |
| 43 | """Configuration related exceptions.""" |
| 44 | pass |
| 45 | |
| 46 | class FakeConfig : |
| 47 | """Fakes a configuration file parser.""" |
| 48 | def get(self, section, option, raw=0) : |
| 49 | """Fakes the retrieval of a global option.""" |
| 50 | raise ConfigError, "Invalid configuration file : no option %s in section [%s]" % (option, section) |
| 51 | |
36 | | self.debug = 1 |
| 56 | confdir = os.environ.get("CUPS_SERVERROOT", ".") |
| 57 | self.conffile = os.path.join(confdir, "cupsoftee.conf") |
| 58 | if os.path.isfile(self.conffile) : |
| 59 | self.config = ConfigParser.ConfigParser() |
| 60 | self.config.read([self.conffile]) |
| 61 | self.debug = self.isTrue(getGlobalOption("debug", ignore=1)) |
| 62 | else : |
| 63 | self.config = FakeConfig() |
| 64 | self.debug = 1 # no config, so force debug mode ! |
| 65 | |
| 66 | def isTrue(self, option) : |
| 67 | """Returns 1 if option is set to true, else 0.""" |
| 68 | if (option is not None) and (option.upper().strip() in ['Y', 'YES', '1', 'ON', 'T', 'TRUE']) : |
| 69 | return 1 |
| 70 | else : |
| 71 | return 0 |
| 72 | |
| 73 | def getGlobalOption(self, option, ignore=0) : |
| 74 | """Returns an option from the global section, or raises a ConfigError if ignore is not set, else returns None.""" |
| 75 | try : |
| 76 | return self.config.get("global", option, raw=1) |
| 77 | except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) : |
| 78 | if ignore : |
| 79 | return |
| 80 | else : |
| 81 | raise ConfigError, "Option %s not found in section global of %s" % (option, self.conffile) |
| 82 | |
| 83 | def getPrinterOption(self, sectionname, option) : |
| 84 | """Returns an option from the printer section, or the global section, or raises a ConfigError.""" |
| 85 | globaloption = self.getGlobalOption(option, ignore=1) |
| 86 | try : |
| 87 | return self.config.get(sectionname, option, raw=1) |
| 88 | except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) : |
| 89 | if globaloption is not None : |
| 90 | return globaloption |
| 91 | else : |
| 92 | raise ConfigError, "Option %s not found in section [%s] of %s" % (option, sectionname, self.conffile) |