Skip to content

Commit

Permalink
add more type hints
Browse files Browse the repository at this point in the history
git-svn-id: https://xpra.org/svn/Xpra/trunk@23843 3bb7dfac-3a0b-4e04-842a-767bc560f471
  • Loading branch information
totaam committed Sep 20, 2019
1 parent 43bce37 commit aa1f522
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 43 deletions.
66 changes: 33 additions & 33 deletions src/xpra/os_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@
BITS = struct.calcsize(b"P")*8


def strtobytes(x):
def strtobytes(x) -> bytes:
if isinstance(x, bytes):
return x
return str(x).encode("latin1")
def bytestostr(x):
def bytestostr(x) -> str:
if isinstance(x, bytes):
return x.decode("latin1")
return str(x)
def hexstr(v):
def hexstr(v) -> str:
return bytestostr(binascii.hexlify(strtobytes(v)))


Expand All @@ -51,7 +51,7 @@ def get_util_logger():
util_logger = Logger("util")
return util_logger

def memoryview_to_bytes(v):
def memoryview_to_bytes(v) -> bytes:
if isinstance(v, bytes):
return v
if isinstance(v, memoryview):
Expand All @@ -67,17 +67,17 @@ def setsid():
os.setsid()


def getuid():
def getuid() -> int:
if POSIX:
return os.getuid()
return 0

def getgid():
def getgid() -> int:
if POSIX:
return os.getgid()
return 0

def get_shell_for_uid(uid):
def get_shell_for_uid(uid) -> str:
if POSIX:
from pwd import getpwuid
try:
Expand All @@ -86,7 +86,7 @@ def get_shell_for_uid(uid):
pass
return ""

def get_username_for_uid(uid):
def get_username_for_uid(uid) -> str:
if POSIX:
from pwd import getpwuid
try:
Expand All @@ -95,7 +95,7 @@ def get_username_for_uid(uid):
pass
return ""

def get_home_for_uid(uid):
def get_home_for_uid(uid) -> str:
if POSIX:
from pwd import getpwuid
try:
Expand All @@ -110,7 +110,7 @@ def get_groups(username):
return [gr.gr_name for gr in grp.getgrall() if username in gr.gr_mem]
return []

def get_group_id(group):
def get_group_id(group) -> int:
try:
import grp #@UnresolvedImport
gr = grp.getgrnam(group)
Expand All @@ -135,7 +135,7 @@ def platform_release(release):
return release


