aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/i3blocks/.local/bin/i3blocks-net
blob: 2faa071464b0c49a609b385414a5d3069a01ff81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python3

import glob
import os
from enum import Enum


class IFaceType(Enum):
    ETHERNET = "\U000f0200"
    WIRELESS = "\U000f05a9"
    TUNNEL = "\U000f0582"
    UNKNOWN = ""

    @staticmethod
    def from_path(path: str):
        try:
            with open(os.path.join(path, "type"), "r") as f:
                match int(f.readline()):
                    case 1 if os.path.isdir(
                        os.path.join(path, "wireless")
                    ) or os.path.islink(os.path.join(path, "phy80211")):
                        return IFaceType.WIRELESS
                    case 1:
                        return IFaceType.ETHERNET
                    case 65534:
                        return IFaceType.TUNNEL
                    case _:
                        raise ValueError("Invalid interface type")
        except Exception:
            return IFaceType.UNKNOWN


class IFace:
    def __init__(self, path: str):
        self.path = path
        self.name = os.path.basename(path)
        self._type = IFaceType.from_path(path)

    def __str__(self):
        return self._type.value


def main():
    ifaces = []
    for net in glob.glob("/sys/class/net/*"):
        iface = IFace(net)
        if iface.name != "lo" and iface._type != IFaceType.UNKNOWN:
            ifaces.append(str(iface))

    print(f" {" ".join(ifaces)} \n")


if __name__ == "__main__":
    main()