debug angepasst und konsequenter gemacht

This commit is contained in:
2013-01-14 22:23:48 +00:00
parent 3e40027923
commit 6c339db86b
3 changed files with 84 additions and 41 deletions

View File

@@ -31,14 +31,16 @@ class Debug(object):
self.verbose = verbose
# def setverbose
def addverbose(self):
"""docstring for setverbose"""
self.verbose += 1
# def addverbose
def debug(self, out, level=0):
"""docstring for debug"""
if self.verbose:
if level > 0:
print "*"*level,out
else:
print out
# if verbose
if self.verbose >= level:
print out
# if level
# def debug
# class Debug
@@ -89,9 +91,9 @@ def read_skip_comment(fp, commentstring):
def diff(destfile, tempfile, commentstring, debug):
"""diff destfile and tempfile, returns True if files differ, False if they are the same"""
debug.debug("Diffing %s and %s" % (destfile, tempfile))
debug.debug("Diffing %s and %s" % (destfile, tempfile), 3)
if not os.path.isfile(destfile):
debug.debug("Destfile %s does not exist, returning True." % destfile)
debug.debug("Destfile %s does not exist, returning True." % destfile, 3)
# destfile does not exist -> copy tempfile over
return True
# if not destfile
@@ -142,6 +144,7 @@ def userConfigGenerated(filename, cfg):
def backupFile(filename, debug):
"""make backup of filename, returns True if backup is successful, False else"""
if os.path.isfile(filename):
debug.debug("%s exists, finding backup name." % filename, 3)
backupname = filename+".userconfig."+time.strftime("%F")
testbackupname = backupname
counter = 0
@@ -149,9 +152,11 @@ def backupFile(filename, debug):
counter+=1
testbackupname=backupname+"."+str(counter)
# while
debug.debug("Renaming %s to %s" % (filename, testbackupname))
debug.debug("Renaming %s to %s" % (filename, testbackupname), 1)
os.rename(filename, testbackupname)
return True
else:
debug.debug("%s does not exist, do not need backup." % filename, 3)
# if filename
return False
# def backupFile
@@ -161,11 +166,14 @@ def copyFile(sourcefile, destfile, debug):
if os.path.isfile(sourcefile):
# sourcefile exists
debug.debug("Source file %s exists, proceeding with copy." % sourcefile, 3)
if not os.path.isfile(destfile) or os.access(destfile, os.W_OK):
debug.debug("Copying %s to %s" % (sourcefile, destfile))
debug.debug("Copying %s to %s" % (sourcefile, destfile), 1)
shutil.copy(sourcefile, destfile)
return True
# destfile is writable
else:
debug.debug("Destination file %s does not exist or is not writable." % destfile, 3)
# if write ok
return False
# def copyFile

View File

@@ -12,11 +12,13 @@ Copyright (c) 2013 __MyCompanyName__. All rights reserved.
"""
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
"""
@@ -25,7 +27,12 @@ class Conf(object):
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
"""
@@ -34,9 +41,28 @@ class Conf(object):
raise Exception('Cannot read config file ' + filename)
# if cannot read
self.cfgfile = filename
configdir=self.get("Main","configdir").replace("$HOME",os.environ['HOME'])
self.confobj.set("Main","configdir",configdir)
if self.debug:
self.debug.debug("Read config file %s" % filename, 2)
self.debug.debug("Replacing environment variables in %s." % filename, 3)
# if debug
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]))
# if debug
self.set(s, i, val.replace("$"+varname, os.environ[varname]))
# if has_key
# if tempre
# for i
# for s
# def setfilename
def get(self, section, option):