#!/usr/bin/env python
# pylint disable C0103,C0111

import os
import sys
import time

INTERVAL = int(os.getenv('INTERVAL', '1'))
STATS = (
    'rx_bytes',
    'rx_packets',
    'tx_bytes',
    'tx_packets',
)


def read_stat(device, stat):
    with open('/sys/class/net/{0}/statistics/{1}'.format(device, stat)) as devfile:
        return int(devfile.read().strip())


def fetch_stats(device):
    return dict((stat, read_stat(device, stat)) for stat in STATS)


def delta(prev, cur):
    return dict((stat, cur[stat] - prev[stat]) for stat in STATS)


def fallback_spaces(n, sep=' '):
    output = str()
    while n / 1000 > 0:
        output = str(n % 1000).zfill(3) + sep + output
        n /= 1000
    return str(n % 1000) + sep + output


def print_diff(diff):
    if os.getenv('NOSPACES'):
        print '\t'.join(':'.join((stat, str(diff[stat]))) for stat in STATS)
        return
    if sys.version_info[0:2] == (2, 6):
        print '\t'.join(':'.join((stat, fallback_spaces(diff[stat]))) for stat in STATS)
        return
    print '\t'.join(':'.join((stat, "{0:,}".format(diff[stat]))) for stat in STATS)


def loop(device):
    cur = prev = None
    while True:
        cur = fetch_stats(device)
        if prev:
            print_diff(delta(prev, cur))
        prev = cur
        time.sleep(INTERVAL)


if __name__ == "__main__":
    dev = 'eth1' if len(sys.argv) < 2 else sys.argv[1]
    loop(dev)
