Skip to content

Instantly share code, notes, and snippets.

@mrzechonek
Created March 10, 2020 21:14
Show Gist options
  • Save mrzechonek/f4c2b4d9ee3934c90d0964342afcb453 to your computer and use it in GitHub Desktop.
Save mrzechonek/f4c2b4d9ee3934c90d0964342afcb453 to your computer and use it in GitHub Desktop.
import sys
from functools import partial
from typing import Tuple
from docopt import DocoptExit, docopt
class GatewayConfigClient:
def configuration_get(self, destination: int, net_index: int):
pass
def configuration_set(
self,
destination: int,
net_index: int,
mtu: int,
mac: str,
reconnect: int,
server: Tuple[str, int],
netmask: int = None,
ip: str = None,
dns: str = None,
gateway: str = None,
):
pass
def dhcp_get(self, destination: int, net_index: int):
pass
def dhcp_set(
self, destination: int, net_index: int, dhcp_enable: bool,
):
pass
def packets_get(self, destination: int, net_index: int):
pass
def packets_clear(self, destination: int, net_index: int):
pass
def mtu_set(self, destination: int, net_index: int, mtu_size: int):
pass
def mac_set(self, destination: int, net_index: int, mac_addr: str):
pass
def dns_set(self, destination: int, net_index: int, dns_ip_addr: str):
pass
def server_set(
self, destination: int, net_index: int, server: Tuple[str, int],
):
pass
def reconnect_et(self, destination: int, net_index: int, reconnect_interval: int):
pass
def ip_set(self, destination: int, net_index: int, ip_addr: str):
pass
def gateway_set(self, destination: int, net_index: int, gateway_ip_addr: str):
pass
def netmask_set(self, destination: int, net_index: int, netmask: int):
pass
class GatewayConfigurationCommand:
USAGE = (
"""
Usage:
%(cmd)s ("""
"--mtu=MTU | --mac=MAC | --server=HOST:PORT | --reconnect=INTERVAL | "
"--ip=IP | --dns=IP | --gateway=IP | --netmask=NETMASK | --dhcp=DHCP"
""") <uuid>
%(cmd)s --mtu=MTU --mac=MAC --reconnect=INTERVAL --server=HOST:PORT <uuid>
%(cmd)s --mtu=MTU --mac=MAC --reconnect=INTERVAL --ip=IP --netmask=NETMASK --gateway=IP --server=HOST:PORT <uuid>
%(cmd)s --mtu=MTU --mac=MAC --reconnect=INTERVAL --ip=IP --netmask=NETMASK --gateway=IP --dns=IP --server=HOST:PORT <uuid>
%(cmd)s [--get=TYPE] <uuid>
Options:
--mtu=MTU Set MTU size
--mac=MAC Set MAC address
--server=SERVER:PORT Set server's name nad port
--reconnect=INTERVAL Set reconnect interval
--ip=IP Set IP address
--dns=IP Set DNS IP address
--gateway=IP Set gateway UP address
--netmask=NETMASK Set netmask
--dhcp=DHCP Set DHCP setting
--get=TYPE Get current configuration [default: configuration]
"""
)
CMD = "gateway"
def get_usage(self, **kwargs):
return self.USAGE % dict(cmd=self.CMD, **kwargs)
def __call__(self, application, arguments):
model = GatewayConfigClient
# strip'--' prefixes from options and drop Nones
kwargs = {
k.lstrip("-"): v
for k, v in arguments.items()
if k.startswith("--") and v is not None
}
# convert HOST:PORT into a tuple
server = kwargs.pop("server", None)
if server is not None:
try:
host, port = server.split(":")
except ValueError:
raise DocoptExit
kwargs["server"] = (host, int(port))
getter = kwargs.pop("get", None)
# more than one option: it's a 'big' setter
if len(kwargs) > 1:
method = partial(model.configuration_set, **kwargs)
elif len(kwargs) > 0:
((name, value),) = kwargs.items()
method = partial(getattr(model, f"{name}_set"), value)
else:
method = getattr(model, f"{getter}_get")
return method
if __name__ == "__main__":
handler = GatewayConfigurationCommand()
usage = handler.get_usage()
arguments = docopt(usage)
try:
print(handler(None, arguments))
except DocoptExit as ex:
print(usage.strip("\n").rstrip())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment