Skip to content

Commit

Permalink
Use new style Python property syntax for read-only properties.
Browse files Browse the repository at this point in the history
Use @Property methods for read-only properties to increase
readability in the code.
  • Loading branch information
dcantrell committed Mar 16, 2009
1 parent 785972d commit 2fc0ee2
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 59 deletions.
57 changes: 37 additions & 20 deletions src/parted/disk.py
Expand Up @@ -78,8 +78,18 @@ def __ne__(self, other):

return self.device != other.device or not self._hasSameParts(other)

def __readOnly(self, property):
raise parted.ReadOnlyProperty, property
def __str__(self):
s = ("parted.Disk instance --\n"
" type: %(type)s primaryPartitionCount: %(primaryCount)s\n"
" lastPartitionNumber: %(last)s maxPrimaryPartitionCount: %(max)s\n"
" partitions: %(partitions)s\n"
" device: %(device)r\n"
" PedDisk: %(ped)r" %
{"type": self.type, "primaryCount": self.primaryPartitionCount,
"last": self.lastPartitionNumber, "max": self.maxPrimaryPartitionCount,
"partitions": self.partitions, "device": self.device,
"ped": self.__disk})
return s

def __getPartitions(self):
"""Construct a list of partitions on the disk. This is called only as
Expand All @@ -100,25 +110,32 @@ def __getPartitions(self):

return partitions

type = property(lambda s: s.__disk.type.name, lambda s, v: setattr(s.__disk, "type", parted.diskType[v]))
primaryPartitionCount = property(lambda s: s.__disk.get_primary_partition_count(), lambda s, v: s.__readOnly("primaryPartitionCount"))
lastPartitionNumber = property(lambda s: s.__disk.get_last_partition_num(), lambda s, v: s.__readOnly("lastPartitionNumber"))
maxPrimaryPartitionCount = property(lambda s: s.__disk.get_max_primary_partition_count(), lambda s, v: s.__readOnly("maxPrimaryPartitionCount"))
partitions = property(lambda s: s._partitions, lambda s, v: s.__readOnly("partitions"))
device = property(lambda s: s._device, lambda s, v: s.__readOnly("device"))
@property
def primaryPartitionCount(self):
"""The number of primary partitions on this disk."""
return self.__disk.get_primary_partition_count()

def __str__(self):
s = ("parted.Disk instance --\n"
" type: %(type)s primaryPartitionCount: %(primaryCount)s\n"
" lastPartitionNumber: %(last)s maxPrimaryPartitionCount: %(max)s\n"
" partitions: %(partitions)s\n"
" device: %(device)r\n"
" PedDisk: %(ped)r" %
{"type": self.type, "primaryCount": self.primaryPartitionCount,
"last": self.lastPartitionNumber, "max": self.maxPrimaryPartitionCount,
"partitions": self.partitions, "device": self.device,
"ped": self.__disk})
return s
@property
def lastPartitionNumber(self):
"""The last assigned partition number currently on this disk."""
return self.__disk.get_last_partition_num()

@property
def maxPrimaryPartitionCount(self):
"""The maximum number of primary partitions allowed on this disk."""
return self.__disk.get_max_primary_partition_count()

@property
def partitions(self):
"""The list of partitions currently on this disk."""
return self._partitions

@property
def device(self):
"""The underlying Device holding this disk and partitions."""
return s._device

type = property(lambda s: s.__disk.type.name, lambda s, v: setattr(s.__disk, "type", parted.diskType[v]))

def duplicate(self):
"""Make a deep copy of this Disk."""
Expand Down
22 changes: 15 additions & 7 deletions src/parted/filesystem.py
Expand Up @@ -53,9 +53,6 @@ def __init__(self, type=None, geometry=None, checked=False, PedFileSystem=None):
else:
self._checked = False

def __readOnly(self, property):
raise parted.ReadOnlyProperty, property

def __eq__(self, other):
return not self.__ne__(other)

Expand All @@ -68,10 +65,6 @@ def __ne__(self, other):

return self.type != other.type or self.geometry != other.geometry

type = property(lambda s: s._type, lambda s, v: s.__readOnly("type"))
geometry = property(lambda s: s._geometry, lambda s, v: s.__readOnly("geometry"))
checked = property(lambda s: s._checked, lambda s, v: s.__readOnly("checked"))

def __str__(self):
s = ("parted.FileSystem instance --\n"
" type: %(type)s geometry: %(geometry)r checked: %(checked)s\n"
Expand All @@ -80,6 +73,21 @@ def __str__(self):
"checked": self.checked, "ped": self.__fileSystem})
return s

@property
def type(self):
"""The type of this filesystem, e.g. ext3."""
return self._type

@property
def geometry(self):
"""The Geometry object describing this filesystem."""
return self._geometry

@property
def checked(self):
"""True if this filesystem has been checked, False otherwise."""
return bool(self._checked)

def clobber(self):
return self.__fileSystem.clobber()

Expand Down
17 changes: 9 additions & 8 deletions src/parted/geometry.py
Expand Up @@ -69,14 +69,6 @@ def __ne__(self, other):

return self.device != other.device or self.start != other.start or self.length != other.length

def __readOnly(self, property):
raise parted.ReadOnlyProperty, property

start = property(lambda s: s.__geometry.start, lambda s, v: s.__geometry.set_start(v))
end = property(lambda s: s.__geometry.end, lambda s, v: s.__geometry.set_end(v))
length = property(lambda s: s.__geometry.length, lambda s, v: s.__geometry.set(s.__geometry.start, v))
device = property(lambda s: s._device, lambda s, v: s.__readOnly("device"))

def __str__(self):
s = ("parted.Geometry instance --\n"
" start: %(start)s end: %(end)s length: %(length)s\n"
Expand All @@ -85,6 +77,15 @@ def __str__(self):
"device": self.device, "ped": self.__geometry})
return s

@property
def device(self):
"""The Device this geometry describes."""
return self._device

start = property(lambda s: s.__geometry.start, lambda s, v: s.__geometry.set_start(v))
end = property(lambda s: s.__geometry.end, lambda s, v: s.__geometry.set_end(v))
length = property(lambda s: s.__geometry.length, lambda s, v: s.__geometry.set(s.__geometry.start, v))

def check(self, offset, granularity, count, timer=None):
"""Check the region described by self for errors on the disk.
offset -- The beginning of the region to check, in sectors from the
Expand Down
58 changes: 34 additions & 24 deletions src/parted/partition.py
Expand Up @@ -73,30 +73,6 @@ def __ne__(self, other):

