Skip to content

Commit dcf49c5

Browse files
committed
Group DEVICE_TYPE_* constants in an Enum
For other code using blivet, it is useful to know all of the device types that blivet supports. This is better accomplished by grouping all of the device type constants into an Enum type so that the consuming code can import just the Enum and then retrieve all the values it needs from there. This code makes the Enum the canonical source for that information but also recreates the variables that have existed up to now from the new Enum.
1 parent 1b0157a commit dcf49c5

File tree

10 files changed

+145
-127
lines changed

10 files changed

+145
-127
lines changed

blivet/blivet.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ def new_mdarray(self, *args, **kwargs):
542542

543543
name = kwargs.pop("name", None)
544544
if name:
545-
safe_name = self.safe_device_name(name, devicefactory.DEVICE_TYPE_MD)
545+
safe_name = self.safe_device_name(name, devicefactory.DeviceTypes.MD)
546546
if safe_name != name:
547547
log.warning("using '%s' instead of specified name '%s'",
548548
safe_name, name)
@@ -575,13 +575,13 @@ def new_vg(self, *args, **kwargs):
575575

576576
name = kwargs.pop("name", None)
577577
if name:
578-
safe_name = self.safe_device_name(name, devicefactory.DEVICE_TYPE_LVM)
578+
safe_name = self.safe_device_name(name, devicefactory.DeviceTypes.LVM)
579579
if safe_name != name:
580580
log.warning("using '%s' instead of specified name '%s'",
581581
safe_name, name)
582582
name = safe_name
583583
else:
584-
name = self.suggest_container_name(container_type=devicefactory.DEVICE_TYPE_LVM)
584+
name = self.suggest_container_name(container_type=devicefactory.DeviceTypes.LVM)
585585

