Skip to content

Commit

Permalink
Style of coding enhanced
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
surevs authored and Vinay Shankar Shukla committed Sep 4, 2018
1 parent 470078f commit abbc0c2
Show file tree
Hide file tree
Showing 16 changed files with 372 additions and 205 deletions.
8 changes: 5 additions & 3 deletions getgauge/lsp_server.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import threading

from getgauge import processor, validator, refactor
from getgauge.messages import lsp_pb2_grpc
from getgauge.messages.lsp_pb2 import Empty
from getgauge.messages.messages_pb2 import Message, ImplementationFileGlobPatternResponse, StepNamesResponse, \
from getgauge.messages.messages_pb2 import Message, \
ImplementationFileGlobPatternResponse, StepNamesResponse, \
ImplementationFileListResponse
from getgauge.registry import registry
from getgauge.util import get_impl_files, get_step_impl_dir


class LspServerHandler(lsp_pb2_grpc.lspServiceServicer):

def __init__(self, server):
self.server = server
self.kill_event = threading.Event()
Expand Down Expand Up @@ -37,7 +38,8 @@ def GetImplementationFiles(self, request, context):

def ImplementStub(self, request, context):
res = Message()
processor.stub_impl_response(request.codes, request.implementationFilePath, res)
processor.stub_impl_response(
request.codes, request.implementationFilePath, res)
return res.fileDiff

def ValidateStep(self, request, context):
Expand Down
55 changes: 34 additions & 21 deletions getgauge/parser_parso.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,30 @@


class ParsoPythonFile(object):

@staticmethod
def parse(file_path, content=None):
"""
Create a PythonFile object with specified file_path and content. If content is None
then, it is loaded from the file_path method. Otherwise, file_path is only used for
reporting errors.
Create a PythonFile object with specified file_path and content.
If content is None then, it is loaded from the file_path method.
Otherwise, file_path is only used for reporting errors.
"""
try:
# Parso reads files in binary mode and converts to unicode using python_bytes_to_unicode()
# function. As a result, we no longer have information about original file encoding and
# output of module.get_content() can not be converted back to bytes. For now we can make a
# compromise by reading the file ourselves and passing content to parse() function.
# Parso reads files in binary mode and converts to unicode
# using python_bytes_to_unicode() function. As a result,
# we no longer have information about original file encoding and
# output of module.get_content() can't be converted back to bytes
# For now we can make a compromise by reading the file ourselves
# and passing content to parse() function.
if content is None:
with open(file_path) as f:
content = f.read()
py_tree = _parser.parse(content, path=file_path, error_recovery=False)
py_tree = _parser.parse(
content, path=file_path, error_recovery=False)
return ParsoPythonFile(file_path, py_tree)
except parso.parser.ParserSyntaxError as ex:
logging.error("Failed to parse %s:%d '%s'", file_path, ex.error_leaf.line, ex.error_leaf.get_code())
logging.error("Failed to parse %s:%d '%s'", file_path,
ex.error_leaf.line, ex.error_leaf.get_code())

def __init__(self, file_path, py_tree):
self.file_path = file_path
Expand All @@ -51,7 +56,10 @@ def _iter_step_func_decorators(self):
break

def _step_decorator_args(self, decorator):
"""Get the arguments passed to step decorators converted to python objects"""
"""
Get the arguments passed to step decorators
converted to python objects.
"""
args = decorator.children[3:-2]
step = None
if len(args) == 1:
Expand All @@ -68,15 +76,15 @@ def _step_decorator_args(self, decorator):
self.file_path, decorator.start_pos[0])

def iter_steps(self):
"""Iterate over steps in the parsed file"""
"""Iterate over steps in the parsed file."""
for func, decorator in self._iter_step_func_decorators():
step = self._step_decorator_args(decorator)
if step:
span = self._span_from_pos(decorator.start_pos, func.end_pos)
yield step, func.name.value, span

def _find_step_node(self, step_text):
"""Find the ast node which contains the text"""
"""Find the ast node which contains the text."""
for func, decorator in self._iter_step_func_decorators():
step = self._step_decorator_args(decorator)
arg_node = decorator.children[3]
Expand All @@ -97,7 +105,8 @@ def _create_param_node(self, parent, name, prefix, is_last):
start_pos = parent[-1].end_pos[0], parent[-1].end_pos[1] + len(prefix)
children = [parso.python.tree.Name(name, start_pos, prefix)]
if not is_last:
children.append(parso.python.tree.Operator(',', children[-1].end_pos))
children.append(parso.python.tree.Operator(
',', children[-1].end_pos))
return parso.python.tree.Param(children, parent)

