10
10
11
11
12
12
class ParsoPythonFile (object ):
13
+
13
14
@staticmethod
14
15
def parse (file_path , content = None ):
15
16
"""
16
- Create a PythonFile object with specified file_path and content. If content is None
17
- then, it is loaded from the file_path method. Otherwise, file_path is only used for
18
- reporting errors.
17
+ Create a PythonFile object with specified file_path and content.
18
+ If content is None then, it is loaded from the file_path method.
19
+ Otherwise, file_path is only used for reporting errors.
19
20
"""
20
21
try :
21
- # Parso reads files in binary mode and converts to unicode using python_bytes_to_unicode()
22
- # function. As a result, we no longer have information about original file encoding and
23
- # output of module.get_content() can not be converted back to bytes. For now we can make a
24
- # compromise by reading the file ourselves and passing content to parse() function.
22
+ # Parso reads files in binary mode and converts to unicode
23
+ # using python_bytes_to_unicode() function. As a result,
24
+ # we no longer have information about original file encoding and
25
+ # output of module.get_content() can't be converted back to bytes
26
+ # For now we can make a compromise by reading the file ourselves
27
+ # and passing content to parse() function.
25
28
if content is None :
26
29
with open (file_path ) as f :
27
30
content = f .read ()
28
- py_tree = _parser .parse (content , path = file_path , error_recovery = False )
31
+ py_tree = _parser .parse (
32
+ content , path = file_path , error_recovery = False )
29
33
return ParsoPythonFile (file_path , py_tree )
30
34
except parso .parser .ParserSyntaxError as ex :
31
- logging .error ("Failed to parse %s:%d '%s'" , file_path , ex .error_leaf .line , ex .error_leaf .get_code ())
35
+ logging .error ("Failed to parse %s:%d '%s'" , file_path ,
36
+ ex .error_leaf .line , ex .error_leaf .get_code ())
32
37
33
38
def __init__ (self , file_path , py_tree ):
34
39
self .file_path = file_path
@@ -51,7 +56,10 @@ def _iter_step_func_decorators(self):
51
56
break
52
57
53
58
def _step_decorator_args (self , decorator ):
54
- """Get the arguments passed to step decorators converted to python objects"""
59
+ """
60
+ Get the arguments passed to step decorators
61
+ converted to python objects.
62
+ """
55
63
args = decorator .children [3 :- 2 ]
56
64
step = None
57
65
if len (args ) == 1 :
@@ -68,15 +76,15 @@ def _step_decorator_args(self, decorator):
68
76
self .file_path , decorator .start_pos [0 ])
69
77
70
78
def iter_steps (self ):
71
- """Iterate over steps in the parsed file"""
79
+ """Iterate over steps in the parsed file. """
72
80
for func , decorator in self ._iter_step_func_decorators ():
73
81
step = self ._step_decorator_args (decorator )
74
82
if step :
75
83
span = self ._span_from_pos (decorator .start_pos , func .end_pos )
76
84
yield step , func .name .value , span
77
85
78
86
def _find_step_node (self , step_text ):
79
- """Find the ast node which contains the text"""
87
+ """Find the ast node which contains the text. """
80
88
for func , decorator in self ._iter_step_func_decorators ():
81
89
step = self ._step_decorator_args (decorator )
82
90
arg_node = decorator .children [3 ]
@@ -97,7 +105,8 @@ def _create_param_node(self, parent, name, prefix, is_last):
97
105
start_pos = parent [- 1 ].end_pos [0 ], parent [- 1 ].end_pos [1 ] + len (prefix )
98
106
children = [parso .python .tree .Name (name , start_pos , prefix )]
99
107
if not is_last :
100
- children .append (parso .python .tree .Operator (',' , children [- 1 ].end_pos ))
108
+ children .append (parso .python .tree .Operator (
109
+ ',' , children [- 1 ].end_pos ))
101
110
return parso .python .tree .Param (children , parent )
102
111
103
112
def _move_param_nodes (self , param_nodes , move_param_from_idx ):
@@ -109,26 +118,29 @@ def _move_param_nodes(self, param_nodes, move_param_from_idx):
109
118
return param_nodes
110
119
# Get the prefix from second parameter to use with new parameters
111
120
prefix = param_nodes [2 ].name .prefix if num_params > 1 else ' '
112
- new_param_nodes = [parso .python .tree .Operator ('(' , param_nodes [0 ].start_pos )]
121
+ new_param_nodes = [parso .python .tree .Operator (
122
+ '(' , param_nodes [0 ].start_pos )]
113
123
for i , move_from in enumerate (move_param_from_idx ):
114
124
param = self ._create_param_node (
115
125
new_param_nodes ,
116
- self ._get_param_name (param_nodes , i ) if move_from < 0 else param_nodes [move_from + 1 ].name .value ,
126
+ self ._get_param_name (
127
+ param_nodes , i ) if move_from < 0 else param_nodes [move_from + 1 ].name .value ,
117
128
'' if i == 0 else prefix ,
118
129
i >= len (move_param_from_idx ) - 1
119
130
)
120
131
new_param_nodes .append (param )
121
- new_param_nodes .append (parso .python .tree .Operator (')' , new_param_nodes [- 1 ].end_pos ))
132
+ new_param_nodes .append (parso .python .tree .Operator (
133
+ ')' , new_param_nodes [- 1 ].end_pos ))
122
134
# Change the parent to actual function
123
135
for node in new_param_nodes :
124
136
node .parent = param_nodes [0 ].parent
125
137
return new_param_nodes
126
138
127
139
def refactor_step (self , old_text , new_text , move_param_from_idx ):
128
140
"""
129
- Find the step with old_text and change it to new_text. The step function
130
- parameters are also changed according to move_param_from_idx. Each entry in
131
- this list should specify parameter position from old
141
+ Find the step with old_text and change it to new_text.The step function
142
+ parameters are also changed according to move_param_from_idx.
143
+ Each entry in this list should specify parameter position from old.
132
144
"""
133
145
diffs = []
134
146
step , func = self ._find_step_node (old_text )
@@ -137,7 +149,8 @@ def refactor_step(self, old_text, new_text, move_param_from_idx):
137
149
step_diff = self ._refactor_step_text (step , old_text , new_text )
138
150
diffs .append (step_diff )
139
151
params_list_node = func .children [2 ]
140
- moved_params = self ._move_param_nodes (params_list_node .children , move_param_from_idx )
152
+ moved_params = self ._move_param_nodes (
153
+ params_list_node .children , move_param_from_idx )
141
154
if params_list_node .children is not moved_params :
142
155
# Record original parameter list span excluding braces
143
156
params_span = self ._span_from_pos (
@@ -150,7 +163,7 @@ def refactor_step(self, old_text, new_text, move_param_from_idx):
150
163
return diffs
151
164
152
165
def get_code (self ):
153
- """Returns current content of the tree."""
166
+ """Return current content of the tree."""
154
167
return self .py_tree .get_code ()
155
168
156
169
def _get_param_name (self , param_nodes , i ):
0 commit comments