[welcome] format with black

This commit is contained in:
Dmytro Meleshko 2018-08-08 21:16:26 +03:00
parent 5698aa29a0
commit 3b2f56fa65

View file

@ -11,250 +11,261 @@ COLORS = [ansi.code_to_chars(30 + color_index) for color_index in range(0, 8)]
def colored(string, *colors): def colored(string, *colors):
return ''.join(colors + (string, Style.RESET_ALL)) return "".join(colors + (string, Style.RESET_ALL))
def bright_colored(string, *colors): def bright_colored(string, *colors):
return ''.join(colors + (Style.BRIGHT, string, Style.RESET_ALL)) return "".join(colors + (Style.BRIGHT, string, Style.RESET_ALL))
def format_timedelta(timedelta): def format_timedelta(timedelta):
result = [] result = []
days = timedelta.days days = timedelta.days
mm, ss = divmod(timedelta.seconds, 60) mm, ss = divmod(timedelta.seconds, 60)
hh, mm = divmod(mm, 60) hh, mm = divmod(mm, 60)
def plural(n): def plural(n):
return n, 's' if abs(n) != 1 else '' return n, "s" if abs(n) != 1 else ""
if days > 0: if days > 0:
result.append('%d day%s' % plural(days)) result.append("%d day%s" % plural(days))
if hh > 0 or result: if hh > 0 or result:
result.append('%d hour%s' % plural(hh)) result.append("%d hour%s" % plural(hh))
if mm > 0 or result: if mm > 0 or result:
result.append('%d min%s' % plural(mm)) result.append("%d min%s" % plural(mm))
if len(result) <= 1: if len(result) <= 1:
result.append('%d sec%s' % plural(ss)) result.append("%d sec%s" % plural(ss))
return ', '.join(result) return ", ".join(result)
def humanize_bytes(bytes): def humanize_bytes(bytes):
units = ['B', 'kB', 'MB', 'GB'] units = ["B", "kB", "MB", "GB"]
factor = 1 factor = 1
for unit in units: for unit in units:
next_factor = factor << 10 next_factor = factor << 10
if bytes < next_factor: if bytes < next_factor:
break break
factor = next_factor factor = next_factor
return '%.2f %s' % (float(bytes) / factor, unit) return "%.2f %s" % (float(bytes) / factor, unit)
def colorize_percent(percent, warning, critical, inverse=False): def colorize_percent(percent, warning, critical, inverse=False):
COLORS = [Fore.GREEN, Fore.YELLOW, Fore.RED] COLORS = [Fore.GREEN, Fore.YELLOW, Fore.RED]
color_index = 0 if percent < warning else 1 if percent < critical else 2 color_index = 0 if percent < warning else 1 if percent < critical else 2
if inverse: if inverse:
color_index = 2 - color_index color_index = 2 - color_index
return colored('%.2f%%' % percent, COLORS[color_index]) return colored("%.2f%%" % percent, COLORS[color_index])
def get_hostname(): def get_hostname():
hostname = socket.gethostname() hostname = socket.gethostname()
local_ip = socket.gethostbyname(hostname) local_ip = socket.gethostbyname(hostname)
return hostname, local_ip return hostname, local_ip
def uptime(): def uptime():
return datetime.now() - datetime.fromtimestamp(psutil.boot_time()) return datetime.now() - datetime.fromtimestamp(psutil.boot_time())
def users(): def users():
users = {} users = {}
for user in psutil.users(): for user in psutil.users():
name = user.name name = user.name
terminal = user.terminal terminal = user.terminal
if name in users: if name in users:
users[name].append(terminal) users[name].append(terminal)
else: else:
users[name] = [terminal] users[name] = [terminal]
result = [] result = []
for name in users: for name in users:
terminals = users[name] terminals = users[name]
colored_name = bright_colored(name, Fore.BLUE) colored_name = bright_colored(name, Fore.BLUE)
colored_terminals = [ colored_terminals = [
colored(term, Style.BRIGHT, Fore.BLACK) for term in terminals colored(term, Style.BRIGHT, Fore.BLACK) for term in terminals
] ]
terminals_str = ', '.join(colored_terminals) terminals_str = ", ".join(colored_terminals)
if len(colored_terminals) > 1: if len(colored_terminals) > 1:
terminals_str = '(%s)' % terminals_str terminals_str = "(%s)" % terminals_str
result.append(colored_name + '@' + terminals_str) result.append(colored_name + "@" + terminals_str)
return ', '.join(result) return ", ".join(result)
def shell(): def shell():
return os.environ['SHELL'] return os.environ["SHELL"]
def cpu_usage(): def cpu_usage():
percent = psutil.cpu_percent() percent = psutil.cpu_percent()
return colorize_percent(percent, warning=60, critical=80) return colorize_percent(percent, warning=60, critical=80)
def memory(): def memory():
memory = psutil.virtual_memory() memory = psutil.virtual_memory()
return (humanize_bytes(memory.used), humanize_bytes(memory.total), return (
colorize_percent(memory.percent, warning=60, critical=80)) humanize_bytes(memory.used),
humanize_bytes(memory.total),
colorize_percent(memory.percent, warning=60, critical=80),
)
def disks(): def disks():
result = [] result = []
for disk in psutil.disk_partitions(all=False): for disk in psutil.disk_partitions(all=False):
if psutil.WINDOWS and ('cdrom' in disk.opts or disk.fstype == ''): if psutil.WINDOWS and ("cdrom" in disk.opts or disk.fstype == ""):
# skip cd-rom drives with no disk in it on Windows; they may raise ENOENT, # skip cd-rom drives with no disk in it on Windows; they may raise ENOENT,
# pop-up a Windows GUI error for a non-ready partition or just hang # pop-up a Windows GUI error for a non-ready partition or just hang
continue continue
usage = psutil.disk_usage(disk.mountpoint) usage = psutil.disk_usage(disk.mountpoint)
result.append((disk.mountpoint, humanize_bytes(usage.used), result.append(
humanize_bytes(usage.total), (
colorize_percent(usage.percent, warning=70, critical=85))) disk.mountpoint,
return result humanize_bytes(usage.used),
humanize_bytes(usage.total),
colorize_percent(usage.percent, warning=70, critical=85),
)
)
return result
def battery(): def battery():
if not hasattr(psutil, 'sensors_battery'): if not hasattr(psutil, "sensors_battery"):
return None return None
try: try:
battery = psutil.sensors_battery() battery = psutil.sensors_battery()
except Exception as e: except Exception as e:
print(e) print(e)
return None return None
if battery is None: if battery is None:
return None return None
percent = battery.percent percent = battery.percent
if battery.power_plugged: if battery.power_plugged:
status = 'charging' if percent < 100 else 'fully charged' status = "charging" if percent < 100 else "fully charged"
else: else:
status = '%s left' % format_timedelta(timedelta(seconds=battery.secsleft)) status = "%s left" % format_timedelta(timedelta(seconds=battery.secsleft))
return colorize_percent( return colorize_percent(percent, critical=10, warning=20, inverse=True), status
percent, critical=10, warning=20, inverse=True), status
def is_android(): def is_android():
return os.path.isdir('/system/app') and os.path.isdir('/system/priv-app') return os.path.isdir("/system/app") and os.path.isdir("/system/priv-app")
def get_distro_info(): def get_distro_info():
if psutil.WINDOWS: if psutil.WINDOWS:
return 'windows', platform.system(), platform.release(), '' return "windows", platform.system(), platform.release(), ""
elif psutil.OSX: elif psutil.OSX:
from plistlib import readPlist from plistlib import readPlist
sw_vers = readPlist('/System/Library/CoreServices/SystemVersion.plist')
return 'mac', sw_vers['ProductName'], sw_vers['ProductVersion'], '' sw_vers = readPlist("/System/Library/CoreServices/SystemVersion.plist")
elif is_android(): return "mac", sw_vers["ProductName"], sw_vers["ProductVersion"], ""
from subprocess import check_output elif is_android():
android_version = check_output(['getprop', 'ro.build.version.release']) from subprocess import check_output
return 'android', 'Android', android_version.decode().strip(), ''
elif psutil.LINUX: android_version = check_output(["getprop", "ro.build.version.release"])
import distro return "android", "Android", android_version.decode().strip(), ""
return distro.id(), distro.name(), distro.version(), distro.codename() elif psutil.LINUX:
else: import distro
raise NotImplementedError('unsupported OS')
return distro.id(), distro.name(), distro.version(), distro.codename()
else:
raise NotImplementedError("unsupported OS")
def get_system_info(): def get_system_info():
info_lines = [] info_lines = []
def info(name, value, *format_args): def info(name, value, *format_args):
line = colored(name + ':', Style.BRIGHT, Fore.YELLOW) + ' ' + value line = colored(name + ":", Style.BRIGHT, Fore.YELLOW) + " " + value
if format_args: if format_args:
line = line % format_args line = line % format_args
info_lines.append(line) info_lines.append(line)
username = getuser() username = getuser()
hostname, local_ip = get_hostname() hostname, local_ip = get_hostname()
info_lines.append( info_lines.append(
bright_colored(username, Fore.BLUE) + '@' + bright_colored(username, Fore.BLUE) + "@" + bright_colored(hostname, Fore.RED)
bright_colored(hostname, Fore.RED)) )
info_lines.append('') info_lines.append("")
distro_id, distro_name, distro_version, distro_codename = get_distro_info() distro_id, distro_name, distro_version, distro_codename = get_distro_info()
info('OS', ' '.join([distro_name, distro_version, distro_codename])) info("OS", " ".join([distro_name, distro_version, distro_codename]))
logo_path = os.path.join(os.path.dirname(__file__), 'logos', distro_id) logo_path = os.path.join(os.path.dirname(__file__), "logos", distro_id)
with open(logo_path) as logo_file: with open(logo_path) as logo_file:
logo_lines = logo_file.read().splitlines() logo_lines = logo_file.read().splitlines()
info('Kernel', '%s %s', platform.system(), platform.release()) info("Kernel", "%s %s", platform.system(), platform.release())
info('Uptime', format_timedelta(uptime())) info("Uptime", format_timedelta(uptime()))
users_info = users() users_info = users()
if users_info: if users_info:
info('Users', users_info) info("Users", users_info)
info('Shell', shell()) info("Shell", shell())
info('IP address', local_ip) info("IP address", local_ip)
info_lines.append('') info_lines.append("")
info('CPU Usage', '%s', cpu_usage()) info("CPU Usage", "%s", cpu_usage())
info('Memory', '%s / %s (%s)', *memory()) info("Memory", "%s / %s (%s)", *memory())
for disk_info in disks(): for disk_info in disks():
info('Disk (%s)', '%s / %s (%s)', *disk_info) info("Disk (%s)", "%s / %s (%s)", *disk_info)
battery_info = battery() battery_info = battery()
if battery_info is not None: if battery_info is not None:
info('Battery', '%s (%s)', *battery_info) info("Battery", "%s (%s)", *battery_info)
return logo_lines, info_lines return logo_lines, info_lines
print('') print("")
logo_lines, info_lines = get_system_info() logo_lines, info_lines = get_system_info()
logo_line_widths = [len(re.sub(r'{\d}', '', line)) for line in logo_lines] logo_line_widths = [len(re.sub(r"{\d}", "", line)) for line in logo_lines]
logo_width = max(logo_line_widths) logo_width = max(logo_line_widths)
for line_index in range(0, max(len(logo_lines), len(info_lines))): for line_index in range(0, max(len(logo_lines), len(info_lines))):
line = '' line = ""
logo_line_width = 0 logo_line_width = 0
if line_index < len(logo_lines): if line_index < len(logo_lines):
logo_line = logo_lines[line_index] logo_line = logo_lines[line_index]
logo_line_width = logo_line_widths[line_index] logo_line_width = logo_line_widths[line_index]
line += Style.BRIGHT line += Style.BRIGHT
line += logo_line.format(*COLORS) line += logo_line.format(*COLORS)
line += Style.RESET_ALL line += Style.RESET_ALL
line += ' ' * (logo_width - logo_line_width + 3) line += " " * (logo_width - logo_line_width + 3)
if line_index < len(info_lines): if line_index < len(info_lines):
info_line = info_lines[line_index] info_line = info_lines[line_index]
line += info_line line += info_line
print(line) print(line)
print("")
print('')