Skip to content

Commit

Permalink
Better print client list
Browse files Browse the repository at this point in the history
Improved client list printing. If tabulate is available use it (in the same way that BaseConsole). Pointed out by @gelim in #12, thanks!
  • Loading branch information
martingalloar committed Feb 20, 2017
1 parent c33e720 commit cf17d58
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions examples/router_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
from pysap.SAPNI import SAPNI, SAPNIStreamSocket
from pysap.SAPRouter import (SAPRouter, router_is_error, get_router_version,
SAPRouterInfoClients, SAPRouterInfoServer)
# Optional imports
try:
from tabulate import tabulate
except ImportError:
tabulate = None


# Bind the SAPRouter layer
Expand Down Expand Up @@ -106,6 +111,14 @@ def parse_options():
return options


def print_table(clients):
"""Prints the client table"""
if tabulate:
print(tabulate(clients))
else:
print("\n".join("\t| ".join([str(col).strip() for col in line]).expandtabs(20) for line in clients))


# Main function
def main():
options = parse_options()
Expand Down Expand Up @@ -233,9 +246,7 @@ def main():
# Decode the first packet as a list of info client
raw_response.decode_payload_as(SAPRouterInfoClients)

clients = []
clients.append("\t".join(["ID", "Client", "Partner", "Service", "Connected on"]))
clients.append("-" * 60)
clients = [["ID", "Client", "Partner", "Service", "Connected on"]]
for client in raw_response.clients:

# If the trace flag is set, add a mark
Expand All @@ -246,7 +257,7 @@ def main():
"%s%s" % (flag, client.partner) if client.flag_routed else "(no partner)",
client.service if client.flag_routed else "",
datetime.fromtimestamp(client.connected_on).ctime()]
clients.append("\t".join(fields))
clients.append(fields)

# Decode the second packet as server info
raw_response = conn.recv()
Expand All @@ -257,8 +268,7 @@ def main():
"Parent process: PID = %d, port = %d\n" % (raw_response.port, raw_response.pid,
datetime.fromtimestamp(raw_response.started_on).ctime(),
raw_response.ppid, raw_response.pport))

print("\n".join(clients))
print_table(clients)
print("(*) Connections being traced")

# Show the plain packets returned
Expand Down

0 comments on commit cf17d58

Please sign in to comment.