/usr/lib/python2.7/dist-packages/Halberd/conflib.py is in python-halberd 0.2.4-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 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 | # -*- coding: iso-8859-1 -*-
"""Configuration file management module.
Halberd uses configuration files to store relevant information needed for
certain protocols (SSL) or modes of operation (proxy, distributed
client/server, etc.).
This module takes care of reading and writing configuration files.
@var default_proxy_port: Default TCP port to listen when acting as a proxy.
@type default_proxy_port: C{int}
"""
# Copyright (C) 2004, 2005, 2006, 2010 Juan M. Bello Rivas <jmbr@superadditive.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import os
import ConfigParser
default_proxy_port = 8080
default_conf = r"""
# ============================================================================
# halberd configuration file.
# ============================================================================
[proxy]
address:
port: 8080
[ssl]
keyfile:
certfile:
"""
class InvalidConfFile(Exception):
"""Invalid configuration file.
"""
class ConfReader:
"""Takes care of turning configuration files into meaningful information.
"""
def __init__(self):
self.__dict = {}
self.__conf = None
self.confparser = ConfigParser.SafeConfigParser()
def open(self, fname):
"""Opens the configuration file.
@param fname: Pathname to the configuration file.
@type fname: C{str}
@raise InvalidConfFile: In case the passed file is not a valid one.
"""
self.__conf = open(os.path.expanduser(fname), 'r')
try:
self.confparser.readfp(self.__conf, fname)
except ConfigParser.MissingSectionHeaderError, msg:
raise InvalidConfFile, msg
def close(self):
"""Release the configuration file's descriptor.
"""
if self.__conf:
self.__conf.close()
def _getAddr(self, sectname, default_port):
"""Read a network address from the given section.
"""
section = self.__dict[sectname]
addr = section.get('address', '')
try:
port = int(section.get('port', default_port))
except ValueError:
port = default_port
return (addr, port)
def parse(self):
"""Parses the configuration file.
"""
assert self.__conf, 'The configuration file is not open'
proxy_serv_addr = ()
# The orthodox way of doing this is via ConfigParser.get*() but those
# methods lack the convenience of dict.get. While another approach
# could be to subclass ConfigParser I think it's overkill for the
# current situation.
for section in self.confparser.sections():
sec = self.__dict.setdefault(section, {})
for name, value in self.confparser.items(section):
sec.setdefault(name, value)
if self.__dict.has_key('proxy'):
proxy_serv_addr = self._getAddr('proxy', default_proxy_port)
keyfile = self.__dict['ssl'].get('keyfile', None)
certfile = self.__dict['ssl'].get('certfile', None)
if keyfile == '':
keyfile = None
if certfile == '':
certfile = None
return proxy_serv_addr, keyfile, certfile
def writeDefault(self, conf_file):
"""Write a bare-bones configuration file
@param conf_file: Target file where the default conf. will be written.
@type conf_file: C{str}
"""
assert conf_file and isinstance(conf_file, basestring)
conf_fp = open(conf_file, 'w')
conf_fp.write(default_conf)
conf_fp.close()
def __del__(self):
self.close()
# vim: ts=4 sw=4 et
|