Overview
Comment: | Default values for config file processing, now script doesn't brakes if some fields are not present. |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | master | trunk |
Files: | files | file ages | folders |
SHA3-256: |
fc934cead10eab40375c38fb668530b4 |
User & Date: | c.kworr@d4daf22a-8aaf-11de-a64d-234b64dd91b4 on 2009-10-13 13:20:08.000 |
Other Links: | branch diff | manifest | tags |
Context
2009-10-13
| ||
15:21 | Some code rearrengements for future alternate reactors support. check-in: b93dc49210 user: c.kworr@d4daf22a-8aaf-11de-a64d-234b64dd91b4 tags: master, trunk | |
13:20 | Default values for config file processing, now script doesn't brakes if some fields are not present. check-in: fc934cead1 user: c.kworr@d4daf22a-8aaf-11de-a64d-234b64dd91b4 tags: master, trunk | |
2009-10-12
| ||
19:43 | Simplied function calls and made them nested. check-in: c90fda69e1 user: c.kworr@d4daf22a-8aaf-11de-a64d-234b64dd91b4 tags: master, trunk | |
Changes
Modified squid-tagger.py
from [4873692447]
to [4e6c4ae05d].
︙ | ︙ | |||
107 108 109 110 111 112 113 114 | writeline(line) def writeline(string): log.info('sending: ' + string) sys.stdout.write(string) sys.stdout.flush() class Config: | > | > > > > > > > > > > > > > > > > > > > > > > > | 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | writeline(line) def writeline(string): log.info('sending: ' + string) sys.stdout.write(string) sys.stdout.flush() # this classes processes config file and substitutes default values class Config: __slots__ = frozenset(['_config', '_defaults', '_section']) _defaults = { 'log': { 'silent': 'no', }, 'database': { 'host': 'localhost', 'database': 'squidTag', },} # function to read in config file def __init__(self): parser = optparse.OptionParser() parser.add_option('-c', '--config', dest = 'config', help = 'config file location', metavar = 'FILE', default = '/usr/local/etc/squid-tagger.conf') (options, args) = parser.parse_args() if not os.access(options.config, os.R_OK): print("Can't read {}: exitting".format(options.config)) sys.exit(2) self._config = configparser.ConfigParser() self._config.readfp(open(options.config)) # function to select config file section or create one def section(self, section): if not self._config.has_section(section): self._config.add_section(section) self._section = section # function to get config parameter, if parameter doesn't exists the default # value or None is substituted def __getitem__(self, name): if not self._config.has_option(self._section, name): if self._default.has_key(self._section): if self._default[self._section].has_key(name): self._config.set(self._section, name, self._default[self._section][name]) else: self._config.set(self._section, name, None) else: self._config.set(self._section, name, None) return self._config.get(self._section, name) # initializing and reading in config file config = Config() log = Logger() db = tagDB() checker = CheckerThread(db,log) while True: line = sys.stdin.readline() if len(line) == 0: break checker.check(line) |