586586
if name in self.names:
587587
raise ValueError("name '%s' is already in use" % name)
@@ -659,9 +659,9 @@ def new_lv(self, *args, **kwargs):
659659
name = kwargs.pop("name", None)
660660
if name:
661661
# make sure the specified name is sensible
662-
safe_vg_name = self.safe_device_name(vg.name, devicefactory.DEVICE_TYPE_LVM)
662+
safe_vg_name = self.safe_device_name(vg.name, devicefactory.DeviceTypes.LVM)
663663
full_name = "%s-%s" % (safe_vg_name, name)
664-
safe_name = self.safe_device_name(full_name, devicefactory.DEVICE_TYPE_LVM)
664+
safe_name = self.safe_device_name(full_name, devicefactory.DeviceTypes.LVM)
665665
if safe_name != full_name:
666666
new_name = safe_name[len(safe_vg_name) + 1:]
667667
log.warning("using '%s' instead of specified name '%s'",
@@ -772,7 +772,7 @@ def new_btrfs(self, *args, **kwargs):
772772
dev_class = BTRFSVolumeDevice
773773
# set up the volume label, using hostname if necessary
774774
if not name:
775-
name = self.suggest_container_name(container_type=devicefactory.DEVICE_TYPE_BTRFS)
775+
name = self.suggest_container_name(container_type=devicefactory.DeviceTypes.BTRFS)
776776
if "label" not in fmt_args:
777777
fmt_args["label"] = name
778778
fmt_args["subvolspec"] = MAIN_VOLUME_ID
@@ -823,13 +823,13 @@ def new_stratis_pool(self, *args, **kwargs):
823823

824824
name = kwargs.pop("name", None)
825825
if name:
826-
safe_name = self.safe_device_name(name, devicefactory.DEVICE_TYPE_STRATIS)
826+
safe_name = self.safe_device_name(name, devicefactory.DeviceTypes.STRATIS)
827827
if safe_name != name:
828828
log.warning("using '%s' instead of specified name '%s'",
829829
safe_name, name)
830830
name = safe_name
831831
else:
832-
name = self.suggest_container_name(container_type=devicefactory.DEVICE_TYPE_STRATIS)
832+
name = self.suggest_container_name(container_type=devicefactory.DeviceTypes.STRATIS)
833833

834834
if name in self.names:
835835
raise ValueError("name '%s' is already in use" % name)
@@ -857,7 +857,7 @@ def new_stratis_filesystem(self, *args, **kwargs):
857857
if name:
858858
# make sure the specified name is sensible
859859
full_name = "%s/%s" % (pool.name, name)
860-
safe_name = self.safe_device_name(full_name, devicefactory.DEVICE_TYPE_STRATIS)
860+
safe_name = self.safe_device_name(full_name, devicefactory.DeviceTypes.STRATIS)
861861
if safe_name != full_name:
862862
new_name = safe_name[len(pool.name) + 1:]
863863
log.warning("using '%s' instead of specified name '%s'",
@@ -866,7 +866,7 @@ def new_stratis_filesystem(self, *args, **kwargs):
866866
else:
867867
name = self.suggest_device_name(parent=pool,
868868
mountpoint=mountpoint,
869-
device_type=devicefactory.DEVICE_TYPE_STRATIS)
869+
device_type=devicefactory.DeviceTypes.STRATIS)
870870

871871
if "%s/%s" % (pool.name, name) in self.names:
872872
raise ValueError("name '%s' is already in use" % name)
@@ -1019,13 +1019,13 @@ def safe_device_name(self, name, device_type=None):
10191019
device name limits.
10201020
"""
10211021

1022-
if device_type in (devicefactory.DEVICE_TYPE_LVM, devicefactory.DEVICE_TYPE_LVM_THINP):
1022+
if device_type in (devicefactory.DeviceTypes.LVM, devicefactory.DeviceTypes.LVM_THINP):
10231023
allowed = devicelibs.lvm.safe_name_characters
1024-
elif device_type == devicefactory.DEVICE_TYPE_MD:
1024+
elif device_type == devicefactory.DeviceTypes.MD:
10251025
allowed = devicelibs.mdraid.safe_name_characters
1026-
elif device_type == devicefactory.DEVICE_TYPE_BTRFS:
1026+
elif device_type == devicefactory.DeviceTypes.BTRFS:
10271027
allowed = devicelibs.btrfs.safe_name_characters
1028-
elif device_type == devicefactory.DEVICE_TYPE_STRATIS:
1028+
elif device_type == devicefactory.DeviceTypes.STRATIS:
10291029
allowed = devicelibs.stratis.safe_name_characters
10301030
else:
10311031
allowed = "0-9a-zA-Z._-"
@@ -1053,7 +1053,7 @@ def safe_device_name(self, name, device_type=None):
10531053
def unique_device_name(self, name, parent=None, name_set=True, device_type=None):
10541054
""" Turn given name into a unique one by adding numeric suffix to it """
10551055

1056-
if device_type == devicefactory.DEVICE_TYPE_STRATIS:
1056+
if device_type == devicefactory.DeviceTypes.STRATIS:
10571057
parent_separator = "/"
10581058
else:
10591059
parent_separator = "-"
@@ -1127,7 +1127,7 @@ def suggest_device_name(self, parent=None, swap=None,
11271127

11281128
name = self.safe_device_name(prefix + body, device_type)
11291129

1130-
if device_type == devicefactory.DEVICE_TYPE_STRATIS:
1130+
if device_type == devicefactory.DeviceTypes.STRATIS:
11311131
parent_separator = "/"
11321132
else:
11331133
parent_separator = "-"
@@ -1281,11 +1281,11 @@ def get_fstype(self, mountpoint=None):
12811281

12821282
return fstype
12831283

1284-
def factory_device(self, device_type=devicefactory.DEVICE_TYPE_LVM, **kwargs):
1284+
def factory_device(self, device_type=devicefactory.DeviceTypes.LVM, **kwargs):
12851285
""" Schedule creation of a device based on a top-down specification.
12861286
12871287
:param device_type: device type constant
1288-
:type device_type: int (:const:`~.devicefactory.DEVICE_TYPE_*`)
1288+
:type device_type: int (:const:`~.devicefactory.DeviceTypes.*`)
12891289
:returns: the newly configured device
12901290
:rtype: :class:`~.devices.StorageDevice`
12911291
@@ -1305,8 +1305,8 @@ def factory_device(self, device_type=devicefactory.DEVICE_TYPE_LVM, **kwargs):
13051305
kwargs["mountpoint"] = None
13061306

13071307
if kwargs["fstype"] == "swap" and \
1308-
device_type == devicefactory.DEVICE_TYPE_BTRFS:
1309-
device_type = devicefactory.DEVICE_TYPE_PARTITION
1308+
device_type == devicefactory.DeviceTypes.BTRFS:
1309+
device_type = devicefactory.DeviceTypes.PARTITION
13101310

13111311
factory = devicefactory.get_device_factory(self, device_type=device_type, **kwargs)
13121312

blivet/dbus/blivet.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323

2424
from .. import Blivet
2525
from ..callbacks import callbacks
26-
from ..devicefactory import DEVICE_TYPE_PARTITION, DEVICE_TYPE_LVM, DEVICE_TYPE_LVM_THINP
27-
from ..devicefactory import DEVICE_TYPE_MD, DEVICE_TYPE_BTRFS
26+
from ..devicefactory import DeviceTypes
2827
from ..errors import StorageError
2928
from ..size import Size
3029
from ..util import ObjectID
@@ -79,11 +78,11 @@ def interface(self):
7978
@property
8079
def properties(self):
8180
props = {"Devices": self.ListDevices(),
82-
"DEVICE_TYPE_LVM": DEVICE_TYPE_LVM,
83-
"DEVICE_TYPE_LVM_THINP": DEVICE_TYPE_LVM_THINP,
84-
"DEVICE_TYPE_PARTITION": DEVICE_TYPE_PARTITION,
85-
"DEVICE_TYPE_MD": DEVICE_TYPE_MD,
86-
"DEVICE_TYPE_BTRFS": DEVICE_TYPE_BTRFS}
81+
"DEVICE_TYPE_LVM": DeviceTypes.LVM,
82+
"DEVICE_TYPE_LVM_THINP": DeviceTypes.LVM_THINP,
83+
"DEVICE_TYPE_PARTITION": DeviceTypes.PARTITION,
84+
"DEVICE_TYPE_MD": DeviceTypes.MD,
85+
"DEVICE_TYPE_BTRFS": DeviceTypes.BTRFS}
8786
return props
8887