def platform_name(sys_platform, release=None):
def platform_name(sys_platform, release=None) -> str:
if not sys_platform:
return "unknown"
PLATFORMS = {"win32" : "Microsoft Windows",
Expand All @@ -159,17 +159,17 @@ def rel(v):
return rel(sys_platform)


def get_rand_chars(l=16, chars=b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"):
def get_rand_chars(l=16, chars=b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") -> bytes:
import random
return b"".join(chars[random.randint(0, len(chars)-1):][:1] for _ in range(l))

def get_hex_uuid():
def get_hex_uuid() -> str:
return uuid.uuid4().hex

def get_int_uuid():
def get_int_uuid() -> int:
return uuid.uuid4().int

def get_machine_id():
def get_machine_id() -> str:
"""
Try to get uuid string which uniquely identifies this machine.
Warning: only works on posix!
Expand Down Expand Up @@ -231,7 +231,7 @@ def is_Wayland():
)


def is_distribution_variant(variant=b"Debian"):
def is_distribution_variant(variant=b"Debian") -> bool:
if not POSIX:
return False
try:
Expand All @@ -249,7 +249,7 @@ def is_distribution_variant(variant=b"Debian"):
return False

os_release_file_data = False
def load_os_release_file():
def load_os_release_file() -> bytes:
global os_release_file_data
if os_release_file_data is False:
try:
Expand All @@ -258,25 +258,25 @@ def load_os_release_file():
os_release_file_data = None
return os_release_file_data

def is_Ubuntu():
def is_Ubuntu() -> bool:
return is_distribution_variant(b"Ubuntu")

def is_Debian():
def is_Debian() -> bool:
return is_distribution_variant(b"Debian")

def is_Raspbian():
def is_Raspbian() -> bool:
return is_distribution_variant(b"Raspbian")

def is_Fedora():
def is_Fedora() -> bool:
return is_distribution_variant(b"Fedora")

def is_Arch():
def is_Arch() -> bool:
return is_distribution_variant(b"Arch")

def is_CentOS():
def is_CentOS() -> bool:
return is_distribution_variant(b"CentOS")

def is_RedHat():
def is_RedHat() -> bool:
return is_distribution_variant(b"RedHat")


Expand Down Expand Up @@ -320,17 +320,17 @@ def getUbuntuVersion():
pass
return ()

def is_unity():
def is_unity() -> bool:
return os.environ.get("XDG_CURRENT_DESKTOP", "").lower().find("unity")>=0

def is_gnome():
def is_gnome() -> bool:
return os.environ.get("XDG_CURRENT_DESKTOP", "").lower().find("gnome")>=0

def is_kde():
def is_kde() -> bool:
return os.environ.get("XDG_CURRENT_DESKTOP", "").lower().find("kde")>=0


def is_WSL():
def is_WSL() -> bool:
if not POSIX:
return False
r = None
Expand All @@ -341,7 +341,7 @@ def is_WSL():
return r is not None and r.find(b"Microsoft")>=0


def get_generic_os_name():
def get_generic_os_name() -> str:
for k,v in {
"linux" : "linux",
"darwin" : "osx",
Expand All @@ -353,14 +353,14 @@ def get_generic_os_name():
return sys.platform


def filedata_nocrlf(filename):
def filedata_nocrlf(filename) -> str:
v = load_binary_file(filename)
if v is None:
get_util_logger().error("failed to load '%s'", filename)
return None
return v.strip("\n\r")

def load_binary_file(filename):
def load_binary_file(filename) -> bytes:
if not os.path.exists(filename):
return None
try:
Expand Down Expand Up @@ -716,14 +716,14 @@ def get_status_output(*args, **kwargs):
return p.returncode, stdout.decode("utf-8"), stderr.decode("utf-8")


def is_systemd_pid1():
def is_systemd_pid1() -> bool:
if not POSIX:
return False
d = load_binary_file("/proc/1/cmdline")
return d and d.find(b"/systemd")>=0


def get_ssh_port():
def get_ssh_port() -> int:
#FIXME: how do we find out which port ssh is on?
if WIN32:
return 0
Expand Down
4 changes: 2 additions & 2 deletions src/xpra/queue_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from xpra.util import AtomicInteger
from xpra.log import Logger

log = Logger("proxy")
log = Logger("util")


#emulate the glib main loop using a single thread + queue:
Expand All @@ -23,7 +23,7 @@ def __init__(self):
self.timers = {}
self.timer_lock = RLock()

def source_remove(self, tid):
def source_remove(self, tid : int):
log("source_remove(%i)", tid)
with self.timer_lock:
try:
Expand Down
2 changes: 1 addition & 1 deletion src/xpra/simple_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def to_std_unit(v, unit=1000):
return "K", v//unit
return "", v

def std_unit(v, unit=1000):
def std_unit(v, unit=1000) -> str:
unit, value = to_std_unit(v, unit)
return "%s%s" % (int(value), unit)

Expand Down
14 changes: 7 additions & 7 deletions src/xpra/version_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def warn(msg, *args, **kwargs):
get_util_logger().warn(msg, *args, **kwargs)


def full_version_str():
def full_version_str() -> str:
s = XPRA_VERSION
try:
from xpra.src_info import REVISION, LOCAL_MODIFICATIONS
Expand All @@ -43,10 +43,10 @@ def full_version_str():
return s


def version_as_numbers(version):
def version_as_numbers(version : str):
return [int(x) for x in version.split(".")]

def version_compat_check(remote_version):
def version_compat_check(remote_version : str):
if remote_version is None:
msg = "remote version not available!"
log(msg)
Expand All @@ -68,7 +68,7 @@ def version_compat_check(remote_version):
return None


def get_host_info():
def get_host_info() -> dict:
#this function is for non UI thread info
info = {
"pid" : os.getpid(),
Expand All @@ -90,7 +90,7 @@ def get_host_info():
})
return info

def get_version_info():
def get_version_info() -> dict:
props = {
"version" : XPRA_VERSION
}
Expand All @@ -102,7 +102,7 @@ def get_version_info():
warn("missing some source information: %s", e)
return props

def get_version_info_full():
def get_version_info_full() -> dict:
props = get_version_info()
try:
from xpra import build_info
Expand All @@ -129,7 +129,7 @@ def get_version_info_full():
log("get_version_info_full()=%s", props)
return props

def do_get_platform_info():
def do_get_platform_info() -> dict:
from xpra.os_util import platform_name, platform_release
pp = sys.modules.get("platform", python_platform)
def get_processor_name():
Expand Down

0 comments on commit aa1f522

Please sign in to comment.