Skip to content

Commit

Permalink
Add python linter and improve formatting (flake8) (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
J535D165 authored Oct 1, 2020
1 parent 40fd7d7 commit c181aca
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 130 deletions.
22 changes: 18 additions & 4 deletions .github/workflows/ci-workflow.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
name: test-suite
on: [push, pull_request]
jobs:
lint-python:
name: lint-python
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- uses: actions/setup-python@v1
with:
python-version: '3.6'
architecture: 'x64'
- name: Install flake8
run: |
pip install flake8
- name: Lint python with flake8
run: |
flake8 . --max-complexity=10 --statistics
test-master:
name: pytest
name: test-asreview-latest
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand All @@ -17,16 +32,15 @@ jobs:
with:
python-version: '3.6'
architecture: 'x64'
- name: Install packages and run tests
- name: Install packages and run tests
run: |
pip install pytest
pip install --upgrade setuptools>=41.0.0
pip install ./asr-core[all]
pip install ./asr-plot
pytest asr-plot/tests
test-older:
name: pytest
name: test-asreview-0-7-2
runs-on: ubuntu-latest
strategy:
matrix:
Expand Down
4 changes: 2 additions & 2 deletions asreviewcontrib/visualization/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from asreviewcontrib.visualization.plot import Plot
from asreviewcontrib.visualization.entrypoint import PlotEntryPoint
from asreviewcontrib.visualization.plot import Plot # noqa
from asreviewcontrib.visualization.entrypoint import PlotEntryPoint # noqa

from ._version import get_versions
__version__ = get_versions()['version']
Expand Down
116 changes: 73 additions & 43 deletions asreviewcontrib/visualization/_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# This file helps to compute a version number in source trees obtained from
# git-archive tarball (such as those provided by githubs download-from-tag
# feature). Distribution tarballs (built by setup.py sdist) and build
Expand All @@ -7,7 +6,6 @@

# This file is released into the public domain. Generated by
# versioneer-0.18 (https://github.com/warner/python-versioneer)

"""Git implementation of _version.py."""

import errno
Expand Down Expand Up @@ -64,10 +62,15 @@ def decorate(f):
HANDLERS[vcs] = {}
HANDLERS[vcs][method] = f
return f

return decorate


def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False,
def run_command(commands,
args,
cwd=None,
verbose=False,
hide_stderr=False,
env=None):
"""Call the given command(s)."""
assert isinstance(commands, list)
Expand All @@ -76,10 +79,12 @@ def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False,
try:
dispcmd = str([c] + args)
# remember shell=False, so use git.cmd on windows, not just git
p = subprocess.Popen([c] + args, cwd=cwd, env=env,
stdout=subprocess.PIPE,
stderr=(subprocess.PIPE if hide_stderr
else None))
p = subprocess.Popen(
[c] + args,
cwd=cwd,
env=env,
stdout=subprocess.PIPE,
stderr=(subprocess.PIPE if hide_stderr else None))
break
except EnvironmentError:
e = sys.exc_info()[1]
Expand All @@ -91,7 +96,7 @@ def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False,
return None, None
else:
if verbose:
print("unable to find command, tried %s" % (commands,))
print("unable to find command, tried %s" % (commands, ))
return None, None
stdout = p.communicate()[0].strip()
if sys.version_info[0] >= 3:
Expand All @@ -116,9 +121,13 @@ def versions_from_parentdir(parentdir_prefix, root, verbose):
for i in range(3):
dirname = os.path.basename(root)
if dirname.startswith(parentdir_prefix):
return {"version": dirname[len(parentdir_prefix):],
"full-revisionid": None,
"dirty": False, "error": None, "date": None}
return {
"version": dirname[len(parentdir_prefix):],
"full-revisionid": None,
"dirty": False,
"error": None,
"date": None
}
else:
rootdirs.append(root)
root = os.path.dirname(root) # up a level
Expand Down Expand Up @@ -201,16 +210,23 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose):
r = ref[len(tag_prefix):]
if verbose:
print("picking %s" % r)
return {"version": r,
"full-revisionid": keywords["full"].strip(),
"dirty": False, "error": None,
"date": date}
return {
"version": r,
"full-revisionid": keywords["full"].strip(),
"dirty": False,
"error": None,
"date": date
}
# no suitable tags, so version is "0+unknown", but full hex is still there
if verbose:
print("no suitable tags, using unknown + full revision id")
return {"version": "0+unknown",
"full-revisionid": keywords["full"].strip(),
"dirty": False, "error": "no suitable tags", "date": None}
return {
"version": "0+unknown",
"full-revisionid": keywords["full"].strip(),
"dirty": False,
"error": "no suitable tags",
"date": None
}


