Skip to content

Commit

Permalink
use 'Final' type hint for constants
Browse files Browse the repository at this point in the history
  • Loading branch information
totaam committed Jul 21, 2024
1 parent 2bcf3f8 commit 3e3bc89
Show file tree
Hide file tree
Showing 17 changed files with 97 additions and 78 deletions.
22 changes: 11 additions & 11 deletions xpra/net/file_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import subprocess
import hashlib
import uuid
from typing import Any
from typing import Any, Final
from time import monotonic
from dataclasses import dataclass
from collections.abc import Callable
Expand Down Expand Up @@ -40,15 +40,15 @@
SEND_REQUEST_TIMEOUT = max(300, envint("XPRA_SEND_REQUEST_TIMEOUT", 3600))
CHUNK_TIMEOUT = 10 * 1000

MIMETYPE_EXTS = {
MIMETYPE_EXTS: dict[str, str] = {
"application/postscript": "ps",
"application/pdf": "pdf",
"raw": "raw",
}

DENY = 0
ACCEPT = 1 # the file / URL will be sent
OPEN = 2 # don't send, open on sender
DENY: Final[int] = 0
ACCEPT: Final[int] = 1 # the file / URL will be sent
OPEN: Final[int] = 2 # don't send, open on sender


def osclose(fd: int) -> None:
Expand Down Expand Up @@ -399,7 +399,7 @@ def _process_send_file_chunk(self, packet: PacketType) -> None:
filelog("got chunk for a cancelled file transfer, ignoring it")
return

def progress(position, error=None):
def progress(position: int, error="") -> None:
s_elapsed = monotonic() - chunk_state.start
self.transfer_progress_update(False, chunk_state.send_id, s_elapsed, position, chunk_state.filesize, error)

Expand Down Expand Up @@ -603,7 +603,7 @@ def process_downloaded_file(self, filename: str, mimetype: str,
filelog("started process-download thread: %s", t)

def do_process_downloaded_file(self, filename: str, mimetype: str, printit: bool, openit: bool, filesize: int,
options: typedict):
options: typedict) -> None:
filelog("do_process_downloaded_file%s", (filename, mimetype, printit, openit, filesize, options))
if printit:
self._print_file(filename, mimetype, options)
Expand Down Expand Up @@ -740,7 +740,7 @@ def send_request_file(self, filename: str, openit: bool = True):
self.send("request-file", filename, openit)
self.files_requested[filename] = openit

def _process_open_url(self, packet: PacketType):
def _process_open_url(self, packet: PacketType) -> None:
send_id = str(packet[2])
url = str(packet[1])
if not self.open_url:
Expand All @@ -764,11 +764,11 @@ def send_open_url(self, url: str) -> bool:
self.do_send_open_url(url)
return True

def do_send_open_url(self, url: str, send_id: str = ""):
def do_send_open_url(self, url: str, send_id: str = "") -> None:
self.send("open-url", url, send_id)

def send_file(self, filename: str, mimetype: str, data: SizedBuffer, filesize=0,
printit=False, openit=False, options=None):
printit=False, openit=False, options=None) -> bool:
if printit:
if not self.printing:
printlog.warn("Warning: printing is not enabled for %s", self)
Expand Down Expand Up @@ -838,7 +838,7 @@ def do_process_send_data_request(self, dtype: str, send_id: str, url: str, _, fi
printit: bool, openit: bool, options: typedict) -> None:
filelog(f"do_process_send_data_request: {send_id=}, {url=}, {printit=}, {openit=}, {options=}")

def cb_answer(accept: bool):
def cb_answer(accept: bool) -> bool:
filelog("accept%s=%s", (url, printit, openit), accept)
self.send("send-data-response", send_id, accept)

Expand Down
8 changes: 5 additions & 3 deletions xpra/net/protocol/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
# later version. See the file COPYING for details.

CONNECTION_LOST = "connection-lost"
GIBBERISH = "gibberish"
INVALID = "invalid"
from typing import Final

