mirror of
https://github.com/sigmasternchen/grimoire-ssg
synced 2025-03-15 01:58:54 +00:00
feat: Switch to Python logging
This commit is contained in:
parent
2016173123
commit
f3f2889e44
11 changed files with 50 additions and 33 deletions
|
@ -143,10 +143,11 @@ that modifies the list of available modules:
|
||||||
|
|
||||||
```Python
|
```Python
|
||||||
from grimoiressg.modules import available_modules
|
from grimoiressg.modules import available_modules
|
||||||
|
from grimoiressg.utils import logger
|
||||||
|
|
||||||
|
|
||||||
def test(data, context):
|
def test(data, context):
|
||||||
print("This is test module.")
|
logger.info("This is test module.")
|
||||||
|
|
||||||
|
|
||||||
available_modules["test"] = test
|
available_modules["test"] = test
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
from grimoiressg.modules import available_modules
|
from grimoiressg.modules import available_modules
|
||||||
|
from grimoiressg.utils import logger
|
||||||
|
|
||||||
|
|
||||||
def test(data, context):
|
def test(data, context):
|
||||||
print("This is test module.")
|
logger.info("This is test module.")
|
||||||
|
|
||||||
|
|
||||||
available_modules["test"] = test
|
available_modules["test"] = test
|
||||||
|
|
|
@ -1,14 +1,16 @@
|
||||||
|
import logging
|
||||||
|
|
||||||
from grimoiressg.arguments import parse_arguments_to_initial_context
|
from grimoiressg.arguments import parse_arguments_to_initial_context
|
||||||
from grimoiressg.config import read_config
|
from grimoiressg.config import read_config
|
||||||
from grimoiressg.content_files import recursively_read_files
|
from grimoiressg.content_files import recursively_read_files
|
||||||
from grimoiressg.modules import available_modules
|
from grimoiressg.modules import available_modules
|
||||||
|
from grimoiressg.utils import logger
|
||||||
|
|
||||||
|
|
||||||
def apply_modules(data, config, context):
|
def apply_modules(data, config, context):
|
||||||
for module in config.get("enabled_modules", []):
|
for module in config.get("enabled_modules", []):
|
||||||
print(f"Applying module {module}...")
|
logger.info("Applying module %s...", module)
|
||||||
available_modules[module](data, context)
|
available_modules[module](data, context)
|
||||||
print("")
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
@ -18,7 +20,8 @@ def main():
|
||||||
data = recursively_read_files(context)
|
data = recursively_read_files(context)
|
||||||
apply_modules(data, config, context)
|
apply_modules(data, config, context)
|
||||||
|
|
||||||
print("Done.")
|
logger.info("Done.")
|
||||||
|
logging.shutdown()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
import argparse
|
import argparse
|
||||||
|
|
||||||
|
from grimoiressg.utils import logger
|
||||||
|
|
||||||
|
|
||||||
def parse_arguments_to_initial_context():
|
def parse_arguments_to_initial_context():
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
|
@ -21,11 +23,10 @@ def parse_arguments_to_initial_context():
|
||||||
"filenames": args.content_file
|
"filenames": args.content_file
|
||||||
}
|
}
|
||||||
|
|
||||||
print(f"Output directory: {context['output_dir']}")
|
logger.debug("Output directory: %s", context['output_dir'])
|
||||||
print(f"Config file: {context['config_file']}")
|
logger.debug("Config file: %s", context['config_file'])
|
||||||
print("Content files:")
|
logger.debug("Content files:")
|
||||||
for filename in context["filenames"]:
|
for filename in context["filenames"]:
|
||||||
print(f" - {filename}")
|
logger.debug(" - %s", filename)
|
||||||
print()
|
|
||||||
|
|
||||||
return context
|
return context
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
|
import logging
|
||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
from yaml import Loader
|
from yaml import Loader
|
||||||
|
|
||||||
from grimoiressg.modules import available_modules, load_external_module
|
from grimoiressg.modules import available_modules, load_external_module
|
||||||
|
from grimoiressg.utils import logger
|
||||||
|
|
||||||
|
|
||||||
def default_config():
|
def default_config():
|
||||||
|
@ -18,24 +21,23 @@ def read_config(context):
|
||||||
config_file = context.get("config_file", None)
|
config_file = context.get("config_file", None)
|
||||||
|
|
||||||
if not config_file:
|
if not config_file:
|
||||||
print("No config file given; using default config")
|
logger.info("No config file given; using default config")
|
||||||
config = default_config()
|
config = default_config()
|
||||||
else:
|
else:
|
||||||
print("Loading config file...")
|
logger.info("Loading config file...")
|
||||||
with open(config_file, "r") as file:
|
with open(config_file, "r") as file:
|
||||||
config = yaml.load(file, Loader) or {}
|
config = yaml.load(file, Loader) or {}
|
||||||
|
|
||||||
for module in config.get("load_modules", []):
|
for module in config.get("load_modules", []):
|
||||||
print(f" Loading external module {module}")
|
logger.debug(" Loading external module %s", module)
|
||||||
load_external_module(module)
|
load_external_module(module)
|
||||||
print()
|
|
||||||
|
|
||||||
print("Enabled modules:")
|
logger.debug("Enabled modules:")
|
||||||
for module in config.get("enabled_modules", []):
|
for module in config.get("enabled_modules", []):
|
||||||
print(f" - {module}")
|
logger.debug(" - %s", module)
|
||||||
if module not in available_modules:
|
if module not in available_modules:
|
||||||
print(f" ERROR: Module does not exist")
|
logger.critical("Module does not exist: %s", module)
|
||||||
|
logging.shutdown()
|
||||||
exit(1)
|
exit(1)
|
||||||
print()
|
|
||||||
|
|
||||||
return config
|
return config
|
||||||
|
|
|
@ -3,11 +3,11 @@ import os
|
||||||
import yaml
|
import yaml
|
||||||
from yaml import Loader
|
from yaml import Loader
|
||||||
|
|
||||||
from grimoiressg.utils.files import for_each_glob, to_relative
|
from grimoiressg.utils import logger, for_each_glob, to_relative
|
||||||
|
|
||||||
|
|
||||||
def handle_file(filename):
|
def handle_file(filename):
|
||||||
print(f" Reading {to_relative(filename)}...")
|
logger.debug(" Reading %s...", to_relative(filename))
|
||||||
|
|
||||||
with open(filename, "r") as file:
|
with open(filename, "r") as file:
|
||||||
data = yaml.load(file, Loader)
|
data = yaml.load(file, Loader)
|
||||||
|
@ -28,12 +28,11 @@ def handle_file(filename):
|
||||||
def recursively_read_files(context):
|
def recursively_read_files(context):
|
||||||
data = []
|
data = []
|
||||||
|
|
||||||
print("Reading content files...")
|
logger.info("Reading content files...")
|
||||||
|
|
||||||
for filename in context["filenames"]:
|
for filename in context["filenames"]:
|
||||||
data.extend(for_each_glob(filename, handle_file))
|
data.extend(for_each_glob(filename, handle_file))
|
||||||
|
|
||||||
print(f"Read {len(data)} files in total.")
|
logger.info(f"Read %d files in total.", len(data))
|
||||||
print()
|
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
import markdown
|
import markdown
|
||||||
|
|
||||||
|
from grimoiressg.utils import logger
|
||||||
|
|
||||||
|
|
||||||
def compile_markdown(data, context):
|
def compile_markdown(data, context):
|
||||||
for entry in data:
|
for entry in data:
|
||||||
if "markdown" in entry:
|
if "markdown" in entry:
|
||||||
print(f"Compiling markdown for {entry['relative_filename']}...")
|
logger.debug("Compiling markdown for %s...", entry['relative_filename'])
|
||||||
entry["markdown_compiled"] = markdown.markdown(entry["markdown"])
|
entry["markdown_compiled"] = markdown.markdown(entry["markdown"])
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
from grimoiressg.utils import logger
|
||||||
|
|
||||||
|
|
||||||
def extract_tags(data, context):
|
def extract_tags(data, context):
|
||||||
tags = {}
|
tags = {}
|
||||||
|
@ -9,10 +11,10 @@ def extract_tags(data, context):
|
||||||
tags[tag] = entry_list
|
tags[tag] = entry_list
|
||||||
|
|
||||||
if tags:
|
if tags:
|
||||||
print("Found tags:")
|
logger.debug("Found tags:")
|
||||||
for tag in tags.keys():
|
for tag in tags.keys():
|
||||||
print(f" - {tag} ({len(tags[tag])} files)")
|
logger.debug(" - %s (%d files)", tag, len(tags[tag]))
|
||||||
else:
|
else:
|
||||||
print("No tags found.")
|
logger.debug("No tags found.")
|
||||||
|
|
||||||
context["tags"] = tags
|
context["tags"] = tags
|
|
@ -1,9 +1,9 @@
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from grimoiressg.utils import to_relative
|
|
||||||
|
|
||||||
from jinja2 import Environment, FileSystemLoader
|
from jinja2 import Environment, FileSystemLoader
|
||||||
|
|
||||||
|
from grimoiressg.utils import to_relative, logger
|
||||||
|
|
||||||
jinja_env = Environment(
|
jinja_env = Environment(
|
||||||
loader=FileSystemLoader("/")
|
loader=FileSystemLoader("/")
|
||||||
)
|
)
|
||||||
|
@ -16,7 +16,7 @@ def render_templates(data, context):
|
||||||
if "template" in entry:
|
if "template" in entry:
|
||||||
template_path = os.path.realpath(os.path.dirname(entry["filename"]) + "/" + entry["template"])
|
template_path = os.path.realpath(os.path.dirname(entry["filename"]) + "/" + entry["template"])
|
||||||
template_dir = os.path.dirname(template_path)
|
template_dir = os.path.dirname(template_path)
|
||||||
print(f"Rendering template for {entry['relative_filename']}...")
|
logger.debug("Rendering template for %s...", entry['relative_filename'])
|
||||||
template = jinja_env.get_template(template_path)
|
template = jinja_env.get_template(template_path)
|
||||||
entry["rendered"] = template.render(
|
entry["rendered"] = template.render(
|
||||||
**context,
|
**context,
|
||||||
|
@ -28,9 +28,9 @@ def render_templates(data, context):
|
||||||
if "rendered" in entry and "output" in entry:
|
if "rendered" in entry and "output" in entry:
|
||||||
files_written += 1
|
files_written += 1
|
||||||
filename = os.path.realpath(context["output_dir"] + "/" + entry["output"])
|
filename = os.path.realpath(context["output_dir"] + "/" + entry["output"])
|
||||||
print(f" writing to {to_relative(filename)}")
|
logger.debug(" writing to %s", to_relative(filename))
|
||||||
os.makedirs(os.path.dirname(filename), exist_ok=True)
|
os.makedirs(os.path.dirname(filename), exist_ok=True)
|
||||||
with open(filename, "w") as file:
|
with open(filename, "w") as file:
|
||||||
file.write(entry["rendered"])
|
file.write(entry["rendered"])
|
||||||
|
|
||||||
print(f"{files_written} rendered")
|
logger.debug("%d rendered", files_written)
|
||||||
|
|
|
@ -1 +1,2 @@
|
||||||
from .files import to_relative as to_relative
|
from .files import to_relative as to_relative, for_each_glob as for_each_glob
|
||||||
|
from .logger import logger as logger
|
||||||
|
|
5
grimoiressg/utils/logger.py
Normal file
5
grimoiressg/utils/logger.py
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
import logging
|
||||||
|
import sys
|
||||||
|
|
||||||
|
logging.basicConfig(stream=sys.stdout, level=logging.INFO, format="%(asctime)s %(levelname)-9s: %(message)s")
|
||||||
|
logger = logging.getLogger("grimoire")
|
Loading…
Reference in a new issue