@register_vcs_handler("git", "pieces_from_vcs")
Expand All @@ -225,7 +241,8 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):
if sys.platform == "win32":
GITS = ["git.cmd", "git.exe"]

out, rc = run_command(GITS, ["rev-parse", "--git-dir"], cwd=root,
out, rc = run_command(GITS, ["rev-parse", "--git-dir"],
cwd=root,
hide_stderr=True)
if rc != 0:
if verbose:
Expand All @@ -234,9 +251,10 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):

# if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty]
# if there isn't one, this yields HEX[-dirty] (no NUM)
describe_out, rc = run_command(GITS, ["describe", "--tags", "--dirty",
"--always", "--long",
"--match", "%s*" % tag_prefix],
describe_out, rc = run_command(GITS, [
"describe", "--tags", "--dirty", "--always", "--long", "--match",
"%s*" % tag_prefix
],
cwd=root)
# --long was added in git-1.5.5
if describe_out is None:
Expand Down Expand Up @@ -269,8 +287,8 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):
mo = re.search(r'^(.+)-(\d+)-g([0-9a-f]+)$', git_describe)
if not mo:
# unparseable. Maybe git-describe is misbehaving?
pieces["error"] = ("unable to parse git-describe output: '%s'"
% describe_out)
pieces["error"] = ("unable to parse git-describe output: '%s'" %
describe_out)
return pieces

# tag
Expand All @@ -279,8 +297,8 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):
if verbose:
fmt = "tag '%s' doesn't start with prefix '%s'"
print(fmt % (full_tag, tag_prefix))
pieces["error"] = ("tag '%s' doesn't start with prefix '%s'"
% (full_tag, tag_prefix))
pieces["error"] = ("tag '%s' doesn't start with prefix '%s'" %
(full_tag, tag_prefix))
return pieces
pieces["closest-tag"] = full_tag[len(tag_prefix):]

Expand Down Expand Up @@ -330,8 +348,7 @@ def render_pep440(pieces):
rendered += ".dirty"
else:
# exception #1
rendered = "0+untagged.%d.g%s" % (pieces["distance"],
pieces["short"])
rendered = "0+untagged.%d.g%s" % (pieces["distance"], pieces["short"])
if pieces["dirty"]:
rendered += ".dirty"
return rendered
Expand Down Expand Up @@ -445,11 +462,13 @@ def render_git_describe_long(pieces):
def render(pieces, style):
"""Render the given version pieces into the requested style."""
if pieces["error"]:
return {"version": "unknown",
"full-revisionid": pieces.get("long"),
"dirty": None,
"error": pieces["error"],
"date": None}
return {
"version": "unknown",
"full-revisionid": pieces.get("long"),
"dirty": None,
"error": pieces["error"],
"date": None
}

if not style or style == "default":
style = "pep440" # the default
Expand All @@ -469,9 +488,13 @@ def render(pieces, style):
else:
raise ValueError("unknown style '%s'" % style)

return {"version": rendered, "full-revisionid": pieces["long"],
"dirty": pieces["dirty"], "error": None,
"date": pieces.get("date")}
return {
"version": rendered,
"full-revisionid": pieces["long"],
"dirty": pieces["dirty"],
"error": None,
"date": pieces.get("date")
}


def get_versions():
Expand All @@ -498,10 +521,13 @@ def get_versions():
for i in cfg.versionfile_source.split('/'):
root = os.path.dirname(root)
except NameError:
return {"version": "0+unknown", "full-revisionid": None,
"dirty": None,
"error": "unable to find root of source tree",
"date": None}
return {
"version": "0+unknown",
"full-revisionid": None,
"dirty": None,
"error": "unable to find root of source tree",
"date": None
}

try:
pieces = git_pieces_from_vcs(cfg.tag_prefix, root, verbose)
Expand All @@ -515,6 +541,10 @@ def get_versions():
except NotThisMethod:
pass

return {"version": "0+unknown", "full-revisionid": None,
"dirty": None,
"error": "unable to compute version", "date": None}
return {
"version": "0+unknown",
"full-revisionid": None,
"dirty": None,
"error": "unable to compute version",
"date": None
}
Loading

0 comments on commit c181aca

Please sign in to comment.