Skip to content

Commit

Permalink
Updates copyres so files are gathered upfront and available in templa…
Browse files Browse the repository at this point in the history
…tes as "respaths" and "resfilenames"

Also creates change events for hooks.
Change sitemap to exclude resources (can add via config SITEMAP_RESOURCE_EXT, excluding images/video)
  • Loading branch information
hooli committed May 27, 2013
1 parent 40c55cf commit ee52e1f
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 31 deletions.
31 changes: 30 additions & 1 deletion acrylamid/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
import abc
import codecs
import traceback
import glob

BOM_UTF8 = codecs.BOM_UTF8.decode('utf8')

from os.path import join, getmtime, relpath, splitext
from os.path import join, getmtime, relpath, splitext, dirname, basename, isfile, normpath
from fnmatch import fnmatch
from datetime import datetime, tzinfo, timedelta

Expand Down Expand Up @@ -294,6 +295,10 @@ def __init__(self, path, conf):
if m.group(1) is not None:
meta['cats'] = m.group(1).split('/')

if 'copyres' in meta:
meta['respaths'] = self.getresources(meta.get('copyres'), conf)
meta['resfilenames'] = [basename(val) for val in meta['respaths']]

self.offset = i
Reader.__init__(self, conf, meta)

Expand Down Expand Up @@ -322,6 +327,22 @@ def date(self):
"Fallback to last modification timestamp if date is unset."
return Date.fromtimestamp(getmtime(self.filename)).replace(tzinfo=self.tzinfo)

def getresources(self, wildcards, conf):
"""Generate a list of resources files based on the wildcard(s) passed in."""
reslist = []
if isinstance(wildcards, list):
for term in wildcards:
# exclude missing and non file types
reslist.extend([ normpath(f) for f in glob.glob(join(dirname(self.filename), term)) if isfile(f) ])
else:
if wildcards is None:
# use default wildcard appended to entry filename
reslist = [ normpath(f) for f in glob.glob(splitext(self.filename)[0] + conf.get('copyres_wildcard', '_[0-9]*.*')) if isfile(f) ]
else:
# provided wildcard appended to input directory
reslist = [ normpath(f) for f in glob.glob(join(dirname(self.filename), wildcards)) if isfile(f) ]
return reslist


class MetadataMixin(object):

Expand Down Expand Up @@ -405,6 +426,14 @@ def tags(self):
return [fx]
return fx

@property
def respaths(self):
return self.props.get('respaths', [])

@property
def resfilenames(self):
return self.props.get('resfilenames', [])

@property
def draft(self):
"""If set to True, the entry will not appear in articles, index, feed and tag view."""
Expand Down
56 changes: 28 additions & 28 deletions acrylamid/views/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
import io
import glob

from os.path import isfile, splitext, dirname, split, normpath
from os.path import isfile, dirname, basename, getmtime, join
from collections import defaultdict

from acrylamid import refs
from acrylamid.refs import modified, references
from acrylamid.views import View
from acrylamid import log
from acrylamid.errors import AcrylamidException
from acrylamid.helpers import expand, union, joinurl, event, link
from acrylamid.helpers import expand, union, joinurl, event, link, mkfile


class Base(View):
Expand Down Expand Up @@ -66,34 +66,34 @@ def generate(self, conf, env, data):

if all([isfile(path), unmodified, not tt.modified, not entry.modified,
not modified(*references(entry))]):
event.skip(self.name, path)
continue

html = tt.render(conf=conf, entry=entry, env=union(env,
entrylist=[entry], type=self.__class__.__name__.lower(),
prev=prev, next=next, route=expand(self.path, entry)))

yield html, path

if entry.hasproperty('copyres'):
copyres = entry.props.get('copyres')
if isinstance(copyres, list):
copyreslist = [normpath(split(entry.filename)[0] + '/' + val) for val in copyres]
else:
if copyres is None:
# use default wildcard appended to entry filename
wcard = splitext(entry.filename)[0] + conf.get('copyres_wildcard', '_[0-9]*.*')
else:
# provided wildcard appended to input directory
wcard = split(entry.filename)[0] + '/' + copyres
copyreslist = glob.glob(wcard)

for resource in copyreslist:
entryskipped = True # log later incase a resource is updated, change will be reflected in sitemap
else:
entryskipped = False
html = tt.render(conf=conf, entry=entry, env=union(env,
entrylist=[entry], type=self.__class__.__name__.lower(),
prev=prev, next=next, route=expand(self.path, entry)))
yield html, path

# check if any resources need to be moved
if entry.hasproperty('respaths'):
for res_src in entry.props.get('respaths'):
res_dest = join(dirname(path), basename(res_src))
# Note, presence of res_src check in FileReader.getresources
if isfile(res_dest) and getmtime(res_dest) > getmtime(res_src):
event.skip(self.name, res_dest)
continue
try:
fp = io.open(resource, 'rb')
yield fp , dirname(path) + '/' + split(resource)[1]
fp = io.open(res_src, 'rb')
if entryskipped == True:
event.update(self.name, path)
entryskipped = False
# use mkfile rather than yield so different ns can be specified (and filtered by sitemap)
mkfile(fp, res_dest, ns='resource', force=env.options.force, dryrun=env.options.dryrun)
except IOError as e:
log.warn("Failed to copy '%s' whilst processing '%s' (%s)" % (resource, entry.filename, e.strerror))
log.warn("Failed to copy resource '%s' whilst processing '%s' (%s)" % (res_src, entry.filename, e.strerror))
if entryskipped == True:
event.skip(self.name, path)


class Entry(Base):
"""Creates single full-length entry
Expand Down
15 changes: 13 additions & 2 deletions acrylamid/views/sitemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import io

from time import strftime, gmtime
from os.path import getmtime, exists
from os.path import getmtime, exists, splitext

from acrylamid.views import View
from acrylamid.helpers import event, joinurl, rchop
Expand Down Expand Up @@ -53,14 +53,25 @@ class Sitemap(View):
def init(self, conf, env):

def track(ns, path):
self.files.add((ns, path))
if ns != 'resource':
self.files.add((ns, path))
elif self.resext and splitext(path) in self.resext:
self.files.add((ns, path))

def changed(ns, path):
if not self.modified:
self.modified = True

self.files = set([])
self.modified = False

# use extension to check if resource should be tracked (eg PDF but should excluding images and video)
self.resext = conf.get('sitemap_resource_ext', [])
# Considered adding images and video, but need to be associated with loc. Better to have access to entry obj
# see http://support.google.com/webmasters/bin/answer.py?hl=en&answer=183668
#
#self.imgext = conf.get('sitemap_image_ext', [])
#self.vidext = conf.get('sitemap_video_ext', [])

# track output files
event.register(track, to=['create', 'update', 'skip', 'identical'])
Expand Down

0 comments on commit ee52e1f

Please sign in to comment.