Skip to content

Commit 7a6e6b7

Browse files
committed
addd rope library #220
1 parent 7e07bb5 commit 7a6e6b7

73 files changed

Lines changed: 17530 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

pythonFiles/rope/__init__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""rope, a python refactoring library"""
2+
3+
INFO = __doc__
4+
VERSION = '0.10.3'
5+
COPYRIGHT = """\
6+
Copyright (C) 2014-2015 Matej Cepl
7+
Copyright (C) 2006-2012 Ali Gholami Rudi
8+
Copyright (C) 2009-2012 Anton Gritsay
9+
10+
This program is free software; you can redistribute it and/or modify it
11+
under the terms of GNU General Public License as published by the
12+
Free Software Foundation; either version 2 of the license, or (at your
13+
opinion) any later version.
14+
15+
This program is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
GNU General Public License for more details."""

pythonFiles/rope/base/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""Base rope package
2+
3+
This package contains rope core modules that are used by other modules
4+
and packages.
5+
6+
"""
7+
8+
__all__ = ['project', 'libutils', 'exceptions']

pythonFiles/rope/base/arguments.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import rope.base.evaluate
2+
from rope.base import ast
3+
4+
5+
class Arguments(object):
6+
"""A class for evaluating parameters passed to a function
7+
8+
You can use the `create_arguments` factory. It handles implicit
9+
first arguments.
10+
11+
"""
12+
13+
def __init__(self, args, scope):
14+
self.args = args
15+
self.scope = scope
16+
self.instance = None
17+
18+
def get_arguments(self, parameters):
19+
result = []
20+
for pyname in self.get_pynames(parameters):
21+
if pyname is None:
22+
result.append(None)
23+
else:
24+
result.append(pyname.get_object())
25+
return result
26+
27+
def get_pynames(self, parameters):
28+
result = [None] * max(len(parameters), len(self.args))
29+
for index, arg in enumerate(self.args):
30+
if isinstance(arg, ast.keyword) and arg.arg in parameters:
31+
result[parameters.index(arg.arg)] = self._evaluate(arg.value)
32+
else:
33+
result[index] = self._evaluate(arg)
34+
return result
35+
36+
def get_instance_pyname(self):
37+
if self.args:
38+
return self._evaluate(self.args[0])
39+
40+
def _evaluate(self, ast_node):
41+
return rope.base.evaluate.eval_node(self.scope, ast_node)
42+
43+
44+
def create_arguments(primary, pyfunction, call_node, scope):
45+
"""A factory for creating `Arguments`"""
46+
args = list(call_node.args)
47+
args.extend(call_node.keywords)
48+
called = call_node.func
49+
# XXX: Handle constructors
50+
if _is_method_call(primary, pyfunction) and \
51+
isinstance(called, ast.Attribute):
52+
args.insert(0, called.value)
53+
return Arguments(args, scope)
54+
55+
56+
class ObjectArguments(object):
57+
58+
def __init__(self, pynames):
59+
self.pynames = pynames
60+
61+
def get_arguments(self, parameters):
62+
result = []
63+
for pyname in self.pynames:
64+
if pyname is None:
65+
result.append(None)
66+
else:
67+
result.append(pyname.get_object())
68+
return result
69+
70+
def get_pynames(self, parameters):
71+
return self.pynames
72+
73+
def get_instance_pyname(self):
74+
return self.pynames[0]
75+
76+
77+
class MixedArguments(object):
78+
79+
def __init__(self, pyname, arguments, scope):
80+
"""`argumens` is an instance of `Arguments`"""
81+
self.pyname = pyname
82+
self.args = arguments
83+
84+
def get_pynames(self, parameters):
85+
return [self.pyname] + self.args.get_pynames(parameters[1:])
86+
87+
def get_arguments(self, parameters):
88+
result = []
89+
for pyname in self.get_pynames(parameters):
90+
if pyname is None:
91+
result.append(None)
92+
else:
93+
result.append(pyname.get_object())
94+
return result
95+
96+
def get_instance_pyname(self):
97+
return self.pyname
98+
99+
100+
def _is_method_call(primary, pyfunction):
101+
if primary is None:
102+
return False
103+
pyobject = primary.get_object()
104+
if isinstance(pyobject.get_type(), rope.base.pyobjects.PyClass) and \
105+
isinstance(pyfunction, rope.base.pyobjects.PyFunction) and \
106+
isinstance(pyfunction.parent, rope.base.pyobjects.PyClass):
107+
return True
108+
if isinstance(pyobject.get_type(), rope.base.pyobjects.AbstractClass) and \
109+
isinstance(pyfunction, rope.base.builtins.BuiltinFunction):
110+
return True
111+
return False

