Skip to content

Commit

Permalink
Check for ENOENT before file extension, add warnings about \t\n
Browse files Browse the repository at this point in the history
Resolves #31
Resolves #30
  • Loading branch information
cmlsharp committed Oct 3, 2017
1 parent efdc350 commit a0eaf85
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
3 changes: 2 additions & 1 deletion style50/languages.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class C(StyleCheck):
"--pad-header", "--pad-oper", "--max-code-length=100",
"--convert-tabs", "--indent=spaces=4",
"--indent-continuation=1", "--indent-switches",
"--min-conditional-indent=1", "--style=allman"
"--lineend=linux", "--min-conditional-indent=1",
"--style=allman"
]

# Match (1) /**/ comments, and (2) // comments.
Expand Down
39 changes: 24 additions & 15 deletions style50/style50.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def __init__(self, paths, output="character"):
elif output == "json":
self.run = self.run_json
else:
self._warn_chars = set()
self.run = self.run_diff
# Set diff function as needed
if output == "character":
Expand Down Expand Up @@ -110,12 +111,19 @@ def run_diff(self):
# Display results
if results.diffs:
print(*self.diff(results.original, results.styled), sep="\n")
if results.comment_ratio < results.COMMENT_MIN:
termcolor.cprint("And consider adding more comments!", "yellow")
conjunction = "And"
else:
termcolor.cprint("Looks good!", "green")

This comment has been minimized.

Copy link
@dmalan

dmalan Oct 4, 2017

Member

Don't think you meant to remove this, @crossroads1112!

if results.comment_ratio < results.COMMENT_MIN:
termcolor.cprint("But consider adding more comments!", "yellow")
conjunction = "But"

if results.comment_ratio < results.COMMENT_MIN:
termcolor.cprint("{} consider adding more comments!".format(conjunction), "yellow")

for type, _, c in sorted(self._warn_chars):
color, verb = ("on_green", "inserted") if type == "+" else ("on_red", "removed")
termcolor.cprint("* ", "cyan", end="")
termcolor.cprint(repr(c)[1:-1], None, color, end="")
termcolor.cprint(" means that a {} character should be {}".format(
"newline" if c == "\n" else "tab", verb), "cyan")

def run_json(self):
"""
Expand Down Expand Up @@ -166,18 +174,16 @@ def _check(self, file):
Run apropriate check based on `file`'s extension and return it,
otherwise raise an Error
"""

if not os.path.exists(file):
raise Error("file \"{}\" not found".format(file))

_, extension = os.path.splitext(file)
try:
check = self.extension_map[extension[1:]]

with open(file) as f:
code = f.read()

except (OSError, IOError) as e:
if e.errno == errno.ENOENT:
raise Error("file \"{}\" not found".format(file))
else:
raise
except KeyError:
raise Error("unknown file type \"{}\", skipping...".format(file))
else:
Expand Down Expand Up @@ -229,8 +235,7 @@ def color_transition(old_type, new_type):

return self._char_diff(old, new, color_transition)

@staticmethod
def _char_diff(old, new, transition, fmt=lambda c: c):
def _char_diff(self, old, new, transition, fmt=lambda c: c):
"""
Returns a char-based diff between `old` and `new` where each character
is formatted by `fmt` and transitions between blocks are determined by `transition`.
Expand All @@ -255,13 +260,17 @@ def _char_diff(old, new, transition, fmt=lambda c: c):

if d[2] == "\n":
if dtype != " ":
self._warn_chars.add(d)
# Show added/removed newlines.
line += [fmt(r"\n"), transition(dtype, " ")]
yield "".join(line)
line = [transition(" ", dtype)]
else:
elif dtype != " " and d[2] == "\t":
# Show added/removed tabs.
line.append(fmt(d[2] if dtype == " " else d[2].replace("\t", r"\t")))
line.append(fmt("\\t"))
self._warn_chars.add(d)
else:
line.append(fmt(d[2]))

# Flush buffer before quitting.
last = "".join(line)
Expand Down

0 comments on commit a0eaf85

Please sign in to comment.