diff --git a/twander.py b/twander.py index 4973895..d672171 100755 --- a/twander.py +++ b/twander.py @@ -6,7 +6,7 @@ # Program Information PROGNAME = "twander" -RCSID = "$Id: twander.py,v 3.118 2003/03/01 08:21:28 tundra Exp $" +RCSID = "$Id: twander.py,v 3.119 2003/03/01 11:05:35 tundra Exp $" VERSION = RCSID.split()[2] # Copyright Information @@ -333,7 +333,9 @@ Name2Key[x.lower()] = index index += 1 -MAXSORTFIELD = len(Name2Key) +# Highest key index needed by Drive List View + +MAXDLVKEY = 5 ##### @@ -507,6 +509,8 @@ UNAVAILABLE = "Unavailable" WIN32GROUP = "win32" + FILEGROUP WIN32OWNER = "win32" + FILEOWNER +WIN32FREE = "free" +WIN32TOTAL = "total" ##### @@ -817,10 +821,14 @@ # a space. ##### -def PadString(string, width): +def PadString(string, width, Rjust=FALSE): - string = string[:(width-1)] - string += " " * (width - len(string)) + string = string[:(width-1)] + if Rjust: + string = string.rjust(width) + else: + string = string.ljust(width) + return string # End of 'PadString()' @@ -3211,79 +3219,129 @@ # The user requested Drive List View. else: + + dlv = {} + dList = [] + + # There are more keys in normal view than in Drive List View + # If the user has selected one of these, map them to sort + # by name in Drive List View. + + dlvkey = Name2Key[SORTBYFIELD.lower()] + 1 + if dlvkey > MAXDLVKEY: + dlvkey = MAXDLVKEY + drivelist = GetWin32Drives() - # Sort and, optionally, reverse the order + # Create a detailed entry for each drive on the system + # Store it as a string in the details list which will + # then be returned to the caller - drivelist.sort() - if SORTREVERSE: - drivelist.reverse() + for drive in drivelist: - # If details are off, just return the list of drives - if not UI.DetailsOn: - return drivelist + fields = [""] - # We're returning detailed information about each drive - else: - - details = [] + # Drive Label - Drive Might Not Be Available + try: + label = GetVolumeInformation(drive)[0] + except: + label = NODRIVE - # Create a detailed entry for each drive on the system - # Store it as a string in the details list which will - # then be returned to the caller + if not label: + label = NOLABEL - for drive in drivelist: + # Type Of Drive - We need this now to get hostname + drivetype = GetDriveType(drive) + typestring = '' - # Drive Label - Drive Might Not Be Available - try: - label = GetVolumeInformation(drive)[0] - except: - label = NODRIVE + for type, stype in win32all_type: + if drivetype == type: + typestring = stype - if not label: - label = NOLABEL - - # Type Of Drive - We need this now to get hostname - drivetype = GetDriveType(drive) - typestring = '' + # Machine Hosting The Drive + if drivetype == win32con.DRIVE_REMOTE: + name = WNetGetUniversalName(drive, 1) + else: + name = label - for type, stype in win32all_type: - if drivetype == type: - typestring = stype + entry = PadString(name, SZ_DRIVE_HOST + SZ_DRIVE_LABEL) + fields.append(name) - # Machine Hosting The Drive - if drivetype == win32con.DRIVE_REMOTE: - name = WNetGetUniversalName(drive, 1) + # Add the drive type + entry += PadString(typestring, SZ_DRIVE_TYPE) + fields.append(typestring) + + # Get Drive Space Information - Drive Might Not Be Available + try: + clustersz, sectorsz, freeclusters, totalclusters = GetDiskFreeSpace(drive) + + # Free Space + fspace = clustersz * sectorsz * freeclusters + freespace = FileLength(fspace) + + # Total Space + tspace = clustersz * sectorsz * totalclusters + totalspace = FileLength(tspace) + except: + fspace, tspace = (0, 0) + freespace, totalspace = ('0', '0') + + + freespace = PadString(freespace, SZ_DRIVE_FREE, Rjust=TRUE) + totalspace = PadString(totalspace, SZ_DRIVE_TTL, Rjust=TRUE) + + + entry += "%s %s %s %s" % (freespace, WIN32FREE, totalspace, WIN32TOTAL) + fields.append(fspace) + fields.append(tspace) + + # Finally, tack on the drive name with some leading space + entry += " %s" % drive + fields.append(drive) + + # If we're not going to sort later, just build the list + # of drives now + + if SORTBYFIELD.lower() == fNONE.lower(): + if UI.DetailsOn: + dList.append(entry) else: - name = label + dList.append(drive) + + # Nope, prepare to sort later + else: + idx = fields[dlvkey] + dlv.setdefault(idx,[]).append((drive, entry)) + + + # Now that we've built the list, sort by indicated parameter + # if user has so indicated + + if SORTBYFIELD.lower() != fNONE.lower(): + + + indexes = dlv.keys() + indexes.sort() + + if SORTREVERSE: + indexes.reverse() + + # Now build output list in sorted order + + for x in indexes: + + for entry in dlv[x]: + + if UI.DetailsOn: + dList.append(entry[1]) + else: + dList.append(entry[0]) + + # Return the list + + return dList - entry = PadString(name, SZ_DRIVE_HOST + SZ_DRIVE_LABEL) - # Add the drive type - entry += PadString(typestring, SZ_DRIVE_TYPE) - - # Get Drive Space Information - Drive Might Not Be Available - try: - clustersz, sectorsz, freeclusters, totalclusters = GetDiskFreeSpace(drive) - - # Free Space - freespace = FileLength(clustersz * sectorsz * freeclusters) - - # Total Space - totalspace = FileLength(clustersz * sectorsz * totalclusters) - except: - freespace, totalspace = ('0', '0') - - sizes = "%s/%s" % (freespace, totalspace) - entry += PadString(sizes, SZ_DRIVE_FREE + SZ_DRIVE_TTL) - - # Finally, tack on the drive name - entry += drive - - # Save final result in detailed list - details.append(entry) - - return details # End of 'BuildDirList()'