From 7df9852ddf77304dde98217c71910723602dbde1 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Fri, 26 Oct 2012 13:18:21 -0400 Subject: storage:util:upgrade: make yaml import optional (unless it isn't) Importing `yaml` may fail (if the user doesn't have PyYAML installed), but don't die until we need to use it. This way users without the old YAML formats on disk can run BE without installing PyYAML. --- libbe/storage/util/upgrade.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'libbe/storage/util/upgrade.py') diff --git a/libbe/storage/util/upgrade.py b/libbe/storage/util/upgrade.py index fdbc578..98091f2 100644 --- a/libbe/storage/util/upgrade.py +++ b/libbe/storage/util/upgrade.py @@ -27,7 +27,11 @@ import os, os.path import sys import types -import yaml +try: + import yaml +except ImportError as e: + yaml = None + _yaml_import_error = e import libbe import libbe.bug @@ -50,6 +54,8 @@ def generate_yaml_mapfile(map): >>> generate_yaml_mapfile({'q':u'hello'}) 'q: hello\\n\\n' """ + if yaml is None: + raise _yaml_import_error keys = map.keys() keys.sort() for key in keys: @@ -93,6 +99,8 @@ def parse_yaml_mapfile(contents): >>> dict['q'] u'Fran\\xe7ais' """ + if yaml is None: + raise _yaml_import_error c = yaml.safe_load(contents) if type(c) == types.StringType: raise mapfile.InvalidMapfileContents( -- cgit