Package Pyblosxom :: Module tools
[show private | hide private]
[frames | no frames]

Module Pyblosxom.tools

Tools module

The swiss army knife for all things pyblosxom
Classes
LogFilter Filters out messages from log-channels that are not listed in the log_filter config variable.
Replacer Class for replacing variables in a template
Stripper Strips HTML
VariableDict Wraps around a standard dict allowing for escaped and urlencoding of internal data by tacking on a "_urlencoded" or a "_escaped" to the end of the key name.

Function Summary
  cleanup()
Cleanup the tools module.
  escape_text(s)
tuple of 9 ints filestat(request, filename)
Returns the filestat on a given file.
string generateRandStr(minlen, maxlen)
Generate a random string
Pyblosxom.cache.base.BlosxomCacheBase subclass get_cache(request)
Retrieves the cache from the request or fetches a new CacheDriver instance.
logging.Logger for Python >=2.3, Pyblosxom._logging.Logger for Python <2.3 getLogger(log_file)
Creates and retuns a log channel.
object importName(modulename, name)
Module importer
  initialize(config)
Initialize the tools module.
boolean is_year(s)
Checks s to see if it's likely to be a year or not.
  lock(f, flags)
  log_caller(frame_num, log_file)
Logs some info about the calling function/method.
  log_exception(log_file)
Logs an exception to the given file.
string parse(request, encoding, var_dict, template)
This method parses the open file object passed, replacing any keys found using the replacement dictionary passed.
  render_url(cdict, pathinfo, querystring)
Takes a url and a querystring and renders the page that corresponds with that by creating a Request and a PyBlosxom object and passing it through.
dict run_callback(chain, input, mappingfunc, donefunc, defaultfunc)
Executes a callback chain on a given piece of data.
  unlock(f)
  update_static_entry(cdict, entry_filename)
This is a utility function that allows plugins to easily update statically rendered entries without going through all the rigamarole.
  urlencode_text(s)
list Walk(request, root, recurse, pattern, return_folders)
This function walks a directory tree starting at a specified root folder, and returns a list of all of the files (and optionally folders) that match our pattern(s).
string what_ext(extensions, filepath)
Takes in a filepath and a list of extensions and tries them all until it finds the first extension that works.
  __walk_internal(root, recurse, pattern, ignorere, return_folders)

Variable Summary
str __revision__ = '$Id: tools.py,v 1.50 2005/12/09 05:09:11...
dict month2num: A dict of literal months to its number format
list MONTHS: A list of valid literal and numeral months
dict num2month: A dict of number month format to its literal format
SRE_Pattern VAR_REGEXP: Regular expression for detection and substituion of variables
NoneType _config = None                                                                  
dict _loghandler_registry = {}
bool _use_custom_logger = False

Function Details

cleanup()

Cleanup the tools module. This should be called from Pyblosxom.pyblosxom.PyBlosxom.cleanup.

filestat(request, filename)

Returns the filestat on a given file. We store the filestat in case we've already retrieved it this time.
Parameters:
request - the Pyblosxom Request object
           (type=Request)
filename - the name of the file to stat
           (type=string)
Returns:
the mtime of the file (same as returned by time.localtime(...))
           (type=tuple of 9 ints)

generateRandStr(minlen=5, maxlen=10)

Generate a random string

Tool to generate a random string between minlen to maxlen characters
Parameters:
minlen - The minimum length the string should be
           (type=integer)
maxlen - The maximum length the string could be
           (type=integer)
Returns:
A string containing random characters
           (type=string)

get_cache(request)

Retrieves the cache from the request or fetches a new CacheDriver instance.
Parameters:
request - the Request object for this run
           (type=Request)
Returns:
A BlosxomCache object reference
           (type=Pyblosxom.cache.base.BlosxomCacheBase subclass)

getLogger(log_file=None)

Creates and retuns a log channel. If no log_file is given the system-wide logfile as defined in config.py is used. If a log_file is given that's where the created logger logs to.
Parameters:
log_file - optional, the file to log to.
           (type=str)
Returns:
a log channel (Logger instance)
           (type=logging.Logger for Python >=2.3, Pyblosxom._logging.Logger for Python <2.3)

importName(modulename, name)

Module importer

For modules that can only be determined during runtime
Parameters:
modulename - The base name of the module to import from
           (type=string)
name - The name of the module to import from the modulename
           (type=string)
Returns:
If successful, returns an imported object reference, else None
           (type=object)

initialize(config)

Initialize the tools module. This gives the module a chance to use configuration from the pyblosxom config.py file. This should be called from Pyblosxom.pyblosxom.PyBlosxom.initialize.

is_year(s)

Checks s to see if it's likely to be a year or not. In order to be considered to be a year, it must pass the following criteria:
  1. four digits
  2. first two digits are either 19 or 20.
Parameters:
s - the string to check for "year-hood"
           (type=string)
Returns:
1 if s is likely to be a year or 0 if it is not
           (type=boolean)

log_caller(frame_num=1, log_file=None)

Logs some info about the calling function/method.
Usefull for debugging.
Usage:
    import tools
    tools.log_caller() # logs frame 1
    tools.log_caller(2)
    tools.log_caller(3, log_file="/path/to/file")

@param frame_num: optional, index of the frame
@type frame_num: C{int}
@param log_file: optional, the file to log to
@type log_file: C{str}

log_exception(log_file=None)