pythonFiles/rope/base/ast.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import _ast
2+
from _ast import *
3+
4+
from rope.base import fscommands
5+
6+
try:
7+
unicode
8+
except NameError:
9+
unicode = str
10+
11+
12+
def parse(source, filename='<string>'):
13+
# NOTE: the raw string should be given to `compile` function
14+
if isinstance(source, unicode):
15+
source = fscommands.unicode_to_file_data(source)
16+
if b'\r' in source:
17+
source = source.replace(b'\r\n', b'\n').replace(b'\r', b'\n')
18+
if not source.endswith(b'\n'):
19+
source += b'\n'
20+
try:
21+
return compile(source, filename, 'exec', _ast.PyCF_ONLY_AST)
22+
except (TypeError, ValueError) as e:
23+
error = SyntaxError()
24+
error.lineno = 1
25+
error.filename = filename
26+
error.msg = str(e)
27+
raise error
28+
29+
30+
def walk(node, walker):
31+
"""Walk the syntax tree"""
32+
method_name = '_' + node.__class__.__name__
33+
method = getattr(walker, method_name, None)
34+
if method is not None:
35+
if isinstance(node, _ast.ImportFrom) and node.module is None:
36+
# In python < 2.7 ``node.module == ''`` for relative imports
37+
# but for python 2.7 it is None. Generalizing it to ''.
38+
node.module = ''
39+
return method(node)
40+
for child in get_child_nodes(node):
41+
walk(child, walker)
42+
43+
44+
def get_child_nodes(node):
45+
if isinstance(node, _ast.Module):
46+
return node.body
47+
result = []
48+
if node._fields is not None:
49+
for name in node._fields:
50+
child = getattr(node, name)
51+
if isinstance(child, list):
52+
for entry in child:
53+
if isinstance(entry, _ast.AST):
54+
result.append(entry)
55+
if isinstance(child, _ast.AST):
56+
result.append(child)
57+
return result
58+
59+
60+
def call_for_nodes(node, callback, recursive=False):
61+
"""If callback returns `True` the child nodes are skipped"""
62+
result = callback(node)
63+
if recursive and not result:
64+
for child in get_child_nodes(node):
65+
call_for_nodes(child, callback, recursive)
66+
67+
68+
def get_children(node):
69+
result = []
70+
if node._fields is not None:
71+
for name in node._fields:
72+
if name in ['lineno', 'col_offset']:
73+
continue
74+
child = getattr(node, name)
75+
result.append(child)
76+
return result

pythonFiles/rope/base/astutils.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from rope.base import ast
2+
3+
4+
def get_name_levels(node):
5+
"""Return a list of ``(name, level)`` tuples for assigned names
6+
7+
The `level` is `None` for simple assignments and is a list of
8+
numbers for tuple assignments for example in::
9+
10+
a, (b, c) = x
11+
12+
The levels for for `a` is ``[0]``, for `b` is ``[1, 0]`` and for
13+
`c` is ``[1, 1]``.
14+
15+
"""
16+
visitor = _NodeNameCollector()
17+
ast.walk(node, visitor)
18+
return visitor.names
19+
20+
21+
class _NodeNameCollector(object):
22+
23+
def __init__(self, levels=None):
24+
self.names = []
25+
self.levels = levels
26+
self.index = 0
27+
28+
def _add_node(self, node):
29+
new_levels = []
30+
if self.levels is not None:
31+
new_levels = list(self.levels)
32+
new_levels.append(self.index)
33+
self.index += 1
34+
self._added(node, new_levels)
35+
36+
def _added(self, node, levels):
37+
if hasattr(node, 'id'):
38+
self.names.append((node.id, levels))
39+
40+
def _Name(self, node):
41+
self._add_node(node)
42+
43+
def _Tuple(self, node):
44+
new_levels = []
45+
if self.levels is not None:
46+
new_levels = list(self.levels)
47+
new_levels.append(self.index)
48+
self.index += 1
49+
visitor = _NodeNameCollector(new_levels)
50+
for child in ast.get_child_nodes(node):
51+
ast.walk(child, visitor)
52+
self.names.extend(visitor.names)
53+
54+
def _Subscript(self, node):
55+
self._add_node(node)
56+
57+
def _Attribute(self, node):
58+
self._add_node(node)
59+
60+
def _Slice(self, node):
61+
self._add_node(node)

0 commit comments

Comments
 (0)