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, OpenBSD, NetBSD, SunOS, AIX
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_UNKNOWN = "unknown"
66 STATUS_WAITING = "waiting" # FreeBSD, SunOS
67 STATUS_WAKE_KILL = "wake-kill" # never reported; undocumented
68 STATUS_WAKING = "waking" # OpenBSD, NetBSD
69 STATUS_ZOMBIE = "zombie"
70
71
72# psutil.Process.net_connections() and psutil.net_connections()
73class ConnectionStatus(StrEnum):
74 CONN_CLOSE = "CLOSE"
75 CONN_CLOSE_WAIT = "CLOSE_WAIT"
76 CONN_CLOSING = "CLOSING"
77 CONN_ESTABLISHED = "ESTABLISHED"
78 CONN_FIN_WAIT1 = "FIN_WAIT1"
79 CONN_FIN_WAIT2 = "FIN_WAIT2"
80 CONN_LAST_ACK = "LAST_ACK"
81 CONN_LISTEN = "LISTEN"
82 CONN_NONE = "NONE"
83 CONN_SYN_RECV = "SYN_RECV"
84 CONN_SYN_SENT = "SYN_SENT"
85 CONN_TIME_WAIT = "TIME_WAIT"
86 if WINDOWS:
87 CONN_DELETE_TCB = "DELETE_TCB"
88 if SUNOS:
89 CONN_BOUND = "BOUND"
90 CONN_IDLE = "IDLE"
91
92
93# psutil.net_if_stats()
94class NicDuplex(enum.IntEnum):
95 NIC_DUPLEX_FULL = 2
96 NIC_DUPLEX_HALF = 1
97 NIC_DUPLEX_UNKNOWN = 0
98
99
100# psutil.sensors_battery()
101class BatteryTime(enum.IntEnum):
102 POWER_TIME_UNKNOWN = -1
103 POWER_TIME_UNLIMITED = -2
104
105
106if LINUX:
107
108 # psutil.Process.ionice(ioclass=…)
109 class ProcessIOPriority(enum.IntEnum):
110 # ioprio_* constants http://linux.die.net/man/2/ioprio_get
111 IOPRIO_CLASS_NONE = 0
112 IOPRIO_CLASS_RT = 1
113 IOPRIO_CLASS_BE = 2
114 IOPRIO_CLASS_IDLE = 3
115
116
117if WINDOWS:
118
119 # psutil.Process.ionice(ioclass=…)
120 class ProcessIOPriority(enum.IntEnum):
121 IOPRIO_VERYLOW = 0
122 IOPRIO_LOW = 1
123 IOPRIO_NORMAL = 2
124 IOPRIO_HIGH = 3
125
126 # psutil.Process.nice()
127 class ProcessPriority(enum.IntEnum):
128 ABOVE_NORMAL_PRIORITY_CLASS = _psutil.ABOVE_NORMAL_PRIORITY_CLASS
129 BELOW_NORMAL_PRIORITY_CLASS = _psutil.BELOW_NORMAL_PRIORITY_CLASS
130 HIGH_PRIORITY_CLASS = _psutil.HIGH_PRIORITY_CLASS
131 IDLE_PRIORITY_CLASS = _psutil.IDLE_PRIORITY_CLASS
132 NORMAL_PRIORITY_CLASS = _psutil.NORMAL_PRIORITY_CLASS
133 REALTIME_PRIORITY_CLASS = _psutil.REALTIME_PRIORITY_CLASS
134
135
136if LINUX or FREEBSD:
137
138 # psutil.Process.rlimit()
139 ProcessRlimit = enum.IntEnum(
140 "ProcessRlimit",
141 (
142 (name, getattr(_psutil, name))
143 for name in dir(_psutil)
144 if name.startswith("RLIMIT_") and name.isupper()
145 ),
146 )