Logs an exception to the given file. Uses the system-wide log_file as defined in config.py if none is given here.
Parameters:
log_file - optional, the file to log to
           (type=str)

parse(request, encoding, var_dict, template)

This method parses the open file object passed, replacing any keys found using the replacement dictionary passed. Uses the Replacer object. From OPAGCGI library
Parameters:
request - the Request object
           (type=Request)
encoding - the encoding to use
           (type=string)
var_dict - The name value pair list containing variable replacements
           (type=dict)
template - A template file with placeholders for variable replacements
           (type=string)
Returns:
Substituted template
           (type=string)

render_url(cdict, pathinfo, querystring='')

Takes a url and a querystring and renders the page that corresponds with that by creating a Request and a PyBlosxom object and passing it through.
Parameters:
cdict - the config.py dict
           (type=dict)
pathinfo - the path_info string. ex: "/dev/pyblosxom/firstpost.html"
           (type=string)
querystring - the querystring (if any). ex: "debug=yes"
           (type=string)

run_callback(chain, input, mappingfunc=<function <lambda> at 0x40268ed4>, donefunc=<function <lambda> at 0x40268f0c>, defaultfunc=None)

Executes a callback chain on a given piece of data. passed in is a dict of name/value pairs. Consult the documentation for the specific callback chain you're executing.

Callback chains should conform to their documented behavior. This function allows us to do transforms on data, handling data, and also callbacks.

The difference in behavior is affected by the mappingfunc passed in which converts the output of a given function in the chain to the input for the next function.

If this is confusing, read through the code for this function.
Parameters:
chain - the callback chain to run
           (type=string)
input - data is a dict filled with name/value pairs--refer to the callback chain documentation for what's in the data dict.
           (type=dict)
mappingfunc - the function that maps output arguments to input arguments for the next iteration. It must take two arguments: the original dict and the return from the previous function. It defaults to returning the original dict.
           (type=function)
donefunc - this function tests whether we're done doing what we're doing. This function takes as input the output of the most recent iteration. If this function returns true (1) then we'll drop out of the loop. For example, if you wanted a callback to stop running when one of the registered functions returned a 1, then you would pass in "donefunc=lambda x:x".
           (type=function)
defaultfunc - if this is set and we finish going through all the functions in the chain and none of them have returned something that satisfies the donefunc, then we'll execute the defaultfunc with the latest version of the input dict.
           (type=function)
Returns:
the transformed dict
           (type=dict)

update_static_entry(cdict, entry_filename)

This is a utility function that allows plugins to easily update statically rendered entries without going through all the rigamarole.

First we figure out whether this blog is set up for static rendering. If not, then we return--no harm done.

If we are, then we call render_url for each static_flavour of the entry and then for each static_flavour of the index page.
Parameters:
cdict - the config.py dict
           (type=dict)
entry_filename - the filename of the entry (ex. /movies/xmen2)
           (type=string)

Walk(request, root='.', recurse=0, pattern='', return_folders=0)

This function walks a directory tree starting at a specified root folder, and returns a list of all of the files (and optionally folders) that match our pattern(s). Taken from the online Python Cookbook and modified to own needs.

It will look at the config "ignore_directories" for a list of directories to ignore. It uses a regexp that joins all the things you list. So the following:
  config.py["ignore_directories"] = ["CVS", "dev/pyblosxom"]
turns into the regexp:
  .*?(CVS|dev/pyblosxom)$
It will also skip all directories that start with a period.
Parameters:
request - the Request object
           (type=Request)
root - Starting point to walk from
           (type=string)
recurse - Depth of recursion,
  • 0: All the way
  • 1: Just this level
  • n: n depth of recursion

           (type=integer)
pattern - A re.compile'd object
           (type=object)
return_folders - If true, just return list of folders
           (type=boolean)
Returns:
A list of file paths
           (type=list)

what_ext(extensions, filepath)

Takes in a filepath and a list of extensions and tries them all until it finds the first extension that works.
Parameters:
extensions - the list of extensions to test
           (type=list of strings)
filepath - the complete file path (minus the extension) to test
           (type=string)
Returns:
the extension that was successful or None
           (type=string)

Variable Details

__revision__

Type:
str
Value:
'$Id: tools.py,v 1.50 2005/12/09 05:09:11 willhelm Exp $'              

month2num

A dict of literal months to its number format
Type:
dict
Value:
{'Apr': '04',
 'Aug': '08',
 'Dec': '12',
 'Feb': '02',
 'Jan': '01',
 'Jul': '07',
 'Jun': '06',
 'Mar': '03',
...                                                                    

MONTHS

A list of valid literal and numeral months
Type:
list
Value:
[0, 1, 2, 3, 4, 5, 6, 7, 8]                                            

num2month

A dict of number month format to its literal format
Type:
dict
Value:
{0: 'nil',
 1: 'Jan',
 2: 'Feb',
 3: 'Mar',
 4: 'Apr',
 5: 'May',
 6: 'Jun',
 7: 'Jul',
...                                                                    

VAR_REGEXP

Regular expression for detection and substituion of variables
Type:
SRE_Pattern
Value:
(?!\\)\$((?:\w|-|::\w)+(?:\(.*?(?!\\)\))?)                             

_config

Type:
NoneType
Value:
None                                                                  

_loghandler_registry

Type:
dict
Value:
{}                                                                     

_use_custom_logger

Type:
bool
Value:
False                                                                  

Generated by Epydoc 2.1 on Tue Feb 7 21:43:58 2006 http://epydoc.sf.net