8988
def _device_removed(self, device, keep=True):

blivet/devicefactory.py

Lines changed: 64 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
# Red Hat Author(s): David Lehman <[email protected]>
2121
#
2222

23+
from enum import IntEnum, unique
24+
2325
from .storage_log import log_method_call
2426
from .errors import DeviceFactoryError, StorageError
2527
from .devices import BTRFSDevice, DiskDevice
@@ -52,14 +54,36 @@
5254
SIZE_POLICY_MAX = -1
5355
SIZE_POLICY_AUTO = 0
5456

55-
DEVICE_TYPE_LVM = 0
56-
DEVICE_TYPE_MD = 1
57-
DEVICE_TYPE_PARTITION = 2
58-
DEVICE_TYPE_BTRFS = 3
59-
DEVICE_TYPE_DISK = 4
60-
DEVICE_TYPE_LVM_THINP = 5
61-
DEVICE_TYPE_LVM_VDO = 6
62-
DEVICE_TYPE_STRATIS = 7
57+
58+
@unique
59+
class DeviceTypes(IntEnum):
60+
"""
61+
Types of devices supported by `blivet.devicefactory`
62+
63+
.. note::
64+
Subclasses IntEnum so that code which wants to work with blivet can create their own
65+
Enum based on this and have the members be interoperable. (The members of IntEnums
66+
compare based on the interger value of the member. The members of normal Enums compare
67+
based on the identity of the member so they are never equal to members of other Enums,
68+
even when their values are the same. The members of IntEnums compare based on the
69+
integer `value` of the member so they can compare True with the member of a different
70+
Enum's member when they have the same value.
71+
"""
72+
73+
LVM = 0
74+
MD = 1
75+
PARTITION = 2
76+
BTRFS = 3
77+
DISK = 4
78+
LVM_THINP = 5
79+
LVM_VDO = 6
80+
STRATIS = 7
81+
82+
83+
# Backwards compat. Device types could only be accessed via these individual
84+
# variable names through 3.12.1
85+
for dt_pair in DeviceTypes:
86+
globals()['DEVICE_TYPE_%s' % dt_pair.name] = dt_pair.value
6387

6488

