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

dist/tools/headerguards: fix flake8 issues #8306

Merged
merged 1 commit into from Jan 3, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 14 additions & 9 deletions dist/tools/headerguards/headerguards.py
@@ -1,39 +1,42 @@
#!/usr/bin/env python3

import os, sys
import os
import sys
import difflib
#from string import maketrans
from io import BytesIO, TextIOWrapper

_in = "/-."
_out = "___"

transtab = str.maketrans(_in, _out)


def path_to_guardname(filepath):
res = filepath.upper().translate(transtab)
if res.startswith("_"):
res = "PRIV" + res
return res


def get_guard_name(filepath):
parts = filepath.split(os.sep)
start = 0
found = False
for i, part in enumerate(parts):
if part == "include":
found = True
start = i+1
start = i + 1
break

if not found:
start = len(parts) -1
start = len(parts) - 1

return path_to_guardname(os.path.join(*parts[start:]))


def fix_headerguard(filename):
supposed = get_guard_name(filename)
with open(filename, "r",encoding='utf-8', errors='ignore') as f:
with open(filename, "r", encoding='utf-8', errors='ignore') as f:
inlines = f.readlines()

tmp = TextIOWrapper(BytesIO(), encoding="utf-8", errors="ignore")
Expand All @@ -42,7 +45,7 @@ def fix_headerguard(filename):
guard_found = 0
guard_name = ""
ifstack = 0
for n, line in enumerate(inlines):
for line in inlines:
if guard_found == 0:
if line.startswith("#ifndef"):
guard_found += 1
Expand All @@ -68,16 +71,18 @@ def fix_headerguard(filename):

tmp.seek(0)
if guard_found == 3:
for line in difflib.unified_diff(inlines, tmp.readlines(), "%s" % filename, "%s" % filename):
for line in difflib.unified_diff(inlines, tmp.readlines(),
"%s" % filename, "%s" % filename):
sys.stdout.write(line)
else:
print("%s: no / broken header guard" % filename, file=sys.stderr)
return False

if __name__=="__main__":

if __name__ == "__main__":
error = False
for filename in sys.argv[1:]:
if fix_headerguard(filename) == False:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this changes semantics

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed, so fix_headerguard should return True at the end ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix_headerguard() returns "False" on error. The call site checks for that. Why change?

Copy link
Contributor Author

@aabadie aabadie Dec 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change?

flake8 --config=dist/tools/flake8/flake8.cfg dist/tools/headerguards/
...
dist/tools/headerguards/headerguards.py:80:38: E712 comparison to False should be 'if cond is False:' or 'if not cond:'

I chose the second solution, because it's cleaner: fix_headerguard() should explicitly return True when problems are detected, False otherwise.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then why not if fix_headerguard() is False:?

it's cleaner: fix_headerguard() should explicitly return True when problems are detected, False otherwise.

debatable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in general explicit is better than implicit. With the first suggestion, the function returns None when problems are detected, False otherwise. Testing between False and None could lead to confusion and buggy code. That's why I chose the other proposition.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code was explicit before you broke it. And why returning True on error leads to less confusion than returning False is something I don't think you'll find in any style guide.

The function could return True when there's no error. Would that improve readability? I doubt it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not spend too much time on this and move forward. I've just updated the PR with your preference.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its basically a function that would benefit from being split in parts and the error case done with an exception, but its another PR.

if fix_headerguard(filename) is False:
error = True

if error:
Expand Down