Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added functionality to ignore Keywords in doctrings #64

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
3 changes: 1 addition & 2 deletions dyc/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ def initialize(self, change=None):

fileLines = list(fileinput.input(self.filename))
i = 0

for line in fileinput.input(self.filename):
filename = fileinput.filename()
lineno = fileinput.lineno()
Expand Down Expand Up @@ -211,4 +210,4 @@ def extensions(self):
filter(
None, map(lambda fmt: fmt.get("extension"), self.config.get("formats"))
)
)
)
1 change: 1 addition & 0 deletions dyc/configs/defaults.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ formats:
- self
class:
keywords: ['class']

111 changes: 60 additions & 51 deletions dyc/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ def extract_and_set_information(self, filename, start, line, length):

if not end:
end = length

#print("\nMethod String: \n",method_string)
#print("Initial Line: \n",initial_line)
#print("Comparision : ", len(method_string.strip()) , len(initial_line.strip()))
if len(method_string.strip()) == len(initial_line.strip()):
method_string = ""
initial_line = ""
linecache.clearcache()
return MethodInterface(
plain=method_string,
Expand All @@ -89,7 +94,7 @@ def validate(self, result):
if not result:
return False
name = result.name
if name not in self.config.get(
if name != "" and name not in self.config.get(
"ignore", []
) and not self.is_first_line_documented(result):
if (
Expand Down Expand Up @@ -207,16 +212,19 @@ def _get_name(self, line):
----------
str line: String line that has the method's name
"""
for keyword in self.config.get("keywords", []):
clear_defs = re.sub("{} ".format(keyword), "", line.strip())
name = re.sub(r"\([^)]*\)\:", "", clear_defs).strip()
if re.search(r"\(([\s\S]*)\)", name):
try:
name = re.match(r"^[^\(]+", name).group()
except:
pass
if name:
return name
if line != "":
for keyword in self.config.get("keywords", []):
clear_defs = re.sub("{} ".format(keyword), "", line.strip())
name = re.sub(r"\([^)]*\)\:", "", clear_defs).strip()
if re.search(r"\(([\s\S]*)\)", name):
try:
name = re.match(r"^[^\(]+", name).group()
except:
pass
if name:
return name
else:
return ""

def _is_method(self, line):
"""
Expand Down Expand Up @@ -358,44 +366,45 @@ def confirm(self, polished):
str polished: complete polished string before popping up
"""
polished = add_start_end(polished)
method_split = self.plain.split("\n")
if self.config.get("within_scope"):
# Check if method comes in an unusual format
keywords = self.config.get("keywords")
firstLine = method_split[0]
pos = 1
while not is_one_line_method(firstLine, keywords):
firstLine += method_split[pos]
pos += 1
method_split.insert(pos, polished)
else:
method_split.insert(0, polished)

try:
result = "\n".join(method_split)
message = click.edit(
"## CONFIRM: MODIFY DOCSTRING BETWEEN START AND END LINES ONLY\n\n"
+ result
)
message = result if message == None else "\n".join(message.split("\n")[2:])
except:
print("Quitting the program in the editor terminates the process. Thanks")
sys.exit()

final = []
start = False
end = False

for x in message.split("\n"):
stripped = x.strip()
if stripped == "## END":
end = True
if start and not end:
final.append(x)
if stripped == "## START":
start = True

self.result = "\n".join(final)
if self.plain != "":
method_split = self.plain.split("\n")
if self.config.get("within_scope"):
# Check if method comes in an unusual format
keywords = self.config.get("keywords")
firstLine = method_split[0]
pos = 1
while not is_one_line_method(firstLine, keywords):
firstLine += method_split[pos]
pos += 1
method_split.insert(pos, polished)
else:
method_split.insert(0, polished)

try:
result = "\n".join(method_split)
message = click.edit(
"## CONFIRM: MODIFY DOCSTRING BETWEEN START AND END LINES ONLY\n\n"
+ result
)
message = result if message == None else "\n".join(message.split("\n")[2:])
except:
print("Quitting the program in the editor terminates the process. Thanks")
sys.exit()

final = []
start = False
end = False

for x in message.split("\n"):
stripped = x.strip()
if stripped == "## END":
end = True
if start and not end:
final.append(x)
if stripped == "## START":
start = True

self.result = "\n".join(final)

def polish(self):
"""
Expand Down Expand Up @@ -514,4 +523,4 @@ def sanitize(self):
"""
Sanitizes arguments to validate all arguments are correct
"""
return map(lambda arg: re.findall(r"[a-zA-Z0-9_]+", arg)[0], self.args)
return map(lambda arg: re.findall(r"[a-zA-Z0-9\_\,\s\=\[\]\(\)\{\}\*\&\%\!\-\"\'\+\;\.]*", arg)[0], self.args)
9 changes: 5 additions & 4 deletions dyc/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import yaml
import string
import re

INDENT_OPTIONS = {"tab": "\t", "2 spaces": " ", "False": ""}

Expand Down Expand Up @@ -138,8 +139,6 @@ def get_hunk(patch):
----------
str patch: Diff patched text
"""
import re

pat = r".*?\@\@(.*)\@\@.*"
match = re.findall(pat, patch)
return [m.strip() for m in match]
Expand Down Expand Up @@ -171,8 +170,10 @@ def is_one_line_method(line, keywords):
list keywords: list of keywords like def for python, func for go etc.
"""
found = [word.lstrip() for word in line.split(" ") if word.lstrip() in keywords]
pattern = r'^[\s]*(def)\s[a-zA-Z0-9\_]*\([a-zA-Z0-9\_\,\s\=\[\]\(\)\{\}\*\&\%\!\-\"\'\+\;\.]*\)(\s\:|\:)'
if found:
return line.count("(") == line.count(")")
match = re.search(pattern,line)
return True if line.count("(") == line.count(")") and match else False
return bool(found)


Expand All @@ -186,4 +187,4 @@ def is_comment(line, comments):
str line: The line string in a file
list comments: A list of potential comment keywords
"""
return line.lstrip(' ')[0] in comments
return line.lstrip(' ')[0] in comments
158 changes: 79 additions & 79 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,79 +1,79 @@
from dyc.utils import (
get_leading_whitespace,
read_yaml,
get_indent,
get_extension,
is_comment,
)


class TestGetLeadingWhitespace:
def test_tabs(self):
"""Test tabs functionality"""
text = '\t\tHello'
expected = '\t\t'
got = get_leading_whitespace(text)
assert expected == got

def test_whitespace(self):
"""Test whitespace functionality"""
space = ' '
text = '{space}Such a long whitespace'.format(space=space)
expected = space
got = get_leading_whitespace(text)
assert expected == got


class TestReadYaml:
def test_should_return_none_if_not_found(self):
random_path = '/path/to/non/existing/file.yaml'
expected = None
got = read_yaml(random_path)
assert expected == got


class TestGetIndent:
def test_tabs(self):
assert get_indent('tab') == '\t'

def test_2_spaces(self):
assert get_indent('2 spaces') == ' '

def test_falsy_value(self):
assert get_indent(False) == ''

def test_default_4_spaces(self):
assert get_indent(None) == ' '


class TestGetExtension:
def test_existing_extension_valid(self):
ext = 'file.puk'
expected = 'puk'
got = get_extension(ext)
assert expected == got

def test_non_existing_extension(self):
ext = 'file'
expected = ''
got = get_extension(ext)
assert expected == got

def test_wrong_extension_type(self):
exts = [dict(), False, True, [], 123]
expected = ''
for ext in exts:
got = get_extension(ext)
assert expected == got


class TestIsComment:
def test_valid_comments(self):
"""Testing valid comments"""
text = '# Hello World'
assert is_comment(text, ['#']) == True

def test_invalid_comments(self):
"""Testing invalid comments"""
text = '# Hello World'
assert is_comment(text, ['//']) == False
from dyc.utils import (
get_leading_whitespace,
read_yaml,
get_indent,
get_extension,
is_comment,
)
class TestGetLeadingWhitespace:
def test_tabs(self):
"""Test tabs functionality"""
text = '\t\tHello'
expected = '\t\t'
got = get_leading_whitespace(text)
assert expected == got
def test_whitespace(self):
"""Test whitespace functionality"""
space = ' '
text = '{space}Such a long whitespace'.format(space=space)
expected = space
got = get_leading_whitespace(text)
assert expected == got
class TestReadYaml:
def test_should_return_none_if_not_found(self):
random_path = '/path/to/non/existing/file.yaml'
expected = None
got = read_yaml(random_path)
assert expected == got
class TestGetIndent:
def test_tabs(self):
assert get_indent('tab') == '\t'
def test_2_spaces(self):
assert get_indent('2 spaces') == ' '
def test_falsy_value(self):
assert get_indent(False) == ''
def test_default_4_spaces(self):
assert get_indent(None) == ' '
class TestGetExtension:
def test_existing_extension_valid(self):
ext = 'file.puk'
expected = 'puk'
got = get_extension(ext)
assert expected == got
def test_non_existing_extension(self):
ext = 'file'
expected = ''
got = get_extension(ext)
assert expected == got
def test_wrong_extension_type(self):
exts = [dict(), False, True, [], 123]
expected = ''
for ext in exts:
got = get_extension(ext)
assert expected == got
class TestIsComment:
def test_valid_comments(self):
"""Testing valid comments"""
text = '# Hello World'
assert is_comment(text, ['#']) == True
def test_invalid_comments(self):
"""Testing invalid comments"""
text = '# Hello World'
assert is_comment(text, ['//']) == False