#!/usr/bin/python

import re,types, socket, logging,string
import afs


useRXOSD=True
# log-level is set in AfsConfig
Logger=logging.getLogger("afs.util")
SizeUnit=['kB','MB','GB','TB','PB']
PartRX=re.compile("/?(?:vicep)?([a-z][a-z]?)")


class utilError(Exception):
    def __init__(self, message, Errors=[]):
        Exception.__init__(self, message)
        # Now for your custom code...
        self.Errors = Errors
  
    def __str__(self):
      #FIXME parse build a complete message with stack
      return repr(self.message)

def humanReadableSize(Size) :
    for s in range(len(SizeUnit)) :
        if float(Size) / (1024**(s+1)) < 1 : break
    return "%3.2f %s" % (float(Size) / (1024**s),SizeUnit[s])  

def canonicalizePartition(part) :
    if type(part) == types.StringType :
       if part.isdigit() :
           part=int(part)
    if type(part) == types.IntType : 
       firstLetter=part/26
       secondLetter=part%26
       partition=""
       if firstLetter != 0 :
           partition += chr(ord("a")+firstLetter) 
       partition += chr(ord("a")+secondLetter) 
    else :
       MObj=PartRX.match(part)
       if not MObj :
           raise utilError("Cannot canonicalize \"%s\"" % part)
       partition=MObj.groups()[0] 
    return partition
 
 
def canonicalizeVolume(volname):
    
    if volname.endswith(".readonly"):
        return volname[0:len(volname)-9]
    
    if volname.endswith(".backup"):
        return volname[0:len(volname)-6]

def isName(ambiguous) :
    """
    checks if name_or_ip or name_or_id is acutally the name or an numerical ID
    """
    # first, convert to string 
    ambiguous = "%s" % ambiguous    
    ambiguous=ambiguous.strip()
    if len(ambiguous) == 0 :
        raise utilError("isName called with empty string!")
    Logger.debug("isName: got '%s'" % ambiguous)
    if ambiguous[0] in string.digits : 
        return False
    else :
        return True

if __name__ == "__main__"  :
   print "Some basic methods used for afspy"
   print humanReadableSize(32768*29+2342)

@@ -163,30 +163,26 @@
 t=%5B'
-kB
+K
 ',
+ 
 'M
-B
 ','G
-B
 ','T
-B
 ','P
-B
 '%5D%0AP
@@ -227,16 +227,58 @@
 -z%5D?)%22)%0A
+HumanSizeRX=re.compile(%22(%5Cd+)(%5BKMGTP%5D?)%22)%0A
 %0A%0Aclass 
@@ -683,16 +683,16 @@
 : break%0A
-
     retu
@@ -748,16 +748,548 @@
 %5Bs%5D)  %0A%0A
+def parseHumanWriteableSize(Size) :%0A    %22%22%22%0A    return absolute Value of sth like 100M%0A    base 1024 used.%0A    %22%22%22 %0A    MObj=HumanSizeRX.match(Size)%0A    if not MObj:%0A%09raise utilError(%22Cannot parse value %25s. Should be an integer with an optional size-unit of %5BK,M,G,T,P%5D%22)  %0A    number,su=MObj.groups()%0A    number=int(number)%0A    multi=1%0A    if len(su) != 0 : %0A        for s in range(len(SizeUnit)) :%0A            if su == SizeUnit%5Bs%5D :%0A                multi = 1024**(s+1)%0A                break%0A%0A    return multi*number            %0A%0A%0A
 def cano

add method to parse human writeable sizes such as