Pelican Themes Randomizer
To streamline testing operations, I opted to move the theme-picker out of the pelican settings file.
With this setup, this script random_theme
can be executed before building the site, as shown in the following example:
./random_theme ; make clean devserver
– Enjoy!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
# pick_random_theme - Thursday, June 20, 2019 | |
""" This script will choose a random theme from pelican-themes and symlink it | |
in the project folder. Previously chosen themes are stored in TESTED_THEMES_FILENAME. | |
To loop through the themes again, delete TESTED_THEMES_FILENAME from disk or change | |
the filename. | |
Usage: pick_random_theme | |
Set THEME = 'random_theme' in your Pelican settings file (ie. pelicanconf.py) | |
""" | |
__version__ = '1.0.0' | |
import os | |
import sys | |
from os.path import basename, dirname, exists, lexists, realpath, abspath | |
from random import choice | |
sys.path.append(os.curdir) | |
from themes import Themes | |
TESTED_THEMES_FILENAME = 'tested_themes.txt' | |
def main(): | |
random_theme = choice(Themes) | |
tested_themes = [] | |
if os.path.exists(TESTED_THEMES_FILENAME): | |
with open(TESTED_THEMES_FILENAME, 'rt') as f: | |
for line in f.readlines(): | |
tested_themes.append(line.strip()) | |
while random_theme in tested_themes: | |
random_theme = choice(Themes) | |
with open(TESTED_THEMES_FILENAME, 'at') as f: | |
f.write(random_theme + os.linesep) | |
make_symlink(random_theme, 'random_theme', overwrite=True) | |
print("\n\tTheme: %s\n" % random_theme) | |
return | |
def make_symlink(filename, linkname, overwrite=False): | |
if exists(linkname) and overwrite: | |
if realpath(filename) != realpath(linkname): | |
os.unlink(linkname) | |
if not lexists(linkname): | |
if dirname(abspath(filename)) == dirname(abspath(linkname)): | |
cur_dir = abspath(os.curdir) | |
dir_name = dirname(filename) | |
os.chdir(dir_name) | |
filename = basename(filename) | |
linkname = basename(linkname) | |
# print("%s/%s -> %s" % (dir_name, linkname, filename)) | |
os.symlink(filename, linkname) | |
os.chdir(cur_dir) | |
else: | |
os.symlink(filename, linkname) | |
return | |
if __name__ == '__main__': | |
main() | |