Back to index

moin  1.9.0~rc2
Functions
MoinMoin.util.pysupport Namespace Reference

Functions

def isImportable
 Module import / Plugins.
def getPluginModules
def getPackageModules
def importName
def makeThreadSafe

Function Documentation

Return a list of modules for a package, omitting any modules
    starting with an underscore.

Definition at line 39 of file pysupport.py.

00039 
00040 def getPackageModules(packagefile):
00041     """ Return a list of modules for a package, omitting any modules
00042         starting with an underscore.
00043     """
00044     import os, re
00045 
00046     packagedir = os.path.dirname(packagefile)
00047 
00048     in_plugin_dir = lambda dir, ops=os.path.split: ops(ops(dir)[0])[1] == "plugin"
00049 
00050     moinmodule = __import__('MoinMoin')
00051 
00052     # Is it in a .zip file?
00053     if not in_plugin_dir(packagedir) and hasattr(moinmodule, '__loader__'):
00054         pyre = re.compile(r"^([^_].*)\.py(?:c|o)$")
00055         zipfiles = moinmodule.__loader__._files
00056         dirlist = [entry[0].replace(r'/', '\\').split('\\')[-1]
00057                    for entry in zipfiles.values() if packagedir in entry[0]]
00058     else:
00059         pyre = re.compile(r"^([^_].*)\.py$")
00060         dirlist = os.listdir(packagedir)
00061 
00062     matches = [pyre.match(fn) for fn in dirlist]
00063     modules = [match.group(1) for match in matches if match]
00064 
00065     modules.sort()
00066     return modules
00067 

Return a list of plugin modules for a given plugin package dir,
omitting any that start with an underscore.

Definition at line 23 of file pysupport.py.

00023 
00024 def getPluginModules(packagedir):
00025     """
00026     Return a list of plugin modules for a given plugin package dir,
00027     omitting any that start with an underscore.
00028     """
00029     import os, re
00030 
00031     pyre = re.compile(r"^([^_].*)\.py$")
00032     dirlist = os.listdir(packagedir)
00033     matches = [pyre.match(fn) for fn in dirlist]
00034     modules = [match.group(1) for match in matches if match]
00035 
00036     modules.sort()
00037     return modules
00038 

def MoinMoin.util.pysupport.importName (   modulename,
  name 
)
Import name dynamically from module

Used to do dynamic import of modules and names that you know their
names only in runtime.

Any error raised here must be handled by the caller.

@param modulename: full qualified mudule name, e.g. x.y.z
@param name: name to import from modulename
@rtype: any object
@return: name from module

Definition at line 68 of file pysupport.py.

00068 
00069 def importName(modulename, name):
00070     """ Import name dynamically from module
00071 
00072     Used to do dynamic import of modules and names that you know their
00073     names only in runtime.
00074 
00075     Any error raised here must be handled by the caller.
00076 
00077     @param modulename: full qualified mudule name, e.g. x.y.z
00078     @param name: name to import from modulename
00079     @rtype: any object
00080     @return: name from module
00081     """
00082     module = __import__(modulename, globals(), {}, [name])
00083     return getattr(module, name)
00084 

Module import / Plugins.

Check whether a certain module is available.

Definition at line 13 of file pysupport.py.

00013 
00014 def isImportable(module):
00015     """ Check whether a certain module is available.
00016     """
00017     try:
00018         __import__(module)
00019         return 1
00020     except ImportError:
00021         return 0
00022 

def MoinMoin.util.pysupport.makeThreadSafe (   function,
  lock = None 
)
Call with a function you want to make thread safe

Call without lock to make the function thread safe using one lock per
function. Call with existing lock object if you want to make several
functions use same lock, e.g. all functions that change same data
structure.

@param function: function to make thread safe
@param lock: threading.Lock instance or None
@rtype: function
@return: function decorated with locking

Definition at line 85 of file pysupport.py.

00085 
00086 def makeThreadSafe(function, lock=None):
00087     """ Call with a function you want to make thread safe
00088 
00089     Call without lock to make the function thread safe using one lock per
00090     function. Call with existing lock object if you want to make several
00091     functions use same lock, e.g. all functions that change same data
00092     structure.
00093 
00094     @param function: function to make thread safe
00095     @param lock: threading.Lock instance or None
00096     @rtype: function
00097     @return: function decorated with locking
00098     """
00099     if lock is None:
00100         import threading
00101         lock = threading.Lock()
00102 
00103     def decorated(*args, **kw):
00104         lock.acquire()
00105         try:
00106             return function(*args, **kw)
00107         finally:
00108             lock.release()
00109 
00110     return decorated