def test_get_theme_dir_importerror(self, mock_iter): theme = mock.Mock() theme.name = 'mkdocs2' theme.dist.name = 'mkdocs2' theme.load.side_effect = ImportError() mock_iter.return_value = [theme] with self.assertRaises(ImportError): utils.get_theme_dir(theme.name)
def _load_theme_config(self, name): """ Recursively load theme and any parent themes. """ theme_dir = utils.get_theme_dir(name) self.dirs.append(theme_dir) try: file_path = os.path.join(theme_dir, 'mkdocs_theme.yml') with open(file_path, 'rb') as f: theme_config = utils.yaml_load(f) except IOError as e: log.debug(e) raise ValidationError( "The theme '{0}' does not appear to have a configuration file. " "Please upgrade to a current version of the theme.".format(name) ) log.debug("Loaded theme configuration for '%s' from '%s': %s", name, file_path, theme_config) parent_theme = theme_config.pop('extends', None) if parent_theme: themes = utils.get_theme_names() if parent_theme not in themes: raise ValidationError( "The theme '{0}' inherits from '{1}', which does not appear to be installed. " "The available installed themes are: {2}".format(name, parent_theme, ', '.join(themes)) ) self._load_theme_config(parent_theme) self.static_templates.update(theme_config.pop('static_templates', [])) self._vars.update(theme_config)
def post_validation(self, config, key_name): theme_in_config = any(['theme' in c for c in config.user_configs]) package_dir = os.path.abspath( os.path.join(os.path.dirname(__file__), '..')) theme_dir = [ utils.get_theme_dir(config['theme']), ] config['mkdocs_templates'] = os.path.join(package_dir, 'templates') if config['theme_dir'] is not None: # If the user has given us a custom theme but not a # builtin theme name then we don't want to merge them. if not theme_in_config: theme_dir = [] theme_dir.insert(0, config['theme_dir']) config['theme_dir'] = theme_dir # Add the search assets to the theme_dir, this means that # they will then we copied into the output directory but can # be overwritten by themes if needed. search_assets = os.path.join(package_dir, 'assets', 'search') config['theme_dir'].append(search_assets)
def _load_theme_config(self, name): """ Recursively load theme and any parent themes. """ theme_dir = utils.get_theme_dir(name) self.dirs.append(theme_dir) try: file_path = os.path.join(theme_dir, 'mkdocs_theme.yml') with open(file_path, 'rb') as f: theme_config = utils.yaml_load(f) if theme_config is None: theme_config = {} except IOError as e: log.debug(e) raise ValidationError( "The theme '{0}' does not appear to have a configuration file. " "Please upgrade to a current version of the theme.".format(name) ) log.debug("Loaded theme configuration for '%s' from '%s': %s", name, file_path, theme_config) parent_theme = theme_config.pop('extends', None) if parent_theme: themes = utils.get_theme_names() if parent_theme not in themes: raise ValidationError( "The theme '{0}' inherits from '{1}', which does not appear to be installed. " "The available installed themes are: {2}".format(name, parent_theme, ', '.join(themes)) ) self._load_theme_config(parent_theme) self.static_templates.update(theme_config.pop('static_templates', [])) self._vars.update(theme_config)
def test_get_theme_dir(self, mock_iter): path = 'some/path' theme = mock.Mock() theme.name = 'mkdocs2' theme.dist.key = 'mkdocs2' theme.load().__file__ = os.path.join(path, '__init__.py') mock_iter.return_value = iter([theme]) self.assertEqual(utils.get_theme_dir(theme.name), os.path.abspath(path))
def test_get_theme_dir(self, mock_iter): path = 'some/path' theme = mock.Mock() theme.name = 'mkdocs2' theme.dist.key = 'mkdocs2' theme.load().__file__ = os.path.join(path, '__init__.py') mock_iter.return_value = iter([theme]) self.assertEqual(utils.get_theme_dir(theme.name), os.path.abspath(path))
def post_validation(self, config, key_name): theme_in_config = any(['theme' in c for c in config.user_configs]) package_dir = os.path.abspath( os.path.join(os.path.dirname(__file__), '..')) theme_dir = [utils.get_theme_dir(config['theme']), ] config['mkdocs_templates'] = os.path.join(package_dir, 'templates') if config['theme_dir'] is not None: # If the user has given us a custom theme but not a # builtin theme name then we don't want to merge them. if not theme_in_config: theme_dir = [] theme_dir.insert(0, config['theme_dir']) config['theme_dir'] = theme_dir # Add the search assets to the theme_dir, this means that # they will then we copied into the output directory but can # be overwritten by themes if needed. search_assets = os.path.join(package_dir, 'assets', 'search') config['theme_dir'].append(search_assets)
def test_get_theme_dir_keyerror(self): with self.assertRaises(KeyError): utils.get_theme_dir('nonexistanttheme')