Coding style.

- Simplify thread safe code since no longer usings threads.
- Don't store pickle_path unnecessarily. Save memory.
- Change Row's style to match Column.
This commit is contained in:
Andrew Hamilton 2020-03-29 16:58:25 +10:00
parent a2aaa201c5
commit 4742d152c8
3 changed files with 21 additions and 22 deletions

View file

@ -91,7 +91,7 @@ class Entry:
self.path = path self.path = path
self.summary = summary self.summary = summary
self.highlighted = highlighted self.highlighted = highlighted
self.widgets = results self.results = results
if set_results: if set_results:
# FIX: this is missed for entries appended later # FIX: this is missed for entries appended later
for result in results: for result in results:
@ -104,25 +104,24 @@ class Entry:
return self.path == other.path return self.path == other.path
def __len__(self): def __len__(self):
return len(self.widgets) return len(self.results)
def __getitem__(self, index): def __getitem__(self, index):
return self.widgets[index] return self.results[index]
def appearance_min(self): def appearance_min(self):
appearance = self.appearance_cache if self.appearance_cache is None \
if appearance is None or self.last_width != self.summary._max_width: or self.last_width != self.summary._max_width:
self.last_width = self.summary._max_width self.last_width = self.summary._max_width
if self.highlighted is not None: if self.highlighted is not None:
self.widget[self.highlighted].is_highlighted = True self.results[self.highlighted].is_highlighted = True
new_appearance = self.widget.appearance_min() row_appearance = self.widget.appearance_min()
path = tools.path_colored(self.path) path = tools.path_colored(self.path)
padding = " " * (self.last_width - len(self.widget) + 1) padding = " " * (self.last_width - len(self.results) + 1)
new_appearance[0] = new_appearance[0] + padding + path self.appearance_cache = [row_appearance[0] + padding + path]
self.appearance_cache = appearance = new_appearance
if self.highlighted is not None: if self.highlighted is not None:
self.widget[self.highlighted].is_highlighted = False self.results[self.highlighted].is_highlighted = False
return appearance return self.appearance_cache
def as_html(self): def as_html(self):
html_parts = [] html_parts = []

View file

@ -5,7 +5,6 @@
# Licensed under the Artistic License 2.0. # Licensed under the Artistic License 2.0.
import asyncio import asyncio
import collections
import contextlib import contextlib
import itertools import itertools
import os import os
@ -77,11 +76,10 @@ def appearance_as_html(appearance):
"\n<pre>" + "<br>".join(lines) + "</pre>") "\n<pre>" + "<br>".join(lines) + "</pre>")
class Row(collections.UserList): class Row:
def __init__(self, widgets, widths_func=even_widths): def __init__(self, widgets, widths_func=even_widths):
collections.UserList.__init__(self, widgets) self.widgets = widgets
self.widgets = self.data
self.widths_func = widths_func self.widths_func = widths_func
def appearance(self, dimensions): def appearance(self, dimensions):

View file

@ -545,11 +545,13 @@ class Result:
self.path = path self.path = path
self.tool = tool self.tool = tool
self.compression = None self.compression = None
self.pickle_path = os.path.join(CACHE_PATH, path + "-" + tool.__name__)
self.scroll_position = (0, 0) self.scroll_position = (0, 0)
self.status = Status.pending self.status = Status.pending
self.is_highlighted = False self.is_highlighted = False
def pickle_path(self):
return os.path.join(CACHE_PATH, self.path + "-" + self.tool.__name__)
@property @property
@lru_cache_with_eviction(maxsize=50) @lru_cache_with_eviction(maxsize=50)
def result(self): def result(self):
@ -558,15 +560,15 @@ class Result:
return unknown_label return unknown_label
try: try:
with compression_open_func(self.compression)( with compression_open_func(self.compression)(
self.pickle_path, "rb") as pickle_file: self.pickle_path(), "rb") as pickle_file:
return pickle.load(pickle_file) return pickle.load(pickle_file)
except FileNotFoundError: except FileNotFoundError:
return unknown_label return unknown_label
@result.setter @result.setter
def result(self, value): def result(self, value):
os.makedirs(os.path.dirname(self.pickle_path), exist_ok=True) os.makedirs(os.path.dirname(self.pickle_path()), exist_ok=True)
dump_pickle_safe(value, self.pickle_path, dump_pickle_safe(value, self.pickle_path(),
open=compression_open_func(self.compression)) open=compression_open_func(self.compression))
Result.result.fget.evict(self) Result.result.fget.evict(self)
@ -610,11 +612,11 @@ class Result:
STATUS_TO_TERMSTR[self.status]]) STATUS_TO_TERMSTR[self.status]])
def get_pages_dir(self): def get_pages_dir(self):
return self.pickle_path + ".pages" return self.pickle_path() + ".pages"
def delete(self): def delete(self):
with contextlib.suppress(FileNotFoundError): with contextlib.suppress(FileNotFoundError):
os.remove(self.pickle_path) os.remove(self.pickle_path())
with contextlib.suppress(FileNotFoundError): with contextlib.suppress(FileNotFoundError):
shutil.rmtree(self.get_pages_dir()) shutil.rmtree(self.get_pages_dir())
Result.result.fget.evict(self) Result.result.fget.evict(self)