Skip to content

Instantly share code, notes, and snippets.

@palevell
Created June 20, 2019 11:46
Show Gist options
  • Save palevell/111bad0abe970095746d1d85bdde20bf to your computer and use it in GitHub Desktop.
Save palevell/111bad0abe970095746d1d85bdde20bf to your computer and use it in GitHub Desktop.
This script helps with testing pelican-themes.
#!/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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment