Source

rope_py3k / rope / base / pycore.py

Ali Gholami Rudi 15d7481 
Ali Gholami Rudi 246b5e9 
Ali Gholami Rudi 04bc7cf 
Ali Gholami Rudi 9ef5738 
Ali Gholami Rudi 04bc7cf 
Ali Gholami Rudi 07efce3 
Ali Gholami Rudi 87d6aef 
Ali Gholami Rudi 07efce3 
Ali Gholami Rudi fa7fae9 
Ali Gholami Rudi 979e5e3 
Ali Gholami Rudi 208ca7c 
Ali Gholami Rudi 4339069 
Ali Gholami Rudi 4873be7 
Ali Gholami Rudi 339732b 
Ali Gholami Rudi 04bc7cf 





Ali Gholami Rudi 8080dde 
Ali Gholami Rudi f44ad86 
Ali Gholami Rudi 8080dde 
Ali Gholami Rudi 934bf21 
Ali Gholami Rudi f44ad86 
Ali Gholami Rudi 4339069 
Ali Gholami Rudi 3f3ef60 
Ali Gholami Rudi 569336a 
Ali Gholami Rudi f44ad86 
Ali Gholami Rudi 4339069 






Ali Gholami Rudi f44ad86 
Ali Gholami Rudi 1cfb59c 
Ali Gholami Rudi 4873be7 
Ali Gholami Rudi 1cfb59c 
Ali Gholami Rudi 4873be7 
Ali Gholami Rudi abae011 
Ali Gholami Rudi a409273 
Ali Gholami Rudi 569336a 


angri f5c2fc6 

Ali Gholami Rudi 569336a 
Ali Gholami Rudi 3f3ef60 
Ali Gholami Rudi d98daef 
Ali Gholami Rudi f44ad86 
Ali Gholami Rudi 3f3ef60 
Ali Gholami Rudi 4873be7 
Ali Gholami Rudi 1cfb59c 
Ali Gholami Rudi a409273 

Ali Gholami Rudi d98daef 

Ali Gholami Rudi b4b0707 

Ali Gholami Rudi d98daef 
Ali Gholami Rudi 3f3ef60 
Ali Gholami Rudi 26a2846 

Ali Gholami Rudi 0a9b5fe 
Ali Gholami Rudi 3f3ef60 
Ali Gholami Rudi a409273 

Ali Gholami Rudi 4339069 




Ali Gholami Rudi 04bc7cf 
Ali Gholami Rudi 73e3c9a 
Ali Gholami Rudi 04bc7cf 
zjes 568092f 



Ali Gholami Rudi 73e3c9a 
Ali Gholami Rudi 04bc7cf 
zjes 568092f 
Ali Gholami Rudi 04bc7cf 
Ali Gholami Rudi 41c4490 
Ali Gholami Rudi b5633d6 








Ali Gholami Rudi 339732b 
Ali Gholami Rudi 934bf21 
Ali Gholami Rudi 339732b 
Ali Gholami Rudi 73e3c9a 

Ali Gholami Rudi 04bc7cf 
Ali Gholami Rudi 979e5e3 
Ali Gholami Rudi 04bc7cf 
Ali Gholami Rudi 41c4490 
Ali Gholami Rudi 73e3c9a 

Ali Gholami Rudi 8e1ebf1 





Ali Gholami Rudi 73e3c9a 
Ali Gholami Rudi 04bc7cf 
Ali Gholami Rudi 73e3c9a 


Ali Gholami Rudi 04bc7cf 
Ali Gholami Rudi 55488c8 
Ali Gholami Rudi f44ad86 

Ali Gholami Rudi 04bc7cf 
Ali Gholami Rudi 73e3c9a 


Ali Gholami Rudi 04bc7cf 




Ali Gholami Rudi 54ce709 






Ali Gholami Rudi 41c4490 
Ali Gholami Rudi 04bc7cf 
Alexander Solovy… ca136d4 
Ali Gholami Rudi 04bc7cf 
Ali Gholami Rudi a2f28dc 
Ali Gholami Rudi 04bc7cf 
Ali Gholami Rudi 93a289d 
Ali Gholami Rudi 04bc7cf 
Ali Gholami Rudi a176b0d 
Ali Gholami Rudi 04bc7cf 

Ali Gholami Rudi 41c4490 
Ali Gholami Rudi 73e3c9a 
Ali Gholami Rudi 22696ee 
Ali Gholami Rudi 41c4490 
Ali Gholami Rudi 04bc7cf 

Ali Gholami Rudi 54ce709 
Ali Gholami Rudi 41c4490 
Ali Gholami Rudi 73e3c9a 
Ali Gholami Rudi 04bc7cf 
Ali Gholami Rudi 73e3c9a 


Ali Gholami Rudi 04bc7cf 
Ali Gholami Rudi 54ce709 
Ali Gholami Rudi 41c4490 
Ali Gholami Rudi 73e3c9a 
Ali Gholami Rudi 54ce709 
Ali Gholami Rudi 04bc7cf 
Ali Gholami Rudi 73e3c9a 
Ali Gholami Rudi 04bc7cf 


Ali Gholami Rudi 73e3c9a 
Ali Gholami Rudi 04bc7cf 

Ali Gholami Rudi 73e3c9a 

Ali Gholami Rudi 04bc7cf 



Ali Gholami Rudi 199b488 




Ali Gholami Rudi 04bc7cf 

Ali Gholami Rudi 93a289d 

Ali Gholami Rudi 569336a 


Ali Gholami Rudi 41c4490 
Ali Gholami Rudi 3df7cba 
Ali Gholami Rudi 69124d3 
Ali Gholami Rudi 41c4490 
Ali Gholami Rudi 04bc7cf 


Ali Gholami Rudi a409273 
Ali Gholami Rudi 04bc7cf 













Ali Gholami Rudi 6f04198 
Ali Gholami Rudi 04bc7cf 




Ali Gholami Rudi 41c4490 
Ali Gholami Rudi 04bc7cf 
Ali Gholami Rudi bc49874 

Ali Gholami Rudi 07efce3 
Ali Gholami Rudi bc49874 


Ali Gholami Rudi b4b0707 

Ali Gholami Rudi d825ec2 

Ali Gholami Rudi 28592e0 
Ali Gholami Rudi 07efce3 
Ali Gholami Rudi d298609 
Ali Gholami Rudi 80c0e9f 
Ali Gholami Rudi 04bc7cf 

Ali Gholami Rudi 41c4490 
Ali Gholami Rudi 15d7481 
Ali Gholami Rudi 30b81ff 
Ali Gholami Rudi fbbfe66 

Ali Gholami Rudi d4a653a 

Ali Gholami Rudi b307aed 
Ali Gholami Rudi d4a653a 
Ali Gholami Rudi 15d7481 


Ali Gholami Rudi c8ad79a 


Ali Gholami Rudi fbbfe66 
Ali Gholami Rudi 30b81ff 

Ali Gholami Rudi fbbfe66 
Ali Gholami Rudi 30b81ff 

Ali Gholami Rudi fbbfe66 
Ali Gholami Rudi 80c0e9f 
Ali Gholami Rudi 07efce3 
Ali Gholami Rudi 69124d3 
Ali Gholami Rudi fbbfe66 
Ali Gholami Rudi 72bae67 
Ali Gholami Rudi 5fb1f87 


Ali Gholami Rudi f44ad86 
Ali Gholami Rudi 7808056 
Ali Gholami Rudi 8080dde 

Ali Gholami Rudi 333a5f5 











Ali Gholami Rudi 8af0a3d 
Ali Gholami Rudi 333a5f5 



Ali Gholami Rudi bc1ffcd 
Ali Gholami Rudi fa7fae9 
Ali Gholami Rudi bc1ffcd 
Ali Gholami Rudi fa7fae9 
Ali Gholami Rudi 16bad14 
Ali Gholami Rudi fa7fae9 

Ali Gholami Rudi bc1ffcd 
Ali Gholami Rudi 8080dde 










Ali Gholami Rudi 7c2d7a0 
Ali Gholami Rudi 8080dde 


Ali Gholami Rudi 3df7cba 
Ali Gholami Rudi 8080dde 


Ali Gholami Rudi acf3966 

Ali Gholami Rudi 8080dde 
Ali Gholami Rudi 3df7cba 

angri 3ff4992 

Ali Gholami Rudi 8080dde 



Ali Gholami Rudi 12d8d27 
Ali Gholami Rudi 80c0e9f 

Ali Gholami Rudi 8080dde 


Ali Gholami Rudi 7808056 
Ali Gholami Rudi f44ad86 
Ali Gholami Rudi 934bf21 


Ali Gholami Rudi bc1ffcd 
Ali Gholami Rudi 934bf21 




Ali Gholami Rudi bc1ffcd 

Ali Gholami Rudi b5633d6 
Ali Gholami Rudi 934bf21 


Ali Gholami Rudi 3f3ef60 
Ali Gholami Rudi 0a9b5fe 





Ali Gholami Rudi 15d7481 


Ali Gholami Rudi 0a9b5fe 

Ali Gholami Rudi 15d7481 



Ali Gholami Rudi 55d2477 
Ali Gholami Rudi 0a9b5fe 


Ali Gholami Rudi d4a653a 
Ali Gholami Rudi 246b5e9 








Ali Gholami Rudi 15d7481 
Ali Gholami Rudi 246b5e9 


Ali Gholami Rudi 15d7481 
Ali Gholami Rudi 246b5e9 
Ali Gholami Rudi 15d7481 

Ali Gholami Rudi 246b5e9 





Ali Gholami Rudi 15d7481 


Ali Gholami Rudi 246b5e9 
Ali Gholami Rudi 15d7481 










  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
import bisect
import difflib
import sys
import warnings

import rope.base.oi.doa
import rope.base.oi.objectinfo
import rope.base.oi.soa
from rope.base import ast, exceptions, taskhandle, utils, stdmods
from rope.base.exceptions import ModuleNotFoundError
from rope.base.pyobjectsdef import PyModule, PyPackage, PyClass
import rope.base.resources
import rope.base.resourceobserver
from rope.base import builtins


class PyCore(object):

    def __init__(self, project):
        self.project = project
        self._init_resource_observer()
        self.cache_observers = []
        self.module_cache = _ModuleCache(self)
        self.extension_cache = _ExtensionCache(self)
        self.object_info = rope.base.oi.objectinfo.ObjectInfoManager(project)
        self._init_python_files()
        self._init_automatic_soa()
        self._init_source_folders()

    def _init_python_files(self):
        self.python_matcher = None
        patterns = self.project.prefs.get('python_files', None)
        if patterns is not None:
            self.python_matcher = rope.base.resources._ResourceMatcher()
            self.python_matcher.set_patterns(patterns)

    def _init_resource_observer(self):
        callback = self._invalidate_resource_cache
        observer = rope.base.resourceobserver.ResourceObserver(
            changed=callback, moved=callback, removed=callback)
        self.observer = rope.base.resourceobserver.FilteredResourceObserver(observer)
        self.project.add_observer(self.observer)

    def _init_source_folders(self):
        self._custom_source_folders = []
        for path in self.project.prefs.get('source_folders', []):
            folder = self.project.get_resource(path)
            self._custom_source_folders.append(folder)

    def _init_automatic_soa(self):
        if not self.automatic_soa:
            return
        callback = self._file_changed_for_soa
        observer = rope.base.resourceobserver.ResourceObserver(
            changed=callback, moved=callback, removed=callback)
        self.project.add_observer(observer)

    @property
    def automatic_soa(self):
        auto_soa = self.project.prefs.get('automatic_soi', None)
        return self.project.prefs.get('automatic_soa', auto_soa)

    def _file_changed_for_soa(self, resource, new_resource=None):
        old_contents = self.project.history.\
                       contents_before_current_change(resource)
        if old_contents is not None:
            perform_soa_on_changed_scopes(self.project, resource, old_contents)

    def is_python_file(self, resource):
        if resource.is_folder():
            return False
        if self.python_matcher is None:
            return resource.name.endswith('.py')
        return self.python_matcher.does_match(resource)

    def get_module(self, name, folder=None):
        """Returns a `PyObject` if the module was found."""
        # check if this is a builtin module
        pymod = self._builtin_module(name)
        if pymod is not None:
            return pymod
        module = self.find_module(name, folder)
        if module is None:
            raise ModuleNotFoundError('Module %s not found' % name)
        return self.resource_to_pyobject(module)

    def _builtin_submodules(self, modname):
        result = {}
        for extension in self.extension_modules:
            if extension.startswith(modname + '.'):
                name = extension[len(modname) + 1:]
                if '.' not in name:
                    result[name] = self._builtin_module(extension)
        return result

    def _builtin_module(self, name):
        return self.extension_cache.get_pymodule(name)

    def get_relative_module(self, name, folder, level):
        module = self.find_relative_module(name, folder, level)
        if module is None:
            raise ModuleNotFoundError('Module %s not found' % name)
        return self.resource_to_pyobject(module)

    def get_string_module(self, code, resource=None, force_errors=False):
        """Returns a `PyObject` object for the given code

        If `force_errors` is `True`, `exceptions.ModuleSyntaxError` is
        raised if module has syntax errors.  This overrides
        ``ignore_syntax_errors`` project config.

        """
        return PyModule(self, code, resource, force_errors=force_errors)

    def get_string_scope(self, code, resource=None):
        """Returns a `Scope` object for the given code"""
        return self.get_string_module(code, resource).get_scope()

    def _invalidate_resource_cache(self, resource, new_resource=None):
        for observer in self.cache_observers:
            observer(resource)

    def _find_module_in_folder(self, folder, modname):
        module = folder
        packages = modname.split('.')
        for pkg in packages[:-1]:
            if  module.is_folder() and module.has_child(pkg):
                module = module.get_child(pkg)
            else:
                return None
        if module.is_folder():
            if module.has_child(packages[-1]) and \
               module.get_child(packages[-1]).is_folder():
                return module.get_child(packages[-1])
            elif module.has_child(packages[-1] + '.py') and \
                 not module.get_child(packages[-1] + '.py').is_folder():
                return module.get_child(packages[-1] + '.py')

    def get_python_path_folders(self):
        import rope.base.project
        result = []
        for src in self.project.prefs.get('python_path', []) + sys.path:
            try:
                src_folder = rope.base.project.get_no_project().get_resource(src)
                result.append(src_folder)
            except rope.base.exceptions.ResourceNotFoundError:
                pass
        return result

    def find_module(self, modname, folder=None):
        """Returns a resource corresponding to the given module

        returns None if it can not be found
        """
        return self._find_module(modname, folder)

    def find_relative_module(self, modname, folder, level):
        for i in range(level - 1):
            folder = folder.parent
        if modname == '':
            return folder
        else:
            return self._find_module_in_folder(folder, modname)

    def _find_module(self, modname, folder=None):
        """Return `modname` module resource"""
        for src in self.get_source_folders():
            module = self._find_module_in_folder(src, modname)
            if module is not None:
                return module
        for src in self.get_python_path_folders():
            module = self._find_module_in_folder(src, modname)
            if module is not None:
                return module
        if folder is not None:
            module = self._find_module_in_folder(folder, modname)
            if module is not None:
                return module
        return None

    # INFO: It was decided not to cache source folders, since:
    #  - Does not take much time when the root folder contains
    #    packages, that is most of the time
    #  - We need a separate resource observer; `self.observer`
    #    does not get notified about module and folder creations
    def get_source_folders(self):
        """Returns project source folders"""
        if self.project.root is None:
            return []
        result = list(self._custom_source_folders)
        result.extend(self._find_source_folders(self.project.root))
        return result

    def resource_to_pyobject(self, resource, force_errors=False):
        return self.module_cache.get_pymodule(resource, force_errors)

    def get_python_files(self):
        """Returns all python files available in the project"""
        return [resource for resource in self.project.get_files()
                if self.is_python_file(resource)]

    def _is_package(self, folder):
        if folder.has_child('__init__.py') and \
           not folder.get_child('__init__.py').is_folder():
            return True
        else:
            return False

    def _find_source_folders(self, folder):
        for resource in folder.get_folders():
            if self._is_package(resource):
                return [folder]
        result = []
        for resource in folder.get_files():
            if resource.name.endswith('.py'):
                result.append(folder)
                break
        for resource in folder.get_folders():
            result.extend(self._find_source_folders(resource))
        return result

    def run_module(self, resource, args=None, stdin=None, stdout=None):
        """Run `resource` module

        Returns a `rope.base.oi.doa.PythonFileRunner` object for
        controlling the process.

        """
        perform_doa = self.project.prefs.get('perform_doi', True)
        perform_doa = self.project.prefs.get('perform_doa', perform_doa)
        receiver = self.object_info.doa_data_received
        if not perform_doa:
            receiver = None
        runner = rope.base.oi.doa.PythonFileRunner(
            self, resource, args, stdin, stdout, receiver)
        runner.add_finishing_observer(self.module_cache.forget_all_data)
        runner.run()
        return runner

    def analyze_module(self, resource, should_analyze=lambda py: True,
                       search_subscopes=lambda py: True, followed_calls=None):
        """Analyze `resource` module for static object inference

        This function forces rope to analyze this module to collect
        information about function calls.  `should_analyze` is a
        function that is called with a `PyDefinedObject` argument.  If
        it returns `True` the element is analyzed.  If it is `None` or
        returns `False` the element is not analyzed.

        `search_subscopes` is like `should_analyze`; The difference is
        that if it returns `False` the sub-scopes are all ignored.
        That is it is assumed that `should_analyze` returns `False`
        for all of its subscopes.

        `followed_calls` override the value of ``soa_followed_calls``
        project config.
        """
        if followed_calls is None:
            followed_calls = self.project.prefs.get('soa_followed_calls', 0)
        pymodule = self.resource_to_pyobject(resource)
        self.module_cache.forget_all_data()
        rope.base.oi.soa.analyze_module(
            self, pymodule, should_analyze, search_subscopes, followed_calls)

    def get_classes(self, task_handle=taskhandle.NullTaskHandle()):
        warnings.warn('`PyCore.get_classes()` is deprecated',
                      DeprecationWarning, stacklevel=2)
        return []

    def __str__(self):
        return str(self.module_cache) + str(self.object_info)

    def modname(self, resource):
        if resource.is_folder():
            module_name = resource.name
            source_folder = resource.parent
        elif resource.name == '__init__.py':
            module_name = resource.parent.name
            source_folder = resource.parent.parent
        else:
            module_name = resource.name[:-3]
            source_folder = resource.parent

        while source_folder != source_folder.parent and \
              source_folder.has_child('__init__.py'):
            module_name = source_folder.name + '.' + module_name
            source_folder = source_folder.parent
        return module_name

    @property
    @utils.cacheit
    def extension_modules(self):
        result = set(self.project.prefs.get('extension_modules', []))
        if self.project.prefs.get('import_dynload_stdmods', False):
            result.update(stdmods.dynload_modules())
        return result