return self.path != other.path or self.type != other.type or self.geometry != other.geometry or self.fileSystem != other.fileSystem

def __readOnly(self, property):
raise parted.ReadOnlyProperty, property

def __writeOnly(self, property):
raise parted.WriteOnlyProperty, property

active = property(lambda s: s.__partition.is_active(), lambda s, v: s.__readOnly("active"))
busy = property(lambda s: s.__partition.is_busy(), lambda s, v: s.__readOnly("busy"))

#
# XXX: disk, fileSystem, and geometry need to set the property in
# s.__partition as writing to the properties that we have here...maybe?
# figure this out.
#
disk = property(lambda s: s._disk, lambda s, v: s.__readOnly("disk"))
fileSystem = property(lambda s: s._fileSystem, lambda s, v: setattr(s, "_fileSystem", v))
geometry = property(lambda s: s._geometry, lambda s, v: setattr(s, "_geometry", v))

number = property(lambda s: s.__partition.num, lambda s, v: setattr(s.__partition, "num", v))
path = property(lambda s: s.__partition.get_path(), lambda s, v: s.__readOnly("path"))
system = property(lambda s: s.__writeOnly("system"), lambda s, v: s.__partition.set_system(v))
type = property(lambda s: s.__partition.type, lambda s, v: setattr(s.__partition, "type", v))
name = property(lambda s: s.__partition.get_name(), lambda s, v: s.__readOnly("name"))

def __str__(self):
try:
name = self.name
Expand All @@ -114,6 +90,40 @@ def __str__(self):
"busy": self.busy, "ped": self.__partition})
return s

def __writeOnly(self, property):
raise parted.WriteOnlyProperty, property

@property
def active(self):
"""True if the partition is active, False otherwise."""
return bool(self.__partition.is_active())

@property
def busy(self):
"""True if the partition is active, False otherwise."""
return bool(self.__partition.is_busy())

@property
def disk(self):
"""The Disk this partition belongs to."""
return self._disk

@property
def path(self):
"""The filesystem path to this partition's device node."""
return self.__partition.get_path()

@property
def name(self):
"""The name of this partition."""
return self.__partition.get_name()

fileSystem = property(lambda s: s._fileSystem, lambda s, v: setattr(s, "_fileSystem", v))
geometry = property(lambda s: s._geometry, lambda s, v: setattr(s, "_geometry", v))
number = property(lambda s: s.__partition.num, lambda s, v: setattr(s.__partition, "num", v))
system = property(lambda s: s.__writeOnly("system"), lambda s, v: s.__partition.set_system(v))
type = property(lambda s: s.__partition.type, lambda s, v: setattr(s.__partition, "type", v))

def getFlag(self, flag):
"""Get the value of a particular flag on the partition. Valid flags
are the _ped.PARTITION_* constants. See _ped.flag_get_name() and
Expand Down

0 comments on commit 2fc0ee2

Please sign in to comment.