|
| 1 | +import logging |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +from mkdocs.config import Config |
| 5 | +from mkdocs.config import config_options as c |
| 6 | + |
| 7 | +log: logging.Logger = logging.getLogger("mkdocs") |
| 8 | + |
| 9 | +config_scheme_legacy = { |
| 10 | + "full-doc": "full_doc", |
| 11 | + "ignore-errors": "ignore_errors", |
| 12 | + "save-api": "custom_api_folder", |
| 13 | + "doxygen-bin-path": "doxygen_bin_path", |
| 14 | +} |
| 15 | + |
| 16 | +config_project_legacy = { |
| 17 | + "src-dirs": "src_dirs", |
| 18 | + "full-doc": "full_doc", |
| 19 | + "ignore-errors": "ignore_errors", |
| 20 | + "doxy-cfg": "doxy_config_dict", |
| 21 | + "doxy-cfg-file": "doxy_config_file", |
| 22 | + "template-dir": "custom_template_dir", |
| 23 | +} |
| 24 | + |
| 25 | + |
| 26 | +class MkDoxyConfigProject(Config): |
| 27 | + """! Configuration for each project in the MkDoxy configuration file. |
| 28 | + @details New type of configuration for each project in the MkDoxy configuration file. |
| 29 | + It will replace the old configuration type. |
| 30 | +
|
| 31 | + @param src_dirs: (str) Source directories for Doxygen - INPUT |
| 32 | + @param full_doc: (bool) Generate full documentation |
| 33 | + @param debug: (bool) Debug mode |
| 34 | + @param ignore_errors: (bool) Ignore errors |
| 35 | + @param doxy_config_dict: (dict) Doxygen additional configuration |
| 36 | + @param doxy_config_default: (bool) Use default MkDoxy Doxygen configuration |
| 37 | + @param doxy_config_file: (str) Doxygen configuration file |
| 38 | + @param doxy_config_file_force: (bool) Do not use default MkDoxy Doxygen configuration, use only Doxygen configuration file |
| 39 | + @param custom_template_dir: (str) Custom template directory |
| 40 | + """ |
| 41 | + |
| 42 | + src_dirs = c.Type(str) |
| 43 | + full_doc = c.Type(bool, default=True) |
| 44 | + debug = c.Type(bool, default=False) |
| 45 | + ignore_errors = c.Type(bool, default=False) |
| 46 | + doxy_config_dict = c.Type(dict, default={}) |
| 47 | + doxy_config_default = c.Type(bool, default=True) |
| 48 | + doxy_config_file = c.Optional(c.Type(Path)) |
| 49 | + doxy_config_file_force = c.Type(bool, default=False) |
| 50 | + custom_template_dir = c.Optional(c.Type(str)) |
| 51 | + |
| 52 | + |
| 53 | +class MkDoxyConfig(Config): |
| 54 | + """! Global configuration for the MkDoxy plugin. |
| 55 | + @details New type of global configuration for the MkDoxy plugin. It will replace the old configuration type. |
| 56 | + @param projects: (dict) Project configuration - multiple projects |
| 57 | + @param full_doc: (bool) Generate full documentation - global (all projects) |
| 58 | + @param debug: (bool) Debug mode |
| 59 | + @param ignore_errors: (bool) Ignore errors |
| 60 | + @param custom_api_folder: (str) Custom API folder for Doxygen and MD output (default in temp folder) |
| 61 | + @param doxygen_bin_path: (str) Path to Doxygen binary - default "doxygen" |
| 62 | + """ |
| 63 | + |
| 64 | + projects = c.DictOfItems(c.SubConfig(MkDoxyConfigProject), default={}) # project configuration - multiple projects |
| 65 | + full_doc = c.Type(bool, default=True) # generate full documentation - global (all projects) |
| 66 | + debug = c.Type(bool, default=False) # debug mode |
| 67 | + ignore_errors = c.Type(bool, default=False) # ignore errors |
| 68 | + custom_api_folder = c.Optional(c.Type(str)) # custom API folder for Doxygen and MD output (default in temp folder) |
| 69 | + doxy_config_dict = c.Type( |
| 70 | + dict, default={} |
| 71 | + ) # Doxygen additional configuration - it is overwritten by project config |
| 72 | + doxygen_bin_path = c.Type(Path, default=Path("doxygen")) # path to Doxygen binary (default "doxygen" |
| 73 | + |
| 74 | + generate_diagrams = c.Type(bool, default=False) # generate diagrams |
| 75 | + generate_diagrams_format = c.Choice(("svg", "png", "jpg", "gif"), default="svg") # diagram format |
| 76 | + generate_diagrams_type = c.Choice(("dot", "uml"), default="dot") # diagram type |
| 77 | + |
| 78 | + |
| 79 | +# def load_config_by_key(key: str, legacy_key: str, config: Config, legacy: list) -> any: |
| 80 | +# """! Load the configuration value from the global configuration |
| 81 | +# @details Legacy config option is by default None, but if it is not None, it will print a warning and return value. |
| 82 | +# @param key: (str) The new configuration key. |
| 83 | +# @param legacy_key: (str) The legacy configuration key. |
| 84 | +# @param config: (Config) The global configuration object. |
| 85 | +# @param legacy: (list) The list of legacy configuration options. |
| 86 | +# @return: (Optional[str]) The configuration value. |
| 87 | +# """ |
| 88 | +# if config.get(legacy_key) is not None: |
| 89 | +# legacy.append(f"Found legacy configuration options: '{legacy_key}' -> replace with '{key}'") |
| 90 | +# return config.get(legacy_key) |
| 91 | +# return config.get(key) |
| 92 | +# |
| 93 | +# |
| 94 | +# def process_configuration(config: Config) -> MkDoxyConfig: |
| 95 | +# """! Process the configuration for the MkDoxy plugin |
| 96 | +# @details Process the configuration for the MkDoxy plugin and validate the configuration. |
| 97 | +# It will try to load new configuration, but it will also check for legacy configuration options. |
| 98 | +# @param config: (Config) The global configuration object. |
| 99 | +# @return: (MkDoxyConfig) The new validated configuration object. |
| 100 | +# @throws ConfigurationError: If the configuration is invalid. |
| 101 | +# """ |
| 102 | +# legacy_options = [] |
| 103 | +# doxy_config = MkDoxyConfig() |
| 104 | +# doxy_config.full_doc = load_config_by_key("full_doc", "full-doc", config, legacy_options) |
| 105 | +# doxy_config.debug = config.get("debug", False) |
| 106 | +# doxy_config.ignore_errors = load_config_by_key("ignore_errors", "ignore-errors", config, legacy_options) |
| 107 | +# doxy_config.custom_api_folder = load_config_by_key("custom_api_folder", "save-api", config, legacy_options) |
| 108 | +# doxy_config.doxygen_bin_path = load_config_by_key("doxygen_bin_path", "doxygen-bin-path", config, legacy_options) |
| 109 | +# |
| 110 | +# doxy_config.generate_diagrams = config.get("generate_diagrams") |
| 111 | +# doxy_config.generate_diagrams_format = config.get("generate_diagrams_format") |
| 112 | +# doxy_config.generate_diagrams_type = config.get("generate_diagrams_type") |
| 113 | +# |
| 114 | +# # Validate the global configuration |
| 115 | +# validate_project_config(doxy_config, legacy_options) |
| 116 | +# |
| 117 | +# # Validate and load project configuration |
| 118 | +# for project_name, project_cfg in config.get("projects", {}).items(): |
| 119 | +# doxy_config.projects[project_name] = load_project_config(project_cfg, project_name) |
| 120 | +# |
| 121 | +# return doxy_config |
| 122 | +# |
| 123 | +# |
| 124 | +# def validate_project_config(doxy_cfg: Config, legacy_options: list[str]) -> None: |
| 125 | +# """! Validate the project configuration for the MkDoxy plugin |
| 126 | +# @details Validate the project configuration for the MkDoxy plugin and check for errors and warnings. |
| 127 | +# @param doxy_cfg: (MkDoxyConfig) The project configuration object. |
| 128 | +# @param legacy_options: (list) The list of problems. |
| 129 | +# @return: None |
| 130 | +# @throws ConfigurationError: If the configuration is invalid. |
| 131 | +# """ |
| 132 | +# if legacy_options: |
| 133 | +# log.warning("Found some legacy configuration options, please update your configuration!") |
| 134 | +# log.warning("Run command 'mkdoxy migrate mkdocs.yaml' to update your configuration to the new format!") |
| 135 | +# log.warning("More information in the documentation: https://mkdoxy.kubaandrysek.cz/") |
| 136 | +# for problem in legacy_options: |
| 137 | +# log.warning(f" -> {problem}") |
| 138 | +# |
| 139 | +# failed, warnings = doxy_cfg.validate() |
| 140 | +# |
| 141 | +# for config_name, warning in warnings: |
| 142 | +# log.warning(f" -> Config value: '{config_name}'. Warning: {warning}") |
| 143 | +# |
| 144 | +# for config_name, error in failed: |
| 145 | +# log.error(f" -> Config value: '{config_name}'. Error: {error}") |
| 146 | +# raise exceptions.ConfigurationError(f"Config value: '{config_name}'. Error: {error}") |
| 147 | +# |
| 148 | +# |
| 149 | +# def load_project_config_by_key(key: str, legacy_key: str, project_cfg: dict, project_name: str, problems: list) -> any: |
| 150 | +# """! Load the project configuration value from the project configuration |
| 151 | +# @details Legacy project config option is by default None, but if it is not None, |
| 152 | +# it will print a warning and return the value. |
| 153 | +# @param key: (str) The new project configuration key. |
| 154 | +# @param legacy_key: (str) The legacy project configuration key. |
| 155 | +# @param project_cfg: (dict) The project configuration object. |
| 156 | +# @param project_name: (str) The project name. |
| 157 | +# @param problems: (list) The list of problems. |
| 158 | +# @return: (Optional[str]) The project configuration value. |
| 159 | +# """ |
| 160 | +# if project_cfg.get(legacy_key) is not None: |
| 161 | +# problems.append( |
| 162 | +# f"Found legacy configuration options: '{legacy_key}' -> replace with '{key}'" |
| 163 | +# f" in project '{project_name}'" |
| 164 | +# ) |
| 165 | +# return project_cfg.get(legacy_key) |
| 166 | +# return project_cfg.get(key) |
| 167 | +# |
| 168 | +# |
| 169 | +# def load_project_config(project_cfg: dict, project_name: str) -> MkDoxyConfigProject: |
| 170 | +# """! Load the project configuration for the MkDoxy plugin |
| 171 | +# @details Load the project configuration for the MkDoxy plugin and validate the configuration. |
| 172 | +# @param project_cfg: (dict) The project configuration object. |
| 173 | +# @param project_name: (str) The project name. |
| 174 | +# @return: (MkDoxyConfigProject) The new validated project configuration object. |
| 175 | +# """ |
| 176 | +# legacy_options = [] |
| 177 | +# doxy_project_cfg = MkDoxyConfigProject() |
| 178 | +# doxy_project_cfg.src_dirs = load_project_config_by_key( |
| 179 | +# "src_dirs", "src-dirs", project_cfg, project_name, legacy_options |
| 180 | +# ) |
| 181 | +# |
| 182 | +# doxy_project_cfg.full_doc = load_project_config_by_key( |
| 183 | +# "full_doc", "full-doc", project_cfg, project_name, legacy_options |
| 184 | +# ) |
| 185 | +# doxy_project_cfg.debug = project_cfg.get("debug", False) |
| 186 | +# doxy_project_cfg.ignore_errors = load_project_config_by_key( |
| 187 | +# "ignore_errors", "ignore-errors", project_cfg, project_name, legacy_options |
| 188 | +# ) |
| 189 | +# doxy_project_cfg.doxy_config_dict = load_project_config_by_key( |
| 190 | +# "doxy_config_dict", "doxy-cfg", project_cfg, project_name, legacy_options |
| 191 | +# ) |
| 192 | +# |
| 193 | +# validate_config_file: Optional[str] = load_project_config_by_key( |
| 194 | +# "doxy_config_file", "doxy-cfg-file", project_cfg, project_name, legacy_options |
| 195 | +# ) |
| 196 | +# doxy_project_cfg.doxy_config_file = None if validate_config_file is None else Path(validate_config_file) |
| 197 | +# |
| 198 | +# validate_template_dir: Optional[str] = load_project_config_by_key( |
| 199 | +# "custom_template_dir", "template-dir", project_cfg, project_name, legacy_options |
| 200 | +# ) |
| 201 | +# doxy_project_cfg.custom_template_dir = None if validate_template_dir is None else Path(validate_template_dir) |
| 202 | +# |
| 203 | +# validate_project_config(doxy_project_cfg, legacy_options) |
| 204 | +# return doxy_project_cfg |
0 commit comments