|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2005 - 2009 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Package implementing class browsers for various languages. |
|
8 |
|
9 Currently it offers class browser support for the following |
|
10 programming languages. |
|
11 |
|
12 <ul> |
|
13 <li>CORBA IDL</li> |
|
14 <li>Python</li> |
|
15 <li>Ruby</li> |
|
16 </ul> |
|
17 """ |
|
18 |
|
19 import os |
|
20 import imp |
|
21 |
|
22 import Preferences |
|
23 |
|
24 PY_SOURCE = imp.PY_SOURCE |
|
25 PTL_SOURCE = 128 |
|
26 RB_SOURCE = 129 |
|
27 IDL_SOURCE = 130 |
|
28 |
|
29 SUPPORTED_TYPES = [PY_SOURCE, PTL_SOURCE, RB_SOURCE, IDL_SOURCE] |
|
30 |
|
31 __extensions = { |
|
32 "IDL" : [".idl"], |
|
33 "Python" : [".py", ".pyw", ".ptl"], |
|
34 "Ruby" : [".rb"], |
|
35 } |
|
36 |
|
37 def readmodule(module, path=[], isPyFile = False): |
|
38 ''' |
|
39 Read a source file and return a dictionary of classes, functions, modules, etc. . |
|
40 |
|
41 The real work of parsing the source file is delegated to the individual file |
|
42 parsers. |
|
43 |
|
44 @param module name of the source file (string) |
|
45 @param path path the file should be searched in (list of strings) |
|
46 @return the resulting dictionary |
|
47 ''' |
|
48 ext = os.path.splitext(module)[1].lower() |
|
49 |
|
50 if ext in __extensions["IDL"]: |
|
51 import idlclbr |
|
52 dict = idlclbr.readmodule_ex(module, path) |
|
53 idlclbr._modules.clear() |
|
54 elif ext in __extensions["Ruby"]: |
|
55 import rbclbr |
|
56 dict = rbclbr.readmodule_ex(module, path) |
|
57 rbclbr._modules.clear() |
|
58 elif ext in Preferences.getPython("PythonExtensions") or \ |
|
59 ext in Preferences.getPython("Python3Extensions") or \ |
|
60 isPyFile: |
|
61 import pyclbr |
|
62 dict = pyclbr.readmodule_ex(module, path, isPyFile = isPyFile) |
|
63 pyclbr._modules.clear() |
|
64 else: |
|
65 # try Python if it is without extension |
|
66 import pyclbr |
|
67 dict = pyclbr.readmodule_ex(module, path) |
|
68 pyclbr._modules.clear() |
|
69 |
|
70 return dict |
|
71 |
|
72 def find_module(name, path, isPyFile = False): |
|
73 """ |
|
74 Module function to extend the Python module finding mechanism. |
|
75 |
|
76 This function searches for files in the given path. If the filename |
|
77 doesn't have an extension or an extension of .py, the normal search |
|
78 implemented in the imp module is used. For all other supported files |
|
79 only path is searched. |
|
80 |
|
81 @param name filename or modulename to search for (string) |
|
82 @param path search path (list of strings) |
|
83 @return tuple of the open file, pathname and description. Description |
|
84 is a tuple of file suffix, file mode and file type) |
|
85 @exception ImportError The file or module wasn't found. |
|
86 """ |
|
87 ext = os.path.splitext(name)[1].lower() |
|
88 |
|
89 if ext in __extensions["Ruby"]: |
|
90 for p in path: # only search in path |
|
91 pathname = os.path.join(p, name) |
|
92 if os.path.exists(pathname): |
|
93 return (open(pathname), pathname, (ext, 'r', RB_SOURCE)) |
|
94 raise ImportError |
|
95 |
|
96 elif ext in __extensions["IDL"]: |
|
97 for p in path: # only search in path |
|
98 pathname = os.path.join(p, name) |
|
99 if os.path.exists(pathname): |
|
100 return (open(pathname), pathname, (ext, 'r', IDL_SOURCE)) |
|
101 raise ImportError |
|
102 |
|
103 elif ext == '.ptl': |
|
104 for p in path: # only search in path |
|
105 pathname = os.path.join(p, name) |
|
106 if os.path.exists(pathname): |
|
107 return (open(pathname), pathname, (ext, 'r', PTL_SOURCE)) |
|
108 raise ImportError |
|
109 |
|
110 if name.lower().endswith('.py'): |
|
111 name = name[:-3] |
|
112 if type(name) == type(u""): |
|
113 name = name.encode('utf-8') |
|
114 |
|
115 try: |
|
116 return imp.find_module(name, path) |
|
117 except ImportError: |
|
118 if name.lower().endswith(tuple(Preferences.getPython("PythonExtensions") + \ |
|
119 Preferences.getPython("Python3Extensions"))) or \ |
|
120 isPyFile: |
|
121 for p in path: # search in path |
|
122 pathname = os.path.join(p, name) |
|
123 if os.path.exists(pathname): |
|
124 return (open(pathname), pathname, (ext, 'r', PY_SOURCE)) |
|
125 raise ImportError |