CONNECTION_LOST: Final[str] = "connection-lost"
GIBBERISH: Final[str] = "gibberish"
INVALID: Final[str] = "invalid"
8 changes: 5 additions & 3 deletions xpra/server/dbus/server_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
# later version. See the file COPYING for details.

from typing import Final

import dbus.service # @UnresolvedImport
from dbus import PROPERTIES_IFACE # @UnresolvedImport
from dbus.exceptions import DBusException # @UnresolvedImport
Expand All @@ -13,9 +15,9 @@

log = Logger("dbus", "server")

BUS_NAME = "org.xpra.Server"
INTERFACE = "org.xpra.Server"
PATH = "/org/xpra/Server"
BUS_NAME: Final[str] = "org.xpra.Server"
INTERFACE: Final[str] = "org.xpra.Server"
PATH: Final[str] = "/org/xpra/Server"


class DBUS_Server_Base(dbus.service.Object):
Expand Down
8 changes: 5 additions & 3 deletions xpra/server/dbus/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
# later version. See the file COPYING for details.

from typing import Final

import dbus.service # @UnresolvedImport
from dbus import PROPERTIES_IFACE # @UnresolvedImport
from dbus.exceptions import DBusException # @UnresolvedImport
Expand All @@ -16,9 +18,9 @@

log = Logger("dbus", "server")

BUS_NAME = "org.xpra.Server"
INTERFACE = "org.xpra.Client"
PATH = "/org/xpra/Client"
BUS_NAME: Final[str] = "org.xpra.Server"
INTERFACE: Final[str] = "org.xpra.Client"
PATH: Final[str] = "/org/xpra/Client"


def n(*args):
Expand Down
8 changes: 5 additions & 3 deletions xpra/x11/bindings/send_wm.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
# later version. See the file COPYING for details.

from typing import Final

from xpra.x11.bindings.window import constants, X11WindowBindings
from xpra.log import Logger

log = Logger("x11", "focus")

X11Window = X11WindowBindings()

CurrentTime = constants["CurrentTime"]
SubstructureNotifyMask = constants["SubstructureNotifyMask"]
SubstructureRedirectMask = constants["SubstructureRedirectMask"]
CurrentTime: Final[int] = constants["CurrentTime"]
SubstructureNotifyMask: Final[int] = constants["SubstructureNotifyMask"]
SubstructureRedirectMask: Final[int] = constants["SubstructureRedirectMask"]


def send_wm_take_focus(xid: int, timestamp: int = CurrentTime) -> None:
Expand Down
6 changes: 3 additions & 3 deletions xpra/x11/gtk/clipboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import os
import struct
from typing import Any
from typing import Any, Final
from collections.abc import Iterable, Callable, Sequence

from xpra.os_util import gi_import
Expand Down Expand Up @@ -39,8 +39,8 @@

log = Logger("x11", "clipboard")

CurrentTime: int = constants["CurrentTime"]
StructureNotifyMask: int = constants["StructureNotifyMask"]
CurrentTime: Final[int] = constants["CurrentTime"]
StructureNotifyMask: Final[int] = constants["StructureNotifyMask"]

sizeof_long = struct.calcsize(b'@L')

Expand Down
4 changes: 3 additions & 1 deletion xpra/x11/gtk/composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
# later version. See the file COPYING for details.

from typing import Final

from xpra.os_util import gi_import
from xpra.gtk.gobject import one_arg_signal
from xpra.gtk.error import xlog
Expand All @@ -23,7 +25,7 @@
X11Window = X11WindowBindings()
X11Window.ensure_XComposite_support()

StructureNotifyMask = constants["StructureNotifyMask"]
StructureNotifyMask: Final[int] = constants["StructureNotifyMask"]


class CompositeHelper(WindowDamageHandler, GObject.GObject):
Expand Down
3 changes: 2 additions & 1 deletion xpra/x11/gtk/damage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
# later version. See the file COPYING for details.

from typing import Final

from xpra.util.env import envbool
from xpra.gtk.gobject import one_arg_signal
Expand All @@ -21,7 +22,7 @@
X11Window = X11WindowBindings()
X11Window.ensure_XDamage_support()

