Back to index

moin  1.9.0~rc2
pysupport.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - Supporting function for Python magic
00004 
00005     @copyright: 2002 Juergen Hermann <jh@web.de>
00006     @license: GNU GPL, see COPYING for details.
00007 """
00008 
00009 #############################################################################
00010 ### Module import / Plugins
00011 #############################################################################
00012 
00013 def isImportable(module):
00014     """ Check whether a certain module is available.
00015     """
00016     try:
00017         __import__(module)
00018         return 1
00019     except ImportError:
00020         return 0
00021 
00022 
00023 def getPluginModules(packagedir):
00024     """
00025     Return a list of plugin modules for a given plugin package dir,
00026     omitting any that start with an underscore.
00027     """
00028     import os, re
00029 
00030     pyre = re.compile(r"^([^_].*)\.py$")
00031     dirlist = os.listdir(packagedir)
00032     matches = [pyre.match(fn) for fn in dirlist]
00033     modules = [match.group(1) for match in matches if match]
00034 
00035     modules.sort()
00036     return modules
00037 
00038 
00039 def getPackageModules(packagefile):
00040     """ Return a list of modules for a package, omitting any modules
00041         starting with an underscore.
00042     """
00043     import os, re
00044 
00045     packagedir = os.path.dirname(packagefile)
00046 
00047     in_plugin_dir = lambda dir, ops=os.path.split: ops(ops(dir)[0])[1] == "plugin"
00048 
00049     moinmodule = __import__('MoinMoin')
00050 
00051     # Is it in a .zip file?
00052     if not in_plugin_dir(packagedir) and hasattr(moinmodule, '__loader__'):
00053         pyre = re.compile(r"^([^_].*)\.py(?:c|o)$")
00054         zipfiles = moinmodule.__loader__._files
00055         dirlist = [entry[0].replace(r'/', '\\').split('\\')[-1]
00056                    for entry in zipfiles.values() if packagedir in entry[0]]
00057     else:
00058         pyre = re.compile(r"^([^_].*)\.py$")
00059         dirlist = os.listdir(packagedir)
00060 
00061     matches = [pyre.match(fn) for fn in dirlist]
00062     modules = [match.group(1) for match in matches if match]
00063 
00064     modules.sort()
00065     return modules
00066 
00067 
00068 def importName(modulename, name):
00069     """ Import name dynamically from module
00070 
00071     Used to do dynamic import of modules and names that you know their
00072     names only in runtime.
00073 
00074     Any error raised here must be handled by the caller.
00075 
00076     @param modulename: full qualified mudule name, e.g. x.y.z
00077     @param name: name to import from modulename
00078     @rtype: any object
00079     @return: name from module
00080     """
00081     module = __import__(modulename, globals(), {}, [name])
00082     return getattr(module, name)
00083 
00084 
00085 def makeThreadSafe(function, lock=None):
00086     """ Call with a function you want to make thread safe
00087 
00088     Call without lock to make the function thread safe using one lock per
00089     function. Call with existing lock object if you want to make several
00090     functions use same lock, e.g. all functions that change same data
00091     structure.
00092 
00093     @param function: function to make thread safe
00094     @param lock: threading.Lock instance or None
00095     @rtype: function
00096     @return: function decorated with locking
00097     """
00098     if lock is None:
00099         import threading
00100         lock = threading.Lock()
00101 
00102     def decorated(*args, **kw):
00103         lock.acquire()
00104         try:
00105             return function(*args, **kw)
00106         finally:
00107             lock.release()
00108 
00109     return decorated