Skip to content

Commit 494147a

Browse files
committed
Use mkdocs.utils.load_config instead of ConfigData
1 parent 4578211 commit 494147a

File tree

12 files changed

+99
-69
lines changed

12 files changed

+99
-69
lines changed

mike/commands.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def _redirect_template(user_template=None):
2222

2323

2424
def _add_redirect_to_commit(commit, template, src, dst,
25-
use_directory_urls=True):
25+
use_directory_urls):
2626
if os.path.splitext(src)[1] == '.html':
2727
reldst = os.path.relpath(dst, os.path.dirname(src))
2828
href = '/'.join(reldst.split(os.path.sep))
@@ -81,15 +81,15 @@ def deploy(cfg, version, title=None, aliases=[], update_aliases=False,
8181
with git_utils.Commit(branch, message) as commit:
8282
commit.delete_files([version_str] + list(info.aliases))
8383

84-
for f in git_utils.walk_real_files(cfg.site_dir):
85-
canonical_file = f.copy(destdir, cfg.site_dir)
84+
for f in git_utils.walk_real_files(cfg['site_dir']):
85+
canonical_file = f.copy(destdir, cfg['site_dir'])
8686
commit.add_file(canonical_file)
8787
for d in alias_destdirs:
88-
alias_file = f.copy(d, cfg.site_dir)
88+
alias_file = f.copy(d, cfg['site_dir'])
8989
if redirect:
9090
_add_redirect_to_commit(
9191
commit, t, alias_file.path, canonical_file.path,
92-
cfg.use_directory_urls
92+
cfg['use_directory_urls']
9393
)
9494
else:
9595
commit.add_file(alias_file)
@@ -172,7 +172,7 @@ def alias(cfg, version, aliases, update_aliases=False, redirect=True,
172172
if redirect:
173173
_add_redirect_to_commit(
174174
commit, t, alias_file.path, canonical_file.path,
175-
cfg.use_directory_urls
175+
cfg['use_directory_urls']
176176
)
177177
else:
178178
commit.add_file(alias_file)

mike/driver.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,20 +84,20 @@ def add_git_arguments(parser, *, commit=True, prefix=True):
8484

8585
def load_mkdocs_config(args, strict=False):
8686
try:
87-
cfg = mkdocs_utils.ConfigData(args.config_file)
87+
cfg = mkdocs_utils.load_config(args.config_file)
8888
if args.branch is None:
89-
args.branch = cfg.remote_branch
89+
args.branch = cfg['remote_branch']
9090
if args.remote is None:
91-
args.remote = cfg.remote_name
91+
args.remote = cfg['remote_name']
9292
return cfg
93-
except OSError:
93+
except FileNotFoundError as e:
9494
if strict:
95-
raise RuntimeError('{!r} not found'.format(args.config_file))
95+
raise
9696
if args.branch is None or args.remote is None:
97-
raise RuntimeError((
98-
'{!r} not found; pass --config-file or set ' +
99-
'--remote/--branch explicitly'
100-
).format(args.config_file))
97+
raise FileNotFoundError(
98+
'{}; pass --config-file or set --remote/--branch explicitly'
99+
.format(str(e))
100+
)
101101

102102

103103
def check_remote_status(args, strict=False):

mike/mkdocs_plugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
try:
1111
from mkdocs.exceptions import PluginError
12-
except ImportError:
12+
except ImportError: # pragma: no cover
1313
PluginError = ValueError
1414

1515

mike/mkdocs_utils.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,34 @@
1+
import mkdocs.config
12
import os
23
import re
34
import subprocess
45
import yaml
6+
from collections.abc import Iterable
57
from contextlib import contextmanager
68
from tempfile import NamedTemporaryFile
79

810
docs_version_var = 'MIKE_DOCS_VERSION'
911

1012

11-
class ConfigData:
12-
def __init__(self, config_file):
13-
with open(config_file) as f:
14-
config = yaml.load(f, Loader=yaml.Loader)
15-
self.site_dir = os.path.join(os.path.dirname(config_file),
16-
config.get('site_dir', 'site'))
17-
self.remote_name = config.get('remote_name', 'origin')
18-
self.remote_branch = config.get('remote_branch', 'gh-pages')
19-
self.use_directory_urls = config.get('use_directory_urls', True)
13+
def _open_config(config_file=None):
14+
if config_file is None:
15+
config_file = ['mkdocs.yml', 'mkdocs.yaml']
16+
elif not isinstance(config_file, Iterable) or isinstance(config_file, str):
17+
config_file = [config_file]
18+
19+
exc = None
20+
for i in config_file:
21+
try:
22+
return open(i, 'rb')
23+
except FileNotFoundError as e:
24+
if not exc:
25+
exc = e
26+
raise exc
27+
28+
29+
def load_config(config_file=None, **kwargs):
30+
with _open_config(config_file) as f:
31+
return mkdocs.config.load_config(f, **kwargs)
2032

2133

2234
@contextmanager

test/data/basic_theme/mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
site_name: test
2+
site_url: https://example.invalid/
23
theme: mkdocs
34

45
nav:

test/data/mkdocs_plugin/mkdocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
site_name: test
2-
site_url: https://example.com/
2+
site_url: https://example.invalid/
33
theme:
44
name: mkdocs
55
plugins:

test/data/no_directory_urls/mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
site_name: test
2+
site_url: https://example.invalid/
23
theme: mkdocs
34
use_directory_urls: false
45

test/data/remote/mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
site_name: test
2+
site_url: https://example.invalid/
23
theme: mkdocs
34
remote_name: myremote
45
remote_branch: mybranch

test/data/site_dir/mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
site_name: test
2+
site_url: https://example.invalid/
23
theme: mkdocs
34
site_dir: built_docs
45

test/integration/test_serve.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
@unittest.skipIf(platform.system() == 'Windows',
1313
"SIGINT doesn't work on windows")
14-
class TestList(unittest.TestCase):
14+
class TestServe(unittest.TestCase):
1515
def setUp(self):
1616
self.stage = stage_dir('serve')
1717
git_init()

0 commit comments

Comments
 (0)