# -*- coding: utf-8 -*-
"""
Display network speed and bandwidth usage.

Configuration parameters:
    cache_timeout: refresh interval for this module (default 2)
    format: display format for this module
        *(default '[\?color=down LAN(Kb): {down}↓ {up}↑]
         [\?color=total T(Mb): {download}↓ {upload}↑ {total}↕]')*
    nic: network interface to use (default None)
    thresholds: color thresholds to use
        *(default {'down': [(0, 'bad'), (30, 'degraded'), (60, 'good')],
        'total': [(0, 'good'), (400, 'degraded'), (700, 'bad')]})*

Format placeholders:
    {down}     number of download speed
    {up}       number of upload speed
    {download} number of download usage
    {upload}   number of upload usage
    {total}    number of total usage

Color thresholds:
    {down}     color threshold of download speed
    {total}    color threshold of total usage

@author Shahin Azad <ishahinism at Gmail>
"""


class GetData:
    """
    Get system status.
    """
    def __init__(self, nic):
        self.nic = nic

    def netBytes(self):
        """
        Get bytes directly from /proc.
        """
        with open('/proc/net/dev') as fh:
            net_data = fh.read().split()
        interface_index = net_data.index(self.nic + ':')
        received_bytes = int(net_data[interface_index + 1])
        transmitted_bytes = int(net_data[interface_index + 9])
        return received_bytes, transmitted_bytes


class Py3status:
    """
    """
    # available configuration parameters
    cache_timeout = 2
    format = u'[\?color=down LAN(Kb): {down}↓ {up}↑] ' + \
        u'[\?color=total T(Mb): {download}↓ {upload}↑ {total}↕]'
    nic = None
    thresholds = {
        'down': [(0, 'bad'), (30, 'degraded'), (60, 'good')],
        'total': [(0, 'good'), (400, 'degraded'), (700, 'bad')]
    }

    class Meta:
        def deprecate_function(config):
            return {
                'thresholds': {
                    'down': [
                        (0, 'bad'),
                        (config.get('low_speed', 30), 'degraded'),
                        (config.get('med_speed', 60), 'good')
                    ],
                    'total': [
                        (0, 'good'),
                        (config.get('low_traffic', 400), 'degraded'),
                        (config.get('med_traffic', 700), 'bad')
                    ]
                }
            }

        deprecated = {
            'function': [
                {'function': deprecate_function},
            ],
            'remove': [
                {
                    'param': 'low_speed',
                    'msg': 'obsolete, set using thresholds parameter',
                },
                {
                    'param': 'med_speed',
                    'msg': 'obsolete, set using thresholds parameter',
                },
                {
                    'param': 'low_traffic',
                    'msg': 'obsolete, set using thresholds parameter',
                },
                {
                    'param': 'med_traffic',
                    'msg': 'obsolete, set using thresholds parameter',
                },
            ],
        }

        update_config = {
            'update_placeholder_format': [
                {
                    'placeholder_formats': {
                        'down': ':5.1f',
                        'up': ':5.1f',
                        'download': ':3.0f',
                        'upload': ':3.0f',
                        'total': ':3.0f',
                    },
                    'format_strings': ['format']
                },
            ],
        }

    def __init__(self):
        self.old_transmitted = 0
        self.old_received = 0

    def post_config_hook(self):
        """
        Get network interface.
        """
        if self.nic is None:
            # Get default gateway directly from /proc.
            with open('/proc/net/route') as fh:
                for line in fh:
                    fields = line.strip().split()
                    if fields[1] == '00000000' and int(fields[3], 16) & 2:
                        self.nic = fields[0]
                        break
            if self.nic is None:
                self.nic = 'lo'
                self.py3.notify_user(
                    'netdata: cannot find a nic to use. selected nic: lo instead.'
                )
            self.py3.log('selected nic: %s' % self.nic)

    def netdata(self):
        """
        Calculate network speed and network traffic.
        """
        data = GetData(self.nic)
        received_bytes, transmitted_bytes = data.netBytes()

        # net_speed (statistic)
        down = (received_bytes - self.old_received) / 1024.
        up = (transmitted_bytes - self.old_transmitted) / 1024.
        self.old_received = received_bytes
        self.old_transmitted = transmitted_bytes

        # net_traffic (statistic)
        download = received_bytes / 1024 / 1024.
        upload = transmitted_bytes / 1024 / 1024.
        total = download + upload

        # color threshold
        self.py3.threshold_get_color(down, 'down')
        self.py3.threshold_get_color(total, 'total')

        netdata = self.py3.safe_format(self.format, {'down': down,
                                                     'up': up,
                                                     'download': download,
                                                     'upload': upload,
                                                     'total': total})
        return {
            'cached_until': self.py3.time_in(self.cache_timeout),
            'full_text': netdata
        }


if __name__ == "__main__":
    """
    Run module in test mode.
    """
    from py3status.module_test import module_test
    module_test(Py3status)

@@ -217,16 +217,22 @@
 efault '
+%7Bnic%7D 
 %5B%5C?color
@@ -261,17 +261,16 @@
  %7Bup%7D%E2%86%91%5D%0A
- 
         
@@ -573,24 +573,57 @@
 aceholders:%0A
+    %7Bnic%7D      network interface%0A
     %7Bdown%7D  
@@ -1585,16 +1585,22 @@
 mat = u'
+%7Bnic%7D 
 %5B%5C?color
@@ -4286,147 +4286,8 @@
 lo'%0A
-                self.py3.notify_user(%0A                    'netdata: cannot find a nic to use. selected nic: lo instead.'%0A                )%0A
     
@@ -5424,16 +5424,86 @@
 ': total
+,%0A                                                     'nic': self.nic
 %7D)%0A     

replace notification with placeholder