generator.py (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
#!/home/petermolnar.net/.venv/bin/python3.5
"""Usage: generator.py [-h] [-f] [-g] [-p] [-d] [-s FILE]
-h --help show this
-f --force force HTML file rendering
-p --pandoc force re-rendering content HTML
-g --regenerate regenerate images
-s --single FILE only (re)generate a single entity
-d --debug set logging level
"""
import os
import shutil
import logging
import atexit
import json
import sys
import tempfile
import glob
from whoosh import index
from docopt import docopt
from ruamel import yaml
from webmentiontools.send import WebmentionSend
import taxonomy
import singular
from slugify import slugify
import arrow
class Engine(object):
lockfile = "/tmp/petermolnar.net.generator.lock"
def __init__(self):
if os.path.isfile(self.lockfile):
raise ValueError("Lockfile %s is present; generator won't run.")
else:
with open(self.lockfile, "w") as lock:
lock.write(arrow.utcnow().format())
lock.close()
atexit.register(self.removelock)
atexit.register(self.removetmp)
self._mkdirs()
self.tags = {}
self.category = {}
self.allposts = None
self.frontposts = None
self.slugsdb = os.path.join(glob.CACHE, "slugs.json")
if os.path.isfile(self.slugsdb):
with open(self.slugsdb) as slugsdb:
self.allslugs = json.loads(slugsdb.read())
slugsdb.close()
else:
self.allslugs = []
self.tmpwhoosh = tempfile.mkdtemp('whooshdb_', dir=tempfile.gettempdir())
self.whoosh = index.create_in(self.tmpwhoosh, glob.schema)
def removelock(self):
os.unlink(self.lockfile)
def removetmp(self):
if os.path.isdir(self.tmpwhoosh):
for root, dirs, files in os.walk(self.tmpwhoosh, topdown=False):
for f in files:
os.remove(os.path.join(root, f))
for d in dirs:
os.rmdir(os.path.join(root, d))
def initbuilder(self):
self._copy_and_compile()
def cleanup(self):
with open(os.path.join(glob.CACHE, "slugs.json"), "w") as db:
logging.info("updating slugs database")
db.write(json.dumps(self.allslugs))
db.close()
tags = []
for tslug, taxonomy in self.tags.items():
tags.append(taxonomy.name)
with open(os.path.join(glob.CACHE, "tags.json"), "w") as db:
logging.info("updating tags database")
db.write(json.dumps(tags))
db.close()
logging.info("deleting old searchdb")
shutil.rmtree(glob.SEARCHDB)
logging.info("moving new searchdb")
shutil.move(self.tmpwhoosh, glob.SEARCHDB)
def _mkdirs(self):
for d in [glob.TARGET, glob.TFILES, glob.TTHEME, glob.CACHE]:
if not os.path.isdir(d):
os.mkdir(d)
def _copy_and_compile(self):
for f in os.listdir(glob.STHEME):
p = os.path.join(glob.STHEME, f)
if os.path.isdir(p):
try:
shutil.copytree(p, os.path.join(glob.TTHEME, f))
except FileExistsError:
pass
else:
path, fname = os.path.split(p)
fname, ext = os.path.splitext(fname)
logging.debug("copying %s", p)
shutil.copy(p, os.path.join(glob.TTHEME, f))
@staticmethod
def postbycategory(fpath, catd=None, catn=None):
if catd == 'photo':
post = singular.PhotoHandler(fpath, category=catn)
elif catd == 'page':
post = singular.PageHandler(fpath)
else:
post = singular.ArticleHandler(fpath, category=catn)
return post
def collect(self):
self.allposts = taxonomy.TaxonomyHandler()
#self.gallery = taxonomy.TaxonomyHandler(taxonomy="photography", name="Photography")
self.frontposts = taxonomy.TaxonomyHandler()
for category in glob.conf['category'].items():
catn, catd = category
catp = os.path.abspath(os.path.join(glob.CONTENT, catn))
if not os.path.exists(catp):
continue
logging.debug("getting posts for category %s from %s", catn, catp)
cat = taxonomy.TaxonomyHandler(taxonomy='category', name=catn)
self.category[catn] = cat
for f in os.listdir(catp):
fpath = os.path.join(catp, f)
if not os.path.isfile(fpath):
continue
logging.debug("parsing %s", fpath)
exclude = False
if 'exclude' in catd:
exclude = bool(catd['exclude'])
ct = None
if 'type' in catd:
ct = catd['type']
post = Engine.postbycategory(fpath, catd=ct, catn=catn)
self.allposts.append(post)
if post.dtime > arrow.utcnow().timestamp:
logging.warning(
"Post '%s' will be posted in the future; "
"skipping it from Taxonomies for now", fpath
)
else:
cat.append(post)
if not exclude:
self.frontposts.append(post)
if hasattr(post, 'tags') and isinstance(post.tags, list):
for tag in post.tags:
tslug = slugify(tag, only_ascii=True, lower=True)
if not tslug in self.tags.keys():
t = taxonomy.TaxonomyHandler(taxonomy='tag', name=tag)
self.tags[tslug] = t
else:
t = self.tags[tslug]
t.append(post)
elif not hasattr(post, 'tags'):
logging.error("%s post does not have tags", post.fname)
elif not isinstance(post.tags, list):
logging.error(
"%s tags are not a list, it's %s ",
post.fname,
type(post.tags)
)
for r in post.redirect.keys():
self.allslugs.append(r)
self.allslugs.append(post.fname)
def renderposts(self):
for p in self.allposts.posts.items():
time, post = p
post.write()
post.redirects()
post.pings()
post.index(self.whoosh)
def rendertaxonomies(self):
for t in [self.tags, self.category]:
for tname, tax in t.items():
if glob.conf['category'].get(tname, False):
if glob.conf['category'][tname].get('nocollection', False):
logging.info("skipping taxonomy '%s' due to config nocollections", tname)
continue
tax.write_paginated()
tax.index(self.whoosh)
self.frontposts.write_paginated()
#self.gallery.write_simple(template='gallery.html')
self.allposts.writesitemap()
def globredirects(self):
redirects = os.path.join(glob.CONTENT,'redirects.yml')
if not os.path.isfile(redirects):
return
ftime = os.stat(redirects)
rdb = {}
with open(redirects, 'r') as db:
rdb = yaml.safe_load(db)
db.close()
for r_ in rdb.items():
target, slugs = r_
for slug in slugs:
singular.SingularHandler.write_redirect(
slug,
"%s/%s" % (glob.conf['site']['url'], target),
ftime.st_mtime
)
def recordlastrun(self):
if os.path.exists(glob.lastrun):
t = arrow.utcnow().timestamp
os.utime(glob.lastrun, (t,t))
else:
open(glob.lastrun, 'a').close()
if __name__ == '__main__':
args = docopt(__doc__, version='generator.py 0.2')
if args['--pandoc']:
glob.CACHEENABLED = False
if args['--force']:
glob.FORCEWRITE = True
if args['--regenerate']:
glob.REGENERATE = True
logform = '%(asctime)s - %(levelname)s - %(message)s'
if args['--debug']:
loglevel = 10
else:
loglevel = 40
while len(logging.root.handlers) > 0:
logging.root.removeHandler(logging.root.handlers[-1])
logging.basicConfig(level=loglevel, format=logform)
if args['--single']:
logging.info("(re)generating a single item only")
path = args['--single'].split('/')
fpath = os.path.join(glob.CONTENT, path[0], path[1])
post = Engine.postbycategory(fpath, catd=path[0])
post.pings()
post.write()
sys.exit(0)
else:
eng = Engine()
eng.initbuilder()
eng.collect()
eng.renderposts()
eng.globredirects()
eng.rendertaxonomies()
eng.recordlastrun()
eng.cleanup()
|