import os def get_parent_from_uri(uri): """ Get the parent folder from an uri """ p = os.path.split(uri) p = os.path.join(p[:-1]) return p[0] def pretty_size(size): if size >= 1024*1024*1024: return "%.1f GiB" % round(size/1024/1024/1024.0) elif size >= 1024*1024: return "%.1f MiB" % round(size/1024/1024.0) elif size >= 1024: return "%.1f KiB" % round(size/1024.0) else: return "%u B" % size def format_disk_space(size): """ Convert bytes to a human readable format. Returns a tuple, for example (20,"MiB") """ if size >= 1024*1024*1024: return ("%.1f"%round(size/1024/1024/1024.0),"GiB") elif size >= 1024*1024: return ("%.1f"%round(size/1024/1024.0),"MiB") elif size >= 1024: return ("%.1f"%round(size/1024.0),"KiB") else: return ("%u"%size,"B")