fixeria has uploaded this change for review. ( 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(-)
git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/92/42892/1
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..12c383f 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 + 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",