6589
def is_supported_device_type(device_type):
@@ -72,19 +96,19 @@ def is_supported_device_type(device_type):
7296
:rtype: bool
7397
"""
7498
devices = []
75-
if device_type == DEVICE_TYPE_BTRFS:
99+
if device_type == DeviceTypes.BTRFS:
76100
devices = [BTRFSDevice]
77-
elif device_type == DEVICE_TYPE_DISK:
101+
elif device_type == DeviceTypes.DISK:
78102
devices = [DiskDevice]
79-
elif device_type in (DEVICE_TYPE_LVM, DEVICE_TYPE_LVM_THINP):
103+
elif device_type in (DeviceTypes.LVM, DeviceTypes.LVM_THINP):
80104
devices = [LVMLogicalVolumeDevice]
81-
elif device_type == DEVICE_TYPE_PARTITION:
105+
elif device_type == DeviceTypes.PARTITION:
82106
devices = [PartitionDevice]
83-
elif device_type == DEVICE_TYPE_MD:
107+
elif device_type == DeviceTypes.MD:
84108
devices = [MDRaidArrayDevice]
85-
elif device_type == DEVICE_TYPE_LVM_VDO:
109+
elif device_type == DeviceTypes.LVM_VDO:
86110
devices = [LVMLogicalVolumeDevice, LVMVDOPoolMixin, LVMVDOLogicalVolumeMixin]
87-
elif device_type == DEVICE_TYPE_STRATIS:
111+
elif device_type == DeviceTypes.STRATIS:
88112
devices = [StratisFilesystemDevice, StratisPoolDevice]
89113

90114
return not any(c.unavailable_type_dependencies() for c in devices)
@@ -100,11 +124,11 @@ def get_supported_raid_levels(device_type):
100124
:rtype: set of :class:`~.devicelibs.raid.RAIDLevel`
101125
"""
102126
pkg = None
103-
if device_type == DEVICE_TYPE_BTRFS:
127+
if device_type == DeviceTypes.BTRFS:
104128
pkg = btrfs
105-
elif device_type in (DEVICE_TYPE_LVM, DEVICE_TYPE_LVM_THINP, DEVICE_TYPE_LVM_VDO):
129+
elif device_type in (DeviceTypes.LVM, DeviceTypes.LVM_THINP, DeviceTypes.LVM_VDO):
106130
pkg = lvm
107-
elif device_type == DEVICE_TYPE_MD:
131+
elif device_type == DeviceTypes.MD:
108132
pkg = mdraid
109133

110134
if pkg and all(d.available for d in pkg.EXTERNAL_DEPENDENCIES):
@@ -118,36 +142,36 @@ def get_device_type(device):
118142
# an empty pool after removing the last thin lv, so the only thing we'll be
119143
# doing with the factory is adjusting the vg to account for the pool's
120144
# removal
121-
device_types = {"partition": DEVICE_TYPE_PARTITION,
122-
"lvmlv": DEVICE_TYPE_LVM,
123-
"lvmthinlv": DEVICE_TYPE_LVM_THINP,
124-
"lvmthinpool": DEVICE_TYPE_LVM,
125-
"lvmvdolv": DEVICE_TYPE_LVM_VDO,
126-
"lvmvdopool": DEVICE_TYPE_LVM,
127-
"btrfs subvolume": DEVICE_TYPE_BTRFS,
128-
"btrfs volume": DEVICE_TYPE_BTRFS,
129-
"mdarray": DEVICE_TYPE_MD,
130-
"stratis filesystem": DEVICE_TYPE_STRATIS}
145+
device_types = {"partition": DeviceTypes.PARTITION,
146+
"lvmlv": DeviceTypes.LVM,
147+
"lvmthinlv": DeviceTypes.LVM_THINP,
148+
"lvmthinpool": DeviceTypes.LVM,
149+
"lvmvdolv": DeviceTypes.LVM_VDO,
150+
"lvmvdopool": DeviceTypes.LVM,
151+
"btrfs subvolume": DeviceTypes.BTRFS,
152+
"btrfs volume": DeviceTypes.BTRFS,
153+
"mdarray": DeviceTypes.MD,
154+
"stratis filesystem": DeviceTypes.STRATIS}
131155

132156
use_dev = device.raw_device
133157
if use_dev.is_disk:
134-
device_type = DEVICE_TYPE_DISK
158+
device_type = DeviceTypes.DISK
135159
else:
136160
device_type = device_types.get(use_dev.type)
137161

138162
return device_type
139163

140164

