Source

pida-main / pida-plugins / python / python.py

Full commit
  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
# -*- coding: utf-8 -*- 

# Copyright (c) 2007 The PIDA Project

#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:

#The above copyright notice and this permission notice shall be included in
#all copies or substantial portions of the Software.

#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#SOFTWARE.

# stdlib
import sys, compiler, os.path, keyword, re
import string

# gtk
import gtk

# PIDA Imports

# core
from pida.core.service import Service
from pida.core.events import EventsConfig
from pida.core.actions import ActionsConfig
from pida.core.options import OptionsConfig
from pida.core.languages import (LanguageService, Outliner, Validator,
    Completer, LanguageServiceFeaturesConfig, LanguageInfo, Definer, 
    Documentator, External)

from pida.utils.languages import (
    COMPLETER, VALIDATOR_LEVEL, VALIDATOR_KIND,
    LANG_PRIO, Definition, Suggestion, Documentation, ValidationError)

# services
import pida.services.filemanager.filehiddencheck as filehiddencheck

# utils
from .pyflakes.checker import Checker
from . import pyflakes
from .pyflakes import messages
# locale
from pida.core.locale import Locale
locale = Locale('python')
_ = locale.gettext

from .ropebrowser import ModuleParser

MAX_FIXES = 10
RE_MATCHES = (
    # traceback match
    (r'''File\s*"([^"]+)",\s*line\s*[0-9]+''',
    # internal
     re.compile(r'\s*File\s*\"(?P<file>[^"]+)\",\s*line\s*(?P<line>\d+).*'))
    ,
    #FIXME: how to handle localisation of this ???
)


class PythonEventsConfig(EventsConfig):

    def subscribe_all_foreign(self):
        self.subscribe_foreign('buffer', 'document-changed',
                    self.on_document_changed)
        self.subscribe_foreign('buffer', 'document-typchanged',
                    self.on_document_changed)

    def on_document_changed(self, document):
        self.svc.set_document(document)


class PythonFeaturesConfig(LanguageServiceFeaturesConfig):

    def subscribe_all_foreign(self):
        LanguageServiceFeaturesConfig.subscribe_all_foreign(self)
        self.subscribe_foreign('filemanager', 'file_hidden_check',
            self.python)
        for match in RE_MATCHES:
            self.subscribe_foreign('commander', 'match-callback',
            ('Python', match[0], match[1], self.svc.match_call))


    @filehiddencheck.fhc(filehiddencheck.SCOPE_PROJECT, 
        _("Hide Python Compiled Files"))
    def python(self, name, path, state):
        return os.path.splitext(name)[1] != '.pyc'


class PythonOptionsConfig(OptionsConfig):

    def create_options(self):
        self.create_option(
            'python_for_executing',
            _('Python Executable for executing'),
            str,
            'python',
            _('The Python executable when executing a module'),
        )


class PythonActionsConfig(ActionsConfig):


    actions = [
        gtk.Action('execute_python',
            _('Execute Python Module'),
            _('Execute the current Python module in a shell'),
            gtk.STOCK_EXECUTE),
    ]

    def on_execute_python(self, action):
        self.svc.execute_current_document()


class PythonOutliner(Outliner):

    priority = LANG_PRIO.VERY_GOOD
    name = "rope"
    plugin = "python"
    description = _("A very deep and precises, but slow outliner")


    filter_type = (
        'import', 'builtin', 'method', 'function',
        'property', 'attribute', 'supermethod',
        )

    def run(self):
        from rope.base.exceptions import RopeError
        try:
            mp = ModuleParser(self.document.filename,
                              project=self.document.project)
        except RopeError:
            return
        try:
            for node, parent in mp.get_nodes():
                yield node
        except Exception, e:
            import traceback
            traceback.print_exc()

    def sync(self):
        try:
            self.document.project['python']['ropeproject'].sync()
        except (KeyError, TypeError), e:
            pass

    def close(self):
        try:
            self.document.project['python']['ropeproject'].close()
        except (KeyError, TypeError), e:
            pass


class PythonDocumentator(Documentator):

    name = "rope"
    plugin = "python"
    description = _("A very good documentator")


    def get_documentation(self, buffer, offset):
        mp = ModuleParser(self.document.filename,
                          project=self.document.project)
        buffer = buffer + ('\n' * 20)
        
        from rope.contrib.codeassist import PyDocExtractor
        from rope.base.exceptions import RopeError
        from rope.contrib import fixsyntax
        try:
            fix = fixsyntax.FixSyntax(mp.project.pycore, buffer, None, maxfixes=MAX_FIXES)
            pymodule = fix.get_pymodule()
            pyname = fix.pyname_at(offset)
        except RopeError:
            return
        if pyname is None:
            return
        pyobject = pyname.get_object()
        rv = Documentation(
            short=PyDocExtractor().get_calltip(pyobject, False, False),
            long_=PyDocExtractor().get_doc(pyobject)
            )
        yield rv

class PythonLanguage(LanguageInfo):
    varchars = string.ascii_letters + string.digits + '_'
    word = varchars

    # . in python
    attributerefs = '.'

    completer_open = '[(,.'
    completer_close = '])}'



def _create_exception_validation(e):
    msg = e
    msg.name = e.__class__.__name__
    value = sys.exc_info()[1]
    (lineno, offset, line) = value[1][1:]
    if line.endswith("\n"):
        line = line[:-1]
    msg.lineno = lineno
    msg.message_args = (line,)
    msg.message = '<tt>%%s</tt>\n<tt>%s^</tt>' % (' ' * (offset - 2))
    msg.type_ = 'error'
    if isinstance(e, SyntaxError):
        msg.kind = 'syntax'
    else:
        msg.kind = 'indentation'
    return [msg]

