summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorTodd Gamblin <tgamblin@llnl.gov>2018-05-12 21:51:05 -0700
committerscheibelp <scheibel1@llnl.gov>2018-05-17 14:10:30 -0700
commit1fe5dbf338ed2d937a22cb32d75fb599dd00abf3 (patch)
treecc0eab5a095981d9e4170588516dd309daa90743 /lib
parent6f2cac9565cd50cded141533b1594faa02c4b2f6 (diff)
downloadspack-1fe5dbf338ed2d937a22cb32d75fb599dd00abf3.tar.gz
spack-1fe5dbf338ed2d937a22cb32d75fb599dd00abf3.tar.bz2
spack-1fe5dbf338ed2d937a22cb32d75fb599dd00abf3.tar.xz
spack-1fe5dbf338ed2d937a22cb32d75fb599dd00abf3.zip
init: only imoprt jsonschema if needed
- It turns out that jsonschema is one of the more expensive imports. - move imports of jsonschema into functions to avoid the performance hits for calls that don't need config.
Diffstat (limited to 'lib')
-rw-r--r--lib/spack/spack/config.py19
1 files changed, 10 insertions, 9 deletions
diff --git a/lib/spack/spack/config.py b/lib/spack/spack/config.py
index 5a551bdc42..149ed5088d 100644
--- a/lib/spack/spack/config.py
+++ b/lib/spack/spack/config.py
@@ -59,9 +59,7 @@ from six import string_types
from six import iteritems
import yaml
-import jsonschema
from yaml.error import MarkedYAMLError
-from jsonschema import Draft4Validator, validators
import llnl.util.tty as tty
from llnl.util.filesystem import mkdirp
@@ -72,7 +70,6 @@ import spack.schema
from spack.error import SpackError
from spack.util.ordereddict import OrderedDict
-
# Hacked yaml for configuration files preserves line numbers.
import spack.util.spack_yaml as syaml
@@ -132,6 +129,7 @@ def _extend_with_default(validator_class):
commented out.
"""
+ import jsonschema
validate_properties = validator_class.VALIDATORS["properties"]
validate_pattern_properties = validator_class.VALIDATORS[
"patternProperties"]
@@ -157,16 +155,12 @@ def _extend_with_default(validator_class):
validator, properties, instance, schema):
yield err
- return validators.extend(validator_class, {
+ return jsonschema.validators.extend(validator_class, {
"properties": set_defaults,
"patternProperties": set_pp_defaults
})
-#: the validator we use for Spack config files
-DefaultSettingValidator = _extend_with_default(Draft4Validator)
-
-
class ConfigScope(object):
"""This class represents a configuration scope.
@@ -192,6 +186,7 @@ class ConfigScope(object):
return self.sections[section]
def write_section(self, section):
+ import jsonschema
filename = self.get_section_filename(section)
data = self.get_section(section)
try:
@@ -545,8 +540,14 @@ def _validate_section(data, schema):
on Spack YAML structures.
"""
+ import jsonschema
+ if not hasattr(_validate_section, 'validator'):
+ DefaultSettingValidator = _extend_with_default(
+ jsonschema.Draft4Validator)
+ _validate_section.validator = DefaultSettingValidator
+
try:
- DefaultSettingValidator(schema).validate(data)
+ _validate_section.validator(schema).validate(data)
except jsonschema.ValidationError as e:
raise ConfigFormatError(e, data)