From 4a2f99d795159a7ea4a7db040220b91bcf6883a1 Mon Sep 17 00:00:00 2001 From: Andrew Hamilton Date: Wed, 4 Dec 2019 18:39:22 +1000 Subject: [PATCH] Coding style. - Linting. --- eris/__main__.py | 28 +++++++++++++++------------- eris/fill3.py | 10 ++-------- eris/gut.py | 1 - eris/lscolors.py | 2 +- eris/paged_list.py | 5 +++-- eris/termstr.py | 25 +++++++++---------------- eris/tools.py | 12 +++++++----- eris/tools.toml | 2 +- eris/webserver.py | 2 +- eris/worker.py | 8 ++++---- make-readme.py | 5 +++-- test_distributions.py | 4 ++-- tests/__main___test.py | 6 +++--- tests/fill3_test.py | 3 +-- tests/paged_list_test.py | 4 ++-- tests/tools_test.py | 2 +- 16 files changed, 55 insertions(+), 64 deletions(-) diff --git a/eris/__main__.py b/eris/__main__.py index bbe12e7..d8973c9 100755 --- a/eris/__main__.py +++ b/eris/__main__.py @@ -17,7 +17,6 @@ directory. import asyncio -import collections import contextlib import functools import gzip @@ -317,9 +316,9 @@ class Summary: if sum(stats) != 0: log_filesystem_changed(log, *stats) with self.keep_selection(): - self._column, self._cache, self.result_total, self.completed_total, \ - self._max_width, self._max_path_length, \ - self.closest_placeholder_generator, self._all_results = ( + (self._column, self._cache, self.result_total, + self.completed_total, self._max_width, self._max_path_length, + self.closest_placeholder_generator, self._all_results) = ( new_column, new_cache, result_total, completed_total, max_width, max_path_length, None, all_results) if jobs_added: @@ -649,6 +648,7 @@ class Screen: self._is_fullscreen = False self._make_widgets() self._key_map = make_key_map(Screen._KEY_DATA) + self._last_mouse_position = 0, 0 def __getstate__(self): state = self.__dict__.copy() @@ -876,7 +876,7 @@ class Screen: self._log.log_message([in_green("Opening "), path_colored, in_green("…")]) subprocess.Popen(["xdg-open", path], stdout=subprocess.PIPE, - stderr=subprocess.PIPE) + stderr=subprocess.PIPE) def save(self): worker.Worker.unsaved_jobs_total = 0 @@ -901,12 +901,14 @@ class Screen: def _is_switching_focus(self, x, y, view_width, view_height): return (not self._is_fullscreen and - (self._is_listing_portrait and (x > view_width and - self._is_summary_focused or x <= view_width and - not self._is_summary_focused) or - not self._is_listing_portrait and (y > view_height and - self._is_summary_focused or y <= view_height and - not self._is_summary_focused))) + (self._is_listing_portrait and + (x > view_width and + self._is_summary_focused or x <= view_width and + not self._is_summary_focused) or + not self._is_listing_portrait and + (y > view_height and + self._is_summary_focused or y <= view_height and + not self._is_summary_focused))) def _on_mouse_event(self, event): x, y = event[2:4] @@ -1072,8 +1074,8 @@ def load_state(pickle_path, jobs_added_event, appearance_changed_event, return summary, screen, log, is_first_run -def main(root_path, loop, worker_count=None, editor_command=None, theme=None, compression=None, - is_being_tested=False): +def main(root_path, loop, worker_count=None, editor_command=None, theme=None, + compression=None, is_being_tested=False): if worker_count is None: worker_count = max(multiprocessing.cpu_count() - 1, 1) if theme is None: diff --git a/eris/fill3.py b/eris/fill3.py index 3383e95..d4b8d46 100644 --- a/eris/fill3.py +++ b/eris/fill3.py @@ -20,9 +20,6 @@ import eris.termstr as termstr def appearance_is_valid(appearance): - """An appearance is a list of strings of equal length. - - An empty list is valid. Empty strings are not allowed.""" return (all(isinstance(line, (str, termstr.TermStr)) and len(line) > 0 for line in appearance) and len(set(len(line) for line in appearance)) < 2) @@ -45,9 +42,6 @@ def appearance_dimensions(appearance): def join(seperator, parts): - """Returns a string if all the parts and the seperator are plain strings. - - In other words it returns a TermStr if anything is a TermStr.""" if parts == []: return "" try: @@ -179,8 +173,8 @@ class Filler: class ScrollBar: - _PARTIAL_CHARS = (["█", "▇", "▆", "▅", "▄", "▃", "▂", "▁"] - ,[" ", "▏", "▎", "▍", "▌", "▋", "▊", "▉"]) + _PARTIAL_CHARS = (["█", "▇", "▆", "▅", "▄", "▃", "▂", "▁"], + [" ", "▏", "▎", "▍", "▌", "▋", "▊", "▉"]) DEFAULT_BAR_COLOR = termstr.Color.grey_100 DEFAULT_BACKGROUND_COLOR = termstr.Color.grey_30 diff --git a/eris/gut.py b/eris/gut.py index 7f9e3ab..cd69bb3 100755 --- a/eris/gut.py +++ b/eris/gut.py @@ -9,7 +9,6 @@ This can be useful when initially reading a codebase. """ - import re import sys diff --git a/eris/lscolors.py b/eris/lscolors.py index e0ee820..a645ca9 100644 --- a/eris/lscolors.py +++ b/eris/lscolors.py @@ -60,7 +60,7 @@ def get_color_codes(environment): if "LS_COLORS" in environment: try: return _parse_ls_colors(environment["LS_COLORS"]) - except: + except Exception: syslog.syslog("Syntax error in LS_COLORS environment variable. " "Using default colors.") return _DEFAULT_COLOR_CODES diff --git a/eris/paged_list.py b/eris/paged_list.py index cbc5127..e6429a3 100644 --- a/eris/paged_list.py +++ b/eris/paged_list.py @@ -36,7 +36,7 @@ class PagedList: def __len__(self): return self._len - def _get_page(self, index): # This is cached, see setup_page_cache. + def _get_page_org(self, index): # This is cached, see setup_page_cache. pickle_path = os.path.join(self.pages_dir, str(index)) with self.open_func(pickle_path, "rb") as file_: return pickle.load(file_) @@ -63,7 +63,8 @@ class PagedList: return self._get_page(page_index)[page_offset] def _setup_page_cache(self): - self._get_page = functools.lru_cache(self.cache_size)(self._get_page) + self._get_page = functools.lru_cache(self.cache_size)( + self._get_page_org) def __getstate__(self): # Don't pickle the lru_cache. state = self.__dict__.copy() diff --git a/eris/termstr.py b/eris/termstr.py index ea1560b..a174e08 100644 --- a/eris/termstr.py +++ b/eris/termstr.py @@ -3,9 +3,6 @@ # Licensed under the Artistic License 2.0. -"""Termstr strings contain characters that have color and style.""" - - import collections import functools import html @@ -24,7 +21,6 @@ xterm_colormap = eris.ColorMap.XTermColorMap() @functools.lru_cache() def xterm_color_to_rgb(color_index): - """Return the rgb color of an xterm color.""" return eris.ColorMap._rgb(xterm_colormap.colors[color_index]) @@ -39,7 +35,6 @@ def _cache_first_result(user_function): class Color: - """A list of common colors.""" # https://en.wikipedia.org/wiki/Natural_Color_System black = (0, 0, 0) @@ -61,10 +56,6 @@ class Color: class CharStyle: - """Characters have a foreground and background color, and styles. - - The styles are bold, italic or underlined. - """ _POOL = weakref.WeakValueDictionary() _TERMINAL256_FORMATTER = \ @@ -109,7 +100,7 @@ class CharStyle: return (f"") - def termcode_of_color(self, color, is_foreground): + def _color_code(self, color, is_foreground): if isinstance(color, int): return terminal.color(color, is_foreground) else: # true color @@ -122,13 +113,15 @@ class CharStyle: @_cache_first_result def code_for_term(self): - fg_termcode = terminal.ESC + self.termcode_of_color(self.fg_color, True) - bg_termcode = terminal.ESC + self.termcode_of_color(self.bg_color, False) + fg_termcode = terminal.ESC + self._color_code(self.fg_color, True) + bg_termcode = terminal.ESC + self._color_code(self.bg_color, False) bold_code = (terminal.ESC + terminal.bold) if self.is_bold else "" - italic_code = (terminal.ESC + terminal.italic) if self.is_italic else "" - underline_code = (terminal.ESC + terminal.underline) if self.is_underlined else "" - return "".join([terminal.ESC, terminal.normal, fg_termcode, bg_termcode, - bold_code, italic_code, underline_code]) + italic_code = ((terminal.ESC + terminal.italic) + if self.is_italic else "") + underline_code = ((terminal.ESC + terminal.underline) + if self.is_underlined else "") + return "".join([terminal.ESC, terminal.normal, fg_termcode, + bg_termcode, bold_code, italic_code, underline_code]) def as_html(self): bold_code = "font-weight:bold; " if self.is_bold else "" diff --git a/eris/tools.py b/eris/tools.py index e830a5a..bde4d11 100755 --- a/eris/tools.py +++ b/eris/tools.py @@ -7,7 +7,6 @@ import contextlib import enum import functools -import gzip import importlib import importlib.resources import math @@ -325,7 +324,8 @@ def python_coverage(path): return Status.not_applicable, f'No "{coverage_path}" file.' if os.stat(path).st_mtime > os.stat(coverage_path).st_mtime: return (Status.not_applicable, - f'File has been modified since "{coverage_path}" file was generated.') + f'File has been modified since "{coverage_path}"' + ' file was generated.') path = os.path.normpath(path) with tempfile.TemporaryDirectory() as temp_dir: _do_command([PYTHON_EXECUTABLE, "-m", "coverage", @@ -473,6 +473,7 @@ def make_tool_function(dependencies, command, url=None, success_status=None, return func +elinks, git_blame, git_log = None, None, None # For linters. with importlib.resources.open_text(eris, "tools.toml") as tools_toml_file: tools_toml = toml.load(tools_toml_file) tools_for_extensions = tools_toml["tools_for_extensions"] @@ -556,8 +557,8 @@ class Result: if self.status == Status.pending or self.compression is None: return unknown_label try: - with compression_open_func(self.compression)(self.pickle_path, "rb") \ - as pickle_file: + with compression_open_func(self.compression)( + self.pickle_path, "rb") as pickle_file: return pickle.load(pickle_file) except FileNotFoundError: return unknown_label @@ -614,7 +615,8 @@ class Result: Result.result.fget.evict(self) def as_html(self): - html, styles = termstr.TermStr(STATUS_TO_TERMSTR[self.status]).as_html() + html, styles = termstr.TermStr( + STATUS_TO_TERMSTR[self.status]).as_html() return (f'{html}', styles) diff --git a/eris/tools.toml b/eris/tools.toml index 2921469..26ea578 100644 --- a/eris/tools.toml +++ b/eris/tools.toml @@ -68,7 +68,7 @@ tools_for_extensions = [ [pydocstyle] dependencies = ["pip/pydocstyle"] url = "http://www.pydocstyle.org/en/2.1.1/usage.html" - command = "python3.8 -m pydocstyle --ignore=D1" + command = "python3.8 -m pydocstyle --ignore=D1,D213" [pyflakes] dependencies = ["pip/pyflakes"] diff --git a/eris/webserver.py b/eris/webserver.py index 4668f03..befc98b 100755 --- a/eris/webserver.py +++ b/eris/webserver.py @@ -25,7 +25,7 @@ Example: def make_page(body_html, title): return (f"{title}{body_html}" - ).encode("utf-8") + ).encode("utf-8") class Webserver(http.server.BaseHTTPRequestHandler): diff --git a/eris/worker.py b/eris/worker.py index 257e429..c46c0c7 100755 --- a/eris/worker.py +++ b/eris/worker.py @@ -5,7 +5,6 @@ import asyncio import os -import shutil import signal import eris.fill3 as fill3 @@ -37,12 +36,13 @@ class Worker: os.setpriority(os.PRIO_PGRP, self.child_pgid, 19) async def run_tool(self, path, tool): - self.process.stdin.write(f"{tool.__qualname__}\n{path}\n".encode("utf-8")) + self.process.stdin.write( + f"{tool.__qualname__}\n{path}\n".encode("utf-8")) data = await self.process.stdout.readline() return tools.Status(int(data)) async def job_runner(self, screen, summary, log, jobs_added_event, - appearance_changed_event): + appearance_changed_event): await self.create_process() self.process.stdin.write(f"{self.compression}\n".encode("utf-8")) while True: @@ -108,7 +108,7 @@ def main(): status, text = tools.run_tool_no_error(path, tool) result.result = make_result_widget(text, result, compression) print(status.value, flush=True) - except: + except Exception: tools.log_error() diff --git a/make-readme.py b/make-readme.py index af14c84..bb17f02 100755 --- a/make-readme.py +++ b/make-readme.py @@ -51,8 +51,9 @@ then to run: Extensions({len(extension_set)-1}) | Tools({len(tool_set)}) ----------:| -----""") for extensions, tools_ in all_tools: - print("%s | %s" % (" ".join("." + extension for extension in extensions), - " • ".join(tool_markup(tool) for tool in tools_))) + print("%s | %s" % ( + " ".join("." + extension for extension in extensions), + " • ".join(tool_markup(tool) for tool in tools_))) if __name__ == "__main__": diff --git a/test_distributions.py b/test_distributions.py index 115545d..7230036 100755 --- a/test_distributions.py +++ b/test_distributions.py @@ -32,8 +32,8 @@ def umount_squashfs_iso(mount_point): def run_in_container(container, command): option = "--directory" if os.path.isdir(container) else "--image" - cmd(f"sudo systemd-nspawn --quiet --chdir=/eris --overlay={ERIS_PATH}:/eris " - f'{option}={container} /bin/bash --login -c "{command}"') + cmd(f"sudo systemd-nspawn --quiet --chdir=/eris --overlay={ERIS_PATH}:" + f'/eris {option}={container} /bin/bash --login -c "{command}"') def build_ubuntu(): diff --git a/tests/__main___test.py b/tests/__main___test.py index f50d612..d532617 100755 --- a/tests/__main___test.py +++ b/tests/__main___test.py @@ -53,8 +53,8 @@ class ScreenWidgetTestCase(unittest.TestCase): appearance_changed_event = asyncio.Event() summary = __main__.Summary(self.temp_dir, jobs_added_event) log = __main__.Log(appearance_changed_event) - self.main_widget = __main__.Screen(summary, log, appearance_changed_event, - _MockMainLoop()) + self.main_widget = __main__.Screen( + summary, log, appearance_changed_event, _MockMainLoop()) def tearDown(self): shutil.rmtree(self.temp_dir) @@ -173,7 +173,7 @@ class SummarySyncWithFilesystem(unittest.TestCase): # self.assertFalse(self.jobs_added_event.is_set()) def test_sync_linked_files(self): - """Symbolic and hard-linked files are given distinct entry objects""" + """Symbolic and hard-linked files are given distinct entry objects.""" baz_path = os.path.join(self.temp_dir, "baz") os.symlink(self.foo_path, baz_path) os.link(self.foo_path, self.zoo_path) diff --git a/tests/fill3_test.py b/tests/fill3_test.py index 15844cb..44187b7 100755 --- a/tests/fill3_test.py +++ b/tests/fill3_test.py @@ -7,7 +7,6 @@ import unittest import eris.fill3 as fill3 -import eris.termstr as termstr class WidgetTests(unittest.TestCase): @@ -78,7 +77,7 @@ class WidgetTests(unittest.TestCase): def assert_string2(self, appearance, expected_string): self.assertEqual( ("\n".join(line.data for line in appearance), - "".join("i" if style.fg_color== + "".join("i" if style.fg_color == fill3.ScrollBar.DEFAULT_BACKGROUND_COLOR else " " for line in appearance for style in line.style)), expected_string) diff --git a/tests/paged_list_test.py b/tests/paged_list_test.py index 67a3cb7..ca2c45a 100755 --- a/tests/paged_list_test.py +++ b/tests/paged_list_test.py @@ -16,9 +16,9 @@ class PagedListTestCase(unittest.TestCase): def test_getitem(self): with tempfile.TemporaryDirectory() as temp_dir: list_ = paged_list.PagedList([3, 4, 5, 6], temp_dir, 4, 2) - self.assertEqual(list_[1], 4) + self.assertEqual(list_[1], 4) self.assertEqual(list_[1:3], [4, 5]) - self.assertEqual(list_[0:4], [3, 4, 5, 6]) + self.assertEqual(list_[0:4], [3, 4, 5, 6]) with tempfile.TemporaryDirectory() as temp_dir: list_ = paged_list.PagedList([3, 4, 5, 6], temp_dir, 2, 2) self.assertEqual(list_[1:3], [4, 5]) diff --git a/tests/tools_test.py b/tests/tools_test.py index 7e64816..216753a 100755 --- a/tests/tools_test.py +++ b/tests/tools_test.py @@ -133,7 +133,7 @@ class ToolsTestCase(unittest.TestCase): self._test_tool(tools.perl_syntax, [("perl.pl", tools.Status.ok), # ("perl6.pl", tools.Status.problem) - ]) + ]) def test_perldoc(self): # FIX: This is failing in an Appimage, inside a nspawn container,