Advertisement
Guest User

tomatousb rstats

a guest
Apr 18th, 2012
1,133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.09 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. from collections import namedtuple
  4. from datetime import date
  5. import gzip
  6. import struct
  7. import sys
  8.  
  9. class RStatsHistory(object):
  10.     ID_V0 = 0x30305352
  11.     ID_V1 = 0x31305352
  12.  
  13.     def __init__(self, rstats_filename):
  14.         self.daily = []
  15.         self.monthly = []
  16.         self.rstats_file = self._decompress(rstats_filename)
  17.         self._load_history()
  18.  
  19.     def _decompress(self, filename):
  20.         try:
  21.             return gzip.open(filename, 'rb')
  22.         except IOError as e:
  23.             sys.stderr.write("File could not be decompressed\n")
  24.  
  25.     def _load_history(self):
  26.         max_daily = 62
  27.         max_monthly = 25
  28.         Bandwidth = namedtuple('Bandwidth', 'date down up')
  29.  
  30.         daily = []
  31.         monthly = []
  32.  
  33.         version, = struct.unpack("I0l", self.rstats_file.read(8))
  34.  
  35.         if version == RStatsHistory.ID_V0:
  36.            max_monthly = 12
  37.  
  38.         for i in range(max_daily):
  39.             self.daily.append(Bandwidth._make(struct.unpack("I2Q", self.rstats_file.read(24))))
  40.  
  41.         dailyp, = struct.unpack("i0l", self.rstats_file.read(8))
  42.  
  43.         for i in range(max_monthly):
  44.             self.monthly.append(Bandwidth._make(struct.unpack("I2Q", self.rstats_file.read(24))))
  45.  
  46.         monthlyp, = struct.unpack("i0l", self.rstats_file.read(8))
  47.  
  48.     @staticmethod
  49.     def get_date(xtime):
  50.         year  = ((xtime >> 16) & 0xFF) + 1900
  51.         month = ((xtime >>  8) & 0xFF) + 1
  52.         day   = xtime & 0xFF
  53.  
  54.         return date(year, month, 1 if day == 0 else day)
  55.  
  56.     @staticmethod
  57.     def to_unit(value, unit=None):
  58.         if unit is not None:
  59.             unit = unit.lower()
  60.         if unit == 'k':
  61.             return value / 1024
  62.         if unit == 'm':
  63.             return value / (1024 * 1024)
  64.         if unit == 'g':
  65.             return value / (1024 * 1024 * 1024)
  66.         return value
  67.  
  68.     def _print_counters(self, counters, unit=None):
  69.         for counter in counters:
  70.             if counter.date != 0:
  71.                 print("{0} : Upload {1} : Download {2}".format(
  72.                     self.get_date(counter.date).strftime("%Y/%m/%d"),
  73.                     self.to_unit(counter.up, unit),
  74.                     self.to_unit(counter.down, unit)))
  75.  
  76.     def print_daily(self, unit=None):
  77.         self._print_counters(self.daily, unit)
  78.  
  79.     def print_monthly(self, unit=None):
  80.         self._print_counters(self.monthly, unit)
  81.  
  82. def main():
  83.     import optparse
  84.     from os.path import isfile
  85.  
  86.     usage = "usage: %prog [options] <filename>"
  87.     parser = optparse.OptionParser(usage)
  88.  
  89.     parser.add_option("-u", "--unit",
  90.                       default = None,
  91.                       help = "Units b (bytes),k (KiB),m (MiB),g (GiB) default is b")
  92.  
  93.     options, args = parser.parse_args()
  94.  
  95.     if len(args) == 1 and isfile(args[0]):
  96.         rstats = RStatsHistory(args[0])
  97.  
  98.         print("Daily:")
  99.         rstats.print_daily(options.unit)
  100.         print("Monthly:")
  101.         rstats.print_monthly(options.unit)
  102.     else:
  103.         print("Incorrect arguments")
  104.         sys.exit(2)
  105.  
  106. if __name__ == "__main__":
  107.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement