1010
1111
1212class ParsoPythonFile (object ):
13+
1314 @staticmethod
1415 def parse (file_path , content = None ):
1516 """
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.
1920 """
2021 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.
2528 if content is None :
2629 with open (file_path ) as f :
2730 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 )
2933 return ParsoPythonFile (file_path , py_tree )
3034 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 ())
3237
3338 def __init__ (self , file_path , py_tree ):
3439 self .file_path = file_path
@@ -51,7 +56,10 @@ def _iter_step_func_decorators(self):
5156 break
5257
5358 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+ """
5563 args = decorator .children [3 :- 2 ]
5664 step = None
5765 if len (args ) == 1 :
@@ -68,15 +76,15 @@ def _step_decorator_args(self, decorator):
6876 self .file_path , decorator .start_pos [0 ])
6977
7078 def iter_steps (self ):
71- """Iterate over steps in the parsed file"""
79+ """Iterate over steps in the parsed file. """
7280 for func , decorator in self ._iter_step_func_decorators ():
7381 step = self ._step_decorator_args (decorator )
7482 if step :
7583 span = self ._span_from_pos (decorator .start_pos , func .end_pos )
7684 yield step , func .name .value , span
7785
7886 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. """
8088 for func , decorator in self ._iter_step_func_decorators ():
8189 step = self ._step_decorator_args (decorator )
8290 arg_node = decorator .children [3 ]
@@ -97,7 +105,8 @@ def _create_param_node(self, parent, name, prefix, is_last):
97105 start_pos = parent [- 1 ].end_pos [0 ], parent [- 1 ].end_pos [1 ] + len (prefix )
98106 children = [parso .python .tree .Name (name , start_pos , prefix )]
99107 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 ))
101110 return parso .python .tree .Param (children , parent )
102111
103112 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):
109118 return param_nodes
110119 # Get the prefix from second parameter to use with new parameters
111120 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 )]
113123 for i , move_from in enumerate (move_param_from_idx ):
114124 param = self ._create_param_node (
115125 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 ,
117128 '' if i == 0 else prefix ,
118129 i >= len (move_param_from_idx ) - 1
119130 )
120131 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 ))
122134 # Change the parent to actual function
123135 for node in new_param_nodes :
124136 node .parent = param_nodes [0 ].parent
125137 return new_param_nodes
126138
127139 def refactor_step (self , old_text , new_text , move_param_from_idx ):
128140 """
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.
132144 """
133145 diffs = []
134146 step , func = self ._find_step_node (old_text )
@@ -137,7 +149,8 @@ def refactor_step(self, old_text, new_text, move_param_from_idx):
137149 step_diff = self ._refactor_step_text (step , old_text , new_text )
138150 diffs .append (step_diff )
139151 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 )
141154 if params_list_node .children is not moved_params :
142155 # Record original parameter list span excluding braces
143156 params_span = self ._span_from_pos (
@@ -150,7 +163,7 @@ def refactor_step(self, old_text, new_text, move_param_from_idx):
150163 return diffs
151164
152165 def get_code (self ):
153- """Returns current content of the tree."""
166+ """Return current content of the tree."""
154167 return self .py_tree .get_code ()
155168
156169 def _get_param_name (self , param_nodes , i ):
0 commit comments