Skip to content

Commit

Permalink
feat: upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
rlaace423 committed Aug 29, 2023
1 parent 80e3bda commit 3d03800
Showing 1 changed file with 44 additions and 9 deletions.
53 changes: 44 additions & 9 deletions examples/sys_info2.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from PIL import ImageFont
import psutil
import subprocess as sp
import socket
from collections import OrderedDict


def get_temp():
Expand All @@ -22,12 +24,10 @@ def get_cpu():


def get_mem():
total_memory, used_memory, free_memory = map(
int, os.popen('free -t -m').readlines()[-1].split()[1:])
return used_memory / total_memory * 100
return psutil.virtual_memory().percent


def get_disk():
def get_disk_usage():
usage = psutil.disk_usage("/")
return usage.used / usage.total * 100

Expand All @@ -37,9 +37,39 @@ def get_uptime():
return "UpTime: %s" % (uptime)


def get_ip():
ip = sp.getoutput("hostname -I").split(' ')[0]
return "IP: %s" % (ip)
def find_single_ipv4_address(addrs):
for addr in addrs:
if addr.family == socket.AddressFamily.AF_INET: # IPv4
return addr.address


def get_ipv4_address(interface_name = None):
if_addrs = psutil.net_if_addrs()

if isinstance(interface_name, str) and interface_name in if_addrs:
addrs = if_addrs.get(interface_name)
address = find_single_ipv4_address(addrs)
return address if isinstance(address, str) else ""
else:
if_stats = psutil.net_if_stats()
# remove loopback
if_stats_filtered = { key:if_stats[key] for key, stat in if_stats.items() if "loopback" not in stat.flags }
# sort interfaces by
# 1. Up/Down
# 2. Duplex mode (full: 2, half: 1, unknown: 0)
if_names_sorted = [stat[0] for stat in sorted(if_stats_filtered.items(), key=lambda x: (x[1].isup, x[1].duplex), reverse=True)]
if_addrs_sorted = OrderedDict((key, if_addrs[key]) for key in if_names_sorted if key in if_addrs)

for _, addrs in if_addrs_sorted.items():
address = find_single_ipv4_address(addrs)
if isinstance(address, str):
return address

return ""


def get_ip(network_interface_name):
return "IP: %s" % (get_ipv4_address(network_interface_name))


def format_percent(percent):
Expand Down Expand Up @@ -84,7 +114,7 @@ def stats(device):
else :
draw_bar_full(draw, 2)

disk = get_disk()
disk = get_disk_usage()
draw_text(draw, 0, 3, "Disk")
if disk < 100 :
draw_text(draw, margin_x_figure, 3, "%s %%" % (format_percent(disk)))
Expand All @@ -95,7 +125,7 @@ def stats(device):
if datetime.now().second % (toggle_interval_seconds * 2) < toggle_interval_seconds :
draw_text(draw, 0, 4, get_uptime())
else :
draw_text(draw, 0, 4, get_ip())
draw_text(draw, 0, 4, get_ip(network_interface_name))


font_size = 12
Expand All @@ -110,6 +140,11 @@ def stats(device):
toggle_interval_seconds = 4


# None : find suitable IPv4 address among all network interfaces
# or specify the desired interface name as string.
network_interface_name = None


device = get_device()
font_default = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf', font_size)
font_full = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf', font_size_full)
Expand Down

0 comments on commit 3d03800

Please sign in to comment.