Back to index

plone3  3.1.7
getObjSize.py
Go to the documentation of this file.
00001 ## Script (Python) "getObjSize"
00002 ##bind container=container
00003 ##bind context=context
00004 ##bind namespace=
00005 ##bind script=script
00006 ##bind subpath=traverse_subpath
00007 ##parameters=obj=None, size=None
00008 ##title=
00009 ##
00010 
00011 from Products.CMFPlone.utils import base_hasattr
00012 
00013 if obj is None:
00014     obj = context
00015 
00016 const = {'kB':1024,
00017          'MB':1024*1024,
00018          'GB':1024*1024*1024}
00019 order = ('GB', 'MB', 'kB')
00020 smaller = order[-1]
00021 
00022 # allow arbitrary sizes to be passed through,
00023 # if there is no size, but there is an object
00024 # look up the object, this maintains backwards 
00025 # compatibility
00026 if size is None and base_hasattr(obj, 'get_size'):
00027     size=obj.get_size()
00028 
00029 # if the size is a float, then make it an int
00030 # happens for large files
00031 try:
00032     size = int(size)
00033 except (ValueError, TypeError):
00034     pass
00035 
00036 if not size:
00037     return '0 %s' % smaller
00038 
00039 if same_type(size, 0) or same_type(size, 0L):
00040     if size < const[smaller]:
00041         return '1 %s' % smaller
00042     for c in order:
00043         if size/const[c] > 0:
00044             break
00045     return '%.1f %s' % (float(size/float(const[c])), c)
00046 
00047 return size