Skip to content

Commit

Permalink
Make project configuration slicker
Browse files Browse the repository at this point in the history
  • Loading branch information
basejumpa committed Aug 4, 2024
1 parent 43e50d6 commit 9f82975
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ __pycache__/

# Backup of configuration
.config.old

# Python translation of .config
config.py
2 changes: 1 addition & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
{
"label": "configure",
"type": "shell",
"command": "make -f docs/Makefile config",
"command": "make -f docs/Makefile configure",
"options": {
"statusbar": {
"hide": false
Expand Down
60 changes: 47 additions & 13 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

# pylint: skip-file

# Modules built into Sphinx distribution
import re
import os
import platform
import sys

# Modules from additional packages
from git import Repo
from sphinx.util.logging import getLogger

Expand All @@ -12,9 +17,38 @@
_conf_location = os.path.realpath(os.path.dirname(__file__))

### Import project configuration ##############################################
with open(".config") as f:
exec(f.read())

def translate_config_file(input_file=".config", output_file="config.py"):
CONFIG_PREFIX = r"^CONFIG_"
# Define the path for the input and output files

current_directory = os.path.dirname(os.path.abspath(__file__))
config_file_path = os.path.join(current_directory, input_file)
output_file_path = os.path.join(current_directory, output_file)

# Read the input file
with open(config_file_path, "r") as config_file:
lines = config_file.readlines()

# Process the lines
processed_lines = []
for line in lines:
if re.match(CONFIG_PREFIX, line):
# Remove the CONFIG_PREFIX
processed_lines.append(re.sub(CONFIG_PREFIX, "", line))
else:
processed_lines.append(line)

# Write to the output file
with open(output_file_path, "w") as output_file:
output_file.writelines(processed_lines)

# Call the function
translate_config_file()

# Import the generated config module
sys.path.append(f"{os.getcwd()}")
import config

### Get SCM information #######################################################

Expand All @@ -33,7 +67,7 @@ def _calculate_repo_root_dir(source_path):

_scm_git_branch = None
try:
__repo = Repo(_calculate_repo_root_dir(CONFIG_BUILD__DIRS__SOURCE))
__repo = Repo(_calculate_repo_root_dir(config.BUILD__DIRS__SOURCE))
_scm_git_branch = __repo.active_branch.name
except:
logger.warning(f"Couldn't get git branch.")
Expand All @@ -45,16 +79,16 @@ def _calculate_repo_root_dir(source_path):
### Project information #######################################################
# @see https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information

project = CONFIG_DOC__PROJECT
author = CONFIG_DOC__AUTHOR
copyright = str(CONFIG_DOC__YEAR) + ", " + CONFIG_DOC__AUTHOR
project = config.DOC__PROJECT
author = config.DOC__AUTHOR
copyright = str(config.DOC__YEAR) + ", " + config.DOC__AUTHOR


### General configuration #####################################################
# @see https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration

# @see https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-language
language = CONFIG_DOC__LANGUAGE
language = config.DOC__LANGUAGE

exclude_patterns = [
]
Expand All @@ -70,12 +104,12 @@ def _calculate_repo_root_dir(source_path):
### Options for HTML output ###################################################
# @see https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

html_title = CONFIG_DOC__TITLE
html_title = config.DOC__TITLE

html_meta = {
'description': CONFIG_DOC__DESCRIPTION,
'keywords': 'CONFIG_DOC__KEYWORDS',
'author': CONFIG_DOC__AUTHOR,
'description': config.DOC__DESCRIPTION,
'keywords': 'config.DOC__KEYWORDS',
'author': config.DOC__AUTHOR,
}

templates_path = ["_templates"]
Expand Down Expand Up @@ -121,7 +155,7 @@ def _calculate_repo_root_dir(source_path):
"github_user": "basejumpa",
"github_repo": "basejumpa.github.io",
"github_version": _scm_git_branch,
"doc_path": CONFIG_BUILD__DIRS__SOURCE,
"doc_path": config.BUILD__DIRS__SOURCE,
}

###############################################################################
Expand All @@ -137,7 +171,7 @@ def _calculate_repo_root_dir(source_path):

extensions.append("sphinx_sitemap")

html_baseurl = CONFIG_PUBLISH__BASEURL
html_baseurl = config.PUBLISH__BASEURL


### Draw diagrams with "draw.io" ##############################################
Expand Down

0 comments on commit 9f82975

Please sign in to comment.