StructureNotifyMask = constants["StructureNotifyMask"]
StructureNotifyMask: Final[int] = constants["StructureNotifyMask"]
USE_XSHM = envbool("XPRA_XSHM", True)


Expand Down
5 changes: 3 additions & 2 deletions xpra/x11/gtk/selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# else steals it, then we should exit.

import sys
from typing import Final
from struct import unpack, calcsize

from xpra.gtk.gobject import no_arg_signal, one_arg_signal
Expand All @@ -29,8 +30,8 @@

SELECTION_EXIT_TIMEOUT = envint("XPRA_SELECTION_EXIT_TIMEOUT", 20)

StructureNotifyMask = constants["StructureNotifyMask"]
XNone = constants["XNone"]
StructureNotifyMask: Final[int] = constants["StructureNotifyMask"]
XNone: Final[int] = constants["XNone"]


class AlreadyOwned(Exception):
Expand Down
11 changes: 6 additions & 5 deletions xpra/x11/gtk/tray.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# later version. See the file COPYING for details.

import struct
from typing import Final
from enum import IntEnum

from xpra.util.env import envint
Expand All @@ -25,8 +26,8 @@

log = Logger("x11", "tray")

XNone = constants["XNone"]
StructureNotifyMask = constants["StructureNotifyMask"]
XNone: Final[int] = constants["XNone"]
StructureNotifyMask: Final[int] = constants["StructureNotifyMask"]

XEMBED_VERSION = 0

Expand Down Expand Up @@ -59,9 +60,9 @@ class XEMBED_FOCUS(IntEnum):
LAST = 2


SELECTION = "_NET_SYSTEM_TRAY_S0"
SYSTRAY_VISUAL = "_NET_SYSTEM_TRAY_VISUAL"
SYSTRAY_ORIENTATION = "_NET_SYSTEM_TRAY_ORIENTATION"
SELECTION: Final[str] = "_NET_SYSTEM_TRAY_S0"
SYSTRAY_VISUAL: Final[str] = "_NET_SYSTEM_TRAY_VISUAL"
SYSTRAY_ORIENTATION: Final[str] = "_NET_SYSTEM_TRAY_ORIENTATION"


class TRAY_ORIENTATION(IntEnum):
Expand Down
14 changes: 7 additions & 7 deletions xpra/x11/gtk/wm.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# later version. See the file COPYING for details.

import os
from typing import Any
from typing import Any, Final

from xpra.util.env import envbool
from xpra.os_util import gi_import
Expand Down Expand Up @@ -37,13 +37,13 @@
screenlog = Logger("x11", "window", "screen")
framelog = Logger("x11", "window", "frame")

CWX = constants["CWX"]
CWY = constants["CWY"]
CWWidth = constants["CWWidth"]
CWHeight = constants["CWHeight"]
CWX: Final[int] = constants["CWX"]
CWY: Final[int] = constants["CWY"]
CWWidth: Final[int] = constants["CWWidth"]
CWHeight: Final[int] = constants["CWHeight"]

NotifyPointerRoot = constants["NotifyPointerRoot"]
NotifyDetailNone = constants["NotifyDetailNone"]
NotifyPointerRoot: Final[int] = constants["NotifyPointerRoot"]
NotifyDetailNone: Final[int] = constants["NotifyDetailNone"]

LOG_MANAGE_FAILURES = envbool("XPRA_LOG_MANAGE_FAILURES", False)

Expand Down
6 changes: 4 additions & 2 deletions xpra/x11/gtk/world_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
# later version. See the file COPYING for details.

from typing import Final

from xpra.os_util import gi_import
from xpra.gtk.error import xlog
from xpra.util.env import IgnoreWarningsContext, ignorewarnings
Expand All @@ -19,8 +21,8 @@
log = Logger("x11", "window")
focuslog = Logger("x11", "window", "focus")

