Skip to content

Commit

Permalink
[#3] Prettify analysis output
Browse files Browse the repository at this point in the history
  • Loading branch information
romankh committed Oct 14, 2017
1 parent 848e3c3 commit 7adb4af
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 70 deletions.
Empty file added core/util/__init__.py
Empty file.
46 changes: 46 additions & 0 deletions core/util/text_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# -*- coding: utf-8 -*-
from string import ljust


def columnize(string_list, columns):
"""
Creates a table for a list of strings with a given number of columns
:param string_list: the list with the strings
:param columns: the number of columns
:return: string representation of the columnized input strings
"""
result = ""
separator = "|"

if not string_list:
return result

nonstrings = [i for i in range(len(string_list))
if not isinstance(string_list[i], str)]
if nonstrings:
raise TypeError, ("string_list[i] not a string for i in %s" %
", ".join(map(str, nonstrings)))

lists = [[] for i in range(columns)]
column_lengths = [0 for i in range(columns)]
tab_length = 1

for i in range(len(string_list)):
lists[i % columns].append(string_list[i])
if len(string_list[i]) > column_lengths[i % columns]:
column_lengths[i % columns] = len(string_list[i])

for i in range(columns):
tab_length += column_lengths[i] + 4

result += "\n" + "=" * tab_length + "\n"
for i in range(len(string_list)):
col = i % columns
result += separator + " " + "".join(ljust(string_list[i], column_lengths[col] + 2))
if col == columns - 1:
if i < columns:
result += separator + "\n" + "=" * tab_length + "\n"
else:
result += separator + "\n" + "-" * tab_length + "\n"

return result
24 changes: 14 additions & 10 deletions plugins/analysis_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from adapter.grgsm.info_extractor import InfoExtractor
from core.plugin.interface import plugin, PluginBase, arg, cmd, subcmd
from core.util.text_utils import columnize

channel_modes = ['BCCH_SDCCH4', 'SDCCH8']

Expand Down Expand Up @@ -51,15 +52,18 @@ def immediate_assignments(self, args):
ia_tas = extractor.gsm_extract_immediate_assignment.get_timing_advances()
ia_mobileallocations = extractor.gsm_extract_immediate_assignment.get_mobile_allocations()

if len(ia_fnrs) > 0:
self.printmsg("IAs:")
if len(ia_fnrs) == 0:
self.printmsg("No Immediate Assignment messages found.")
else:
strings = ["FNR", "TYPE", "TIMESLOT", "TIMING ADVANCE", "SUBCHANNEL", "HOPPING"]

self.printmsg("FNR TYPE TIMESLOT TIMING ADVANCE SUBCHANNEL HOPPING")
for i in range(0, len(ia_fnrs)):
self.printmsg("%s %s %s %s %s %s" % (ia_fnrs[i],
ia_channeltypes[i][:4] if ia_channeltypes[i].startswith(
"GPRS") else
ia_channeltypes[i],
ia_timeslots[i],
ia_tas[i],
ia_subchannels[i],
"Y" if ia_hopping[i] == 1 else "N",))
strings.append(str(ia_fnrs[i]))
strings.append(str(ia_channeltypes[i][:4]) if ia_channeltypes[i].startswith("GPRS") else str(ia_channeltypes[i]))
strings.append(str(ia_timeslots[i]))
strings.append(str(ia_tas[i]))
strings.append(str(ia_subchannels[i]))
strings.append("Y" if ia_hopping[i] == 1 else "N")

self.printmsg(columnize(strings, 6))
60 changes: 0 additions & 60 deletions ui/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,66 +136,6 @@ def handle_cmd(self, line):

return self._execute_command(cmd, arg)

def columnize(self, string_list, displaywidth=80):
"""
columnize method from cmd.cmd module.
Display a list of strings as a compact set of columns.
Each column is only as wide as necessary.
Columns are separated by two spaces (one was not legible enough).
:param string_list: list of strings to display in columns.
:param displaywidth: number of columns to display.
"""
if not string_list:
self.stdout.write("<empty>\n")
return
nonstrings = [i for i in range(len(string_list))
if not isinstance(string_list[i], str)]
if nonstrings:
raise TypeError, ("string_list[i] not a string for i in %s" %
", ".join(map(str, nonstrings)))
size = len(string_list)
if size == 1:
self.stdout.write('%s\n' % str(string_list[0]))
return
# Try every row count from 1 upwards
for nrows in range(1, len(string_list)):
ncols = (size + nrows - 1) // nrows
colwidths = []
totwidth = -2
for col in range(ncols):
colwidth = 0
for row in range(nrows):
i = row + nrows * col
if i >= size:
break
x = string_list[i]
colwidth = max(colwidth, len(x))
colwidths.append(colwidth)
totwidth += colwidth + 2
if totwidth > displaywidth:
break
if totwidth <= displaywidth:
break
else:
nrows = len(string_list)
ncols = 1
colwidths = [0]
for row in range(nrows):
texts = []
for col in range(ncols):
i = row + nrows * col
if i >= size:
x = ""
else:
x = string_list[i]
texts.append(x)
while texts and not texts[-1]:
del texts[-1]
for col in range(len(texts)):
texts[col] = texts[col].ljust(colwidths[col])
self.stdout.write("%s\n" % str(" ".join(texts)))

# replaces readline's original hook
def rl_display_hook(self, substitution, matches, longest_match_length):
"""
Expand Down

0 comments on commit 7adb4af

Please sign in to comment.