Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions confconsole.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,17 @@
import os
import sys
import dialog
import ipaddr
from string import Template

import ifutil
import netinfo
import getopt

import conf

from io import StringIO
import traceback
import subprocess
from subprocess import PIPE, CalledProcessError
import shlex

import netinfo

from libconfconsole import ipaddr, ifutil, conf
import plugin

PLUGIN_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)),
Expand Down
11 changes: 6 additions & 5 deletions debian/confconsole.install
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins.d/ /usr/lib/confconsole/
*.py /usr/lib/confconsole/
conf/* /etc/confconsole/
share/* /usr/share/confconsole/
add-water/* /lib/systemd/system
plugins.d/ /usr/lib/confconsole/
*.py /usr/lib/confconsole/
libconfconsole/ /usr/lib/python3/dist-packages/
conf/* /etc/confconsole/
share/* /usr/share/confconsole/
add-water/* /lib/systemd/system
Empty file added libconfconsole/__init__.py
Empty file.
File renamed without changes.
5 changes: 3 additions & 2 deletions ifutil.py → libconfconsole/ifutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import os
from time import sleep
import subprocess
from typing import Optional
from typing import Optional, Callable

from netinfo import InterfaceInfo
from netinfo import get_hostname
Expand Down Expand Up @@ -299,7 +299,8 @@ def set_dhcp(ifname: str) -> Optional[str]:


def get_ipconf(ifname: str, error: bool = False
) -> tuple[str, str, str, list[str]]:
) -> tuple[Callable[[], Optional[str]],
Optional[str], Optional[str], list[str]]:
net = InterfaceInfo(ifname)
return (net.addr, net.netmask, net.get_gateway(error),
get_nameservers(ifname))
Expand Down
37 changes: 19 additions & 18 deletions ipaddr.py → libconfconsole/ipaddr.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import math


def is_legal_ip(ip):
def is_legal_ip(ip: str) -> bool:
try:
if len([octet for octet in ip.split(".")
if 255 >= int(octet) >= 0]) != 4:
Expand All @@ -22,23 +22,23 @@ def is_legal_ip(ip):
return True


def _str2int(ip):
def _str2int(ip: str) -> str:
bytes = list(map(int, ip.split('.')))
ip, = struct.unpack("!L", struct.pack("BBBB", *bytes))
return ip


def _int2str(num):
def _int2str(num: int) -> str:
bytes = struct.unpack("BBBB", struct.pack("!L", num))
return '.'.join(list(map(str, bytes)))


class Error(Exception):
class IpError(Exception):
pass


class IP(int):
def __new__(cls, arg):
def __new__(cls, arg: str) -> IP:
if isinstance(arg, IP):
return int.__new__(cls, int(arg))

Expand All @@ -47,18 +47,19 @@ def __new__(cls, arg):

else:
if not is_legal_ip(arg):
raise Error("illegal ip (%s)" % arg)
raise IpError(f"illegal ip ({arg})")

return int.__new__(cls, _str2int(arg))

def __str__(self):
def __str__(self) -> str:
return _int2str(self)

def __repr__(self):
return "IP(%r)" % str(self)
def __repr__(self) -> str:
return f"IP({str(self)})"

def _numeric_method(method):
def f(self, other):
@staticmethod
def _numeric_method(method: str):
def f(self, other: str):
return IP(getattr(int, method)(self, other))

return f
Expand All @@ -72,25 +73,25 @@ def f(self, other):

class IPRange:
@classmethod
def from_cidr(cls, arg):
def from_cidr(cls, arg: str) -> IPRange:
address, cidr = arg.split('/')
netmask = 2 ** 32 - (2 ** (32 - int(cidr)))
return cls(address, netmask)

def __init__(self, ip, netmask):
def __init__(self, ip: str, netmask: str):
self.ip = IP(ip)
self.netmask = IP(netmask)
self.network = self.ip & self.netmask
self.broadcast = self.network + 2 ** 32 - self.netmask - 1
self.cidr = int(32 - math.log(2 ** 32 - self.netmask, 2))

def __contains__(self, ip):
def __contains__(self, ip: str) -> bool:
return self.network < IP(ip) < self.broadcast

def __repr__(self):
return "IPRange('%s', '%s')" % (self.ip, self.netmask)
def __repr__(self) -> str:
return f"IPRange('{self.ip}', '{self.netmask}')"

def fmt_cidr(self):
return "%s/%d" % (self.ip, self.cidr)
def fmt_cidr(self) -> str:
return f"{self.ip}/{self.cidr}"

__str__ = fmt_cidr