Skip to content

Commit

Permalink
Improve error handling with theme discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
d0ugal committed May 21, 2015
1 parent 0f8c969 commit 17a2e6f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 8 deletions.
42 changes: 41 additions & 1 deletion mkdocs/tests/utils_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import os
import unittest

from mkdocs import nav, utils
import mock

from mkdocs import nav, utils, exceptions


class UtilsTests(unittest.TestCase):
Expand Down Expand Up @@ -112,6 +114,44 @@ def test_get_themes(self):
'spacelab', 'united', 'readable', 'simplex', 'mkdocs',
'cosmo', 'journal', 'cyborg', 'readthedocs', 'amelia']))

@mock.patch('pkg_resources.iter_entry_points', autospec=True)
def test_get_themes_warning(self, mock_iter):

theme1 = mock.Mock()
theme1.name = 'mkdocs2'
theme1.dist.key = 'mkdocs2'
theme1.load().__file__ = "some/path1"

theme2 = mock.Mock()
theme2.name = 'mkdocs2'
theme2.dist.key = 'mkdocs3'
theme2.load().__file__ = "some/path2"

mock_iter.return_value = iter([theme1, theme2])

self.assertEqual(
sorted(utils.get_theme_names()),
sorted(['mkdocs2', ]))

@mock.patch('pkg_resources.iter_entry_points', autospec=True)
@mock.patch('pkg_resources.get_entry_map', autospec=True)
def test_get_themes_error(self, mock_get, mock_iter):

theme1 = mock.Mock()
theme1.name = 'mkdocs'
theme1.dist.key = 'mkdocs'
theme1.load().__file__ = "some/path1"

theme2 = mock.Mock()
theme2.name = 'mkdocs'
theme2.dist.key = 'mkdocs2'
theme2.load().__file__ = "some/path2"

mock_iter.return_value = iter([theme1, theme2])
mock_get.return_value = {'mkdocs': theme1, }

self.assertRaises(exceptions.ConfigurationError, utils.get_theme_names)

def test_nest_paths(self):

j = os.path.join
Expand Down
30 changes: 23 additions & 7 deletions mkdocs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
and structure of the site and pages in the site.
"""

from pkg_resources import iter_entry_points
import logging
import os
import pkg_resources
import shutil

import markdown
import six

from mkdocs import toc
from mkdocs import toc, exceptions

log = logging.getLogger(__name__)


def reduce_list(data_set):
Expand Down Expand Up @@ -289,12 +292,25 @@ def get_themes():
"""Return a dict of theme names and their locations"""

themes = {}
builtins = pkg_resources.get_entry_map(dist='mkdocs', group='mkdocs.themes')

for theme in pkg_resources.iter_entry_points(group='mkdocs.themes'):

if theme.name in builtins and theme.dist.key != 'mkdocs':
raise exceptions.ConfigurationError(
"The theme {0} is a builtin theme but {1} provides a theme "
"with the same name".format(theme.name, theme.dist.key))

elif theme.name in themes:
multiple_packages = [themes[theme.name].dist.key, theme.dist.key]
log.warning("The theme %s is provided by the Python package "
"'%s'. The one in %s will be used.",
theme.name, ','.join(multiple_packages), theme.dist.key)

themes[theme.name] = theme

for theme in iter_entry_points(group='mkdocs.themes', name=None):
name = theme.name
theme = theme.load()
location = os.path.dirname(theme.__file__)
themes[name] = location
themes = dict((name, os.path.dirname(theme.load().__file__))
for name, theme in themes.items())

return themes

Expand Down

0 comments on commit 17a2e6f

Please sign in to comment.