import re import datetime class Snapshot(object): def __init__(self, snapshot_string): self.snapshot_string = snapshot_string self.snapshot_tuple = self.parse_string_to_table() ## TODO Dunno if it's useful yet. Probably not #self.snapshot_name = self.get_snapshot_name() #self.snapshot_zvol = self.get_snapshot_zvol() #self.snapshot_creation_time = self.get_snapshot_creation_time() #self.snaphot_used_size = self.get_snapshot_used_size() #self.snapshot_referenced_size = self.get_snapshot_referenced_size() def __repr__(self): return self.get_snapshot_name() def get_snapshot_string(self): return self.snapshot_string def parse_string_to_table(self): array = re.split(r'\t+', self.snapshot_string) return tuple(array) ## FIXME Have to precompile regexes for speed. Or maybe use another re method; findall might be slow def get_snapshot_name(self): name_str = self.snapshot_tuple[0] name = re.findall(r"[^/]+$", name_str)[0] return name def get_snapshot_zvol(self): name_str = self.snapshot_tuple[0] zvol = re.findall(r"^.*[\\/]", name_str)[0] return zvol def get_snapshot_creation_time(self): time_epoch = int(self.snapshot_tuple[1]) return str(datetime.datetime.utcfromtimestamp(time_epoch)) def get_snapshot_used_size(self): used_str = self.snapshot_tuple[2] return self.sizeof_fmt(int(used_str)) def get_snapshot_referenced_size(self): referenced_str = self.snapshot_tuple[3] return self.sizeof_fmt(int(referenced_str)) @staticmethod def sizeof_fmt(num, suffix='B'): ## FIXME WOW taken from stack but it's too fucking slow. Probably because of division. Have to profile for unit in ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']: if abs(num) < 1024.0: return "%3.1f%s%s" % (num, unit, suffix) num /= 1024.0 return "%.1f%s%s" % (num, 'Yi', suffix)