Make config_file_path default to empty string

This commit is contained in:
Oleh Prypin
2023-06-23 17:17:08 +02:00
parent 7b1bc92926
commit a59e280d80
6 changed files with 16 additions and 10 deletions

View File

@@ -16,7 +16,7 @@ def _get_language_of_translation_file(path: Path) -> str:
def on_page_markdown(markdown: str, page: Page, config: MkDocsConfig, **kwargs):
if page.file.src_uri == 'user-guide/choosing-your-theme.md':
here = Path(config.config_file_path or '').parent
here = Path(config.config_file_path).parent
def replacement(m: re.Match) -> str:
lines = []

View File

@@ -116,7 +116,7 @@ def gh_deploy(
if message is None:
message = default_message
sha = _get_current_sha(os.path.dirname(config.config_file_path or ''))
sha = _get_current_sha(os.path.dirname(config.config_file_path))
message = message.format(version=mkdocs.__version__, sha=sha)
log.info(

View File

@@ -100,7 +100,8 @@ def serve(
if livereload:
# Watch the documentation files, the config file and the theme files.
server.watch(config.docs_dir)
server.watch(config.config_file_path)
if config.config_file_path:
server.watch(config.config_file_path)
if watch_theme:
for d in config.theme.dirs:

View File

@@ -133,7 +133,7 @@ class Config(UserDict):
"""
_schema: PlainConfigSchema
config_file_path: str | None
config_file_path: str
def __init_subclass__(cls):
schema = dict(getattr(cls, '_schema', ()))
@@ -170,7 +170,7 @@ class Config(UserDict):
config_file_path = config_file_path.decode(encoding=sys.getfilesystemencoding())
except UnicodeDecodeError:
raise ValidationError("config_file_path is not a Unicode string.")
self.config_file_path = config_file_path
self.config_file_path = config_file_path or ''
def set_defaults(self) -> None:
"""
@@ -344,7 +344,9 @@ def _open_config_file(config_file: str | IO | None) -> Iterator[IO]:
result_config_file.close()
def load_config(config_file: str | IO | None = None, **kwargs) -> MkDocsConfig:
def load_config(
config_file: str | IO | None = None, *, config_file_path: str | None = None, **kwargs
) -> MkDocsConfig:
"""
Load the configuration for a given file object or name
@@ -366,7 +368,10 @@ def load_config(config_file: str | IO | None = None, **kwargs) -> MkDocsConfig:
# Initialize the config with the default schema.
from mkdocs.config.defaults import MkDocsConfig
cfg = MkDocsConfig(config_file_path=getattr(fd, 'name', ''))
if config_file_path is None:
if fd is not sys.stdin.buffer:
config_file_path = getattr(fd, 'name', None)
cfg = MkDocsConfig(config_file_path=config_file_path)
# load the config file
cfg.load_file(fd)

View File

@@ -708,7 +708,7 @@ class Dir(FilesystemObject):
class DocsDir(Dir):
def post_validation(self, config: Config, key_name: str):
if config.config_file_path is None:
if not config.config_file_path:
return
# Validate that the dir is not the parent dir of the config file.
@@ -826,7 +826,7 @@ class Theme(BaseConfigOption[theme.Theme]):
# Ensure custom_dir is an absolute path
if 'custom_dir' in theme_config and not os.path.isabs(theme_config['custom_dir']):
config_dir = os.path.dirname(self.config_file_path or '')
config_dir = os.path.dirname(self.config_file_path)
theme_config['custom_dir'] = os.path.join(config_dir, theme_config['custom_dir'])
if 'custom_dir' in theme_config and not os.path.isdir(theme_config['custom_dir']):

View File

@@ -16,7 +16,7 @@ def get_schema() -> base.PlainConfigSchema:
class MkDocsConfig(base.Config):
"""The configuration of MkDocs itself (the root object of mkdocs.yml)."""
config_file_path: str | None = c.Optional(c.Type(str)) # type: ignore[assignment]
config_file_path: str = c.Type(str) # type: ignore[assignment]
"""The path to the mkdocs.yml config file. Can't be populated from the config."""
site_name = c.Type(str)