aliases

global aliases.

fbf.lib.aliases.aliascheck(ievent)

check if alias is available.

fbf.lib.aliases.getaliases(ddir=None, force=True)

return global aliases.

fbf.lib.aliases.savealiases(ddir=None)

return global aliases.

fbf.lib.aliases.setalias(first, second)
fbf.lib.aliases.size()

CODE

# fbf/lib/aliases.py
#
#

""" global aliases. """

fbf imports

from fbf.lib.datadir import getdatadir
from fbf.utils.lazydict import LazyDict

basic imports

import os
import logging

defines

aliases = LazyDict()

getaliases function

def getaliases(ddir=None, force=True):
    """ return global aliases. """
    global aliases
    if not aliases or force:
        from fbf.lib.persist import Persist
        from fbf.utils.lazydict import LazyDict
        d = ddir or getdatadir()
        p = Persist(d + os.sep + "run" + os.sep + "aliases")
        if not p.data: p.data = LazyDict()
        aliases = p.data
    return aliases

def savealiases(ddir=None):
    """ return global aliases. """
    global aliases
    if aliases:
        logging.warn("saving aliases")
        from fbf.lib.persist import Persist
        from fbf.utils.lazydict import LazyDict
        d = ddir or getdatadir()
        p = Persist(d + os.sep + "run" + os.sep + "aliases")
        p.data = aliases
        p.save()
    return aliases

def aliascheck(ievent):
    """ check if alias is available. """
    if not ievent.execstr: return
    try:
        cmnd = ievent.execstr.split()[0]
        alias = aliases[cmnd]
        ievent.txt = ievent.txt.replace(cmnd, alias, 1)
        ievent.execstr = ievent.execstr.replace(cmnd, alias, 1)
        ievent.alias = alias
        ievent.aliased = cmnd
        ievent.prepare()
    except (IndexError, KeyError): pass


def size():
    return len(aliases)

def setalias(first, second):
    global aliases
    aliases[first] = second

Table Of Contents

Previous topic

lib

Next topic

boot

This Page