Preventing ConfigParser from swallowing file read IOErrors, so that the user gets a more useful message.

This commit is contained in:
Dan Helfman 2014-12-01 22:35:25 -08:00
parent d46e370950
commit 626dd66254
3 changed files with 16 additions and 6 deletions

View file

@ -7,7 +7,10 @@ from atticmatic.attic import create_archive, prune_archives
from atticmatic.config import parse_configuration from atticmatic.config import parse_configuration
def main(): def parse_arguments():
'''
Parse the command-line arguments from sys.argv and return them as an ArgumentParser instance.
'''
parser = ArgumentParser() parser = ArgumentParser()
parser.add_argument( parser.add_argument(
'--config', '--config',
@ -26,13 +29,17 @@ def main():
action='store_true', action='store_true',
help='Display verbose progress information', help='Display verbose progress information',
) )
args = parser.parse_args()
return parser.parse_args()
def main():
try: try:
args = parse_arguments()
location_config, retention_config = parse_configuration(args.config_filename) location_config, retention_config = parse_configuration(args.config_filename)
create_archive(args.excludes_filename, args.verbose, *location_config) create_archive(args.excludes_filename, args.verbose, *location_config)
prune_archives(location_config.repository, args.verbose, *retention_config) prune_archives(location_config.repository, args.verbose, *retention_config)
except (ValueError, CalledProcessError) as error: except (ValueError, IOError, CalledProcessError) as error:
print(error, file=sys.stderr) print(error, file=sys.stderr)
sys.exit(1) sys.exit(1)

View file

@ -23,10 +23,12 @@ RetentionConfig = namedtuple('RetentionConfig', CONFIG_FORMAT[CONFIG_SECTION_RET
def parse_configuration(config_filename): def parse_configuration(config_filename):
''' '''
Given a config filename of the expected format, return the parse configuration as a tuple of Given a config filename of the expected format, return the parse configuration as a tuple of
(LocationConfig, RetentionConfig). Raise if the format is not as expected. (LocationConfig, RetentionConfig).
Raise IOError if the file cannot be read, or ValueError if the format is not as expected.
''' '''
parser = ConfigParser() parser = ConfigParser()
parser.read((config_filename,)) parser.readfp(open(config_filename))
section_names = parser.sections() section_names = parser.sections()
expected_section_names = CONFIG_FORMAT.keys() expected_section_names = CONFIG_FORMAT.keys()

View file

@ -6,8 +6,9 @@ from atticmatic import config as module
def insert_mock_parser(section_names): def insert_mock_parser(section_names):
parser = flexmock() parser = flexmock()
parser.should_receive('read') parser.should_receive('readfp')
parser.should_receive('sections').and_return(section_names) parser.should_receive('sections').and_return(section_names)
flexmock(module).open = lambda filename: None
flexmock(module).ConfigParser = parser flexmock(module).ConfigParser = parser
return parser return parser