pySim-shell: fix compatibility problem with cmd2 >= 2.3.0 (bg)

cmd2.fg and cmd2.bg have been deprecated in cmd2 2.3.0 and removed
in cmd2 2.4.0. Let's work around this by a version check.

Related upstream commits:
(See also: https://github.com/python-cmd2/cmd2)

Commit f57b08672af97f9d973148b6c30d74fe4e712d14
Author: Kevin Van Brunt <kmvanbrunt@gmail.com>
Date:   Mon Oct 11 15:20:46 2021 -0400

and

Commit f217861feae45a0a1abb56436e68c5dd859d64c0
Author: Kevin Van Brunt <kmvanbrunt@gmail.com>
Date:   Wed Feb 16 13:34:13 2022 -0500

Change-Id: I9fd32c0fd8f6d40e00a318602af97c288605e8e5
This commit is contained in:
Harald Welte 2023-04-27 17:30:22 +02:00 committed by Philipp Maier
parent c85d4067fd
commit 961b803ec4
1 changed files with 27 additions and 16 deletions

View File

@ -24,7 +24,18 @@ import traceback
import cmd2
from packaging import version
from cmd2 import style, fg
from cmd2 import style
# cmd2 >= 2.3.0 has deprecated the bg/fg in favor of Bg/Fg :(
if version.parse(cmd2.__version__) < version.parse("2.3.0"):
from cmd2 import fg, bg
RED = fg.red
LIGHT_RED = fg.bright_red
LIGHT_GREEN = fg.bright_green
else:
from cmd2 import Fg, Bg # pylint: disable=no-name-in-module
RED = Fg.RED
LIGHT_RED = Fg.LIGHT_RED
LIGHT_GREEN = Fg.LIGHT_GREEN
from cmd2 import CommandSet, with_default_category, with_argparser
import argparse
@ -142,7 +153,7 @@ class PysimApp(cmd2.Cmd):
super().__init__(persistent_history_file='~/.pysim_shell_history', allow_cli_args=False,
auto_load_commands=False, startup_script=script, **kwargs)
self.intro = style('Welcome to pySim-shell!', fg=fg.red)
self.intro = style('Welcome to pySim-shell!', fg=RED)
self.default_category = 'pySim-shell built-in commands'
self.card = None
self.rs = None
@ -302,23 +313,23 @@ class PysimApp(cmd2.Cmd):
sys.stderr = self._stderr_backup
def _show_failure_sign(self):
self.poutput(style(" +-------------+", fg=fg.bright_red))
self.poutput(style(" + ## ## +", fg=fg.bright_red))
self.poutput(style(" + ## ## +", fg=fg.bright_red))
self.poutput(style(" + ### +", fg=fg.bright_red))
self.poutput(style(" + ## ## +", fg=fg.bright_red))
self.poutput(style(" + ## ## +", fg=fg.bright_red))
self.poutput(style(" +-------------+", fg=fg.bright_red))
self.poutput(style(" +-------------+", fg=LIGHT_RED))
self.poutput(style(" + ## ## +", fg=LIGHT_RED))
self.poutput(style(" + ## ## +", fg=LIGHT_RED))
self.poutput(style(" + ### +", fg=LIGHT_RED))
self.poutput(style(" + ## ## +", fg=LIGHT_RED))
self.poutput(style(" + ## ## +", fg=LIGHT_RED))
self.poutput(style(" +-------------+", fg=LIGHT_RED))
self.poutput("")
def _show_success_sign(self):
self.poutput(style(" +-------------+", fg=fg.bright_green))
self.poutput(style(" + ## +", fg=fg.bright_green))
self.poutput(style(" + ## +", fg=fg.bright_green))
self.poutput(style(" + # ## +", fg=fg.bright_green))
self.poutput(style(" + ## # +", fg=fg.bright_green))
self.poutput(style(" + ## +", fg=fg.bright_green))
self.poutput(style(" +-------------+", fg=fg.bright_green))
self.poutput(style(" +-------------+", fg=LIGHT_GREEN))
self.poutput(style(" + ## +", fg=LIGHT_GREEN))
self.poutput(style(" + ## +", fg=LIGHT_GREEN))
self.poutput(style(" + # ## +", fg=LIGHT_GREEN))
self.poutput(style(" + ## # +", fg=LIGHT_GREEN))
self.poutput(style(" + ## +", fg=LIGHT_GREEN))
self.poutput(style(" +-------------+", fg=LIGHT_GREEN))
self.poutput("")
def _process_card(self, first, script_path):