[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [arm/master] Moving in the join() util



commit 1a30fd663f49c9464c98d64a285015b977eda7ea
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Sat Nov 22 14:35:14 2014 -0800

    Moving in the join() util
    
    Presently only used by the graph panel so I'm really itching to kill it
    entirely, but guess I'll let it live a little longer to see if keeping it
    around is useful.
---
 arm/graph_panel.py   |    8 ++++----
 arm/util/__init__.py |   38 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+), 4 deletions(-)

diff --git a/arm/graph_panel.py b/arm/graph_panel.py
index dca6ed6..7779945 100644
--- a/arm/graph_panel.py
+++ b/arm/graph_panel.py
@@ -19,7 +19,7 @@ import arm.controller
 import arm.popups
 import arm.util.tracker
 
-from arm.util import bandwidth_from_state, msg, panel, tor_controller
+from arm.util import bandwidth_from_state, join, msg, panel, tor_controller
 
 from stem.control import EventType, Listener
 from stem.util import conf, enum, log, str_tools, system
@@ -549,19 +549,19 @@ class GraphPanel(panel.Panel):
 
     if self.is_title_visible():
       title = CONFIG['attr.graph.title'].get(self.displayed_stat, '')
-      title_stats = str_tools.join(param.title_stats, ', ', width - len(title) - 4)
+      title_stats = join(param.title_stats, ', ', width - len(title) - 4)
       title = '%s (%s):' % (title, title_stats) if title_stats else '%s:' % title
       self.addstr(0, 0, title, curses.A_STANDOUT)
 
     # top labels
 
     primary_header = CONFIG['attr.graph.header.primary'].get(self.displayed_stat, '')
-    primary_header_stats = str_tools.join(param.primary_header_stats, '', (width / 2) - len(primary_header) - 4)
+    primary_header_stats = join(param.primary_header_stats, '', (width / 2) - len(primary_header) - 4)
     left = '%s (%s):' % (primary_header, primary_header_stats) if primary_header_stats else '%s:' % primary_header
     self.addstr(1, 0, left, curses.A_BOLD, PRIMARY_COLOR)
 
     secondary_header = CONFIG['attr.graph.header.secondary'].get(self.displayed_stat, '')
-    secondary_header_stats = str_tools.join(param.secondary_header_stats, '', (width / 2) - len(secondary_header) - 4)
+    secondary_header_stats = join(param.secondary_header_stats, '', (width / 2) - len(secondary_header) - 4)
     right = '%s (%s):' % (secondary_header, secondary_header_stats) if secondary_header_stats else '%s:' % secondary_header
     self.addstr(1, graph_column + 5, right, curses.A_BOLD, SECONDARY_COLOR)
 
diff --git a/arm/util/__init__.py b/arm/util/__init__.py
index 9546642..9a62617 100644
--- a/arm/util/__init__.py
+++ b/arm/util/__init__.py
@@ -64,6 +64,44 @@ def init_controller(*args, **kwargs):
   return TOR_CONTROLLER
 
 
+def join(entries, joiner = ' ', size = None):
+  """
+  Joins a series of strings similar to str.join(), but only up to a given size.
+  This returns an empty string if none of the entries will fit. For example...
+
+    >>> join(['This', 'is', 'a', 'looooong', 'message'], size = 18)
+    'This is a looooong'
+
+    >>> join(['This', 'is', 'a', 'looooong', 'message'], size = 17)
+    'This is a'
+
+    >>> join(['This', 'is', 'a', 'looooong', 'message'], size = 2)
+    ''
+
+  :param list entries: strings to be joined
+  :param str joiner: strings to join the entries with
+  :param int size: maximum length the result can be, there's no length
+    limitation if **None**
+
+  :returns: **str** of the joined entries up to the given length
+  """
+
+  if size is None:
+    return joiner.join(entries)
+
+  result = ''
+
+  for entry in entries:
+    new_result = joiner.join((result, entry)) if result else entry
+
+    if len(new_result) > size:
+      break
+    else:
+      result = new_result
+
+  return result
+
+
 @uses_settings
 def msg(message, config, **attr):
   """

_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits