[pitivi] utils: Support mixed str/bytes control sequences.



commit d44c33ad2c5d27eb1ad2b9541c812a66d47e691e
Author: Brady J. Garvin <bgarvin cse unl edu>
Date:   Sun Jan 5 13:45:54 2020 -0600

    utils: Support mixed str/bytes control sequences.
    
    It is not safe for `_preformat_levels` to assume that all of the fields in a
    `TerminalController` have the same type; at least in my environment, some of
    these fields are populated with `bytes` while others remain strings.
    
    This change conditionally applies decoding to each control sequence separately
    using a helper function `_as_string`.  As a side-effect, it also eliminates some
    code repetition in `_preformat_levels`.
    
    Closes #2407.

 pitivi/utils/loggable.py                      | 24 ++++++++++++------------
 tests/{test_log.py => test_utils_loggable.py} |  0
 2 files changed, 12 insertions(+), 12 deletions(-)
---
diff --git a/pitivi/utils/loggable.py b/pitivi/utils/loggable.py
index f9606144..2b3eeda5 100644
--- a/pitivi/utils/loggable.py
+++ b/pitivi/utils/loggable.py
@@ -591,22 +591,22 @@ def log_level_name(level):
     return fmt % (_LEVEL_NAMES[level - 1], )
 
 
+def _as_string(string_or_bytes):
+    if isinstance(string_or_bytes, bytes):
+        return string_or_bytes.decode()
+    else:
+        return string_or_bytes
+
+
 def _preformat_levels(enable_color_output):
     terminal_controller = TerminalController()
     for level in ERROR, WARN, FIXME, INFO, DEBUG, LOG:
         if enable_color_output:
-            if isinstance(terminal_controller.BOLD, bytes):
-                formatter = ''.join(
-                    (terminal_controller.BOLD.decode(),
-                     getattr(terminal_controller, COLORS[level]).decode(),
-                     log_level_name(level),
-                     terminal_controller.NORMAL.decode()))
-            else:
-                formatter = ''.join(
-                    (terminal_controller.BOLD,
-                     getattr(terminal_controller, COLORS[level]),
-                     log_level_name(level),
-                     terminal_controller.NORMAL))
+            formatter = ''.join(
+                (_as_string(terminal_controller.BOLD),
+                 _as_string(getattr(terminal_controller, COLORS[level])),
+                 log_level_name(level),
+                 _as_string(terminal_controller.NORMAL)))
         else:
             formatter = log_level_name(level)
         _FORMATTED_LEVELS.append(formatter)
diff --git a/tests/test_log.py b/tests/test_utils_loggable.py
similarity index 100%
rename from tests/test_log.py
rename to tests/test_utils_loggable.py


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