The first stab at a new widget was actually fairly straight forward and 
easy. I'm loving qtile extensibility. I repurposed the Net widget and 
trimmed it down. It seems to work when I refresh my config but it doesn't 
automatically update when new interfaces are added as I would have presumed.

Thoughts?

J--

On Friday, December 27, 2019 at 12:55:56 PM UTC-5, J A wrote:
>
> Sorry to spam the list. Still working my head around qtile development. 
>
> I would like to use the NET widget to show IP address on my title bar. But 
> instead of using something as mundane as eth0 I'd like to have it 
> dynamically added for the tun0 interface so that when I connect It appears 
> and when I disconnect it disappears. 
>
> I've done this in python before with a library but not sure where to plug 
> this into the qtile framework. I suppose that I could have an external 
> custom vpn connect and disconnect command that calls into qtile to make the 
> necessary changes. But I'd love it to be a bit more dynamic and passive. 
>
> So, I was thinking of mimicking the background changer with feh that I 
> have where I start up a new thread that polls every 5 minutes. If i did it 
> this way I could do a search for the tun0 interface once a minute and if 
> it's there add widget and if it isn't remove the widget. 
>
> If anyone can point me to relevant examples or documentation that would be 
> great. 
>

-- 
You received this message because you are subscribed to the Google Groups 
"qtile-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/qtile-dev/22630244-06a2-4f8d-b2d4-d7889b63ee87%40googlegroups.com.
# Copyright (c) 2014 Rock Neurotiko
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import netifaces

from libqtile.log_utils import logger
from libqtile.widget import base

from typing import Tuple
from math import log


class NetIP(base.ThreadedPollText):
    """Displays interface status and IPv4 address"""
    orientations = base.ORIENTATION_HORIZONTAL
    defaults = [
        ('interface', None, 'List of interfaces or single NIC as string to monitor, \
            None to displays all active NICs combined'),
        ('update_interval', 10, 'The update interval.'),
    ]

    def __init__(self, **config):
        base.ThreadedPollText.__init__(self, **config)
        self.add_defaults(NetIP.defaults)
    
        if not isinstance(self.interface, list):
            if self.interface is None:
                ifaces = netifaces.interfaces()
                #no one cares about loopback
                ifaces.remove('lo')
                self.interface = ifaces
            elif isinstance(self.interface, str):
                self.interface = [self.interface]
            else:
                raise AttributeError("Invalid Argument passed: %s\nAllowed Types: List, String, None" % self.interface)

    def poll(self):
        ret_stat = []
        try:
            for intf in self.interface:
                addr = netifaces.ifaddresses(intf)

                #Unable to get IP address
                if not addr:
                    ret_stat.append("{0} : X.X.X.X".format(intf))
                    continue

                ip_addrs = [ip['addr'] for ip in addr[netifaces.AF_INET]]
                ret_stat.append("{0} : {1}".format(intf, ",".join(ip_addrs)))

            return " | ".join(ret_stat)

        except Exception as excp:
            logger.error('%s: Caught Exception:\n%s',
                         self.__class__.__name__, excp)

Reply via email to