Skip to content

Commit

Permalink
Add fallback functionality for Colorama and PrettyTable
Browse files Browse the repository at this point in the history
Resolves quattor#218.
  • Loading branch information
jrha committed Nov 29, 2018
1 parent 3a2f8ae commit f01bd7f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
17 changes: 15 additions & 2 deletions panc/src/main/scripts/panlint/panlint.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,25 @@
from glob import glob
from sys import stdout, exit as sys_exit
from inspect import getmembers, isclass, isfunction
from prettytable import PrettyTable
from colorama import Fore, Style, init as colorama_init

import panlint_formatters
from panlint_formatters import SEVERITY_TEXT, SEVERITY_VALUE

# Import optional modules or fall back to basic functionality
# Disable pylint import order checks from here onwards
# pylint: disable=C0411

try:
from colorama import Fore, Style, init as colorama_init
except ImportError as e:
from panlint_fallback import Fore, Style, init as colorama_init

try:
from prettytable import PrettyTable
except ImportError as e:
from panlint_fallback import PrettyTable


class Message(object):
def __init__(self, message_id, severity, text):
self.id = str(message_id)
Expand Down
39 changes: 39 additions & 0 deletions panc/src/main/scripts/panlint/panlint_fallback.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
""" Panlint fallback functionality for systems without Colorama and PrettyTable """

# Disable pylint too-few methods check as we are providing stub classes rather than useful functionality
# pylint: disable=R0903

class Fore(object):
BLUE = ''
CYAN = ''
GREEN = ''
RED = ''
RESET = ''
YELLOW = ''


class Style(object):
BRIGHT = ''
DIM = ''
RESET_ALL = ''


def init(**_):
pass


class PrettyTable(object):
def __init__(self, cols):
cols = [str(c) for c in cols]
self.rows = ['\t'.join(cols)]
self.align = dict(zip(cols, cols))

def __str__(self):
return '\n'.join(self.rows)

def add_row(self, cols):
cols = [str(c) for c in cols]
self.rows.append('\t'.join(cols))

def sortby(self, _):
pass
6 changes: 5 additions & 1 deletion panc/src/main/scripts/panlint/panlint_formatters.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
""" Panlint Output Formatters """

from sys import stdout
from colorama import Fore, Style

# Import optional modules or fall back to basic functionality
try:
from colorama import Fore, Style
except ImportError as e:
from panlint_fallback import Fore, Style

TAB_ARROW = u'\u2192'

Expand Down

0 comments on commit f01bd7f

Please sign in to comment.