1
0
mirror of https://github.com/checktheroads/hyperglass synced 2024-05-11 05:55:08 +00:00

Complete new config file implementation

This commit is contained in:
thatmattlove
2021-09-25 21:36:08 -07:00
parent 22ae6a97e8
commit 27b6ba09d8
6 changed files with 43 additions and 186 deletions

View File

@@ -1,61 +1,26 @@
"""Markdown processing utility functions."""
# Project
from hyperglass.log import log
# Standard Library
import typing as t
from pathlib import Path
if t.TYPE_CHECKING:
# Project
from hyperglass.models import HyperglassModel
def _get_file(path_obj):
"""Read a file.
def get_markdown(config: "HyperglassModel", default: str, params: t.Dict[str, t.Any]) -> str:
"""Get markdown file if specified, or use default."""
Arguments:
path_obj {Path} -- Path to file.
Returns:
{str} -- File contents
"""
with path_obj.open("r") as raw_file:
return raw_file.read()
def format_markdown(content, params):
"""Format content with config parameters.
Arguments:
content {str} -- Unformatted content
Returns:
{str} -- Formatted content
"""
try:
fmt = content.format(**params)
except KeyError:
fmt = content
return fmt
def get_markdown(config_path, default, params):
"""Get markdown file if specified, or use default.
Format the content with config parameters.
Arguments:
config_path {object} -- content config
default {str} -- default content
Returns:
{str} -- Formatted content
"""
log.trace(f"Getting Markdown content for '{params['title']}'")
if config_path.enable and config_path.file is not None:
md = _get_file(config_path.file)
if config.enable and config.file is not None:
# with config_path.file
if hasattr(config, "file") and isinstance(config.file, Path):
with config.file.open("r") as config_file:
md = config_file.read()
else:
md = default
log.trace(f"Unformatted Content for '{params['title']}':\n{md}")
md_fmt = format_markdown(md, params)
log.trace(f"Formatted Content for '{params['title']}':\n{md_fmt}")
return md_fmt
try:
return md.format(**params)
except KeyError:
return md