From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 111BAA0508; Wed, 30 Mar 2022 09:14:15 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id E1C73406B4; Wed, 30 Mar 2022 09:14:14 +0200 (CEST) Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by mails.dpdk.org (Postfix) with ESMTP id 2552840696 for ; Wed, 30 Mar 2022 09:14:12 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1648624453; x=1680160453; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=jz9Z9J1R5zuw51qBiw/m/oIXaHJqtelYfowTdXaumBg=; b=dhDAQZKzA8ntc9kRbt/JGm8CprjdPTn6gYaVafcoxYiOMBiBtw8Q/241 NfWNEgQdR+w0GJo/4v7vNBEajzHhTgjOfhj9cevSUoorq3xVr9Mzxggob g8UIipJb1GSTsDh0DWb3ul8H6/7UZecrivSf8UC/NOhQUNm33SMo/4Y8h Zs6+jM0FaAcu1PveS08DmlIvrum07Fvy1OFBj8qM0wCaVDrqWOqgNUXBM +QnPFR1+J6kKZrm34iu83qcbfXhqXTcBXyo7y/6Pu9YMICG4tL4yl0Ftx EXZUUi5dD6Jbj54Ng7QZz9wZX+Nk8mPHVYfGNIo8ntHqLJvo4f9A4cR6j g==; X-IronPort-AV: E=McAfee;i="6200,9189,10301"; a="241626969" X-IronPort-AV: E=Sophos;i="5.90,222,1643702400"; d="scan'208";a="241626969" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Mar 2022 00:14:00 -0700 X-IronPort-AV: E=Sophos;i="5.90,222,1643702400"; d="scan'208";a="554556671" Received: from shwdenpg197.ccr.corp.intel.com ([10.253.109.70]) by fmsmga007-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Mar 2022 00:13:53 -0700 From: Jun Dong To: dts@dpdk.org Cc: lijuan.tu@intel.com, qingx.sun@intel.com, junx.dong@intel.com Subject: [V1] framework/*: Replace framework texttable with third party libary Date: Wed, 30 Mar 2022 15:13:47 +0800 Message-Id: <20220330071347.1391-1-junx.dong@intel.com> X-Mailer: git-send-email 2.33.1.windows.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: dts@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: test suite reviews and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dts-bounces@dpdk.org Signed-off-by: Jun Dong --- framework/dts.py | 1 - framework/test_result.py | 5 +- framework/texttable.py | 645 --------------------------------------- requirements.txt | 3 +- tests/perf_test_base.py | 5 +- 5 files changed, 8 insertions(+), 651 deletions(-) delete mode 100644 framework/texttable.py diff --git a/framework/dts.py b/framework/dts.py index 1ffb1c23..c06bcedf 100644 --- a/framework/dts.py +++ b/framework/dts.py @@ -47,7 +47,6 @@ import framework.debugger as debugger import framework.logger as logger import framework.rst as rst # rst file support import framework.settings as settings # dts settings -import framework.texttable as texttable # text format from framework.asan_test import ASanTestProcess from .checkCase import CheckCase diff --git a/framework/test_result.py b/framework/test_result.py index 0231e769..abf8edb9 100644 --- a/framework/test_result.py +++ b/framework/test_result.py @@ -32,7 +32,6 @@ """ Generic result container and reporters """ -import framework.texttable as texttable # text format class Result(object): @@ -445,9 +444,11 @@ class ResultTable(object): rt.add_row(row) rt.table_print() """ + from texttable import Texttable + self.results_table_rows = [] self.results_table_rows.append([]) - self.table = texttable.Texttable(max_width=150) + self.table = Texttable(max_width=150) self.results_table_header = header self.logger = None self.rst = None diff --git a/framework/texttable.py b/framework/texttable.py deleted file mode 100644 index f36346e9..00000000 --- a/framework/texttable.py +++ /dev/null @@ -1,645 +0,0 @@ -# texttable - module for creating simple ASCII tables -# Copyright (C) 2003-2015 Gerome Fournier -# -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2.1 of the License, or (at your option) any later version. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with this library; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA - -"""module for creating simple ASCII tables - - -Example: - - table = Texttable() - table.set_cols_align(["l", "r", "c"]) - table.set_cols_valign(["t", "m", "b"]) - table.add_rows([["Name", "Age", "Nickname"], - ["Mr\\nXavier\\nHuon", 32, "Xav'"], - ["Mr\\nBaptiste\\nClement", 1, "Baby"]]) - print table.draw() + "\\n" - - table = Texttable() - table.set_deco(Texttable.HEADER) - table.set_cols_dtype(['t', # text - 'f', # float (decimal) - 'e', # float (exponent) - 'i', # integer - 'a']) # automatic - table.set_cols_align(["l", "r", "r", "r", "l"]) - table.add_rows([["text", "float", "exp", "int", "auto"], - ["abcd", "67", 654, 89, 128.001], - ["efghijk", 67.5434, .654, 89.6, 12800000000000000000000.00023], - ["lmn", 5e-78, 5e-78, 89.4, .000000000000128], - ["opqrstu", .023, 5e+78, 92., 12800000000000000000000]]) - print table.draw() - -Result: - - +----------+-----+----------+ - | Name | Age | Nickname | - +==========+=====+==========+ - | Mr | | | - | Xavier | 32 | | - | Huon | | Xav' | - +----------+-----+----------+ - | Mr | | | - | Baptiste | 1 | | - | Clement | | Baby | - +----------+-----+----------+ - - text float exp int auto - =========================================== - abcd 67.000 6.540e+02 89 128.001 - efgh 67.543 6.540e-01 90 1.280e+22 - ijkl 0.000 5.000e-78 89 0.000 - mnop 0.023 5.000e+78 92 1.280e+22 -""" - -__all__ = ["Texttable", "ArraySizeError"] - -__author__ = "Gerome Fournier " -__license__ = "LGPL" -__version__ = "0.8.4" -__credits__ = """\ -Jeff Kowalczyk: - - textwrap improved import - - comment concerning header output - -Anonymous: - - add_rows method, for adding rows in one go - -Sergey Simonenko: - - redefined len() function to deal with non-ASCII characters - -Roger Lew: - - columns datatype specifications - -Brian Peterson: - - better handling of unicode errors - -Frank Sachsenheim: - - add Python 2/3-compatibility - -Maximilian Hils: - - fix minor bug for Python 3 compatibility -""" - -import re -import string -import sys - -try: - if sys.version >= "2.3": - import textwrap - elif sys.version >= "2.2": - from optparse import textwrap - else: - from optik import textwrap -except ImportError: - sys.stderr.write("Can't import textwrap module!\n") - raise - -if sys.version >= "2.7": - from functools import reduce - - -def len(iterable): - """Redefining len here so it will be able to work with non-ASCII characters""" - if not isinstance(iterable, str): - return iterable.__len__() - - try: - if sys.version >= "3.0": - return len(str) - else: - return len(str(iterable, "utf")) - except: - return iterable.__len__() - - -TEXT_CODES = { - "bold": {"start": "\x1b[1m", "end": "\x1b[22m"}, - "cyan": {"start": "\x1b[36m", "end": "\x1b[39m"}, - "blue": {"start": "\x1b[34m", "end": "\x1b[39m"}, - "red": {"start": "\x1b[31m", "end": "\x1b[39m"}, - "magenta": {"start": "\x1b[35m", "end": "\x1b[39m"}, - "green": {"start": "\x1b[32m", "end": "\x1b[39m"}, - "yellow": {"start": "\x1b[33m", "end": "\x1b[39m"}, - "underline": {"start": "\x1b[4m", "end": "\x1b[24m"}, -} - - -class TextCodesStripper: - keys = [re.escape(v["start"]) for k, v in list(TEXT_CODES.items())] - keys += [re.escape(v["end"]) for k, v in list(TEXT_CODES.items())] - pattern = re.compile("|".join(keys)) - - @staticmethod - def strip(s): - return re.sub(TextCodesStripper.pattern, "", s) - - -def ansi_len(iterable): - return len(TextCodesStripper.strip(iterable)) - - -class ArraySizeError(Exception): - """Exception raised when specified rows don't fit the required size""" - - def __init__(self, msg): - self.msg = msg - Exception.__init__(self, msg, "") - - def __str__(self): - return self.msg - - -class Texttable: - - BORDER = 1 - HEADER = 1 << 1 - HLINES = 1 << 2 - VLINES = 1 << 3 - - def __init__(self, max_width=80): - """Constructor - - - max_width is an integer, specifying the maximum width of the table - - if set to 0, size is unlimited, therefore cells won't be wrapped - """ - - if max_width <= 0: - max_width = False - self._max_width = max_width - self._precision = 3 - - self._deco = ( - Texttable.VLINES | Texttable.HLINES | Texttable.BORDER | Texttable.HEADER - ) - self.set_chars(["-", "|", "+", "="]) - self.reset() - - def reset(self): - """Reset the instance - - - reset rows and header - """ - - self._hline_string = None - self._row_size = None - self._header = [] - self._rows = [] - - def set_chars(self, array): - """Set the characters used to draw lines between rows and columns - - - the array should contain 4 fields: - - [horizontal, vertical, corner, header] - - - default is set to: - - ['-', '|', '+', '='] - """ - - if len(array) != 4: - raise ArraySizeError("array should contain 4 characters") - array = [x[:1] for x in [str(s) for s in array]] - ( - self._char_horiz, - self._char_vert, - self._char_corner, - self._char_header, - ) = array - - def set_deco(self, deco): - """Set the table decoration - - - 'deco' can be a combination of: - - Texttable.BORDER: Border around the table - Texttable.HEADER: Horizontal line below the header - Texttable.HLINES: Horizontal lines between rows - Texttable.VLINES: Vertical lines between columns - - All of them are enabled by default - - - example: - - Texttable.BORDER | Texttable.HEADER - """ - - self._deco = deco - - def set_cols_align(self, array): - """Set the desired columns alignment - - - the elements of the array should be either "l", "c" or "r": - - * "l": column flushed left - * "c": column centered - * "r": column flushed right - """ - - self._check_row_size(array) - self._align = array - - def set_cols_valign(self, array): - """Set the desired columns vertical alignment - - - the elements of the array should be either "t", "m" or "b": - - * "t": column aligned on the top of the cell - * "m": column aligned on the middle of the cell - * "b": column aligned on the bottom of the cell - """ - - self._check_row_size(array) - self._valign = array - - def set_cols_dtype(self, array): - """Set the desired columns datatype for the cols. - - - the elements of the array should be either "a", "t", "f", "e" or "i": - - * "a": automatic (try to use the most appropriate datatype) - * "t": treat as text - * "f": treat as float in decimal format - * "e": treat as float in exponential format - * "i": treat as int - - - by default, automatic datatyping is used for each column - """ - - self._check_row_size(array) - self._dtype = array - - def set_cols_width(self, array): - """Set the desired columns width - - - the elements of the array should be integers, specifying the - width of each column. For example: - - [10, 20, 5] - """ - - self._check_row_size(array) - try: - array = list(map(int, array)) - if reduce(min, array) <= 0: - raise ValueError - except ValueError: - sys.stderr.write("Wrong argument in column width specification\n") - raise - self._width = array - - def set_precision(self, width): - """Set the desired precision for float/exponential formats - - - width must be an integer >= 0 - - - default value is set to 3 - """ - - if not type(width) is int or width < 0: - raise ValueError("width must be an integer greater then 0") - self._precision = width - - def header(self, array): - """Specify the header of the table""" - - self._check_row_size(array) - self._header = list(map(str, array)) - - def add_row(self, array): - """Add a row in the rows stack - - - cells can contain newlines and tabs - """ - - self._check_row_size(array) - - if not hasattr(self, "_dtype"): - self._dtype = ["a"] * self._row_size - - cells = [] - for i, x in enumerate(array): - cells.append(self._str(i, x)) - self._rows.append(cells) - - def add_rows(self, rows, header=True): - """Add several rows in the rows stack - - - The 'rows' argument can be either an iterator returning arrays, - or a by-dimensional array - - 'header' specifies if the first row should be used as the header - of the table - """ - - # nb: don't use 'iter' on by-dimensional arrays, to get a - # usable code for python 2.1 - if header: - if hasattr(rows, "__iter__") and hasattr(rows, "next"): - self.header(next(rows)) - else: - self.header(rows[0]) - rows = rows[1:] - for row in rows: - self.add_row(row) - - def draw(self): - """Draw the table - - - the table is returned as a whole string - """ - - if not self._header and not self._rows: - return - self._compute_cols_width() - self._check_align() - out = "" - if self._has_border(): - out += self._hline() - if self._header: - out += self._draw_line(self._header, isheader=True) - if self._has_header(): - out += self._hline_header() - length = 0 - for row in self._rows: - length += 1 - out += self._draw_line(row) - if self._has_hlines() and length < len(self._rows): - out += self._hline() - if self._has_border(): - out += self._hline() - return out[:-1] - - def _str(self, i, x): - """Handles string formatting of cell data - - i - index of the cell datatype in self._dtype - x - cell data to format - """ - try: - f = float(x) - except: - try: - return str(x) - except: - return x.encode("utf-8") - - n = self._precision - dtype = self._dtype[i] - - if dtype == "i": - return str(int(round(f))) - elif dtype == "f": - return "%.*f" % (n, f) - elif dtype == "e": - return "%.*e" % (n, f) - elif dtype == "t": - return str(x) - else: - if f - round(f) == 0: - if abs(f) > 1e8: - return "%.*e" % (n, f) - else: - return str(int(round(f))) - else: - if abs(f) > 1e8: - return "%.*e" % (n, f) - else: - return "%.*f" % (n, f) - - def _check_row_size(self, array): - """Check that the specified array fits the previous rows size""" - - if not self._row_size: - self._row_size = len(array) - elif self._row_size != len(array): - raise ArraySizeError("array should contain %d elements" % self._row_size) - - def _has_vlines(self): - """Return a boolean, if vlines are required or not""" - - return self._deco & Texttable.VLINES > 0 - - def _has_hlines(self): - """Return a boolean, if hlines are required or not""" - - return self._deco & Texttable.HLINES > 0 - - def _has_border(self): - """Return a boolean, if border is required or not""" - - return self._deco & Texttable.BORDER > 0 - - def _has_header(self): - """Return a boolean, if header line is required or not""" - - return self._deco & Texttable.HEADER > 0 - - def _hline_header(self): - """Print header's horizontal line""" - - return self._build_hline(True) - - def _hline(self): - """Print an horizontal line""" - - if not self._hline_string: - self._hline_string = self._build_hline() - return self._hline_string - - def _build_hline(self, is_header=False): - """Return a string used to separated rows or separate header from - rows - """ - horiz = self._char_horiz - if is_header: - horiz = self._char_header - # compute cell separator - s = "%s%s%s" % (horiz, [horiz, self._char_corner][self._has_vlines()], horiz) - # build the line - l = s.join([horiz * n for n in self._width]) - # add border if needed - if self._has_border(): - l = "%s%s%s%s%s\n" % (self._char_corner, horiz, l, horiz, self._char_corner) - else: - l += "\n" - return l - - def _len_cell(self, cell): - """Return the width of the cell - - Special characters are taken into account to return the width of the - cell, such like newlines and tabs - """ - - cell_lines = cell.split("\n") - maxi = 0 - for line in cell_lines: - length = 0 - parts = line.split("\t") - for part, i in zip(parts, list(range(1, len(parts) + 1))): - length = length + len(part) - if i < len(parts): - length = (length // 8 + 1) * 8 - maxi = max(maxi, length) - return maxi - - def _compute_cols_width(self): - """Return an array with the width of each column - - If a specific width has been specified, exit. If the total of the - columns width exceed the table desired width, another width will be - computed to fit, and cells will be wrapped. - """ - - if hasattr(self, "_width"): - return - maxi = [] - if self._header: - maxi = [self._len_cell(x) for x in self._header] - for row in self._rows: - for cell, i in zip(row, list(range(len(row)))): - try: - maxi[i] = max(maxi[i], self._len_cell(cell)) - except (TypeError, IndexError): - maxi.append(self._len_cell(cell)) - items = len(maxi) - length = reduce(lambda x, y: x + y, maxi) - if self._max_width and length + items * 3 + 1 > self._max_width: - maxi = [(self._max_width - items * 3 - 1) // items for n in range(items)] - self._width = maxi - - def _check_align(self): - """Check if alignment has been specified, set default one if not""" - - if not hasattr(self, "_align"): - self._align = ["l"] * self._row_size - if not hasattr(self, "_valign"): - self._valign = ["t"] * self._row_size - - def _draw_line(self, line, isheader=False): - """Draw a line - - Loop over a single cell length, over all the cells - """ - - line = self._splitit(line, isheader) - space = " " - out = "" - for i in range(len(line[0])): - if self._has_border(): - out += "%s " % self._char_vert - length = 0 - for cell, width, align in zip(line, self._width, self._align): - length += 1 - cell_line = cell[i] - fill = width - ansi_len(cell_line) - if isheader: - align = "c" - if align == "r": - out += "%s " % (fill * space + cell_line) - elif align == "c": - out += "%s " % ( - int(fill / 2) * space - + cell_line - + int(fill / 2 + fill % 2) * space - ) - else: - out += "%s " % (cell_line + fill * space) - if length < len(line): - out += "%s " % [space, self._char_vert][self._has_vlines()] - out += "%s\n" % ["", self._char_vert][self._has_border()] - return out - - def _splitit(self, line, isheader): - """Split each element of line to fit the column width - - Each element is turned into a list, result of the wrapping of the - string to the desired width - """ - - line_wrapped = [] - for cell, width in zip(line, self._width): - array = [] - for c in cell.split("\n"): - try: - c = str(c) - except UnicodeDecodeError as strerror: - sys.stderr.write( - "UnicodeDecodeError exception for string '%s': %s\n" - % (c, strerror) - ) - if sys.version >= "3.0": - c = str(c, "utf", "replace") - else: - c = str(c, "utf", "replace") - - # imarom - no wrap for now - # array.extend(textwrap.wrap(c, width)) - array.extend([c]) - - line_wrapped.append(array) - max_cell_lines = reduce(max, list(map(len, line_wrapped))) - for cell, valign in zip(line_wrapped, self._valign): - if isheader: - valign = "t" - if valign == "m": - missing = max_cell_lines - len(cell) - cell[:0] = [""] * int(missing / 2) - cell.extend([""] * int(missing / 2 + missing % 2)) - elif valign == "b": - cell[:0] = [""] * (max_cell_lines - len(cell)) - else: - cell.extend([""] * (max_cell_lines - len(cell))) - return line_wrapped - - -if __name__ == "__main__": - table = Texttable() - table.set_cols_align(["l", "r", "c"]) - table.set_cols_valign(["t", "m", "b"]) - table.add_rows( - [ - ["Name", "Age", "Nickname"], - ["Mr\nXavier\nHuon", 32, "Xav'"], - ["Mr\nBaptiste\nClement", 1, "Baby"], - ] - ) - print((table.draw() + "\n")) - - table = Texttable() - table.set_deco(Texttable.HEADER) - table.set_cols_dtype( - [ - "t", # text - "f", # float (decimal) - "e", # float (exponent) - "i", # integer - "a", - ] - ) # automatic - table.set_cols_align(["l", "r", "r", "r", "l"]) - table.add_rows( - [ - ["text", "float", "exp", "int", "auto"], - ["abcd", "67", 654, 89, 128.001], - ["efghijk", 67.5434, 0.654, 89.6, 12800000000000000000000.00023], - ["lmn", 5e-78, 5e-78, 89.4, 0.000000000000128], - ["opqrstu", 0.023, 5e78, 92.0, 12800000000000000000000], - ] - ) - print((table.draw())) diff --git a/requirements.txt b/requirements.txt index dc71e603..f58a64de 100644 --- a/requirements.txt +++ b/requirements.txt @@ -38,4 +38,5 @@ pcapy xlrd scapy==2.4.4 threadpool -isort \ No newline at end of file +isort +texttable \ No newline at end of file diff --git a/tests/perf_test_base.py b/tests/perf_test_base.py index 0666f3bf..82ae74f4 100644 --- a/tests/perf_test_base.py +++ b/tests/perf_test_base.py @@ -44,7 +44,6 @@ from pprint import pformat import numpy as np -import framework.texttable as texttable import framework.utils as utils from framework.config import SuiteConf from framework.exception import VerifyFailure @@ -1039,11 +1038,13 @@ class PerfTestBase(object): return mode_name def __display_suite_result(self, data): + from texttable import Texttable + values = data.get("values") title = data.get("title") max_length = sum([len(item) + 5 for item in title]) self.result_table_create(title) - self._result_table.table = texttable.Texttable(max_width=max_length) + self._result_table.table = Texttable(max_width=max_length) for value in values: self.result_table_add(value) self.result_table_print() -- 2.33.1.windows.1