laforge has submitted this change. ( 
https://gerrit.osmocom.org/c/pysim/+/42892?usp=email )

Change subject: pySim.log, pySim-shell: fix compatibility with cmd2 >= 3.0.0
......................................................................

pySim.log, pySim-shell: fix compatibility with cmd2 >= 3.0.0

Some Linux distributions (e.g. Arch Linux) already ship cmd2 3.x.x,
which removed the style()/Fg/Bg API in favor of stylize()/Color.

Add a version guard to select the right API at runtime.
Adjust the upper bound cap in requirements.txt and setup.py.

Change-Id: Ibf2ac7847933296fb06665c87f53ed6e1f315d27
---
M pySim-shell.py
M pySim/log.py
M requirements.txt
M setup.py
4 files changed, 27 insertions(+), 10 deletions(-)

Approvals:
  Jenkins Builder: Verified
  laforge: Looks good to me, approved
  osmith: Looks good to me, but someone else must approve




diff --git a/pySim-shell.py b/pySim-shell.py
index 2639009..d31cf6a 100755
--- a/pySim-shell.py
+++ b/pySim-shell.py
@@ -24,16 +24,25 @@
 import re
 import cmd2
 from packaging import version
-from cmd2 import style, Fg, Bg
-
 import logging
 from pySim.log import PySimLogger
 from osmocom.utils import auto_uint8

-RED = Fg.RED
-YELLOW = Fg.YELLOW
-LIGHT_RED = Fg.LIGHT_RED
-LIGHT_GREEN = Fg.LIGHT_GREEN
+# cmd2 >= 3.0 replaced Fg + style() with Color + stylize()
+if version.parse(cmd2.__version__) >= version.parse("3.0.0"):
+    from cmd2 import Color, stylize # pylint: disable=no-name-in-module
+    RED = Color.RED
+    YELLOW = Color.YELLOW
+    LIGHT_RED = Color.BRIGHT_RED
+    LIGHT_GREEN = Color.BRIGHT_GREEN
+    def style(text, fg=None, bg=None, bold=False): # pylint: 
disable=function-redefined
+        return stylize(text, fg) if fg else text
+else:
+    from cmd2 import style, Fg # pylint: disable=no-name-in-module
+    RED = Fg.RED
+    YELLOW = Fg.YELLOW
+    LIGHT_RED = Fg.LIGHT_RED
+    LIGHT_GREEN = Fg.LIGHT_GREEN
 from cmd2 import CommandSet, with_default_category, with_argparser
 import argparse

diff --git a/pySim/log.py b/pySim/log.py
index 345cf78..1204c4f 100644
--- a/pySim/log.py
+++ b/pySim/log.py
@@ -24,7 +24,15 @@
 #

 import logging
-from cmd2 import style
+import cmd2
+from packaging import version
+
+if version.parse(cmd2.__version__) >= version.parse("3.0.0"):
+    from cmd2 import stylize as _stylize # pylint: disable=no-name-in-module
+    def _style(text, fg=None): # pylint: disable=function-redefined
+        return _stylize(text, fg) if fg else text
+else: # cmd2>=2.6.2
+    from cmd2 import style as _style # type: ignore[no-redef]

 class _PySimLogHandler(logging.Handler):
     def __init__(self, log_callback):
@@ -121,7 +129,7 @@
                 if isinstance(color, str):
                     PySimLogger.print_callback(color + formatted_message + 
"\033[0m")
                 else:
-                    PySimLogger.print_callback(style(formatted_message, fg = 
color))
+                    PySimLogger.print_callback(_style(formatted_message, fg = 
color))
             else:
                 PySimLogger.print_callback(formatted_message)

diff --git a/requirements.txt b/requirements.txt
index 9088f16..aafccc2 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,7 +1,7 @@
 pyscard
 pyserial
 pytlv
-cmd2>=2.6.2,<3.0
+cmd2>=2.6.2,<4.0
 jsonpath-ng
 construct>=2.10.70
 bidict
diff --git a/setup.py b/setup.py
index d640596..ff093fb 100644
--- a/setup.py
+++ b/setup.py
@@ -21,7 +21,7 @@
         "pyscard",
         "pyserial",
         "pytlv",
-        "cmd2 >= 2.6.2, < 3.0",
+        "cmd2 >= 2.6.2, < 4.0",
         "jsonpath-ng",
         "construct >= 2.10.70",
         "bidict",

--
To view, visit https://gerrit.osmocom.org/c/pysim/+/42892?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings?usp=email

Gerrit-MessageType: merged
Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: Ibf2ac7847933296fb06665c87f53ed6e1f315d27
Gerrit-Change-Number: 42892
Gerrit-PatchSet: 2
Gerrit-Owner: fixeria <[email protected]>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: dexter <[email protected]>
Gerrit-Reviewer: laforge <[email protected]>
Gerrit-Reviewer: osmith <[email protected]>

Reply via email to