Package babel :: Module util

Module util



Various utility classes and functions.

Functions
bool
pathmatch(pattern, filename)
Extended pathname pattern matching.
basestring
relpath(path, start='.')
Compute the relative path to one path from another.
Variables
tzinfo UTC = <UTC>
tzinfo object for UTC (Universal Time).
tzinfo LOCALTZ = LocalTimezone()
tzinfo object for local time-zone.
Function Details

pathmatch(pattern, filename)

 

Extended pathname pattern matching.

This function is similar to what is provided by the fnmatch module in the Python standard library, but:

  • can match complete (relative or absolute) path names, and not just file names, and
  • also supports a convenience pattern ("**") to match files at any directory level.

Examples:

>>> pathmatch('**.py', 'bar.py')
True
>>> pathmatch('**.py', 'foo/bar/baz.py')
True
>>> pathmatch('**.py', 'templates/index.html')
False
>>> pathmatch('**/templates/*.html', 'templates/index.html')
True
>>> pathmatch('**/templates/*.html', 'templates/foo/bar.html')
False
Parameters:
  • pattern - the glob pattern
  • filename - the path name of the file to match against
Returns: bool
True if the path name matches the pattern, False otherwise

relpath(path, start='.')

 

Compute the relative path to one path from another.

>>> relpath('foo/bar.txt', '').replace(os.sep, '/')
'foo/bar.txt'
>>> relpath('foo/bar.txt', 'foo').replace(os.sep, '/')
'bar.txt'
>>> relpath('foo/bar.txt', 'baz').replace(os.sep, '/')
'../foo/bar.txt'
Returns: basestring
the relative path