1# Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Enum containers backing psutil constants.
6
7This module groups constants used by psutil APIs into Enum classes.
8These enums mainly act as containers for related values and are useful
9for type annotations and introspection.
10
11In normal usage constants should be accessed directly from the psutil
12namespace instead of importing these enums, e.g.:
13
14 import psutil
15 if proc.status() == psutil.STATUS_RUNNING:
16 ...
17
18The top-level constants (e.g. ``psutil.STATUS_RUNNING``) are aliases of
19the enum members defined here and represent the primary public API.
20"""
21
22import enum
23
24from ._common import FREEBSD
25from ._common import LINUX
26from ._common import SUNOS
27from ._common import WINDOWS
28
29if WINDOWS or LINUX or FREEBSD:
30 from . import _psutil
31else:
32 _psutil = None
33
34if hasattr(enum, "StrEnum"): # Python >= 3.11
35 StrEnum = enum.StrEnum
36else:
37
38 # A backport of Python 3.11 StrEnum class for >= Python 3.8
39 class StrEnum(str, enum.Enum):
40 def __new__(cls, *values):
41 value = str(*values)
42 member = str.__new__(cls, value)
43 member._value_ = value
44 return member
45
46 __str__ = str.__str__
47
48 @staticmethod
49 def _generate_next_value_(name, _start, _count, _last_values):
50 return name.lower()
51
52
53# psutil.Process.status()
54class ProcessStatus(StrEnum):
55 STATUS_DEAD = "dead"
56 STATUS_DISK_SLEEP = "disk-sleep"
57 STATUS_IDLE = "idle" # Linux, macOS, FreeBSD
58 STATUS_LOCKED = "locked" # FreeBSD
59 STATUS_PARKED = "parked" # Linux
60 STATUS_RUNNING = "running"
61 STATUS_SLEEPING = "sleeping"
62 STATUS_STOPPED = "stopped"
63 STATUS_SUSPENDED = "suspended" # NetBSD
64 STATUS_TRACING_STOP = "tracing-stop"
65 STATUS_WAITING = "waiting" # FreeBSD
66 STATUS_WAKE_KILL = "wake-kill"
67 STATUS_WAKING = "waking"
68 STATUS_ZOMBIE = "zombie"
69
70
71# psutil.Process.net_connections() and psutil.net_connections()
72class ConnectionStatus(StrEnum):
73 CONN_CLOSE = "CLOSE"
74 CONN_CLOSE_WAIT = "CLOSE_WAIT"
75 CONN_CLOSING = "CLOSING"
76 CONN_ESTABLISHED = "ESTABLISHED"
77 CONN_FIN_WAIT1 = "FIN_WAIT1"
78 CONN_FIN_WAIT2 = "FIN_WAIT2"
79 CONN_LAST_ACK = "LAST_ACK"
80 CONN_LISTEN = "LISTEN"
81 CONN_NONE = "NONE"
82 CONN_SYN_RECV = "SYN_RECV"
83 CONN_SYN_SENT = "SYN_SENT"
84 CONN_TIME_WAIT = "TIME_WAIT"
85 if WINDOWS:
86 CONN_DELETE_TCB = "DELETE_TCB"
87 if SUNOS:
88 CONN_BOUND = "CONN_BOUND"
89 CONN_IDLE = "CONN_IDLE"
90
91
92# psutil.net_if_stats()
93class NicDuplex(enum.IntEnum):
94 NIC_DUPLEX_FULL = 2
95 NIC_DUPLEX_HALF = 1
96 NIC_DUPLEX_UNKNOWN = 0
97
98
99# psutil.sensors_battery()
100class BatteryTime(enum.IntEnum):
101 POWER_TIME_UNKNOWN = -1
102 POWER_TIME_UNLIMITED = -2
103
104
105if LINUX:
106
107 # psutil.Process.ionice(ioclass=…)
108 class ProcessIOPriority(enum.IntEnum):
109 # ioprio_* constants http://linux.die.net/man/2/ioprio_get
110 IOPRIO_CLASS_NONE = 0
111 IOPRIO_CLASS_RT = 1
112 IOPRIO_CLASS_BE = 2
113 IOPRIO_CLASS_IDLE = 3
114
115
116if WINDOWS:
117
118 # psutil.Process.ionice(ioclass=…)
119 class ProcessIOPriority(enum.IntEnum):
120 IOPRIO_VERYLOW = 0
121 IOPRIO_LOW = 1
122 IOPRIO_NORMAL = 2
123 IOPRIO_HIGH = 3
124
125 # psutil.Process.nice()
126 class ProcessPriority(enum.IntEnum):
127 ABOVE_NORMAL_PRIORITY_CLASS = _psutil.ABOVE_NORMAL_PRIORITY_CLASS
128 BELOW_NORMAL_PRIORITY_CLASS = _psutil.BELOW_NORMAL_PRIORITY_CLASS
129 HIGH_PRIORITY_CLASS = _psutil.HIGH_PRIORITY_CLASS
130 IDLE_PRIORITY_CLASS = _psutil.IDLE_PRIORITY_CLASS
131 NORMAL_PRIORITY_CLASS = _psutil.NORMAL_PRIORITY_CLASS
132 REALTIME_PRIORITY_CLASS = _psutil.REALTIME_PRIORITY_CLASS
133
134
135if LINUX or FREEBSD:
136
137 # psutil.Process.rlimit()
138 ProcessRlimit = enum.IntEnum(
139 "ProcessRlimit",
140 (
141 (name, getattr(_psutil, name))
142 for name in dir(_psutil)
143 if name.startswith("RLIM") and name.isupper()
144 ),
145 )