rewrite to python3
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
#!/usr/bin/env python
|
||||
#!/usr/bin/env python3
|
||||
# encoding: utf-8
|
||||
#
|
||||
# $Id$
|
||||
# $URL$
|
||||
|
||||
"""
|
||||
cfgfile.py
|
||||
@@ -10,87 +8,69 @@ cfgfile.py
|
||||
Created by Marcus Stoegbauer on 2013-01-12.
|
||||
Copyright (c) 2013 __MyCompanyName__. All rights reserved.
|
||||
"""
|
||||
import ConfigParser
|
||||
import configparser
|
||||
import os
|
||||
import re
|
||||
|
||||
|
||||
class Conf(object):
|
||||
confobj = ConfigParser.RawConfigParser()
|
||||
cfgfile = ''
|
||||
debug = None
|
||||
|
||||
def __init__(self, filename = None):
|
||||
"""if filename is set, open config file and initialize the ConfigParser
|
||||
"""
|
||||
self.confobj = ConfigParser.RawConfigParser()
|
||||
if filename:
|
||||
self.setfilename(filename)
|
||||
# if filename
|
||||
# def __init__
|
||||
|
||||
def setdebug(self, debug):
|
||||
"""docstring for setdebug"""
|
||||
self.debug = debug
|
||||
# def setdebug
|
||||
|
||||
def setfilename(self, filename):
|
||||
"""initialize the ConfigParser
|
||||
"""
|
||||
ret = self.confobj.read(filename)
|
||||
if len(ret) == 0 or ret[0] != filename:
|
||||
raise Exception('Cannot read config file ' + filename)
|
||||
# if cannot read
|
||||
self.cfgfile = filename
|
||||
if self.debug:
|
||||
self.debug.debug("Read config file %s" % filename, 2)
|
||||
self.debug.debug("Replacing environment variables in %s." % filename, 3)
|
||||
# if debug
|
||||
confobj = configparser.RawConfigParser()
|
||||
cfgfile = ''
|
||||
debug = None
|
||||
|
||||
for s in self.confobj.sections():
|
||||
for (i, val) in self.confobj.items(s):
|
||||
tempre = re.search("\$([A-Z]+)[^A-Z]*", val)
|
||||
if tempre:
|
||||
varname = tempre.group(1)
|
||||
if self.debug:
|
||||
self.debug.debug("Found variable %s in %s." % (varname, i), 3)
|
||||
# if debug
|
||||
if os.environ.has_key(varname):
|
||||
if self.debug:
|
||||
self.debug.debug("%s exists in environment, replacing with %s." % (varname, os.environ[varname]), 3)
|
||||
# if debug
|
||||
self.set(s, i, val.replace("$"+varname, os.environ[varname]))
|
||||
# if has_key
|
||||
# if tempre
|
||||
# for i
|
||||
# for s
|
||||
# def setfilename
|
||||
def __init__(self, filename=None):
|
||||
"""if filename is set, open config file and initialize the ConfigParser
|
||||
"""
|
||||
self.confobj = configparser.RawConfigParser()
|
||||
if filename:
|
||||
self.setfilename(filename)
|
||||
|
||||
def get(self, section, option):
|
||||
"""returns the value of option in section
|
||||
"""
|
||||
if not self.cfgfile:
|
||||
raise Exception('No config file set')
|
||||
# if not cfgfile
|
||||
return self.confobj.get(section, option)
|
||||
# def get
|
||||
def setdebug(self, debug):
|
||||
"""docstring for setdebug"""
|
||||
self.debug = debug
|
||||
|
||||
def set(self, section, option, value):
|
||||
"""docstring for update"""
|
||||
self.confobj.set(section, option, value)
|
||||
# def set
|
||||
def setfilename(self, filename):
|
||||
"""initialize the ConfigParser
|
||||
"""
|
||||
ret = self.confobj.read(filename)
|
||||
if len(ret) == 0 or ret[0] != filename:
|
||||
raise Exception('Cannot read config file ' + filename)
|
||||
self.cfgfile = filename
|
||||
if self.debug:
|
||||
self.debug.debug("Read config file %s" % filename, 2)
|
||||
self.debug.debug("Replacing environment variables in %s." % filename, 3)
|
||||
|
||||
for s in self.confobj.sections():
|
||||
for (i, val) in self.confobj.items(s):
|
||||
tempre = re.search(r"\$([A-Z]+)[^A-Z]*", val)
|
||||
if tempre:
|
||||
varname = tempre.group(1)
|
||||
if self.debug:
|
||||
self.debug.debug("Found variable %s in %s." % (varname, i), 3)
|
||||
if varname in os.environ:
|
||||
if self.debug:
|
||||
self.debug.debug("%s exists in environment, replacing with %s." %
|
||||
(varname, os.environ[varname]), 3)
|
||||
self.set(s, i, val.replace("$"+varname, os.environ[varname]))
|
||||
|
||||
def getitems(self, section):
|
||||
"""returns all items in section
|
||||
"""
|
||||
if not self.cfgfile:
|
||||
raise Exception('No config file set')
|
||||
# if not cfgfile
|
||||
return self.confobj.items(section)
|
||||
# def getitems
|
||||
def get(self, section, option):
|
||||
"""returns the value of option in section
|
||||
"""
|
||||
if not self.cfgfile:
|
||||
raise Exception('No config file set')
|
||||
return self.confobj.get(section, option)
|
||||
|
||||
def check(self, section, option):
|
||||
"""checks for option in section"""
|
||||
return self.confobj.has_option(section, option)
|
||||
# def check
|
||||
# class Conf
|
||||
def set(self, section, option, value):
|
||||
"""docstring for update"""
|
||||
self.confobj.set(section, option, value)
|
||||
|
||||
def getitems(self, section):
|
||||
"""returns all items in section
|
||||
"""
|
||||
if not self.cfgfile:
|
||||
raise Exception('No config file set')
|
||||
return self.confobj.items(section)
|
||||
|
||||
def check(self, section, option):
|
||||
"""checks for option in section"""
|
||||
return self.confobj.has_option(section, option)
|
||||
|
||||
Reference in New Issue
Block a user