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:
30 from . import _psutil_windows as cext
31elif LINUX:
32 from . import _psutil_linux as cext
33elif FREEBSD:
34 from . import _psutil_bsd as cext
35else:
36 cext = None
37
38if hasattr(enum, "StrEnum"): # Python >= 3.11
39 StrEnum = enum.StrEnum
40else:
41
42 # A backport of Python 3.11 StrEnum class for >= Python 3.8
43 class StrEnum(str, enum.Enum):
44 def __new__(cls, *values):
45 value = str(*values)
46 member = str.__new__(cls, value)
47 member._value_ = value
48 return member
49
50 __str__ = str.__str__
51
52 @staticmethod
53 def _generate_next_value_(name, _start, _count, _last_values):
54 return name.lower()
55
56
57# psutil.Process.status()
58class ProcessStatus(StrEnum):
59 STATUS_DEAD = "dead"
60 STATUS_DISK_SLEEP = "disk-sleep"
61 STATUS_IDLE = "idle" # Linux, macOS, FreeBSD
62 STATUS_LOCKED = "locked" # FreeBSD
63 STATUS_PARKED = "parked" # Linux
64 STATUS_RUNNING = "running"
65 STATUS_SLEEPING = "sleeping"
66 STATUS_STOPPED = "stopped"
67 STATUS_SUSPENDED = "suspended" # NetBSD
68 STATUS_TRACING_STOP = "tracing-stop"
69 STATUS_WAITING = "waiting" # FreeBSD
70 STATUS_WAKE_KILL = "wake-kill"
71 STATUS_WAKING = "waking"
72 STATUS_ZOMBIE = "zombie"
73
74
75# psutil.Process.net_connections() and psutil.net_connections()
76class ConnectionStatus(StrEnum):
77 CONN_CLOSE = "CLOSE"
78 CONN_CLOSE_WAIT = "CLOSE_WAIT"
79 CONN_CLOSING = "CLOSING"
80 CONN_ESTABLISHED = "ESTABLISHED"
81 CONN_FIN_WAIT1 = "FIN_WAIT1"
82 CONN_FIN_WAIT2 = "FIN_WAIT2"
83 CONN_LAST_ACK = "LAST_ACK"
84 CONN_LISTEN = "LISTEN"
85 CONN_NONE = "NONE"
86 CONN_SYN_RECV = "SYN_RECV"
87 CONN_SYN_SENT = "SYN_SENT"
88 CONN_TIME_WAIT = "TIME_WAIT"
89 if WINDOWS:
90 CONN_DELETE_TCB = "DELETE_TCB"
91 if SUNOS:
92 CONN_BOUND = "CONN_BOUND"
93 CONN_IDLE = "CONN_IDLE"
94
95
96# psutil.net_if_stats()
97class NicDuplex(enum.IntEnum):
98 NIC_DUPLEX_FULL = 2
99 NIC_DUPLEX_HALF = 1
100 NIC_DUPLEX_UNKNOWN = 0
101
102
103# psutil.sensors_battery()
104class BatteryTime(enum.IntEnum):
105 POWER_TIME_UNKNOWN = -1
106 POWER_TIME_UNLIMITED = -2
107
108
109if LINUX:
110
111 # psutil.Process.ionice(ioclass=…)
112 class ProcessIOPriority(enum.IntEnum):
113 # ioprio_* constants http://linux.die.net/man/2/ioprio_get
114 IOPRIO_CLASS_NONE = 0
115 IOPRIO_CLASS_RT = 1
116 IOPRIO_CLASS_BE = 2
117 IOPRIO_CLASS_IDLE = 3
118
119
120if WINDOWS:
121
122 # psutil.Process.ionice(ioclass=…)
123 class ProcessIOPriority(enum.IntEnum):
124 IOPRIO_VERYLOW = 0
125 IOPRIO_LOW = 1
126 IOPRIO_NORMAL = 2
127 IOPRIO_HIGH = 3
128
129 # psutil.Process.nice()
130 class ProcessPriority(enum.IntEnum):
131 ABOVE_NORMAL_PRIORITY_CLASS = cext.ABOVE_NORMAL_PRIORITY_CLASS
132 BELOW_NORMAL_PRIORITY_CLASS = cext.BELOW_NORMAL_PRIORITY_CLASS
133 HIGH_PRIORITY_CLASS = cext.HIGH_PRIORITY_CLASS
134 IDLE_PRIORITY_CLASS = cext.IDLE_PRIORITY_CLASS
135 NORMAL_PRIORITY_CLASS = cext.NORMAL_PRIORITY_CLASS
136 REALTIME_PRIORITY_CLASS = cext.REALTIME_PRIORITY_CLASS
137
138
139if LINUX or FREEBSD:
140
141 # psutil.Process.rlimit()
142 ProcessRlimit = enum.IntEnum(
143 "ProcessRlimit",
144 (
145 (name, getattr(cext, name))
146 for name in dir(cext)
147 if name.startswith("RLIM") and name.isupper()
148 ),
149 )