141-
def get_device_factory(blivet, device_type=DEVICE_TYPE_LVM, **kwargs):
165+
def get_device_factory(blivet, device_type=DeviceTypes.LVM, **kwargs):
142166
""" Return a suitable DeviceFactory instance for device_type. """
143-
class_table = {DEVICE_TYPE_LVM: LVMFactory,
144-
DEVICE_TYPE_BTRFS: BTRFSFactory,
145-
DEVICE_TYPE_PARTITION: PartitionFactory,
146-
DEVICE_TYPE_MD: MDFactory,
147-
DEVICE_TYPE_LVM_THINP: LVMThinPFactory,
148-
DEVICE_TYPE_LVM_VDO: LVMVDOFactory,
149-
DEVICE_TYPE_DISK: DeviceFactory,
150-
DEVICE_TYPE_STRATIS: StratisFactory}
167+
class_table = {DeviceTypes.LVM: LVMFactory,
168+
DeviceTypes.BTRFS: BTRFSFactory,
169+
DeviceTypes.PARTITION: PartitionFactory,
170+
DeviceTypes.MD: MDFactory,
171+
DeviceTypes.LVM_THINP: LVMThinPFactory,
172+
DeviceTypes.LVM_VDO: LVMVDOFactory,
173+
DeviceTypes.DISK: DeviceFactory,
174+
DeviceTypes.STRATIS: StratisFactory}
151175

152176
factory_class = class_table[device_type]
153177
log.debug("instantiating %s: %s, %s, %s", factory_class,
@@ -1526,7 +1550,7 @@ def _set_name(self):
15261550
mountpoint=self.mountpoint)
15271551

15281552
lvname = "%s-%s" % (self.vg.name, self.device_name)
1529-
safe_new_name = self.storage.safe_device_name(lvname, DEVICE_TYPE_LVM)
1553+
safe_new_name = self.storage.safe_device_name(lvname, DeviceTypes.LVM)
15301554
if self.device.name != safe_new_name:
15311555
if safe_new_name in self.storage.names:
15321556
log.error("not renaming '%s' to in-use name '%s'",
@@ -2224,7 +2248,7 @@ def _set_name(self):
22242248
mountpoint=self.mountpoint)
22252249

22262250
fsname = "%s/%s" % (self.pool.name, self.device_name)
2227-
safe_new_name = self.storage.safe_device_name(fsname, DEVICE_TYPE_STRATIS)
2251+
safe_new_name = self.storage.safe_device_name(fsname, DeviceTypes.STRATIS)
22282252
if self.device.name != safe_new_name:
22292253
if safe_new_name in self.storage.names:
22302254
log.error("not renaming '%s' to in-use name '%s'",

doc/api/blivet.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,13 @@ blivet
8585

8686
* :mod:`blivet.devicefactory`
8787
* :meth:`~blivet.devicefactory.DeviceFactory.configure`
88-
* :const:`~blivet.devicefactory.DEVICE_TYPE_MD`
89-
* :const:`~blivet.devicefactory.DEVICE_TYPE_PARTITION`
90-
* :const:`~blivet.devicefactory.DEVICE_TYPE_BTRFS`
91-
* :const:`~blivet.devicefactory.DEVICE_TYPE_DISK`
92-
* :const:`~blivet.devicefactory.DEVICE_TYPE_LVM_THINP`
93-
* :const:`~blivet.devicefactory.DEVICE_TYPE_LVM_VDO`
94-
* :const:`~blivet.devicefactory.DEVICE_TYPE_STRATIS`
88+
* :const:`~blivet.devicefactory.DeviceTypes.MD`
89+
* :const:`~blivet.devicefactory.DeviceTypes.PARTITION`
90+
* :const:`~blivet.devicefactory.DeviceTypes.BTRFS`
91+
* :const:`~blivet.devicefactory.DeviceTypes.DISK`
92+
* :const:`~blivet.devicefactory.DeviceTypes.LVM_THINP`
93+
* :const:`~blivet.devicefactory.DeviceTypes.LVM_VDO`
94+
* :const:`~blivet.devicefactory.DeviceTypes.STRATIS`
9595
* :func:`~blivet.devicefactory.is_supported_device_type`
9696
* :func:`~blivet.devicefactory.get_device_factory`
9797
* :func:`~blivet.devicefactory.get_device_type`

0 commit comments

Comments
 (0)