Skip to content

Commit abbc0c2

Browse files
surevsVinay Shankar Shukla
authored andcommitted
Style of coding enhanced
* minor spell correction and gauge link addition for reference * MAINT:styling enhanced * MAINT:Futures added to handle for python2 * MAINT:styling enhanced * MAINT:added grpc to requirements * MAINT:requirement correction as grpcio already exists
1 parent 470078f commit abbc0c2

16 files changed

+372
-205
lines changed

getgauge/lsp_server.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import threading
2-
32
from getgauge import processor, validator, refactor
43
from getgauge.messages import lsp_pb2_grpc
54
from getgauge.messages.lsp_pb2 import Empty
6-
from getgauge.messages.messages_pb2 import Message, ImplementationFileGlobPatternResponse, StepNamesResponse, \
5+
from getgauge.messages.messages_pb2 import Message, \
6+
ImplementationFileGlobPatternResponse, StepNamesResponse, \
77
ImplementationFileListResponse
88
from getgauge.registry import registry
99
from getgauge.util import get_impl_files, get_step_impl_dir
1010

1111

1212
class LspServerHandler(lsp_pb2_grpc.lspServiceServicer):
13+
1314
def __init__(self, server):
1415
self.server = server
1516
self.kill_event = threading.Event()
@@ -37,7 +38,8 @@ def GetImplementationFiles(self, request, context):
3738

3839
def ImplementStub(self, request, context):
3940
res = Message()
40-
processor.stub_impl_response(request.codes, request.implementationFilePath, res)
41+
processor.stub_impl_response(
42+
request.codes, request.implementationFilePath, res)
4143
return res.fileDiff
4244

4345
def ValidateStep(self, request, context):

getgauge/parser_parso.py

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,30 @@
1010

1111

1212
class 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):

getgauge/parser_redbaron.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55

66
class RedbaronPythonFile(object):
7+
78
@staticmethod
89
def parse(file_path, content=None):
910
"""
10-
Create a PythonFile object with specified file_path and content. If content is None
11-
then, it is loaded from the file_path method. Otherwise, file_path is only used for
12-
reporting errors.
11+
Create a PythonFile object with specified file_path and content.
12+
If content is None then, it is loaded from the file_path method.
13+
Otherwise, file_path is only used for reporting errors.
1314
"""
1415
try:
1516
if content is None:
@@ -51,7 +52,7 @@ def calculate_span():
5152
return calculate_span if lazy else calculate_span()
5253

5354
def _iter_step_func_decorators(self):
54-
"""Find top level functions with step decorator in parsed file"""
55+
"""Find top level functions with step decorator in parsed file."""
5556
for node in self.py_tree:
5657
if node.type == 'def':
5758
for decorator in node.decorators:
@@ -60,7 +61,9 @@ def _iter_step_func_decorators(self):
6061
break
6162

6263
def _step_decorator_args(self, decorator):
63-
"""Get the arguments passed to step decorators converted to python objects"""
64+
"""
65+
Get arguments passed to step decorators converted to python objects.
66+
"""
6467
args = decorator.call.value
6568
step = None
6669
if len(args) == 1:
@@ -70,21 +73,22 @@ def _step_decorator_args(self, decorator):
7073
pass
7174
if isinstance(step, six.string_types + (list,)):
7275
return step
73-
logging.error("Decorator step accepts either a string or a list of strings - %s",
76+
logging.error("Decorator step accepts either a string or a list of \
77+
strings - %s",
7478
self.file_path)
7579
else:
7680
logging.error("Decorator step accepts only one argument - %s",
7781
self.file_path)
7882

7983
def iter_steps(self):
80-
"""Iterate over steps in the parsed file"""
84+
"""Iterate over steps in the parsed file."""
8185
for func, decorator in self._iter_step_func_decorators():
8286
step = self._step_decorator_args(decorator)
8387
if step:
8488
yield step, func.name, self._span_for_node(func, True)
8589

8690
def _find_step_node(self, step_text):
87-
"""Find the ast node which contains the text"""
91+
"""Find the ast node which contains the text."""
8892
for func, decorator in self._iter_step_func_decorators():
8993
step = self._step_decorator_args(decorator)
9094
arg_node = decorator.call.value[0].value
@@ -121,9 +125,10 @@ def _move_params(self, params, move_param_from_idx):
121125

122126
def refactor_step(self, old_text, new_text, move_param_from_idx):
123127
"""
124-
Find the step with old_text and change it to new_text. The step function
125-
parameters are also changed according to move_param_from_idx. Each entry in
126-
this list should specify parameter position from old
128+
Find the step with old_text and change it to new_text.
129+
The step function parameters are also changed according
130+
to move_param_from_idx. Each entry in this list should
131+
specify parameter position from old
127132
"""
128133
diffs = []
129134
step, func = self._find_step_node(old_text)
@@ -139,5 +144,5 @@ def refactor_step(self, old_text, new_text, move_param_from_idx):
139144
return diffs
140145

141146
def get_code(self):
142-
"""Returns current content of the tree."""
147+
"""Return current content of the tree."""
143148
return self.py_tree.dumps()

0 commit comments

Comments
 (0)