/usr/lib/python3/dist-packages/mkdocs/config/base.py is in mkdocs 0.16.3-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | from __future__ import unicode_literals
import logging
import os
from mkdocs import exceptions
from mkdocs import utils
log = logging.getLogger('mkdocs.config')
class ValidationError(Exception):
"""Raised during the validation process of the config on errors."""
class Config(utils.UserDict):
"""
MkDocs Configuration dict
This is a fairly simple extension of a standard dictionary. It adds methods
for running validation on the structure and contents.
"""
def __init__(self, schema):
"""
The schema is a Python dict which maps the config name to a validator.
"""
self._schema = schema
self._schema_keys = set(dict(schema).keys())
self.data = {}
self.user_configs = []
self.set_defaults()
def set_defaults(self):
"""
Set the base config by going through each validator and getting the
default if it has one.
"""
for key, config_option in self._schema:
self[key] = config_option.default
def _validate(self):
failed, warnings = [], []
for key, config_option in self._schema:
try:
value = self.get(key)
self[key] = config_option.validate(value)
warnings.extend([(key, w) for w in config_option.warnings])
config_option.reset_warnings()
except ValidationError as e:
failed.append((key, e))
for key in (set(self.keys()) - self._schema_keys):
warnings.append((
key, "Unrecognised configuration name: {0}".format(key)
))
return failed, warnings
def _pre_validate(self):
failed, warnings = [], []
for key, config_option in self._schema:
try:
config_option.pre_validation(self, key_name=key)
warnings.extend([(key, w) for w in config_option.warnings])
config_option.reset_warnings()
except ValidationError as e:
failed.append((key, e))
return failed, warnings
def _post_validate(self):
failed, warnings = [], []
for key, config_option in self._schema:
try:
config_option.post_validation(self, key_name=key)
warnings.extend([(key, w) for w in config_option.warnings])
config_option.reset_warnings()
except ValidationError as e:
failed.append((key, e))
return failed, warnings
def validate(self):
failed, warnings = self._pre_validate()
run_failed, run_warnings = self._validate()
failed.extend(run_failed)
warnings.extend(run_warnings)
# Only run the post validation steps if there are no failures, warnings
# are okay.
if len(failed) == 0:
post_failed, post_warnings = self._post_validate()
failed.extend(post_failed)
warnings.extend(post_warnings)
return failed, warnings
def load_dict(self, patch):
if not isinstance(patch, dict):
raise exceptions.ConfigurationError(
"The configuration is invalid. The expected type was a key "
"value mapping (a python dict) but we got an object of type: "
"{0}".format(type(patch)))
self.user_configs.append(patch)
self.data.update(patch)
def load_file(self, config_file):
return self.load_dict(utils.yaml_load(config_file))
def _open_config_file(config_file):
# Default to the standard config filename.
if config_file is None:
config_file = os.path.abspath('mkdocs.yml')
# If closed file descriptor, get file path to reopen later.
if hasattr(config_file, 'closed') and config_file.closed:
config_file = config_file.name
log.debug("Loading configuration file: {0}".format(config_file))
# If it is a string, we can assume it is a path and attempt to open it.
if isinstance(config_file, utils.string_types):
if os.path.exists(config_file):
config_file = open(config_file, 'rb')
else:
raise exceptions.ConfigurationError(
"Config file '{0}' does not exist.".format(config_file))
# Ensure file descriptor is at begining
config_file.seek(0)
return config_file
def load_config(config_file=None, **kwargs):
"""
Load the configuration for a given file object or name
The config_file can either be a file object, string or None. If it is None
the default `mkdocs.yml` filename will loaded.
Extra kwargs are passed to the configuration to replace any default values
unless they themselves are None.
"""
options = kwargs.copy()
# Filter None values from the options. This usually happens with optional
# parameters from Click.
for key, value in options.copy().items():
if value is None:
options.pop(key)
config_file = _open_config_file(config_file)
options['config_file_path'] = getattr(config_file, 'name', '')
# Initialise the config with the default schema .
from mkdocs import config
cfg = Config(schema=config.DEFAULT_SCHEMA)
# First load the config file
cfg.load_file(config_file)
# Then load the options to overwrite anything in the config.
cfg.load_dict(options)
errors, warnings = cfg.validate()
for config_name, warning in warnings:
log.warning("Config value: '%s'. Warning: %s", config_name, warning)
for config_name, error in errors:
log.error("Config value: '%s'. Error: %s", config_name, error)
for key, value in cfg.items():
log.debug("Config value: '%s' = %r", key, value)
if len(errors) > 0:
raise exceptions.ConfigurationError(
"Aborted with {0} Configuration Errors!".format(len(errors))
)
elif cfg['strict'] and len(warnings) > 0:
raise exceptions.ConfigurationError(
"Aborted with {0} Configuration Warnings in 'strict' mode!".format(len(warnings))
)
return cfg
|