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

Module tools

source code

Tools module

The swiss army knife for all things pyblosxom

Classes [hide private]
  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
SGMLParser that removes HTML formatting code.
  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.
Functions [hide private]
list
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).
source code
 
__walk_internal(root, recurse, pattern, ignorere, return_folders)
Note: This is an internal function--don't use it and don't expect it to stay the same between PyBlosxom releases.
source code
 
cleanup()
Cleanup the tools module.
source code
 
create_entry(datadir, category, filename, mtime, title, metadata, body)
Creates a new entry in the blog.
source code
string
escape_text(s)
Takes in a string and escapes ' to ' and " to ".
source code
tuple of 9 ints
filestat(request, filename)
Returns the filestat on a given file.
source code
string
generateRandStr(minlen=5, maxlen=10)
Generate a random string
source code
logging.Logger for Python >=2.3, Pyblosxom._logging.Logger for Python <2.3
getLogger(log_file=None)
Creates and retuns a log channel.
source code
Pyblosxom.cache.base.BlosxomCacheBase subclass
get_cache(request)
Retrieves the cache from the request or fetches a new CacheDriver instance.
source code
object
importname(modulename, name)
Imports modules for modules that can only be determined during runtime.
source code
 
initialize(config)
Initialize the tools module.
source code
boolean
is_year(checks)
Checks to see if the string is likely to be a year or not.
source code
 
lock(f, flags) source code
 
log_caller(frame_num=1, log_file=None)
Logs some info about the calling function/method.
source code
 
log_exception(log_file=None)
Logs an exception to the given file.
source code
string
parse(request, encoding, var_dict, template)
This method parses the open file object passed, replacing any keys found using the replacement dictionary passed.
source code
list of tuples of (string, string)
parse_args(args)
Takes in a list of args and parses it out into a hashmap of arg-name to value(s).
source code
 
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.
source code
 
render_url_statically(cdict, url, q) source code
dict
run_callback(chain, input, mappingfunc=<function <lambda> at 0x84db9cc>, donefunc=<function <lambda> at 0x84dba04>, defaultfunc=None)
Executes a callback chain on a given piece of data.
source code
 
unlock(f) source code
 
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.
source code
string
urlencode_text(s)
Calls urllib.quote on the string.
source code
list
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).
source code
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.
source code
Variables [hide private]
  LOCK_EX = 2
  LOCK_NB = 4
  LOCK_SH = 1
  MONTHS = None
A list of valid literal and numeral months
  QUOTES = {'"': '&quot;', '\'': '&apos;'}
  VAR_REGEXP = re.compile(r'(?<!\\)\$((?:\w|-|::\w)+(?:\(.*?(?<!...
Regular expression for detection and substituion of variables
  __revision__ = '$Revision: 1061 $'
  _config = None
  _loghandler_registry = {}
  _use_custom_logger = False
  month2num = None
A dict of literal months to its number format
  num2month = None
A dict of number month format to its literal format
Function Details [hide private]

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

source code 

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 (Request) - the Request object
  • root (string) - Starting point to walk from
  • recurse (integer) - Depth of recursion,
    • 0: All the way
    • 1: Just this level
    • n: n depth of recursion
  • pattern (object) - A re.compile'd object
  • return_folders (boolean) - If true, just return list of folders
Returns: list
A list of file paths

cleanup()

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

create_entry(datadir, category, filename, mtime, title, metadata, body)

source code 

Creates a new entry in the blog.

This is primarily used by the testing system, but it could be used by scripts and other tools.
Parameters:
  • datadir (string) - the directory of the datadir where the blog entries are stored.
  • category (string) - the category of the entry.
  • filename (string) - the name of the blog entry (filename and extension).
  • mtime (float) - the mtime for the entry (seconds since the epoch).
  • title (string) - the title for the entry.
  • metadata (dict) - any metadata for this entry.
  • body (string) - the content of the entry.
Raises:
  • IOError - if the datadir + category directory exists, but isn't a directory.

escape_text(s)

source code 
Takes in a string and escapes ' to &apos; and " to &quot;. If s is None, then we return None.
Parameters:
  • s (string) - the input string to escape
Returns: string
the escaped string

filestat(request, filename)

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

generateRandStr(minlen=5, maxlen=10)

source code 

Generate a random string

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

getLogger(log_file=None)

source code 
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 (str) - optional, the file to log to.
Returns: logging.Logger for Python >=2.3, Pyblosxom._logging.Logger for Python <2.3
a log channel (Logger instance)

get_cache(request)

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

importname(modulename, name)

source code 
Imports modules for modules that can only be determined during runtime.
Parameters:
  • modulename (string) - The base name of the module to import from
  • name (string) - The name of the module to import from the modulename
Returns: object
If successful, returns an imported object reference, else None

initialize(config)

source code 
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(checks)

source code 
Checks to see if the string is 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:
  • checks (string) - the string to check for "year-hood"
Returns: boolean
1 if checks is likely to be a year or 0 if it is not

log_caller(frame_num=1, log_file=None)

source code 

Logs some info about the calling function/method. Useful for debugging.

Usage:
   import tools
   tools.log_caller() # logs frame 1
   tools.log_caller(2)
   tools.log_caller(3, log_file="/path/to/file")
Parameters:
  • frame_num (int) - optional, index of the frame
  • log_file (str) - optional, the file to log to

log_exception(log_file=None)

source code 
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 (str) - optional, the file to log to

parse(request, encoding, var_dict, template)

source code 
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 (Request) - the Request object
  • encoding (string) - the encoding to use
  • var_dict (dict) - The name value pair list containing variable replacements
  • template (string) - A template file with placeholders for variable replacements
Returns: string
Substituted template

parse_args(args)

source code 
Takes in a list of args and parses it out into a hashmap of arg-name to value(s).
Parameters:
  • args (list of strings) - the list of command-line arguments
Returns: list of tuples of (string, string)
list of tuples of (arg, value) pairings

render_url(cdict, pathinfo, querystring='')

source code 
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. It then returns the resulting Response.
Parameters:
  • cdict (dict) - the config.py dict
  • pathinfo (string) - the path_info string. ex: /dev/pyblosxom/firstpost.html
  • querystring (string) - the querystring (if any). ex: debug=yes
Returns:
Response

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

source code 

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 (string) - the callback chain to run
  • input (dict) - data is a dict filled with name/value pairs--refer to the callback chain documentation for what's in the data dict.
  • mappingfunc (function) - 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.
  • donefunc (function) - 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 .
  • defaultfunc (function) - 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.
Returns: dict
the transformed dict

update_static_entry(cdict, entry_filename)

source code 

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 (dict) - the config.py dict
  • entry_filename (string) - the filename of the entry (ex. /movies/xmen2)

urlencode_text(s)

source code 
Calls urllib.quote on the string. If is None, then we return None.
Parameters:
  • s (string) - the string to be urlencoded.
Returns: string
the urlencoded string

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

source code 

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 (Request) - the Request object
  • root (string) - Starting point to walk from
  • recurse (integer) - Depth of recursion,
    • 0: All the way
    • 1: Just this level
    • n: n depth of recursion
  • pattern (object) - A re.compile'd object
  • return_folders (boolean) - If true, just return list of folders
Returns: list
A list of file paths

what_ext(extensions, filepath)

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

Variables Details [hide private]

VAR_REGEXP

Regular expression for detection and substituion of variables
Value:
re.compile(r'(?<!\\)\$((?:\w|-|::\w)+(?:\(.*?(?<!\\)\))?)')