class _ModuleCache(object):

    def __init__(self, pycore):
        self.pycore = pycore
        self.module_map = {}
        self.pycore.cache_observers.append(self._invalidate_resource)
        self.observer = self.pycore.observer

    def _invalidate_resource(self, resource):
        if resource in self.module_map:
            self.forget_all_data()
            self.observer.remove_resource(resource)
            del self.module_map[resource]

    def get_pymodule(self, resource, force_errors=False):
        if resource in self.module_map:
            return self.module_map[resource]
        if resource.is_folder():
            result = PyPackage(self.pycore, resource,
                               force_errors=force_errors)
        else:
            result = PyModule(self.pycore, resource=resource,
                              force_errors=force_errors)
            if result.has_errors:
                return result
        self.module_map[resource] = result
        self.observer.add_resource(resource)
        return result

    def forget_all_data(self):
        for pymodule in self.module_map.values():
            pymodule._forget_concluded_data()

    def __str__(self):
        return 'PyCore caches %d PyModules\n' % len(self.module_map)


class _ExtensionCache(object):

    def __init__(self, pycore):
        self.pycore = pycore
        self.extensions = {}

    def get_pymodule(self, name):
        if name == '__builtin__':
            return builtins.builtins
        allowed = self.pycore.extension_modules
        if name not in self.extensions and name in allowed:
            self.extensions[name] = builtins.BuiltinModule(name, self.pycore)
        return self.extensions.get(name)