XNone = constants["XNone"]
CurrentTime = constants["CurrentTime"]
XNone: Final[int] = constants["XNone"]
CurrentTime: Final[int] = constants["CurrentTime"]


# This file defines Xpra's top-level widget. It is a magic window that
Expand Down
7 changes: 4 additions & 3 deletions xpra/x11/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
# later version. See the file COPYING for details.

from typing import Final
from collections.abc import Callable, Sequence

from xpra.os_util import gi_import
Expand Down Expand Up @@ -37,9 +38,9 @@
_NET_WM_STATE_ADD: "ADD",
_NET_WM_STATE_TOGGLE: "TOGGLE",
}
WithdrawnState: int = constants["WithdrawnState"]
IconicState: int = constants["IconicState"]
NormalState: int = constants["NormalState"]
WithdrawnState: Final[int] = constants["WithdrawnState"]
IconicState: Final[int] = constants["IconicState"]
NormalState: Final[int] = constants["NormalState"]
WM_STATE_NAME: dict[int, str] = {
IconicState: "Iconic",
NormalState: "Normal",
Expand Down
14 changes: 7 additions & 7 deletions xpra/x11/models/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import os
import signal
from socket import gethostname
from typing import Any
from typing import Any, Final
from collections.abc import Callable, Sequence

from xpra.os_util import gi_import
Expand Down Expand Up @@ -55,7 +55,7 @@
DELETE_KILL_PID = envbool("XPRA_DELETE_KILL_PID", True)
DELETE_XKILL = envbool("XPRA_DELETE_XKILL", True)

CurrentTime = constants["CurrentTime"]
CurrentTime: Final[int] = constants["CurrentTime"]

# Re-stacking:
Above = 0
Expand All @@ -72,11 +72,11 @@
}

# grab stuff:
NotifyNormal = constants["NotifyNormal"]
NotifyGrab = constants["NotifyGrab"]
NotifyUngrab = constants["NotifyUngrab"]
NotifyWhileGrabbed = constants["NotifyWhileGrabbed"]
NotifyNonlinearVirtual = constants["NotifyNonlinearVirtual"]
NotifyNormal: Final[int] = constants["NotifyNormal"]
NotifyGrab: Final[int] = constants["NotifyGrab"]
NotifyUngrab: Final[int] = constants["NotifyUngrab"]
NotifyWhileGrabbed: Final[int] = constants["NotifyWhileGrabbed"]
NotifyNonlinearVirtual: Final[int] = constants["NotifyNonlinearVirtual"]
GRAB_CONSTANTS: dict[int, str] = {
NotifyNormal: "NotifyNormal",
NotifyGrab: "NotifyGrab",
Expand Down
17 changes: 9 additions & 8 deletions xpra/x11/models/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
# later version. See the file COPYING for details.

from typing import Final
from math import ceil, floor

from xpra.os_util import gi_import
Expand Down Expand Up @@ -37,14 +38,14 @@

X11Window = X11WindowBindings()

CWX: int = constants["CWX"]
CWY: int = constants["CWY"]
CWWidth: int = constants["CWWidth"]
CWHeight: int = constants["CWHeight"]
CWBorderWidth: int = constants["CWBorderWidth"]
CWSibling: int = constants["CWSibling"]
CWStackMode: int = constants["CWStackMode"]
CONFIGURE_GEOMETRY_MASK: int = CWX | CWY | CWWidth | CWHeight
CWX: Final[int] = constants["CWX"]
CWY: Final[int] = constants["CWY"]
CWWidth: Final[int] = constants["CWWidth"]
CWHeight: Final[int] = constants["CWHeight"]
CWBorderWidth: Final[int] = constants["CWBorderWidth"]
CWSibling: Final[int] = constants["CWSibling"]
CWStackMode: Final[int] = constants["CWStackMode"]
CONFIGURE_GEOMETRY_MASK: Final[int] = CWX | CWY | CWWidth | CWHeight
CW_MASK_TO_NAME: dict[int, str] = {
CWX: "X",
CWY: "Y",
Expand Down
Loading

0 comments on commit 3e3bc89

Please sign in to comment.