Skip to content

Commit

Permalink
Replace configuration_key() method with a plain class variable
Browse files Browse the repository at this point in the history
  • Loading branch information
infothrill committed Jan 29, 2018
1 parent bae13c1 commit c4f5d23
Show file tree
Hide file tree
Showing 28 changed files with 61 additions and 153 deletions.
1 change: 0 additions & 1 deletion dyndnsc/__init__.py
Expand Up @@ -2,7 +2,6 @@

"""Package for dyndnsc."""

from . import updater # noqa: @UnusedImport
from .core import getDynDnsClientForConfig, DynDnsClient # noqa: @UnusedImport

__version__ = "0.5.dev0"
13 changes: 2 additions & 11 deletions dyndnsc/common/dynamiccli.py
Expand Up @@ -21,7 +21,7 @@ def parse_cmdline_args(args, classes):
parsed_args = {}
for kls in classes:
prefix = kls.configuration_key_prefix()
name = kls.configuration_key()
name = kls.configuration_key
if getattr(args, "%s_%s" % (prefix, name), False):
logging.debug(
"Gathering initargs for '%s.%s'", prefix, name)
Expand Down Expand Up @@ -67,7 +67,7 @@ def register_arguments(cls, parser):
if hasattr(cls, "_dont_register_arguments"):
return
prefix = cls.configuration_key_prefix()
cfgkey = cls.configuration_key()
cfgkey = cls.configuration_key
parser.add_argument("--%s-%s" % (prefix, cfgkey),
action="store_true",
dest="%s_%s" % (prefix, cfgkey),
Expand Down Expand Up @@ -107,12 +107,3 @@ def configuration_key_prefix():
Abstract method, must be implemented in subclass.
"""
raise NotImplementedError("Please implement in subclass")

@staticmethod
def configuration_key():
"""
Return configuration key, identifying the class/service with a unique string.
Abstract method, must be implemented in subclass.
"""
raise NotImplementedError("Please implement in subclass")
2 changes: 1 addition & 1 deletion dyndnsc/common/load.py
Expand Up @@ -27,7 +27,7 @@ def load_class(module_name, class_name):
def find_class(name, classes):
"""Return class in ``classes`` identified by configuration key ``name``."""
name = name.lower()
cls = next((c for c in classes if c.configuration_key() == name), None)
cls = next((c for c in classes if c.configuration_key == name), None)
if cls is None:
raise ValueError("No class named '%s' could be found" % name)
return cls
15 changes: 1 addition & 14 deletions dyndnsc/detector/base.py
Expand Up @@ -77,27 +77,14 @@ def set_current_value(self, value):
self._oldvalue = self.get_current_value()
self._currentvalue = value
if self._oldvalue != value:
# self.notify_observers("new_ip_detected", {"ip": value})
LOG.debug("%s.set_current_value(%s)", self.__class__.__name__, value)
return value

def has_changed(self):
"""Detect difference between old and current value."""
return self.get_old_value() != self.get_current_value()

@staticmethod
def names():
"""
Return a list of string names identifying this class/service.
Abstract method, must be implemented in subclass.
"""
raise NotImplementedError("Please implement in subclass")

@classmethod
def configuration_key(cls):
"""Return configuration key, identifying the class/service with a unique string."""
return cls.names()[-1]

@staticmethod
def configuration_key_prefix():
"""Return "detector"."""
Expand Down
7 changes: 2 additions & 5 deletions dyndnsc/detector/command.py
Expand Up @@ -9,6 +9,8 @@
class IPDetector_Command(IPDetector):
"""IPDetector to detect IP address executing shell command/script."""

configuration_key = "command"

def __init__(self, command="", *args, **kwargs):
"""
Initializer.
Expand All @@ -19,11 +21,6 @@ def __init__(self, command="", *args, **kwargs):

self.opts_command = command

@staticmethod
def names():
"""Return a list of string names identifying this class/service."""
return ("command",)

def can_detect_offline(self):
"""Return false, as this detector possibly generates network traffic.
Expand Down
7 changes: 2 additions & 5 deletions dyndnsc/detector/dns.py
Expand Up @@ -43,6 +43,8 @@ def resolve(hostname, family=AF_UNSPEC):
class IPDetector_DNS(IPDetector):
"""Class to resolve a hostname using socket.getaddrinfo()."""

configuration_key = "dns"

def __init__(self, hostname=None, family=None, *args, **kwargs):
"""
Initializer.
Expand All @@ -58,11 +60,6 @@ def __init__(self, hostname=None, family=None, *args, **kwargs):
raise ValueError(
"IPDetector_DNS(): a hostname to be queried in DNS must be specified!")

@staticmethod
def names():
"""Return a list of string names identifying this class/service."""
return ("dns",)

def can_detect_offline(self):
"""Return false, as this detector generates dns traffic.
Expand Down
9 changes: 3 additions & 6 deletions dyndnsc/detector/dnswanip.py
Expand Up @@ -52,7 +52,9 @@ def find_ip(family=AF_INET, flavour="opendns"):


class IPDetector_DnsWanIp(IPDetector):
"""Class to discover the internet visible IP address using publicly available DNS infrastructure."""
"""Detect the internet visible IP address using publicly available DNS infrastructure."""

configuration_key = "dnswanip"

def __init__(self, family=None, *args, **kwargs):
"""
Expand All @@ -64,11 +66,6 @@ def __init__(self, family=None, *args, **kwargs):
family = AF_INET
super(IPDetector_DnsWanIp, self).__init__(*args, family=family, **kwargs)

@staticmethod
def names():
"""Return a list of string names identifying this class/service."""
return ("dnswanip",)

def can_detect_offline(self):
"""Return false, as this detector generates dns traffic.
Expand Down
7 changes: 2 additions & 5 deletions dyndnsc/detector/iface.py
Expand Up @@ -30,6 +30,8 @@ class IPDetector_Iface(IPDetector):
This is roughly equivalent to using `ifconfig` or `ipconfig`.
"""

configuration_key = "iface"

def __init__(self, iface=None, netmask=None, family=None, *args, **kwargs):
"""
Initializer.
Expand Down Expand Up @@ -57,11 +59,6 @@ def __init__(self, iface=None, netmask=None, family=None, *args, **kwargs):
else:
self.netmask = None

@staticmethod
def names():
"""Return a list of string names identifying this class/service."""
return ("iface",)

def can_detect_offline(self):
"""Return true, as this detector only queries local data."""
return True
Expand Down
7 changes: 2 additions & 5 deletions dyndnsc/detector/null.py
Expand Up @@ -12,6 +12,8 @@
class IPDetector_Null(IPDetector):
"""Dummy IP detector."""

configuration_key = "null"

def __init__(self, family=None, *args, **kwargs):
"""
Initializer.
Expand All @@ -20,11 +22,6 @@ def __init__(self, family=None, *args, **kwargs):
"""
super(IPDetector_Null, self).__init__(*args, family=family, **kwargs)

@staticmethod
def names():
"""Return a list of string names identifying this class/service."""
return ("null",)

def can_detect_offline(self):
"""Return true, as this detector generates no network traffic.
Expand Down
7 changes: 2 additions & 5 deletions dyndnsc/detector/rand.py
Expand Up @@ -82,18 +82,15 @@ def __iter__(self):
class IPDetector_Random(IPDetector):
"""Detect randomly generated IP addresses."""

configuration_key = "random"

def __init__(self, *args, **kwargs):
"""Initialize."""
super(IPDetector_Random, self).__init__(*args, **kwargs)

self.opts_family = AF_INET
self.rips = RandomIPGenerator()

@staticmethod
def names():
"""Return a list of string names identifying this class/service."""
return ("random",)

def can_detect_offline(self):
"""
Detect the IP address.
Expand Down
7 changes: 2 additions & 5 deletions dyndnsc/detector/socket_ip.py
Expand Up @@ -13,6 +13,8 @@
class IPDetector_Socket(IPDetector):
"""Detect IPs used by the system to communicate with outside world."""

configuration_key = "socket"

def __init__(self, family=None, *args, **kwargs):
"""
Initializer.
Expand All @@ -21,11 +23,6 @@ def __init__(self, family=None, *args, **kwargs):
"""
super(IPDetector_Socket, self).__init__(*args, family=family, **kwargs)

@staticmethod
def names():
"""Return a list of string names identifying this class/service."""
return ("socket",)

def can_detect_offline(self):
"""Return False, this detector works offline."""
# unsure about this. detector does not really transmit data to outside,
Expand Down
7 changes: 2 additions & 5 deletions dyndnsc/detector/teredo.py
Expand Up @@ -21,15 +21,12 @@ class IPDetector_Teredo(IPDetector_Iface):
Inherits IPDetector_Iface and sets default options only.
"""

configuration_key = "teredo"

def __init__(self, iface="tun0", netmask="2001:0000::/32", *args, **kwargs):
"""Initializer."""
super(IPDetector_Teredo, self).__init__(*args, **kwargs)

self.opts_iface = iface
self.opts_netmask = netmask
self.opts_family = AF_INET6

@staticmethod
def names():
"""Return a list of string names identifying this class/service."""
return ("teredo",)
31 changes: 7 additions & 24 deletions dyndnsc/detector/webcheck.py
Expand Up @@ -74,6 +74,7 @@ class IPDetectorWebCheckBase(IPDetector):
"""Base Class for misc. web service based IP detection classes."""

urls = None # override in child class
configuration_key = None

def __init__(self, url=None, parser=None, *args, **kwargs):
"""
Expand All @@ -91,15 +92,6 @@ def can_detect_offline(self):
"""Return false, as this detector generates http traffic."""
return False

@staticmethod
def names():
"""
Return a list of string names identifying this class/service.
Abstract method, must be implemented in subclass.
"""
raise NotImplementedError("Please implement in subclass")

def detect(self):
"""
Try to contact a remote webservice and parse the returned output.
Expand Down Expand Up @@ -130,6 +122,8 @@ class IPDetectorWebCheck(IPDetectorWebCheckBase):
will simply not detect the IP address.
"""

configuration_key = "webcheck"

# TODO: consider throwing out all URLs with no TLS support
urls = (
("http://checkip.eurodyndns.org/", "checkip"),
Expand All @@ -153,11 +147,6 @@ def __init__(self, *args, **kwargs):

self.opts_family = AF_INET

@staticmethod
def names():
"""Return a list of string names identifying this class/service."""
return ("webcheck", "webcheck4")


class IPDetectorWebCheck6(IPDetectorWebCheckBase):
"""
Expand All @@ -169,6 +158,8 @@ class IPDetectorWebCheck6(IPDetectorWebCheckBase):
will simply not detect the IP address.
"""

configuration_key = "webcheck6"

urls = (
("https://ipv6.icanhazip.com/", "plain"),
("https://ipv6.nsupdate.info/myip", "plain"),
Expand All @@ -181,11 +172,6 @@ def __init__(self, *args, **kwargs):

self.opts_family = AF_INET6

@staticmethod
def names():
"""Return a list of string names identifying this class/service."""
return ("webcheck6", )


class IPDetectorWebCheck46(IPDetectorWebCheckBase):
"""
Expand All @@ -211,6 +197,8 @@ class IPDetectorWebCheck46(IPDetectorWebCheckBase):
with its own SOA record and a low TTL.
"""

configuration_key = "webcheck46"

urls = (
("https://icanhazip.com/", "plain"),
("https://www.nsupdate.info/myip", "plain"),
Expand All @@ -222,8 +210,3 @@ def __init__(self, *args, **kwargs):
super(IPDetectorWebCheck46, self).__init__(*args, **kwargs)

self.opts_family = AF_UNSPEC

@staticmethod
def names():
"""Return a list of string names identifying this class/service."""
return ("webcheck46", "webcheck64")
7 changes: 2 additions & 5 deletions dyndnsc/tests/common/test_dynamiccli.py
Expand Up @@ -10,14 +10,11 @@
class Dummy(DynamicCliMixin):
"""A dummy class used to verify parse_cmdline_args() behaviour."""

configuration_key = "dummy"

def __init__(self, userid, password):
"""Initialize. Do nothing."""

@staticmethod
def configuration_key():
"""Return 'dummy', identifying the protocol."""
return "dummy"

@staticmethod
def configuration_key_prefix():
"""Return 'foo', identifying the protocol prefix."""
Expand Down

0 comments on commit c4f5d23

Please sign in to comment.