def _move_param_nodes(self, param_nodes, move_param_from_idx):
Expand All @@ -109,26 +118,29 @@ def _move_param_nodes(self, param_nodes, move_param_from_idx):
return param_nodes
# Get the prefix from second parameter to use with new parameters
prefix = param_nodes[2].name.prefix if num_params > 1 else ' '
new_param_nodes = [parso.python.tree.Operator('(', param_nodes[0].start_pos)]
new_param_nodes = [parso.python.tree.Operator(
'(', param_nodes[0].start_pos)]
for i, move_from in enumerate(move_param_from_idx):
param = self._create_param_node(
new_param_nodes,
self._get_param_name(param_nodes, i) if move_from < 0 else param_nodes[move_from + 1].name.value,
self._get_param_name(
param_nodes, i) if move_from < 0 else param_nodes[move_from + 1].name.value,
'' if i == 0 else prefix,
i >= len(move_param_from_idx) - 1
)
new_param_nodes.append(param)
new_param_nodes.append(parso.python.tree.Operator(')', new_param_nodes[-1].end_pos))
new_param_nodes.append(parso.python.tree.Operator(
')', new_param_nodes[-1].end_pos))
# Change the parent to actual function
for node in new_param_nodes:
node.parent = param_nodes[0].parent
return new_param_nodes

def refactor_step(self, old_text, new_text, move_param_from_idx):
"""
Find the step with old_text and change it to new_text. The step function
parameters are also changed according to move_param_from_idx. Each entry in
this list should specify parameter position from old
Find the step with old_text and change it to new_text.The step function
parameters are also changed according to move_param_from_idx.
Each entry in this list should specify parameter position from old.
"""
diffs = []
step, func = self._find_step_node(old_text)
Expand All @@ -137,7 +149,8 @@ def refactor_step(self, old_text, new_text, move_param_from_idx):
step_diff = self._refactor_step_text(step, old_text, new_text)
diffs.append(step_diff)
params_list_node = func.children[2]
moved_params = self._move_param_nodes(params_list_node.children, move_param_from_idx)
moved_params = self._move_param_nodes(
params_list_node.children, move_param_from_idx)
if params_list_node.children is not moved_params:
# Record original parameter list span excluding braces
params_span = self._span_from_pos(
Expand All @@ -150,7 +163,7 @@ def refactor_step(self, old_text, new_text, move_param_from_idx):
return diffs

def get_code(self):
"""Returns current content of the tree."""
"""Return current content of the tree."""
return self.py_tree.get_code()

def _get_param_name(self, param_nodes, i):
Expand Down
29 changes: 17 additions & 12 deletions getgauge/parser_redbaron.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@


class RedbaronPythonFile(object):

@staticmethod
def parse(file_path, content=None):
"""
Create a PythonFile object with specified file_path and content. If content is None
then, it is loaded from the file_path method. Otherwise, file_path is only used for
reporting errors.
Create a PythonFile object with specified file_path and content.
If content is None then, it is loaded from the file_path method.
Otherwise, file_path is only used for reporting errors.
"""
try:
if content is None:
Expand Down Expand Up @@ -51,7 +52,7 @@ def calculate_span():
return calculate_span if lazy else calculate_span()

def _iter_step_func_decorators(self):
"""Find top level functions with step decorator in parsed file"""
"""Find top level functions with step decorator in parsed file."""
for node in self.py_tree:
if node.type == 'def':
for decorator in node.decorators:
Expand All @@ -60,7 +61,9 @@ def _iter_step_func_decorators(self):
break

def _step_decorator_args(self, decorator):
"""Get the arguments passed to step decorators converted to python objects"""
"""
Get arguments passed to step decorators converted to python objects.
"""
args = decorator.call.value
step = None
if len(args) == 1:
Expand All @@ -70,21 +73,22 @@ def _step_decorator_args(self, decorator):
pass
if isinstance(step, six.string_types + (list,)):
return step
logging.error("Decorator step accepts either a string or a list of strings - %s",
logging.error("Decorator step accepts either a string or a list of \
strings - %s",
self.file_path)
else:
logging.error("Decorator step accepts only one argument - %s",
self.file_path)

def iter_steps(self):
"""Iterate over steps in the parsed file"""
"""Iterate over steps in the parsed file."""
for func, decorator in self._iter_step_func_decorators():
step = self._step_decorator_args(decorator)
if step:
yield step, func.name, self._span_for_node(func, True)

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

def refactor_step(self, old_text, new_text, move_param_from_idx):
"""
Find the step with old_text and change it to new_text. The step function
parameters are also changed according to move_param_from_idx. Each entry in
this list should specify parameter position from old
Find the step with old_text and change it to new_text.
The step function parameters are also changed according
to move_param_from_idx. Each entry in this list should
specify parameter position from old
"""
diffs = []
step, func = self._find_step_node(old_text)
Expand All @@ -139,5 +144,5 @@ def refactor_step(self, old_text, new_text, move_param_from_idx):
return diffs

def get_code(self):
"""Returns current content of the tree."""
"""Return current content of the tree."""
return self.py_tree.dumps()
Loading

0 comments on commit abbc0c2

Please sign in to comment.