Skip to content

Commit

Permalink
partial typing
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelEischer committed Jul 8, 2022
1 parent 0490aa0 commit 5886255
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions wol.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3

from pyparsing import *
from typing import Dict, Optional
import argparse
import logging
import psutil
Expand All @@ -9,7 +10,7 @@
import os
import re

def getHosts(dhcp_conf):
def getHosts(dhcp_conf) -> Dict[str, Dict[str, str]]:
# https://github.com/cbalfour/dhcp_config_parser/blob/master/grammar/host.py
LBRACE, RBRACE, SEMI = map(Suppress, '{};')
PERIOD = Literal('.')
Expand All @@ -28,7 +29,7 @@ def getHosts(dhcp_conf):
)
host_stanza.ignore(comment)

hostlist = {}
hostlist = {} # type: Dict[str, Dict[str, str]]
for result, start, end in host_stanza.scanString(dhcp_conf.read()):
hostlist[result['host']] = {
'address': result['address'],
Expand All @@ -38,7 +39,7 @@ def getHosts(dhcp_conf):
logging.debug('Parsed configuration file "{}" with {} entries.'.format(dhcp_conf.name, len(hostlist)))
return hostlist

def getInterface(address):
def getInterface(address: str) -> Optional[str]:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.connect((address, 0))
with sock:
Expand All @@ -51,7 +52,7 @@ def getInterface(address):

magicPort = 9
magicRAW = False
def sendMagicPacket(macAddress, iface):
def sendMagicPacket(macAddress: str, iface: str) -> bool:
macRegex = re.compile(r'(?:([0-9a-f]{2})(?:[:-]|$))', re.IGNORECASE)
macBytes = b''.join([int(b,16).to_bytes(1,'little') for b in macRegex.findall(macAddress)])
if iface and len(macBytes) == 6:
Expand All @@ -72,7 +73,7 @@ def sendMagicPacket(macAddress, iface):
logging.exception('Sending magic packet to {} (on {}) failed'.format(macAddress, iface))
return False

def wake(hostname):
def wake(hostname: str) -> bool:
global hosts
if hostname in hosts:
logging.info('Waking up {}...'.format(hostname))
Expand All @@ -87,7 +88,7 @@ def wake(hostname):
logging.warning('Unknown host "{}"'.format(hostname))
return False

def listen(host, port):
def listen(host: str, port: int) -> None:
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
Expand Down Expand Up @@ -175,4 +176,3 @@ def listen(host, port):
for hostname in args.hostnames:
wake(hostname)
sys.exit(0)

0 comments on commit 5886255

Please sign in to comment.