Linux Archive

Linux Archive (http://www.linux-archive.org/)
-   Debian Java (http://www.linux-archive.org/debian-java/)
-   -   Convert Device, DeviceFormat __str__ to __repr__ and add __str__. (http://www.linux-archive.org/debian-java/527106-convert-device-deviceformat-__str__-__repr__-add-__str__.html)

David Lehman 05-16-2011 10:36 PM

Convert Device, DeviceFormat __str__ to __repr__ and add __str__.
 
The __repr__ methods are the verbose/detailed version, while __str__
is a brief description sufficient for most purposes.
---
pyanaconda/storage/devices.py | 80 ++++++++++++++++++------------
pyanaconda/storage/formats/__init__.py | 16 ++++++-
pyanaconda/storage/formats/disklabel.py | 12 +++--
pyanaconda/storage/formats/dmraid.py | 4 +-
pyanaconda/storage/formats/fs.py | 11 ++++-
pyanaconda/storage/formats/luks.py | 4 +-
pyanaconda/storage/formats/lvmpv.py | 4 +-
pyanaconda/storage/formats/mdraid.py | 4 +-
pyanaconda/storage/formats/multipath.py | 4 +-
pyanaconda/storage/formats/swap.py | 4 +-
pyanaconda/storage/partitioning.py | 7 ++-
11 files changed, 98 insertions(+), 52 deletions(-)

diff --git a/pyanaconda/storage/devices.py b/pyanaconda/storage/devices.py
index 51f2a13..a90df06 100644
--- a/pyanaconda/storage/devices.py
+++ b/pyanaconda/storage/devices.py
@@ -96,7 +96,7 @@
import os
import math
import copy
-import time
+import pprint

# device backend modules
from devicelibs import mdraid
@@ -199,7 +199,7 @@ class Device(object):
# This is a counter for generating unique ids for Devices.
_id = 0

- _type = "generic device"
+ _type = "device"
_packages = []
_services = []

@@ -250,15 +250,26 @@ class Device(object):

return new

- def __str__(self):
+ def __repr__(self):
s = ("%(type)s instance (%(id)s) --
"
" name = %(name)s status = %(status)s"
- " parents = %(parents)s
"
- " kids = %(kids)s
"
- " id = %(dev_id)s
" %
+ " kids = %(kids)s id = %(dev_id)s
"
+ " parents = %(parents)s
" %
{"type": self.__class__.__name__, "id": "%#x" % id(self),
- "name": self.name, "parents": self.parents, "kids": self.kids,
- "status": self.status, "dev_id": self.id})
+ "name": self.name, "kids": self.kids, "status": self.status,
+ "dev_id": self.id,
+ "parents": pprint.pformat([str(p) for p in self.parents])})
+ return s
+
+ def __str__(self):
+ exist = "existing"
+ if not self.exists:
+ exist = "non-existent"
+ s = "%s %dMB %s %s (%d)" % (exist, self.size, self.type, self.name,
+ self.id)
+ if self.format.type:
+ s += " with %s" % self.format
+
return s

@property
@@ -419,7 +430,7 @@ class StorageDevice(Device):
represented by an FS instance. A StorageDevice's create method
should create a filesystem if one has been specified.
"""
- _type = "storage device"
+ _type = "storage"
_devDir = "/dev"
sysfsBlockDir = "class/block"
_resizable = False
@@ -567,18 +578,19 @@ class StorageDevice(Device):
lambda s, v: s._setTargetSize(v),
doc="Target size of this device")

- def __str__(self):
- s = Device.__str__(self)
- s += (" uuid = %(uuid)s format = %(format)r size = %(size)s
"
- " major = %(major)s minor = %(minor)r exists = %(exists)s
"
- " sysfs path = %(sysfs)s partedDevice = %(partedDevice)r
"
+ def __repr__(self):
+ s = Device.__repr__(self)
+ s += (" uuid = %(uuid)s size = %(size)s
"
+ " format = %(format)s
"
+ " major = %(major)s minor = %(minor)s exists = %(exists)s
"
+ " sysfs path = %(sysfs)s partedDevice = %(partedDevice)s
"
" target size = %(targetSize)s path = %(path)s
"
" format args = %(formatArgs)s originalFormat = %(origFmt)s" %
{"uuid": self.uuid, "format": self.format, "size": self.size,
"major": self.major, "minor": self.minor, "exists": self.exists,
"sysfs": self.sysfsPath, "partedDevice": self.partedDevice,
"targetSize": self.targetSize, "path": self.path,
- "formatArgs": self.formatArgs, "origFmt": self.originalFormat})
+ "formatArgs": self.formatArgs, "origFmt": self.originalFormat.type})
return s

@property
@@ -1015,8 +1027,8 @@ class DiskDevice(StorageDevice):
serial=serial, model=model,
vendor=vendor, bus=bus)

- def __str__(self):
- s = StorageDevice.__str__(self)
+ def __repr__(self):
+ s = StorageDevice.__repr__(self)
s += (" removable = %(removable)s partedDevice = %(partedDevice)r" %
{"removable": self.removable, "partedDevice": self.partedDevice})
return s
@@ -1171,11 +1183,12 @@ class PartitionDevice(StorageDevice):

self.req_base_weight = weight

- def __str__(self):
- s = StorageDevice.__str__(self)
+ def __repr__(self):
+ s = StorageDevice.__repr__(self)
s += (" grow = %(grow)s max size = %(maxsize)s bootable = %(bootable)s
"
" part type = %(partType)s primary = %(primary)s
"
- " partedPartition = %(partedPart)r disk = %(disk)r
" %
+ " partedPartition = %(partedPart)s
"
+ " disk = %(disk)s
" %
{"grow": self.req_grow, "maxsize": self.req_max_size,
"bootable": self.bootable, "partType": self.partType,
"primary": self.req_primary,
@@ -1690,8 +1703,8 @@ class DMDevice(StorageDevice):
self.target = target
self.dmUuid = dmUuid

- def __str__(self):
- s = StorageDevice.__str__(self)
+ def __repr__(self):
+ s = StorageDevice.__repr__(self)
s += (" target = %(target)s dmUuid = %(dmUuid)s" %
{"target": self.target, "dmUuid": self.dmUuid})
return s
@@ -2006,8 +2019,8 @@ class LVMVolumeGroupDevice(DMDevice):
# They still occupy space in the VG.
self.voriginSnapshots = {}

- def __str__(self):
- s = DMDevice.__str__(self)
+ def __repr__(self):
+ s = DMDevice.__repr__(self)
s += (" free = %(free)s PE Size = %(peSize)s PE Count = %(peCount)s
"
" PE Free = %(peFree)s PV Count = %(pvCount)s
"
" LV Names = %(lv_names)s modified = %(modified)s
"
@@ -2019,7 +2032,9 @@ class LVMVolumeGroupDevice(DMDevice):
"peFree": self.peFree, "pvCount": self.pvCount,
"lv_names": self.lv_names, "modified": self.isModified,
"extents": self.extents, "freeSpace": self.freeSpace,
- "freeExtents": self.freeExtents, "pvs": self.pvs, "lvs": self.lvs})
+ "freeExtents": self.freeExtents,
+ "pvs": pprint.pformat([str(p) for p in self.pvs]),
+ "lvs": pprint.pformat([str(l) for l in self.lvs])})
return s

@property
@@ -2423,9 +2438,10 @@ class LVMLogicalVolumeDevice(DMDevice):
# here we go with the circular references
self.vg._addLogVol(self)

- def __str__(self):
- s = DMDevice.__str__(self)
- s += (" VG device = %(vgdev)r percent = %(percent)s
"
+ def __repr__(self):
+ s = DMDevice.__repr__(self)
+ s += (" VG device = %(vgdev)r
"
+ " percent = %(percent)s
"
" mirrored = %(mirrored)s stripes = %(stripes)d"
" snapshot total = %(snapshots)dMB
"
" VG space used = %(vgspace)dMB" %
@@ -2766,8 +2782,8 @@ class MDRaidArrayDevice(StorageDevice):
else:
return "MDRAID set (%s)" % levelstr

- def __str__(self):
- s = StorageDevice.__str__(self)
+ def __repr__(self):
+ s = StorageDevice.__repr__(self)
s += (" level = %(level)s spares = %(spares)s
"
" members = %(memberDevices)s
"
" total devices = %(totalDevices)s" %
@@ -3703,8 +3719,8 @@ class ZFCPDiskDevice(DiskDevice):
self.fcp_lun = kwargs.pop("fcp_lun")
DiskDevice.__init__(self, device, **kwargs)

- def __str__(self):
- s = DiskDevice.__str__(self)
+ def __repr__(self):
+ s = DiskDevice.__repr__(self)
s += (" hba_id = %(hba_id)s wwpn = %(wwpn)s fcp_lun = %(fcp_lun)s" %
{"hba_id": self.hba_id,
"wwpn": self.wwpn,
diff --git a/pyanaconda/storage/formats/__init__.py b/pyanaconda/storage/formats/__init__.py
index c0b094f..463ce20 100644
--- a/pyanaconda/storage/formats/__init__.py
+++ b/pyanaconda/storage/formats/__init__.py
@@ -180,7 +180,7 @@ class DeviceFormat(object):
#if self.__class__ is DeviceFormat:
# self.exists = True

- def __str__(self):
+ def __repr__(self):
s = ("%(classname)s instance (%(id)s) --
"
" type = %(type)s name = %(name)s status = %(status)s
"
" device = %(device)s uuid = %(uuid)s exists = %(exists)s
"
@@ -194,6 +194,20 @@ class DeviceFormat(object):
return s

@property
+ def _existence_str(self):
+ exist = "existing"
+ if not self.exists:
+ exist = "non-existent"
+ return exist
+
+ @property
+ def desc(self):
+ return str(self.type)
+
+ def __str__(self):
+ return "%s %s" % (self._existence_str, self.desc)
+
+ @property
def dict(self):
d = {"type": self.type, "name": self.name, "device": self.device,
"uuid": self.uuid, "exists": self.exists,
diff --git a/pyanaconda/storage/formats/disklabel.py b/pyanaconda/storage/formats/disklabel.py
index 49bdfe1..734f13e 100644
--- a/pyanaconda/storage/formats/disklabel.py
+++ b/pyanaconda/storage/formats/disklabel.py
@@ -91,14 +91,14 @@ class DiskLabel(DeviceFormat):

return new

- def __str__(self):
- s = DeviceFormat.__str__(self)
+ def __repr__(self):
+ s = DeviceFormat.__repr__(self)
s += (" type = %(type)s partition count = %(count)s"
" sectorSize = %(sectorSize)s
"
" align_offset = %(offset)s align_grain = %(grain)s
"
- " partedDisk = %(disk)r
"
+ " partedDisk = %(disk)s
"
" origPartedDisk = %(orig_disk)r
"
- " partedDevice = %(dev)r
" %
+ " partedDevice = %(dev)s
" %
{"type": self.labelType, "count": len(self.partitions),
"sectorSize": self.partedDevice.sectorSize,
"offset": self.alignment.offset,
@@ -108,6 +108,10 @@ class DiskLabel(DeviceFormat):
return s

@property
+ def desc(self):
+ return "%s %s" % (self.labelType, self.type)
+
+ @property
def dict(self):
d = super(DiskLabel, self).dict
d.update({"labelType": self.labelType,
diff --git a/pyanaconda/storage/formats/dmraid.py b/pyanaconda/storage/formats/dmraid.py
index 5df3a1d..bfc948b 100644
--- a/pyanaconda/storage/formats/dmraid.py
+++ b/pyanaconda/storage/formats/dmraid.py
@@ -78,8 +78,8 @@ class DMRaidMember(DeviceFormat):
# Initialize the attribute that will hold the block object.
self._raidmem = None

- def __str__(self):
- s = DeviceFormat.__str__(self)
+ def __repr__(self):
+ s = DeviceFormat.__repr__(self)
s += (" raidmem = %(raidmem)r" % {"raidmem": self.raidmem})
return s

diff --git a/pyanaconda/storage/formats/fs.py b/pyanaconda/storage/formats/fs.py
index 9260e4e..6d9a8a6 100644
--- a/pyanaconda/storage/formats/fs.py
+++ b/pyanaconda/storage/formats/fs.py
@@ -163,8 +163,8 @@ class FS(DeviceFormat):
if self.supported:
self.loadModule()

- def __str__(self):
- s = DeviceFormat.__str__(self)
+ def __repr__(self):
+ s = DeviceFormat.__repr__(self)
s += (" mountpoint = %(mountpoint)s mountopts = %(mountopts)s
"
" label = %(label)s size = %(size)s"
" targetSize = %(targetSize)s
" %
@@ -174,6 +174,13 @@ class FS(DeviceFormat):
return s

@property
+ def desc(self):
+ s = "%s filesystem" % self.type
+ if self.mountpoint:
+ s += " mounted at %s" % self.mountpoint
+ return s
+
+ @property
def dict(self):
d = super(FS, self).dict
d.update({"mountpoint": self.mountpoint, "size": self._size,
diff --git a/pyanaconda/storage/formats/luks.py b/pyanaconda/storage/formats/luks.py
index 6734721..f6c3147 100644
--- a/pyanaconda/storage/formats/luks.py
+++ b/pyanaconda/storage/formats/luks.py
@@ -91,8 +91,8 @@ class LUKS(DeviceFormat):
elif not self.mapName and self.device:
self.mapName = "luks-%s" % os.path.basename(self.device)

- def __str__(self):
- s = DeviceFormat.__str__(self)
+ def __repr__(self):
+ s = DeviceFormat.__repr__(self)
if self.__passphrase:
passphrase = "(set)"
else:
diff --git a/pyanaconda/storage/formats/lvmpv.py b/pyanaconda/storage/formats/lvmpv.py
index acf0138..e23cb81 100644
--- a/pyanaconda/storage/formats/lvmpv.py
+++ b/pyanaconda/storage/formats/lvmpv.py
@@ -67,8 +67,8 @@ class LVMPhysicalVolume(DeviceFormat):
# for not-yet-created devices
self.peStart = kwargs.get("peStart", 1.0) # in MB

- def __str__(self):
- s = DeviceFormat.__str__(self)
+ def __repr__(self):
+ s = DeviceFormat.__repr__(self)
s += (" vgName = %(vgName)s vgUUID = %(vgUUID)s"
" peStart = %(peStart)s" %
{"vgName": self.vgName, "vgUUID": self.vgUuid,
diff --git a/pyanaconda/storage/formats/mdraid.py b/pyanaconda/storage/formats/mdraid.py
index fe536d3..39164a4 100644
--- a/pyanaconda/storage/formats/mdraid.py
+++ b/pyanaconda/storage/formats/mdraid.py
@@ -66,8 +66,8 @@ class MDRaidMember(DeviceFormat):
#self.probe()
self.biosraid = kwargs.get("biosraid")

- def __str__(self):
- s = DeviceFormat.__str__(self)
+ def __repr__(self):
+ s = DeviceFormat.__repr__(self)
s += (" mdUUID = %(mdUUID)s biosraid = %(biosraid)s" %
{"mdUUID": self.mdUuid, "biosraid": self.biosraid})
return s
diff --git a/pyanaconda/storage/formats/multipath.py b/pyanaconda/storage/formats/multipath.py
index e4c3c6b..0d160e4 100644
--- a/pyanaconda/storage/formats/multipath.py
+++ b/pyanaconda/storage/formats/multipath.py
@@ -67,8 +67,8 @@ class MultipathMember(DeviceFormat):
# Initialize the attribute that will hold the block object.
self._member = None

- def __str__(self):
- s = DeviceFormat.__str__(self)
+ def __repr__(self):
+ s = DeviceFormat.__repr__(self)
s += (" member = %(member)r" % {"member": self.member})
return s

diff --git a/pyanaconda/storage/formats/swap.py b/pyanaconda/storage/formats/swap.py
index 222a459..860a551 100644
--- a/pyanaconda/storage/formats/swap.py
+++ b/pyanaconda/storage/formats/swap.py
@@ -63,8 +63,8 @@ class SwapSpace(DeviceFormat):
self.priority = kwargs.get("priority")
self.label = kwargs.get("label")

- def __str__(self):
- s = DeviceFormat.__str__(self)
+ def __repr__(self):
+ s = DeviceFormat.__repr__(self)
s += (" priority = %(priority)s label = %(label)s" %
{"priority": self.priority, "label": self.label})
return s
diff --git a/pyanaconda/storage/partitioning.py b/pyanaconda/storage/partitioning.py
index 80ba4ca..7b0f963 100644
--- a/pyanaconda/storage/partitioning.py
+++ b/pyanaconda/storage/partitioning.py
@@ -1288,7 +1288,7 @@ class Chunk(object):
for req in requests:
self.addRequest(req)

- def __str__(self):
+ def __repr__(self):
s = ("%(type)s instance --
"
"device = %(device)s start = %(start)d end = %(end)d
"
"length = %(length)d size = %(size)d pool = %(pool)d
"
@@ -1302,6 +1302,11 @@ class Chunk(object):

return s

+ def __str__(self):
+ s = "%d-%d on %s" % (self.geometry.start, self.geometry.end,
+ self.geometry.device.path)
+ return s
+
def addRequest(self, req):
""" Add a Request to this chunk. """
log.debug("adding request %d to chunk %s" % (req.partition.id, self))
--
1.7.3.4

_______________________________________________
Anaconda-devel-list mailing list
Anaconda-devel-list@redhat.com
https://www.redhat.com/mailman/listinfo/anaconda-devel-list


All times are GMT. The time now is 09:32 AM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.