projects
/
sitarba.git
/ commitdiff
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
537b4b2
)
Better error handling
author
Stefan Huber <shuber2@gmx.at>
Mon, 14 May 2012 07:12:42 +0000
(09:12 +0200)
committer
Stefan Huber <shuber2@gmx.at>
Mon, 14 May 2012 07:12:42 +0000
(09:12 +0200)
shbackup
patch
|
blob
|
history
diff --git
a/shbackup
b/shbackup
index db2c66125b3f09514f21f809dbaf842e86270ddb..cbd5396cc897636c93a272c4df36caae195c4818 100755
(executable)
--- a/
shbackup
+++ b/
shbackup
@@
-52,9
+52,11
@@
class Backup:
class Config:
"""Encapsules the configuration for the backup program."""
class Config:
"""Encapsules the configuration for the backup program."""
- class ReadE
xception(Exception
):
+ class ReadE
rror(RuntimeError
):
"""An exception raised when reading configurations."""
"""An exception raised when reading configurations."""
- pass
+ def __init__(self, value):
+ self.value = value
+ self.message = value
class FileSet:
"""A fileset has a name and a list of directories."""
class FileSet:
"""A fileset has a name and a list of directories."""
@@
-92,20
+94,22
@@
class Config:
"""Read configuration from file"""
if not os.path.isfile(filename):
"""Read configuration from file"""
if not os.path.isfile(filename):
- raise Config.ReadE
xception("No
file '" + filename + "'.")
+ raise Config.ReadE
rror("Cannot read config
file '" + filename + "'.")
config = configparser.RawConfigParser()
config.read(filename)
for reqsec in ["destination"]:
if not config.has_section(reqsec):
config = configparser.RawConfigParser()
config.read(filename)
for reqsec in ["destination"]:
if not config.has_section(reqsec):
- raise Config.ReadE
xception
("Section '" + reqsec + "' is missing.")
+ raise Config.ReadE
rror
("Section '" + reqsec + "' is missing.")
self.directory = config.get("destination", "directory")
self.directory = config.get("destination", "directory")
+ if not os.path.isdir(self.directory):
+ raise Config.ReadError("Directory '{0}' does not exist.".format(self.directory))
self.format = config.get("destination", "format")
if not self.format in Config.formats:
self.format = config.get("destination", "format")
if not self.format in Config.formats:
- raise Config.ReadE
xception
("Invalid 'format' given.")
+ raise Config.ReadE
rror
("Invalid 'format' given.")
if config.has_section("history"):
if config.has_section("history"):
@@
-113,24
+117,27
@@
class Config:
if opt.startswith("keep"):
epoch = opt[4:]
if not epoch in RealEpoch.keys():
if opt.startswith("keep"):
epoch = opt[4:]
if not epoch in RealEpoch.keys():
- raise Config.ReadException("Invalid option 'keep" + epoch + "'.")
- self.epochkeeps[epoch] = int(config.getint("history", opt))
+ raise Config.ReadError("Invalid option 'keep" + epoch + "'.")
+ try:
+ self.epochkeeps[epoch] = int(config.getint("history", opt))
+ except ValueError:
+ raise Config.ReadError("Invalid integer given for '" + opt + "'.")
elif opt.startswith("mode"):
epoch = opt[4:]
if not epoch in RealEpoch.keys():
elif opt.startswith("mode"):
epoch = opt[4:]
if not epoch in RealEpoch.keys():
- raise Config.ReadE
xception
("Invalid option 'mode" + epoch + "'.")
+ raise Config.ReadE
rror
("Invalid option 'mode" + epoch + "'.")
self.epochmodes[epoch] = config.get("history", opt)
if not self.epochmodes[epoch] in Mode:
self.epochmodes[epoch] = config.get("history", opt)
if not self.epochmodes[epoch] in Mode:
- raise Config.ReadE
xception
("Invalid mode given.")
+ raise Config.ReadE
rror
("Invalid mode given.")
else:
else:
- raise Config.ReadE
xception
("Invalid option '" + opt + "'.")
+ raise Config.ReadE
rror
("Invalid option '" + opt + "'.")
if config.has_section("input"):
for opt in config.options("input"):
if opt.startswith("exclude"):
self.exclpatterns += [ config.get("input", opt) ]
else:
if config.has_section("input"):
for opt in config.options("input"):
if opt.startswith("exclude"):
self.exclpatterns += [ config.get("input", opt) ]
else:
- raise Config.ReadE
xception
("Invalid option '" + opt + "'.")
+ raise Config.ReadE
rror
("Invalid option '" + opt + "'.")
for sec in config.sections():
if sec in ["destination", "history", "input"]:
for sec in config.sections():
if sec in ["destination", "history", "input"]:
@@
-141,12
+148,12
@@
class Config:
for opt in config.options(sec):
if not opt.startswith("dir"):
for opt in config.options(sec):
if not opt.startswith("dir"):
- raise Config.ReadE
xception
("Unknown option '" + opt + "'.")
+ raise Config.ReadE
rror
("Unknown option '" + opt + "'.")
else:
dirs += [config.get(sec, opt)]
self.sets += [Config.FileSet(name, dirs)]
else:
else:
dirs += [config.get(sec, opt)]
self.sets += [Config.FileSet(name, dirs)]
else:
- raise Config.ReadE
xception
("Unknown section '" + sec + "'.")
+ raise Config.ReadE
rror
("Unknown section '" + sec + "'.")
# Compute checksum of config file
m = hashlib.sha1()
# Compute checksum of config file
m = hashlib.sha1()
@@
-428,11
+435,8
@@
if __name__ == "__main__":
if cmd == "prune":
man.prune()
if cmd == "prune":
man.prune()
- except Config.ReadException as e:
- print("Error reading config file: ", end="")
- for a in e.args:
- print(a, end=" ")
- print()
+ except (Config.ReadError, configparser.DuplicateOptionError) as e:
+ print("Error reading config file: " + e.message)