|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2007 - 2009 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the APIsManager. |
|
8 """ |
|
9 |
|
10 import os |
|
11 |
|
12 from PyQt4.QtCore import * |
|
13 from PyQt4.Qsci import QsciAPIs, QsciLexer |
|
14 |
|
15 import Lexers |
|
16 import Preferences |
|
17 import Utilities |
|
18 |
|
19 class APIs(QObject): |
|
20 """ |
|
21 Class implementing an API storage entity. |
|
22 |
|
23 @signal apiPreparationFinished() emitted after the API preparation has finished |
|
24 @signal apiPreparationCancelled() emitted after the API preparation has been cancelled |
|
25 @signal apiPreparationStarted() emitted after the API preparation has started |
|
26 """ |
|
27 def __init__(self, language, forPreparation = False, parent = None): |
|
28 """ |
|
29 Constructor |
|
30 |
|
31 @param language language of the APIs object (string) |
|
32 @param forPreparation flag indicating this object is just needed |
|
33 for a preparation process (boolean) |
|
34 @param parent reference to the parent object (QObject) |
|
35 """ |
|
36 QObject.__init__(self, parent) |
|
37 self.setObjectName("APIs_%s" % language) |
|
38 |
|
39 self.__inPreparation = False |
|
40 self.__language = language |
|
41 self.__forPreparation = forPreparation |
|
42 self.__lexer = Lexers.getLexer(self.__language) |
|
43 self.__apifiles = Preferences.getEditorAPI(self.__language) |
|
44 self.__apifiles.sort() |
|
45 if self.__lexer is None: |
|
46 self.__apis = None |
|
47 else: |
|
48 self.__apis = QsciAPIs(self.__lexer) |
|
49 self.connect(self.__apis, SIGNAL("apiPreparationFinished()"), |
|
50 self.__apiPreparationFinished) |
|
51 self.connect(self.__apis, SIGNAL("apiPreparationCancelled()"), |
|
52 self.__apiPreparationCancelled) |
|
53 self.connect(self.__apis, SIGNAL("apiPreparationStarted()"), |
|
54 self.__apiPreparationStarted) |
|
55 self.__loadAPIs() |
|
56 |
|
57 def __loadAPIs(self): |
|
58 """ |
|
59 Private method to load the APIs. |
|
60 """ |
|
61 if self.__apis.isPrepared(): |
|
62 # load a prepared API file |
|
63 if not self.__forPreparation and Preferences.getEditor("AutoPrepareAPIs"): |
|
64 self.prepareAPIs() |
|
65 self.__apis.loadPrepared() |
|
66 else: |
|
67 # load the raw files and prepare the API file |
|
68 if not self.__forPreparation and Preferences.getEditor("AutoPrepareAPIs"): |
|
69 self.prepareAPIs(ondemand = True) |
|
70 |
|
71 def reloadAPIs(self): |
|
72 """ |
|
73 Public method to reload the API information. |
|
74 """ |
|
75 if not self.__forPreparation and Preferences.getEditor("AutoPrepareAPIs"): |
|
76 self.prepareAPIs() |
|
77 self.__loadAPIs() |
|
78 |
|
79 def getQsciAPIs(self): |
|
80 """ |
|
81 Public method to get a reference to QsciAPIs object. |
|
82 |
|
83 @return reference to the QsciAPIs object (QsciAPIs) |
|
84 """ |
|
85 if not self.__forPreparation and Preferences.getEditor("AutoPrepareAPIs"): |
|
86 self.prepareAPIs() |
|
87 return self.__apis |
|
88 |
|
89 def __apiPreparationFinished(self): |
|
90 """ |
|
91 Private method called to save an API, after it has been prepared. |
|
92 """ |
|
93 res = self.__apis.savePrepared() |
|
94 self.__inPreparation = False |
|
95 self.emit(SIGNAL('apiPreparationFinished()')) |
|
96 |
|
97 def __apiPreparationCancelled(self): |
|
98 """ |
|
99 Private method called, after the API preparation process has been cancelled. |
|
100 """ |
|
101 self.__inPreparation = False |
|
102 self.emit(SIGNAL('apiPreparationCancelled()')) |
|
103 |
|
104 def __apiPreparationStarted(self): |
|
105 """ |
|
106 Private method called, when the API preparation process started. |
|
107 """ |
|
108 self.__inPreparation = True |
|
109 self.emit(SIGNAL('apiPreparationStarted()')) |
|
110 |
|
111 def prepareAPIs(self, ondemand = False, rawList = None): |
|
112 """ |
|
113 Public method to prepare the APIs if necessary. |
|
114 |
|
115 @keyparam ondemand flag indicating a requested preparation (boolean) |
|
116 @keyparam rawList list of raw API files (list of strings) |
|
117 """ |
|
118 if self.__apis is None or self.__inPreparation: |
|
119 return |
|
120 |
|
121 needsPreparation = False |
|
122 if ondemand: |
|
123 needsPreparation = True |
|
124 else: |
|
125 # check, if a new preparation is necessary |
|
126 preparedAPIs = self.__defaultPreparedName() |
|
127 if preparedAPIs: |
|
128 preparedAPIsInfo = QFileInfo(preparedAPIs) |
|
129 if not preparedAPIsInfo.exists(): |
|
130 needsPreparation = True |
|
131 else: |
|
132 preparedAPIsTime = preparedAPIsInfo.lastModified() |
|
133 apifiles = Preferences.getEditorAPI(self.__language) |
|
134 apifiles.sort() |
|
135 if self.__apifiles != apifiles: |
|
136 needsPreparation = True |
|
137 for apifile in apifiles: |
|
138 if QFileInfo(apifile).lastModified() > preparedAPIsTime: |
|
139 needsPreparation = True |
|
140 break |
|
141 |
|
142 if needsPreparation: |
|
143 # do the preparation |
|
144 self.__apis.clear() |
|
145 if rawList: |
|
146 apifiles = rawList |
|
147 else: |
|
148 apifiles = Preferences.getEditorAPI(self.__language) |
|
149 for apifile in apifiles: |
|
150 self.__apis.load(apifile) |
|
151 self.__apis.prepare() |
|
152 self.__apifiles = apifiles |
|
153 |
|
154 def cancelPreparation(self): |
|
155 """ |
|
156 Public slot to cancel the APIs preparation. |
|
157 """ |
|
158 self.__apis and self.__apis.cancelPreparation() |
|
159 |
|
160 def installedAPIFiles(self): |
|
161 """ |
|
162 Public method to get a list of installed API files. |
|
163 |
|
164 @return list of installed API files (list of strings) |
|
165 """ |
|
166 if self.__apis is not None: |
|
167 return self.__apis.installedAPIFiles() |
|
168 else: |
|
169 return [] |
|
170 |
|
171 def __defaultPreparedName(self): |
|
172 """ |
|
173 Private method returning the default name of a prepared API file. |
|
174 |
|
175 @return complete filename for the Prepared APIs file (string) |
|
176 """ |
|
177 if self.__apis is not None: |
|
178 return self.__apis.defaultPreparedName() |
|
179 else: |
|
180 return "" |
|
181 |
|
182 class APIsManager(QObject): |
|
183 """ |
|
184 Class implementing the APIsManager class, which is the central store for |
|
185 API information used by autocompletion and calltips. |
|
186 """ |
|
187 def __init__(self, parent = None): |
|
188 """ |
|
189 Constructor |
|
190 |
|
191 @param parent reference to the parent object (QObject) |
|
192 """ |
|
193 QObject.__init__(self, parent) |
|
194 self.setObjectName("APIsManager") |
|
195 |
|
196 self.__apis = {} |
|
197 |
|
198 def reloadAPIs(self): |
|
199 """ |
|
200 Public slot to reload the api information. |
|
201 """ |
|
202 for api in self.__apis.values(): |
|
203 api and api.reloadAPIs() |
|
204 |
|
205 def getAPIs(self, language, forPreparation = False): |
|
206 """ |
|
207 Public method to get an apis object for autocompletion/calltips. |
|
208 |
|
209 This method creates and loads an APIs object dynamically upon request. |
|
210 This saves memory for languages, that might not be needed at the moment. |
|
211 |
|
212 @param language the language of the requested api object (string) |
|
213 @param forPreparation flag indicating the requested api object is just needed |
|
214 for a preparation process (boolean) |
|
215 @return the apis object (APIs) |
|
216 """ |
|
217 if forPreparation: |
|
218 return APIs(language, forPreparation = forPreparation) |
|
219 else: |
|
220 try: |
|
221 return self.__apis[language] |
|
222 except KeyError: |
|
223 if language in Lexers.getSupportedLanguages(): |
|
224 # create the api object |
|
225 self.__apis[language] = APIs(language) |
|
226 return self.__apis[language] |
|
227 else: |
|
228 return None |