Skip to content

Commit 628b4cb

Browse files
committed
Draft of a more organized object modelling
[noissue]
1 parent 4b3d0bb commit 628b4cb

File tree

120 files changed

+187
-8074
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

120 files changed

+187
-8074
lines changed

Diff for: pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ dependencies = [
1111
"mkdocstrings-python>=1.9.1",
1212
"mkdocs-macros-plugin",
1313
"mkdocs-site-urls",
14+
"dynaconf",
1415
"importlib_resources",
1516
"httpx",
1617
"rich",

Diff for: src/pulp_docs/cli.py

-150
Original file line numberDiff line numberDiff line change
@@ -4,153 +4,3 @@
44
It defines its interface.
55
"""
66

7-
import os
8-
import subprocess
9-
import sys
10-
import tempfile
11-
import typing as t
12-
from pathlib import Path
13-
14-
import click
15-
from importlib_resources import files
16-
17-
TMP_DIR = Path("tmp")
18-
WORKDIR = Path.home() / "workspace" / "multirepo-prototype"
19-
20-
21-
def get_abspath(name: str) -> Path:
22-
return Path(WORKDIR / name).absolute()
23-
24-
25-
def cast_bool(value: str) -> bool:
26-
return False if value.lower() in ("f", "false") else True
27-
28-
29-
class Config:
30-
"""
31-
Configuration shared among CLI and mkdocs_macro.py hooks.
32-
33-
Params:
34-
mkdocs_file: the base mkdocs used in serving/building
35-
repolist: the configuration repositories (which and how to fetch)
36-
clear_cache: whether to clear cache before downloading from remote
37-
"""
38-
39-
def __init__(self, from_environ: bool = False):
40-
if from_environ is False:
41-
self.verbose = False
42-
self.workdir = Path().absolute()
43-
self.mkdocs_file = files("pulp_docs").joinpath("data/mkdocs.yml").absolute()
44-
self.repolist = files("pulp_docs").joinpath("data/repolist.yml").absolute()
45-
self.clear_cache = False
46-
47-
if env_mkdocs := os.environ.get("PULPDOCS_MKDOCS_FILE"):
48-
self.mkdocs_file = Path(env_mkdocs)
49-
else:
50-
self.verbose = cast_bool(os.environ["PULPDOCS_VERBOSE"])
51-
self.workdir = Path(os.environ["PULPDOCS_WORKDIR"])
52-
self.mkdocs_file = Path(os.environ["PULPDOCS_MKDOCS_FILE"])
53-
self.repolist = Path(os.environ["PULPDOCS_REPOLIST"])
54-
self.clear_cache = cast_bool(os.environ["PULPDOCS_CLEAR_CACHE"])
55-
56-
def get_environ_dict(self):
57-
return {f"PULPDOCS_{k.upper()}": str(v) for k, v in self.__dict__.items()}
58-
59-
60-
pass_config = click.make_pass_decorator(Config, ensure=True)
61-
62-
63-
@click.group()
64-
@click.option("--verbose", "-v", is_flag=True)
65-
@pass_config
66-
def main(config: Config, verbose: bool):
67-
"""
68-
This is pulp-docs, a cli tool to help run and build multirepo documentation within Pulp project.
69-
"""
70-
config.verbose = verbose
71-
72-
73-
# mkdocs help wrapper
74-
watch_help = (
75-
"A directory or file to watch for live reloading. Can be supplied multiple times."
76-
)
77-
no_reload_help = "Disable the live reloading in the development server."
78-
79-
80-
@main.command()
81-
@click.option(
82-
"--clear-cache",
83-
default=False,
84-
is_flag=True,
85-
help="Whether to clear the cache before serving (default=False).",
86-
)
87-
@click.option("--verbose", "-v", is_flag=True)
88-
@click.option(
89-
"-w",
90-
"--watch",
91-
help=watch_help,
92-
type=click.Path(exists=True),
93-
multiple=True,
94-
default=[],
95-
)
96-
@click.option("--no-livereload", "livereload", flag_value=False, help=no_reload_help)
97-
@click.option("--livereload", "livereload", flag_value=True, default=True, hidden=True)
98-
@pass_config
99-
def serve(
100-
config: Config,
101-
clear_cache: bool,
102-
verbose: bool,
103-
watch: t.List[Path],
104-
livereload: bool,
105-
):
106-
"""Run mkdocs server."""
107-
env = os.environ.copy()
108-
config.clear_cache = clear_cache
109-
config.verbose = verbose
110-
env.update(config.get_environ_dict())
111-
112-
watch_list = [("--watch", watched) for watched in watch]
113-
flag_list = []
114-
if livereload is False:
115-
flag_list.append(("--no-livereload",))
116-
117-
options: t.List[tuple] = [("--config-file", config.mkdocs_file)]
118-
options.extend(watch_list)
119-
options.extend(flag_list)
120-
121-
cmd = ["mkdocs", "serve"]
122-
for opt in options:
123-
cmd.extend(opt)
124-
print("Running:", " ".join(str(s) for s in cmd))
125-
subprocess.run(cmd, env=env)
126-
127-
128-
@main.command()
129-
@pass_config
130-
def build(config: Config):
131-
"""Build mkdocs site."""
132-
config.verbose = True
133-
env = os.environ.copy()
134-
env.update(config.get_environ_dict())
135-
136-
options = (
137-
("--config-file", config.mkdocs_file),
138-
("--site-dir", str(Path("site").absolute())),
139-
)
140-
cmd = ["mkdocs", "build"]
141-
for opt in options:
142-
cmd.extend(opt)
143-
print("Building:", " ".join(str(s) for s in cmd))
144-
result = subprocess.run(cmd, env=env)
145-
sys.exit(result.returncode)
146-
147-
148-
@main.command()
149-
@pass_config
150-
def status(config: Config):
151-
"""Print relevant information about repositories that will be used."""
152-
raise NotImplementedError
153-
154-
155-
if __name__ == "__main__":
156-
sys.exit(main())

Diff for: src/pulp_docs/config.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from __future__ import annotations
2+
3+
import tempfile
4+
from dataclasses import dataclass, field
5+
from enum import Enum
6+
from pathlib import Path
7+
from typing import Optional
8+
9+
from dynaconf import Dynaconf, Validator
10+
11+
12+
def init_config(setting_file: Optional[Path] = None) -> ConfigSchema:
13+
setting_files = [setting_file] if setting_file else []
14+
return Dynaconf(settings_file=setting_files)
15+
16+
17+
class FindStrategy(Enum):
18+
"""
19+
Strategies on how to choose local repos, which should be used to override the defaults.
20+
21+
Args:
22+
NONE: Don't use any local overrides.
23+
CURRENT: If the CWD is a matching repo, use that.
24+
PARENT_WORKDIR: Lookup for matching repos in the parent workdir.
25+
LOOKUP_LIST: Use an explicit lookup_list defining the path for each repo override.
26+
"""
27+
28+
NONE = 0
29+
CURRENT = 1
30+
PARENT_WORKDIR = 2
31+
LOOKUP_LIST = 3
32+
33+
34+
@dataclass
35+
class ConfigSchema:
36+
remote_sync_config: RemoteSyncConfig
37+
repo_finder_config: RepoFinderConfig
38+
cli_config: CliConfig
39+
40+
class Meta:
41+
validators: list[Validator] = []
42+
43+
44+
@dataclass
45+
class RemoteSyncConfig:
46+
"""
47+
Args:
48+
sync: Wheter or not to sync (clone and rebase) changes to a managed repo.
49+
manged_repo_path: The local path to where the managed repo should operate.
50+
"""
51+
52+
sync: bool = True
53+
managed_repo_path: Path = Path(tempfile.gettempdir())
54+
55+
56+
@dataclass
57+
class RepoFinderConfig:
58+
strategy: FindStrategy = FindStrategy.CURRENT
59+
lookup_list: list[Path] = field(default_factory=list)
60+
61+
62+
@dataclass
63+
class CliConfig:
64+
livereload: bool = True
65+
watch_list: list[Path] = field(default_factory=list)

Diff for: src/pulp_docs/constants.py

-39
Original file line numberDiff line numberDiff line change
@@ -1,40 +1 @@
1-
ADMIN_NAME = "admin"
2-
USER_NAME = "user"
3-
RESTAPI_URL_TEMPLATE = "https://docs.pulpproject.org/{}/restapi.html"
41

5-
DISPLAY_NAMES = {
6-
"guides": "How-to Guides",
7-
"learn": "Learn More",
8-
"tutorials": "Tutorials",
9-
"reference": "Reference",
10-
}
11-
GUIDES = DISPLAY_NAMES["guides"]
12-
LEARN = DISPLAY_NAMES["learn"]
13-
TUTORIALS = DISPLAY_NAMES["tutorials"]
14-
15-
16-
class Names:
17-
"""Display Names"""
18-
19-
# content types
20-
GUIDES = "How-to Guides"
21-
LEARN = "Learn More"
22-
TUTORIALS = "Tutorials"
23-
REFERENCE = "Reference"
24-
25-
# repo-types
26-
OTHER = "Extra"
27-
CORE = "Pulpcore"
28-
CONTENT = "Plugins"
29-
30-
# personas
31-
USER = "User"
32-
ADMIN = "Admin"
33-
DEV = "Dev"
34-
35-
# other
36-
PULPCORE_TUTORIAL = "Getting Started"
37-
38-
@staticmethod
39-
def get(name: str):
40-
return getattr(Names, name.upper())

Diff for: src/pulp_docs/data/repolist.yml

-83
This file was deleted.

Diff for: src/pulp_docs/mkdocs_hooks.py

-13
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,3 @@
33
44
See: https://www.mkdocs.org/user-guide/configuration/#hooks
55
"""
6-
7-
8-
def on_serve(server, config, builder):
9-
"""
10-
Hook to unwatch the temp dirs.
11-
12-
See: https://www.mkdocs.org/dev-guide/plugins/#on_serve
13-
"""
14-
tmpdir = config["docs_dir"]
15-
mkdocs_yml = config["config_file_path"]
16-
server.unwatch(tmpdir)
17-
server.unwatch(mkdocs_yml)
18-
return server

0 commit comments

Comments
 (0)