commit 752c4640182b4898c8f0afd5e73d0810f20e5171 Author: Damian Johnson <ata...@torproject.org> Date: Tue Sep 1 09:10:11 2015 -0700
Drop ConnectionPanelLine parent class I wrote ConnectionPanelLine long before I knew of @lru_cache, and really the only functionality it provides is caching. Wrapping that into the child class. --- nyx/connections/circ_entry.py | 12 +++++- nyx/connections/conn_entry.py | 89 +++++++++-------------------------------- 2 files changed, 29 insertions(+), 72 deletions(-) diff --git a/nyx/connections/circ_entry.py b/nyx/connections/circ_entry.py index 7cf087f..84e5b2b 100644 --- a/nyx/connections/circ_entry.py +++ b/nyx/connections/circ_entry.py @@ -19,6 +19,12 @@ from nyx.connections import conn_entry from stem.util import str_tools +try: + # added in python 3.2 + from functools import lru_cache +except ImportError: + from stem.util.lru_cache import lru_cache + def to_unix_time(dt): return (dt - datetime.datetime(1970, 1, 1)).total_seconds() @@ -68,6 +74,7 @@ class CircHeaderLine(conn_entry.ConnectionLine): return '' + @lru_cache() def get_details(self, width): if not self.is_built: detail_format = (curses.A_BOLD, nyx.connection_panel.CATEGORY_COLOR[self._entry.get_type()]) @@ -125,9 +132,10 @@ class CircLine(conn_entry.ConnectionLine): listing_type - primary attribute we're listing connections by """ - return conn_entry.ConnectionPanelLine.get_listing_entry(self, width, current_time, listing_type) + return self._get_listing_entry(width, listing_type) - def _get_listing_entry(self, width, current_time, listing_type): + @lru_cache() + def _get_listing_entry(self, width, listing_type): line_format = nyx.util.ui_tools.get_color(nyx.connection_panel.CATEGORY_COLOR[self._entry.get_type()]) # The required widths are the sum of the following: diff --git a/nyx/connections/conn_entry.py b/nyx/connections/conn_entry.py index 366ecba..34e34d9 100644 --- a/nyx/connections/conn_entry.py +++ b/nyx/connections/conn_entry.py @@ -14,6 +14,12 @@ from nyx.connection_panel import Category from stem.util import conf, connection, str_tools +try: + # added in python 3.2 + from functools import lru_cache +except ImportError: + from stem.util.lru_cache import lru_cache + # static data for listing format # <src> --> <dst> <etc><padding> @@ -31,78 +37,12 @@ CONFIG = conf.config_dict('nyx', { }) -class ConnectionPanelLine: - """ - Individual line in the connection panel listing. - """ - - def __init__(self): - # cache for displayed information - self._listing_cache = None - self._listing_cache_args = (None, None) - - self._details_cache = None - self._details_cache_args = None - - self._descriptor_cache = None - self._descriptor_cache_args = None - - def get_listing_prefix(self): - """ - Provides a list of characters to be appended before the listing entry. - """ - - return () - - def get_listing_entry(self, width, current_time, listing_type): - """ - Provides a [(msg, attr)...] tuple list for contents to be displayed in the - connection panel listing. - - Arguments: - width - available space to display in - current_time - unix timestamp for what the results should consider to be - the current time (this may be ignored due to caching) - """ - - if self._listing_cache_args != (width, listing_type): - self._listing_cache = self._get_listing_entry(width, current_time, listing_type) - self._listing_cache_args = (width, listing_type) - - return self._listing_cache - - def _get_listing_entry(self, width, current_time, listing_type): - # implementation of get_listing_entry - return None - - def get_details(self, width): - """ - Provides a list of [(msg, attr)...] tuple listings with detailed - information for this connection. - - Arguments: - width - available space to display in - """ - - if self._details_cache_args != width: - self._details_cache = self._get_details(width) - self._details_cache_args = width - - return self._details_cache - - def _get_details(self, width): - # implementation of get_details - return [] - - -class ConnectionLine(ConnectionPanelLine): +class ConnectionLine(object): """ Display component of the ConnectionEntry. """ def __init__(self, entry, conn, include_port=True, include_expanded_addresses=True): - ConnectionPanelLine.__init__(self) - self._entry = entry self.connection = conn @@ -112,6 +52,13 @@ class ConnectionLine(ConnectionPanelLine): self.include_port = include_port self.include_expanded_addresses = include_expanded_addresses + def get_listing_prefix(self): + """ + Provides a list of characters to be appended before the listing entry. + """ + + return () + def get_locale(self, default = None): """ Provides the two letter country code for the remote endpoint. @@ -168,7 +115,7 @@ class ConnectionLine(ConnectionPanelLine): # fetch our (most likely cached) display entry for the listing - my_listing = ConnectionPanelLine.get_listing_entry(self, width, current_time, listing_type) + my_listing = self._get_listing_entry(width, listing_type) # fill in the current uptime and return the results @@ -182,7 +129,8 @@ class ConnectionLine(ConnectionPanelLine): return my_listing - def _get_listing_entry(self, width, current_time, listing_type): + @lru_cache() + def _get_listing_entry(self, width, listing_type): entry_type = self._entry.get_type() # Lines are split into the following components in reverse: @@ -205,7 +153,8 @@ class ConnectionLine(ConnectionPanelLine): return draw_entry - def _get_details(self, width): + @lru_cache() + def get_details(self, width): """ Provides details on the connection, correlated against available consensus data. _______________________________________________ tor-commits mailing list tor-commits@lists.torproject.org https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits