onpoint/helper.py

32 lines
1.1 KiB
Python

import os
import stat
import yaml
# Calls a YAML parser for the given relative path inside the presentation root
# directory. Returns a dictionary with the YAML content.
def read_yaml(root, path):
with open(os.path.join(root, path), 'r') as stream:
try:
return yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)
# Retrieve the available languages as stated in the meta.yml (key: language).
def get_available_languages(root):
return read_yaml(root, 'meta.yml')['language']
# Replaces the placeholders in the given content with corresponding values in
# the given language from the meta.yml and returns the filled content.
def insert_metadata(content, root, lang):
metadata = read_yaml(root, 'meta.yml')
for key, value in metadata.items():
placeholder = '@' + key
filler = value[lang]
# print('replace', placeholder, 'with', filler)
if '@' + key in content:
content = content.replace(placeholder, filler)
return content
# Retrieve the modification time for a given file.
def get_modification_time(path):
return os.stat(path)[stat.ST_MTIME]