Skip to content

Commit 1926299

Browse files
committed
add custom enums
1 parent 30d4893 commit 1926299

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

pyslurm/utils/enums.pyx

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#########################################################################
2+
# utils/enums.pyx - pyslurm enum helpers
3+
#########################################################################
4+
# Copyright (C) 2025 Toni Harzendorf <[email protected]>
5+
#
6+
# This file is part of PySlurm
7+
#
8+
# PySlurm is free software; you can redistribute it and/or modify
9+
# it under the terms of the GNU General Public License as published by
10+
# the Free Software Foundation; either version 2 of the License, or
11+
# (at your option) any later version.
12+
13+
# PySlurm is distributed in the hope that it will be useful,
14+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
# GNU General Public License for more details.
17+
#
18+
# You should have received a copy of the GNU General Public License along
19+
# with PySlurm; if not, write to the Free Software Foundation, Inc.,
20+
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21+
#
22+
# cython: c_string_type=unicode, c_string_encoding=default
23+
# cython: language_level=3
24+
25+
from enum import Enum, Flag
26+
27+
28+
class SlurmEnum(str, Enum):
29+
30+
def __new__(cls, name, *args):
31+
# https://docs.python.org/3/library/enum.html
32+
#
33+
# 1.
34+
# Second argument to str is encoding, third is error. We don't really
35+
# care for that, so no need to check.
36+
# 2.
37+
# Python Documentation recommends to not call super().__new__, but
38+
# the corresponding types __new__ directly, so str here.
39+
# 3.
40+
# Docs recommend to set _value_
41+
v = str(name)
42+
new_string = str.__new__(cls, v)
43+
new_string._value_ = v
44+
45+
new_string._flag = int(args[0]) if len(args) >= 1 else 0
46+
new_string._clear_flag = int(args[1]) if len(args) >= 2 else 0
47+
return new_string
48+
49+
def __str__(self):
50+
return str(self.value)
51+
52+
@staticmethod
53+
def _generate_next_value_(name, _start, _count, _last_values):
54+
# We just care about the name of the member to be defined.
55+
return name.upper()
56+
57+
@classmethod
58+
def from_flag(cls, flag, default):
59+
out = cls(default)
60+
for item in cls:
61+
if item._flag & flag:
62+
return item
63+
return out
64+
65+
66+
class SlurmFlag(Flag):
67+
68+
def __new__(cls, flag, *args):
69+
obj = super()._new_member_(cls)
70+
obj._value_ = int(flag)
71+
obj._clear_flag = int(args[0]) if len(args) >= 1 else 0
72+
return obj
73+
74+
@classmethod
75+
def from_list(cls, inp):
76+
out = cls(0)
77+
for flag in cls:
78+
if flag.name in inp:
79+
out |= flag
80+
81+
return out
82+
83+
def _get_flags_cleared(self):
84+
val = self.value
85+
for flag in self.__class__:
86+
if flag not in self:
87+
val |= flag._clear_flag
88+
return val

0 commit comments

Comments
 (0)