comparison MoinMoin/util/pysupport.py @ 0:77665d8e2254

tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0 (automatically generated log message) imported from: moin--main--1.5--base-0
author Thomas Waldmann <tw-public@gmx.de>
date Thu, 22 Sep 2005 15:09:50 +0000
parents
children 54d5932d5a03
comparison
equal deleted inserted replaced
-1:000000000000 0:77665d8e2254
1 # -*- coding: iso-8859-1 -*-
2 """
3 MoinMoin - Supporting function for Python magic
4
5 @copyright: 2002 by Jürgen Hermann <jh@web.de>
6 @license: GNU GPL, see COPYING for details.
7 """
8
9 #############################################################################
10 ### Module import / Plugins
11 #############################################################################
12
13 import sys
14
15
16 def isImportable(module):
17 """ Check whether a certain module is available.
18 """
19 try:
20 __import__(module)
21 return 1
22 except ImportError:
23 return 0
24
25
26 def getPackageModules(packagefile):
27 """ Return a list of modules for a package, omitting any modules
28 starting with an underscore.
29 """
30 import os, re
31
32 packagedir = os.path.dirname(packagefile)
33
34 in_plugin_dir = lambda dir, ops=os.path.split: ops(ops(dir)[0])[1] == "plugin"
35
36 moinmodule = __import__('MoinMoin')
37
38 # Is it in a .zip file?
39 if not in_plugin_dir(packagedir) and hasattr(moinmodule, '__loader__'):
40 pyre = re.compile(r"^([^_].*)\.py(?:c|o)$")
41 zipfiles = moinmodule.__loader__._files
42 dirlist = [file[0].replace(r'/', '\\').split('\\')[-1]
43 for file in zipfiles.values() if packagedir in file[0]]
44 else:
45 pyre = re.compile(r"^([^_].*)\.py$")
46 dirlist = os.listdir(packagedir)
47
48 pyfiles = filter(None, map(pyre.match, dirlist))
49 modules = map(lambda x: x.group(1), pyfiles)
50
51 modules.sort()
52 return modules
53
54
55 def importName(modulename, name):
56 """ Import name dynamically from module
57
58 Used to do dynamic import of modules and names that you know their
59 names only in runtime.
60
61 @param modulename: full qualified mudule name, e.g. x.y.z
62 @param name: name to import from modulename
63 @rtype: any object
64 @return: name from module or None if there is no such name
65 """
66 try:
67 module = __import__(modulename, globals(), {}, [name])
68 return getattr(module, name, None)
69 except ImportError:
70 return None
71
72
73 def makeThreadSafe(function, lock=None):
74 """ Call with a function you want to make thread safe
75
76 Call without lock to make the function thread safe using one lock per
77 function. Call with existing lock object if you want to make several
78 functions use same lock, e.g. all functions that change same data
79 structure.
80
81 @param function: function to make thread safe
82 @param lock: threading.Lock instance or None
83 @rtype: function
84 @retrun: function decorated with locking
85 """
86 if lock is None:
87 import threading
88 lock = threading.Lock()
89
90 def decorated(*args, **kw):
91 lock.acquire()
92 try:
93 return function(*args, **kw)
94 finally:
95 lock.release()
96
97 return decorated