Helper Objects

acrylamid.helpers.memoize(key, value=None)

Persistent memory for small values, set and get in a single function. If you set a value, it returns whether the new value is different to the previous.

>>> memoize("Foo", 1)
>>> memoize("Foo", 1)
>>> memoize("Foo", 2)
>>> memoize("Foo")
  • key – get value saved to key, if key does not exist, return None.
  • value – set key to value
acrylamid.helpers.union(first, *args, **kwargs)

Takes a list of dictionaries and performs union of each. Can take additional key=values as parameters to overwrite or add key/value-pairs. No side-effects,

acrylamid.helpers.mkfile(fileobj, path, ctime=0.0, ns=None, force=False, dryrun=False)

Creates entry in filesystem. Overwrite only if fileobj differs.

  • fileobj – rendered html/xml as file-like object
  • path – path to write to
  • ctime – time needed to compile
  • force – force overwrite, even nothing has changed (defaults to False)
  • dryrun – don’t write anything.
acrylamid.helpers.expand(url, obj, re=<_sre.SRE_Pattern object at 0x106aa2c30>)

Substitutes/expands URL parameters beginning with a colon.

  • url – a URL with zero or more :key words
  • obj – a dictionary where we get key from
>>> expand('/:year/:slug/', {'year': 2012, 'slug': 'awesome title'})

Joins multiple urls pieces to one single URL without loosing the root (first element). If the URL ends with a slash, Acrylamid automatically appends index.html.

>>> joinurl('/hello/', '/world/')

Generates an ASCII-only slug. Borrowed from

acrylamid.helpers.paginate(lst, ipp, func=lambda x: x, salt=None, orphans=0)

Yields a triple ((next, current, previous), list of entries, has changed) of a paginated entrylist. It will first filter by the specified function, then split the ist into several sublists and check wether the list or an entry has changed.

  • lst – the entrylist containing Entry instances.
  • ipp – items per page
  • salt – uses as additional identifier in memoize
  • orphans – avoid N orphans on last page
>>> for x, values, _, paginate(entryrange(20), 6, orphans=2):
...    print x, values
(None, 0, 1), [entries 1..6]
(0, 1, 2), [entries 7..12]
(1, 2, None), [entries 12..20]

Safe string to fit in to the YAML standard (hopefully). Counterpart to acrylamid.readers.unsafe().

acrylamid.helpers.system(cmd, stdin=None, **kwargs)

A simple front-end to python’s horrible Popen-interface which lets you run a single shell command (only one, semicolon and && is not supported by os.execvp(). Does not catch OSError!

  • cmd – command to run (a single string or a list of strings).
  • stdin – optional string to pass to stdin.
  • kwargs – is passed to subprocess.Popen.
class acrylamid.helpers.event

This helper class provides an easy mechanism to give user feedback of created, changed or deleted files. As side-effect every it allows you to register your own functions to these events.

Acrylamid has the following, fairly self-explanatory events: create, update, skip, identical and remove. A callback receives the current namespace and the path. The namespace might be None if not specified by the originator, but it is recommended to achieve a informal standard:

  • the views supply their lowercase name such as 'entry' or 'archive' as namespace.
  • asset generation uses the 'assets' namespace.
  • the init, import and new task use their name as namespace.

To make this clear, the following example just records all newly created items from the entry view:

from acrylamid.hepers import event

skipped = []

def callback(ns, path):

    if ns == 'entry':

event.register(callback, to=['create'])


This class is a singleton and should not be initialized

Parameters:event (string) – count calls of this particular event
classmethod register(callback, to=[])

Register a callback to a list of events. Everytime the event eventuates, your callback gets called with all arguments of this particular event handler.

  • callback – a function
  • to – a list of events when your function gets called
acrylamid.helpers.rchop(original_string, substring)

Return the given string after chopping of a substring from the end.

  • original_string – the original string
  • substring – the substring to chop from the end, index, filterfunc=<function <lambda> at 0x106ae0e60>)

Import and initialize modules from directories list.

  • directories – list of directories
  • index – index function

Related Topics

This Page