QScintilla/APIsManager.py

branch
APIs
changeset 5349
bccda3b5920a
parent 4631
5c1a96925da4
child 5358
2584d0a08bf4
equal deleted inserted replaced
5348:96197e13fdf3 5349:bccda3b5920a
32 """ 32 """
33 apiPreparationFinished = pyqtSignal() 33 apiPreparationFinished = pyqtSignal()
34 apiPreparationCancelled = pyqtSignal() 34 apiPreparationCancelled = pyqtSignal()
35 apiPreparationStarted = pyqtSignal() 35 apiPreparationStarted = pyqtSignal()
36 36
37 def __init__(self, language, forPreparation=False, parent=None): 37 def __init__(self, language, projectType="", forPreparation=False,
38 parent=None):
38 """ 39 """
39 Constructor 40 Constructor
40 41
41 @param language language of the APIs object (string) 42 @param language language of the APIs object
43 @type str
44 @param projectType type of the project
45 @type str
42 @param forPreparation flag indicating this object is just needed 46 @param forPreparation flag indicating this object is just needed
43 for a preparation process (boolean) 47 for a preparation process
44 @param parent reference to the parent object (QObject) 48 @type bool
49 @param parent reference to the parent object
50 @type QObject
45 """ 51 """
46 super(APIs, self).__init__(parent) 52 super(APIs, self).__init__(parent)
47 self.setObjectName("APIs_{0}".format(language)) 53 if projectType:
54 self.setObjectName("APIs_{0}_{1}".format(language, projectType))
55 else:
56 self.setObjectName("APIs_{0}".format(language))
48 57
49 self.__inPreparation = False 58 self.__inPreparation = False
50 self.__language = language 59 self.__language = language
60 self.__projectType = projectType
51 self.__forPreparation = forPreparation 61 self.__forPreparation = forPreparation
52 self.__lexer = Lexers.getLexer(self.__language) 62 self.__lexer = Lexers.getLexer(self.__language)
53 self.__apifiles = Preferences.getEditorAPI(self.__language) 63 self.__apifiles = Preferences.getEditorAPI(self.__language,
64 self.__projectType)
54 self.__apifiles.sort() 65 self.__apifiles.sort()
55 if self.__lexer is None: 66 if self.__lexer is None:
56 self.__apis = None 67 self.__apis = None
57 else: 68 else:
58 self.__apis = QsciAPIs(self.__lexer) 69 self.__apis = QsciAPIs(self.__lexer)
71 if self.__apis.isPrepared(): 82 if self.__apis.isPrepared():
72 # load a prepared API file 83 # load a prepared API file
73 if not self.__forPreparation and \ 84 if not self.__forPreparation and \
74 Preferences.getEditor("AutoPrepareAPIs"): 85 Preferences.getEditor("AutoPrepareAPIs"):
75 self.prepareAPIs() 86 self.prepareAPIs()
76 self.__apis.loadPrepared() 87 self.__apis.loadPrepared(self.__preparedName())
77 else: 88 else:
78 # load the raw files and prepare the API file 89 # load the raw files and prepare the API file
79 if not self.__forPreparation and \ 90 if not self.__forPreparation and \
80 Preferences.getEditor("AutoPrepareAPIs"): 91 Preferences.getEditor("AutoPrepareAPIs"):
81 self.prepareAPIs(ondemand=True) 92 self.prepareAPIs(ondemand=True)
110 121
111 def __apiPreparationFinished(self): 122 def __apiPreparationFinished(self):
112 """ 123 """
113 Private method called to save an API, after it has been prepared. 124 Private method called to save an API, after it has been prepared.
114 """ 125 """
115 self.__apis.savePrepared() 126 self.__apis.savePrepared(self.__preparedName())
116 self.__inPreparation = False 127 self.__inPreparation = False
117 self.apiPreparationFinished.emit() 128 self.apiPreparationFinished.emit()
118 129
119 def __apiPreparationCancelled(self): 130 def __apiPreparationCancelled(self):
120 """ 131 """
144 needsPreparation = False 155 needsPreparation = False
145 if ondemand: 156 if ondemand:
146 needsPreparation = True 157 needsPreparation = True
147 else: 158 else:
148 # check, if a new preparation is necessary 159 # check, if a new preparation is necessary
149 preparedAPIs = self.__defaultPreparedName() 160 preparedAPIs = self.__preparedName()
150 if preparedAPIs: 161 if preparedAPIs:
151 preparedAPIsInfo = QFileInfo(preparedAPIs) 162 preparedAPIsInfo = QFileInfo(preparedAPIs)
152 if not preparedAPIsInfo.exists(): 163 if not preparedAPIsInfo.exists():
153 needsPreparation = True 164 needsPreparation = True
154 else: 165 else:
155 preparedAPIsTime = preparedAPIsInfo.lastModified() 166 preparedAPIsTime = preparedAPIsInfo.lastModified()
156 apifiles = sorted( 167 apifiles = sorted(Preferences.getEditorAPI(
157 Preferences.getEditorAPI(self.__language)) 168 self.__language, self.__projectType))
158 if self.__apifiles != apifiles: 169 if self.__apifiles != apifiles:
159 needsPreparation = True 170 needsPreparation = True
160 for apifile in apifiles: 171 for apifile in apifiles:
161 if QFileInfo(apifile).lastModified() > \ 172 if QFileInfo(apifile).lastModified() > \
162 preparedAPIsTime: 173 preparedAPIsTime:
167 # do the preparation 178 # do the preparation
168 self.__apis.clear() 179 self.__apis.clear()
169 if rawList: 180 if rawList:
170 apifiles = rawList 181 apifiles = rawList
171 else: 182 else:
172 apifiles = Preferences.getEditorAPI(self.__language) 183 apifiles = Preferences.getEditorAPI(
184 self.__language, self.__projectType)
173 for apifile in apifiles: 185 for apifile in apifiles:
174 self.__apis.load(apifile) 186 self.__apis.load(apifile)
175 self.__apis.prepare() 187 self.__apis.prepare()
176 self.__apifiles = apifiles 188 self.__apifiles = apifiles
177 189
207 219
208 return self.__apis.installedAPIFiles() 220 return self.__apis.installedAPIFiles()
209 else: 221 else:
210 return [] 222 return []
211 223
212 def __defaultPreparedName(self): 224 def __preparedName(self):
213 """ 225 """
214 Private method returning the default name of a prepared API file. 226 Private method returning the default name of a prepared API file.
215 227
216 @return complete filename for the Prepared APIs file (string) 228 @return complete filename for the Prepared APIs file (string)
217 """ 229 """
230 apisDir = os.path.join(Globals.getConfigDir(), "APIs")
218 if self.__apis is not None: 231 if self.__apis is not None:
219 return self.__apis.defaultPreparedName() 232 if self.__projectType:
233 filename = "{0}_{1}.pap".format(self.__language,
234 self.__projectType)
235 else:
236 filename = "{0}.pap".format(self.__language)
237 return os.path.join(apisDir, filename)
220 else: 238 else:
221 return "" 239 return ""
222 240
223 241
224 class APIsManager(QObject): 242 class APIsManager(QObject):
242 Public slot to reload the api information. 260 Public slot to reload the api information.
243 """ 261 """
244 for api in list(self.__apis.values()): 262 for api in list(self.__apis.values()):
245 api and api.reloadAPIs() 263 api and api.reloadAPIs()
246 264
247 def getAPIs(self, language, forPreparation=False): 265 def getAPIs(self, language, projectType="", forPreparation=False):
248 """ 266 """
249 Public method to get an apis object for autocompletion/calltips. 267 Public method to get an APIs object for autocompletion/calltips.
250 268
251 This method creates and loads an APIs object dynamically upon request. 269 This method creates and loads an APIs object dynamically upon request.
252 This saves memory for languages, that might not be needed at the 270 This saves memory for languages, that might not be needed at the
253 moment. 271 moment.
254 272
255 @param language the language of the requested api object (string) 273 @param language language of the requested APIs object
256 @param forPreparation flag indicating the requested api object is just 274 @type str
257 needed for a preparation process (boolean) 275 @param projectType type of the project
258 @return the apis object (APIs) 276 @type str
277 @param forPreparation flag indicating the requested APIs object is just
278 needed for a preparation process
279 @type bool
280 @return reference to the APIs object
281 @rtype APIs
259 """ 282 """
260 if forPreparation: 283 if forPreparation:
261 return APIs(language, forPreparation=forPreparation) 284 return APIs(language, projectType=projectType,
285 forPreparation=forPreparation)
262 else: 286 else:
263 try: 287 try:
264 return self.__apis[language] 288 return self.__apis[(language, projectType)]
265 except KeyError: 289 except KeyError:
266 if language in Lexers.getSupportedLanguages(): 290 if language in Lexers.getSupportedLanguages():
267 # create the api object 291 # create the api object
268 self.__apis[language] = APIs(language) 292 self.__apis[(language, projectType)] = \
269 return self.__apis[language] 293 APIs(language, projectType=projectType)
294 return self.__apis[(language, projectType)]
270 else: 295 else:
271 return None 296 return None

eric ide

mercurial