forked from SublimeCodeIntel/SublimeCodeIntel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager.py
More file actions
518 lines (445 loc) · 20.5 KB
/
manager.py
File metadata and controls
518 lines (445 loc) · 20.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
#!/usr/bin/env python
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License
# Version 1.1 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
# License for the specific language governing rights and limitations
# under the License.
#
# The Original Code is Komodo code.
#
# The Initial Developer of the Original Code is ActiveState Software Inc.
# Portions created by ActiveState Software Inc are Copyright (C) 2000-2007
# ActiveState Software Inc. All Rights Reserved.
#
# Contributor(s):
# ActiveState Software Inc
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****
"""The "Manager" is the controlling instance for a codeintel system."""
import os
from os.path import dirname, join, abspath, splitext, basename, isabs
import sys
import imp
import logging
from collections import defaultdict
from glob import glob
import threading
from Queue import Queue
import warnings
import traceback
import codecs
from SilverCity import ScintillaConstants
from codeintel2.common import *
from codeintel2.accessor import *
from codeintel2.citadel import Citadel, BinaryBuffer
from codeintel2.buffer import ImplicitBuffer
from codeintel2.langintel import ImplicitLangIntel
from codeintel2.database.database import Database
from codeintel2.environment import DefaultEnvironment
from codeintel2 import indexer
from codeintel2.util import guess_lang_from_path
from codeintel2 import hooks
from codeintel2.udl import XMLParsingBufferMixin, UDLBuffer
import langinfo
if _xpcom_:
from xpcom import components, COMException
from xpcom.client import WeakReference
from xpcom.server import UnwrapObject
#---- global variables
log = logging.getLogger("codeintel")
#log.setLevel(logging.INFO)
#---- public interface
class Manager(threading.Thread, Queue):
# See the module docstring for usage information.
def __init__(self, db_base_dir=None, on_scan_complete=None,
extra_module_dirs=None, env=None,
db_event_reporter=None, db_catalog_dirs=None,
db_import_everything_langs=None):
"""Create a CodeIntel manager.
"db_base_dir" (optional) specifies the base directory for
the codeintel database. If not given it will default to
'~/.codeintel'.
"on_scan_complete" (optional) is a callback for Citadel scan
completion. It will be passed the ScanRequest instance
as an argument.
"extra_module_dirs" (optional) is a list of extra dirs
in which to look for and use "codeintel_*.py"
support modules (and "lang_*.py" modules, DEPRECATED).
"env" (optional) is an Environment instance (or subclass).
See environment.py for details.
"db_event_reporter" (optional) is a callback that will be called
db_event_reporter(<event-desc-string>)
before "significant" long processing events in the DB. This
may be useful to forward to a status bar in a GUI.
"db_catalog_dirs" (optional) is a list of catalog dirs in
addition to the std one to use for the CatalogsZone. All
*.cix files in a catalog dir are made available.
"db_import_everything_langs" (optional) is a set of langs for which
the extra effort to support Database
`lib.hits_from_lpath()' should be made. See class
Database for more details.
"""
threading.Thread.__init__(self, name="CodeIntel Manager")
self.setDaemon(True)
Queue.__init__(self)
self.citadel = Citadel(self)
# Module registry bits.
self._registered_module_canon_paths = set()
self.silvercity_lexer_from_lang = {}
self.buf_class_from_lang = {}
self.langintel_class_from_lang = {}
self._langintel_from_lang_cache = {}
self.import_handler_class_from_lang = {}
self._is_citadel_from_lang = {} # registered langs that are Citadel-based
self._is_cpln_from_lang = {} # registered langs for which completion is supported
self._hook_handlers_from_lang = defaultdict(list)
self.env = env or DefaultEnvironment()
# The database must be enabled before registering modules.
self.db = Database(self, base_dir=db_base_dir,
catalog_dirs=db_catalog_dirs,
event_reporter=db_event_reporter,
import_everything_langs=db_import_everything_langs)
self.lidb = langinfo.get_default_database()
self._register_modules(extra_module_dirs)
self.idxr = indexer.Indexer(self, on_scan_complete)
def upgrade(self):
"""Upgrade the database, if necessary.
It blocks until the upgrade is complete. Alternatively, if you
want more control over upgrading use:
Database.upgrade_info()
Database.upgrade()
Database.reset()
"""
log.debug("upgrade db if necessary")
status, reason = self.db.upgrade_info()
if status == Database.UPGRADE_NECESSARY:
log.info("db upgrade is necessary")
self.db.upgrade()
elif status == Database.UPGRADE_NOT_POSSIBLE:
log.warn("%s (resetting db)", reason)
log.info("reset db at `%s' (creating backup)", self.db.base_dir)
self.db.reset()
elif status == Database.UPGRADE_NOT_NECESSARY:
log.debug("no upgrade necessary")
else:
raise CodeIntelError("unknown db upgrade status: %r" % status)
def initialize(self):
"""Initialize the codeintel system."""
# TODO: Implement DB cleaning.
#self.db.clean()
self.idxr.start()
def _register_modules(self, extra_module_dirs=None):
"""Register codeintel/lang modules.
@param extra_module_dirs {sequence} is an optional list of extra
dirs in which to look for and use "codeintel|lang_*.py"
support modules. By default just the codeintel2 package
directory is used.
"""
dirs = [dirname(__file__)]
if extra_module_dirs:
dirs += extra_module_dirs
for dir in dirs:
for module_path in glob(join(dir, "codeintel_*.py")):
self._register_module(module_path)
for module_path in glob(join(dir, "lang_*.py")):
warnings.warn("%s: `lang_*.py' codeintel modules are deprecated, "
"use `codeintel_*.py'. Support for `lang_*.py' "
"will be dropped in Komodo 5.1." % module_path,
CodeIntelDeprecationWarning)
self._register_module(module_path)
def _register_module(self, module_path):
"""Register the given codeintel support module.
@param module_path {str} is the path to the support module.
@exception ImportError, CodeIntelError
This will import the given module path and call its top-level
`register` function passing it the Manager instance. That is
expected to callback to one or more of:
mgr.set_lang_info(...)
mgr.add_hooks_handler(...)
"""
module_canon_path = canonicalizePath(module_path)
if module_canon_path in self._registered_module_canon_paths:
return
module_dir, module_name = os.path.split(module_path)
module_name = splitext(module_name)[0]
iinfo = imp.find_module(module_name, [module_dir])
module = imp.load_module(module_name, *iinfo)
if hasattr(module, "register"):
log.debug("register `%s' support module", module_path)
try:
module.register(self)
except CodeIntelError, ex:
log.warn("error registering `%s' support module: %s",
module_path, ex)
except:
log.exception("unexpected error registering `%s' "
"support module", module_path)
self._registered_module_canon_paths.add(module_canon_path)
def set_lang_info(self, lang, silvercity_lexer=None, buf_class=None,
import_handler_class=None, cile_driver_class=None,
is_cpln_lang=False, langintel_class=None,
import_everything=False):
"""Called by register() functions in language support modules."""
if silvercity_lexer:
self.silvercity_lexer_from_lang[lang] = silvercity_lexer
if buf_class:
self.buf_class_from_lang[lang] = buf_class
if langintel_class:
self.langintel_class_from_lang[lang] = langintel_class
if import_handler_class:
self.import_handler_class_from_lang[lang] = import_handler_class
if cile_driver_class is not None:
self._is_citadel_from_lang[lang] = True
self.citadel.set_lang_info(lang, cile_driver_class,
is_cpln_lang=is_cpln_lang)
if is_cpln_lang:
self._is_cpln_from_lang[lang] = True
if import_everything:
self.db.import_everything_langs.add(lang)
def add_hook_handler(self, hook_handler):
"""Add a handler for various codeintel hooks.
@param hook_handler {hooks.HookHandler}
"""
assert isinstance(hook_handler, hooks.HookHandler)
assert hook_handler.name is not None, \
"hook handlers must have a name: %r.name is None" % hook_handler
for lang in hook_handler.langs:
self._hook_handlers_from_lang[lang].append(hook_handler)
def finalize(self, timeout=None):
if self.citadel is not None:
self.citadel.finalize()
if self.isAlive():
self.stop()
self.join(timeout)
self.idxr.finalize()
if self.db is not None:
try:
self.db.save()
except Exception:
log.exception("error saving database")
self.db = None # break the reference
# Proxy the batch update API onto our Citadel instance.
def batch_update(self, join=True, updater=None):
return self.citadel.batch_update(join=join, updater=updater)
def report_message(self, msg, details=None, notification_name="codeintel-message"):
"""Reports a unique codeintel message."""
log.info("%s: %s: %r", notification_name, msg, details)
def is_multilang(self, lang):
"""Return True iff this is a multi-lang language.
I.e. Is this a language that supports embedding of different
programming languages. For example RHTML can have Ruby and
JavaScript content, HTML can have JavaScript content.
"""
return issubclass(self.buf_class_from_lang[lang], UDLBuffer)
def is_xml_lang(self, lang):
try:
buf_class = self.buf_class_from_lang[lang]
except KeyError:
return False
return issubclass(buf_class, XMLParsingBufferMixin)
def is_cpln_lang(self, lang):
"""Return True iff codeintel supports completion (i.e. autocomplete
and calltips) for this language."""
return lang in self._is_cpln_from_lang
def get_cpln_langs(self):
return self._is_cpln_from_lang.keys()
def is_citadel_lang(self, lang):
"""Returns True if the given lang has been registered and
is a Citadel-based language.
A "Citadel-based" language is one that uses CIX/CIDB/CITDL tech for
its codeintel. Note that currently not all Citadel-based langs use
the Citadel system for completion (e.g. Tcl).
"""
return lang in self._is_citadel_from_lang
def get_citadel_langs(self):
return self._is_citadel_from_lang.keys()
def langintel_from_lang(self, lang):
if lang not in self._langintel_from_lang_cache:
try:
langintel_class = self.langintel_class_from_lang[lang]
except KeyError:
langintel = ImplicitLangIntel(lang, self)
else:
langintel = langintel_class(self)
self._langintel_from_lang_cache[lang] = langintel
return self._langintel_from_lang_cache[lang]
def hook_handlers_from_lang(self, lang):
return self._hook_handlers_from_lang.get(lang, []) \
+ self._hook_handlers_from_lang.get("*", [])
#XXX
#XXX Cache bufs based on (path, lang) so can share bufs. (weakref)
#XXX
def buf_from_koIDocument(self, doc, env=None):
lang = doc.language
path = doc.displayPath
if doc.isUntitled:
path = join("<Unsaved>", path)
accessor = KoDocumentAccessor(doc,
self.silvercity_lexer_from_lang.get(lang))
encoding = doc.encoding.python_encoding_name
try:
buf_class = self.buf_class_from_lang[lang]
except KeyError:
# No langintel is defined for this class, check if the koILanguage
# defined is a UDL koILanguage.
from koUDLLanguageBase import KoUDLLanguage
if isinstance(UnwrapObject(doc.languageObj), KoUDLLanguage):
return UDLBuffer(self, accessor, env, path, encoding, lang=lang)
# Not a UDL language - use the implicit buffer then.
return ImplicitBuffer(lang, self, accessor, env, path, encoding)
else:
buf = buf_class(self, accessor, env, path, encoding)
return buf
def buf_from_content(self, content, lang, env=None, path=None,
encoding=None):
lexer = self.silvercity_lexer_from_lang.get(lang)
accessor = SilverCityAccessor(lexer, content)
try:
buf_class = self.buf_class_from_lang[lang]
except KeyError:
buf = ImplicitBuffer(lang, self, accessor, env, path, encoding)
else:
buf = buf_class(self, accessor, env, path, encoding)
return buf
def binary_buf_from_path(self, path, lang=None, env=None):
buf = BinaryBuffer(lang, self, env, path)
return buf
MAX_FILESIZE = 50 * 1024 * 1024 # 50MB
def buf_from_path(self, path, lang=None, env=None, encoding=None):
# Detect and abort on large files - to avoid memory errors, bug 88487.
# The maximum size is 50MB - someone uses source code that big?
filestat = os.stat(path)
if filestat.st_size > self.MAX_FILESIZE:
log.warn("File %r has size greater than 50MB (%d)", path, filestat.st_size)
raise CodeIntelError('File too big. Size: %d bytes, path: %r' % (
filestat.st_size, path))
if lang is None or encoding is None:
import textinfo
ti = textinfo.textinfo_from_path(path, encoding=encoding,
follow_symlinks=True)
if lang is None:
lang = (hasattr(ti.langinfo, "komodo_name")
and ti.langinfo.komodo_name
or ti.langinfo.name)
if not ti.is_text:
return self.binary_buf_from_path(path, lang, env)
encoding = ti.encoding
content = ti.text
else:
content = codecs.open(path, 'rb', encoding).read()
#TODO: Re-instate this when have solution for CILE test failures
# that this causes.
#if not isabs(path) and not path.startswith("<Unsaved>"):
# path = abspath(path)
return self.buf_from_content(content, lang, env, path, encoding)
#---- Completion Evaluation Session/Queue handling
# The current eval session (an Evaluator instance). A current session's
# lifetime is as follows:
# - [self._get()] Starts when the evaluator thread (this class) takes it
# off the queue.
# - [self._put()] Can be aborted (via sess.ctlr.abort()) if a new eval
# request comes in.
# - [eval_sess.eval()] Done when the session completes either by
# (1) an unexpected error during sess.eval() or (2) sess.ctlr.is_done()
# after sess.eval().
_curr_eval_sess = None
def request_eval(self, evalr):
"""Request evaluation of the given completion.
"evalr" is the Evaluator instance.
The manager has an evaluation thread on which this evalr will be
scheduled. Only one request is ever eval'd at one time. A new
request will cause an existing on to be aborted and requests made in
the interim will be trumped by this new one.
Dev Notes:
- XXX Add a timeout to the put and raise error on timeout?
"""
#self._handle_eval_sess(evalr)
self.put((evalr, False))
def request_reeval(self, evalr):
"""Occassionally evaluation will need to defer until something (e.g.
scanning into the CIDB) is one. These sessions will re-request
evaluation via this method.
"""
self.put((evalr, True))
def stop(self):
self.put((None, None)) # Sentinel to tell thread mainloop to stop.
def run(self):
while 1:
eval_sess, is_reeval = self.get()
if eval_sess is None: # Sentinel to stop.
break
try:
eval_sess.eval(self)
except:
try:
self._handle_eval_sess_error(eval_sess)
except:
pass
finally:
self._curr_eval_sess = None
self.db.report_event(None)
def _handle_eval_sess_error(self, eval_sess):
exc_info = sys.exc_info()
tb_path, tb_lineno, tb_func \
= traceback.extract_tb(exc_info[2])[-1][:3]
if hasattr(exc_info[0], "__name__"):
exc_str = "%s: %s" % (exc_info[0].__name__, exc_info[1])
else: # string exception
exc_str = exc_info[0]
eval_sess.ctlr.error("error evaluating %s: %s "
"(%s#%s in %s)", eval_sess, exc_str,
tb_path, tb_lineno, tb_func)
log.exception("error evaluating %s" % eval_sess)
eval_sess.ctlr.done("unexpected eval error")
def _put(self, (eval_sess, is_reeval)):
# Only consider re-evaluation if we are still on the same eval
# session.
if is_reeval and self._curr_eval_sess is not eval_sess:
return
replace = True
if hasattr(eval_sess, "ctlr") and eval_sess.ctlr and eval_sess.ctlr.keep_existing:
# Allow multiple eval sessions; currently used for variable
# highlighting (bug 80095), may pick up additional uses. Note that
# these sessions can still get wiped out by a single replace=False
# caller.
replace = False
if replace:
# We only allow *one* eval session at a time.
# - Drop a possible accumulated eval session.
if len(self.queue):
self.queue.clear()
## - Abort the current eval session.
if not is_reeval and self._curr_eval_sess is not None:
self._curr_eval_sess.ctlr.abort()
# Lazily start the eval thread.
if not self.isAlive():
self.start()
Queue._put(self, (eval_sess, is_reeval))
if replace:
assert len(self.queue) == 1
def _get(self):
eval_sess, is_reeval = Queue._get(self)
if is_reeval:
assert self._curr_eval_sess is eval_sess
else:
self._curr_eval_sess = eval_sess
return eval_sess, is_reeval