def perform_soa_on_changed_scopes(project, resource, old_contents):
    pycore = project.pycore
    if resource.exists() and pycore.is_python_file(resource):
        try:
            new_contents = resource.read()
            # detecting changes in new_contents relative to old_contents
            detector = _TextChangeDetector(new_contents, old_contents)
            def search_subscopes(pydefined):
                scope = pydefined.get_scope()
                return detector.is_changed(scope.get_start(), scope.get_end())
            def should_analyze(pydefined):
                scope = pydefined.get_scope()
                start = scope.get_start()
                end = scope.get_end()
                return detector.consume_changes(start, end)
            pycore.analyze_module(resource, should_analyze, search_subscopes)
        except exceptions.ModuleSyntaxError:
            pass


class _TextChangeDetector(object):

    def __init__(self, old, new):
        self.old = old
        self.new = new
        self._set_diffs()

    def _set_diffs(self):
        differ = difflib.Differ()
        self.lines = []
        lineno = 0
        for line in differ.compare(self.old.splitlines(True),
                                   self.new.splitlines(True)):
            if line.startswith(' '):
                lineno += 1
            elif line.startswith('-'):
                lineno += 1
                self.lines.append(lineno)

    def is_changed(self, start, end):
        """Tell whether any of start till end lines have changed

        The end points are inclusive and indices start from 1.
        """
        left, right = self._get_changed(start, end)
        if left < right:
            return True
        return False

    def consume_changes(self, start, end):
        """Clear the changed status of lines from start till end"""
        left, right = self._get_changed(start, end)
        if left < right:
            del self.lines[left:right]
        return left < right

    def _get_changed(self, start, end):
        left = bisect.bisect_left(self.lines, start)
        right = bisect.bisect_right(self.lines, end)
        return left, right
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.