class PythonValidator(Validator):

    priority = LANG_PRIO.GOOD
    name = "pyflakes"
    plugin = "python"
    description = _("A not very precise, non configurable validator, but fast")

    def run(self):
        code_string = self.document.content
        filename = self.document.filename
        try:
            tree = compiler.parse(code_string)
        except (SyntaxError, IndentationError), e:
            messages = _create_exception_validation(e)
        else:
            w = Checker(tree, filename)
            messages = w.messages
        for m in messages:

            #FIXME add pyflakes 0.3 types
            #FIXME make mapping
            if isinstance(m, pyflakes.messages.UnusedImport):
                level = VALIDATOR_LEVEL.INFO
                kind = VALIDATOR_KIND.UNUSED
            elif isinstance(m, pyflakes.messages.RedefinedWhileUnused):
                level = VALIDATOR_LEVEL.WARNING
                kind = VALIDATOR_KIND.REDEFINED
            elif isinstance(m, pyflakes.messages.ImportStarUsed):
                level = VALIDATOR_LEVEL.WARNING
                kind = VALIDATOR_KIND.BADSTYLe
            elif isinstance(m, pyflakes.messages.UndefinedName):
                level = VALIDATOR_LEVEL.ERROR
                kind = VALIDATOR_KIND.UNDEFINED
            elif isinstance(m, pyflakes.messages.DuplicateArgument):
                type_ = VALIDATOR_LEVEL.ERROR
                kind = VALIDATOR_KIND.DUPLICATE
            else:
                level = VALIDATOR_LEVEL.UNKNOWN
                kind = VALIDATOR_KIND.UNKNOWN

            ve = ValidationError(
                message=m.message%m.message_args,
                lineno=m.lineno,
                level=level,
                kind=kind,
                filename=filename
                )
            yield ve


class PythonCompleter(Completer):

    priority = LANG_PRIO.VERY_GOOD
    name = "rope"
    plugin = "python"
    description = _("Creates very exact suggestions at reasonable speed")

    def run(self, base, buffer, offset):

        from rope.contrib.codeassist import code_assist, sorted_proposals
        from rope.base.exceptions import RopeError

        try:
            mp = ModuleParser(self.document.filename, 
                              project=self.document.project)
            buffer = buffer + ('\n' * 20)
            co = code_assist(mp.project, buffer, offset, maxfixes=MAX_FIXES)
        except RopeError, IndentationError:
            return
        so = sorted_proposals(co)
        for c in so:
            if c.name.startswith(base):
                r = Suggestion(c.name)
                #'variable', 'class', 'function', 'imported' , 'paramter'
                if keyword.iskeyword(c.name):
                    r.type_ = COMPLETER.KEYWORD
                elif c.type in ('variable','class', 'builtin',
                                'function', 'parameter',):
                    r.type_ = getattr(COMPLETER, c.type.upper())
                elif c.type == None:
                    if c.kind == "parameter_keyword":
                        r.type_ = COMPLETER.PARAMETER
                else:
                    r.type_ = COMPLETER.UNKNOWN
                yield r

class PythonDefiner(Definer):

    name = "rope"
    plugin = "python"
    description = _("Shows a good definition of a function")

    def run(self, buffer, offset):
        mp = ModuleParser(self.document.filename,
                          project=self.document.project)
        buffer = buffer + ('\n' * 20)

        from rope.contrib.findit  import find_definition
        from rope.base.exceptions import RopeError

        try:
            dl = find_definition(mp.project, buffer, offset, maxfixes=MAX_FIXES)
        except RopeError:
            return

        if not dl:
            return

        if dl.resource is not None:
            file_name = dl.resource.path
        else:
            file_name = self.document.filename


        rv = Definition(file_name=file_name, offset=dl.offset,
                        line=dl.lineno, length=(dl.region[1]-dl.region[0]))

        yield rv

class PythonExternal(External):
    outliner = PythonOutliner
    validator = PythonValidator
    completer = PythonCompleter
    definer = PythonDefiner
    documentator = PythonDocumentator

class Python(LanguageService):

    language_name = 'Python'
    language_info = PythonLanguage
    outliner_factory = PythonOutliner
    validator_factory = PythonValidator
    completer_factory = PythonCompleter
    definer_factory = PythonDefiner
    documentator_factory = PythonDocumentator

    features_config = PythonFeaturesConfig
    actions_config = PythonActionsConfig
    options_config = PythonOptionsConfig
    events_config = PythonEventsConfig

    external = PythonExternal

    def pre_start(self):
        self.execute_action = self.get_action('execute_python')
        self.execute_action.set_sensitive(False)

#     def start(self):
#         for match in RE_MATCHES:
#             self.boss.get_service('commander').register_matcher(
#                 match[0], self.match_call)
#
#     def stop(self):
#         for match in RE_MATCHES:
#             self.boss.get_service('commander').unregister_matcher(
#                 match[0], self.match_call)

    def match_call(self, term, event, match, *args, **kwargs):
        for pattern in RE_MATCHES:
            test = pattern[1].match(match)
            if test:
                self.boss.get_service('buffer').open_file(
                    kwargs['usr'].get_absolute_path(test.groups()[0]),
                    line=int(test.groups()[1]))

    def execute_current_document(self):
        python_ex = self.opt('python_for_executing')
        doc = self.boss.cmd('buffer', 'get_current')
        if doc and doc.filename:
            self.boss.cmd('commander', 'execute',
                commandargs=[python_ex, doc.filename],
                cwd = doc.directory,
                )

    def is_current_python(self):
        doc = self.boss.cmd('buffer', 'get_current')
        if doc.doctype and (doc.doctype.internal == self.language_name):
            return True
        return False

    def set_document(self, document):
        self.execute_action.set_sensitive(self.is_current_python())

# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: