Пример #1
0
 def test_updater_interfaces(self):
     from dyndnsc.updater.manager import updater_classes, get_updater_class
     for cls in updater_classes():
         self.assertTrue(hasattr(cls, 'configuration_key'))
         self.assertEqual(cls, get_updater_class(cls.configuration_key()))
         self.assertTrue(hasattr(cls, 'update'))
         self.assertTrue(hasattr(cls, 'register_arguments'))
         self.assertTrue(hasattr(cls, 'help'))
         self.assertEqual(str, type(cls.configuration_key()))
         self.assertTrue(str, type(cls.help()))
     self.assertTrue(len(updater_classes()) > 0)
     self.assertRaises(KeyError, get_updater_class, 'nonexistent')
Пример #2
0
def getDynDnsClientForConfig(config, plugins=None):
    """Factory method to instantiate and initialize a complete and working
    dyndns client

    @param config: a dictionary with configuration pairs
    """
    if config is None:
        return None
    if not 'hostname' in config:
        log.warning("No hostname configured")
        return None
    from .detector import dns
    dns_detector = dns.IPDetector_DNS(config['hostname'])

    from dyndnsc.updater.manager import get_updater_class
    try:
        klass = get_updater_class(config['protocol'])
    except KeyError:
        log.warning("Invalid update protocol: '%s'", config['protocol'])
        return None
    try:
        ip_updater = klass(**config)
    except (AssertionError, KeyError) as exc:
        log.warning("Invalid update protocol configuration: '%s'",
                    repr(config),
                    exc_info=exc)
        return None

    dyndnsclient = DynDnsClient(sleeptime=config['sleeptime'])
    if plugins is not None:
        log.debug("Attaching plugins to dyndnsc")
        dyndnsclient.plugins = plugins
    dyndnsclient.add_updater(ip_updater)
    dyndnsclient.set_dns_detector(dns_detector)

    # allow config['method'] to be a list or a comma-separated string:
    if type(config['method']) != list:
        dummy = config['method'].split(',')
    else:
        dummy = config['method']
    method = dummy[0]
    if len(dummy) > 1:
        method_optlist = dummy[1:]
    else:
        method_optlist = []
    from .detector import manager
    try:
        klass = manager.get_detector_class(method)
    except (KeyError) as exc:
        log.warning("Invalid change detector configuration: '%s'",
                    method,
                    exc_info=exc)
        return None

    # make a dictionary from method_optlist:
    opts = {}
    colon = ":"
    for opt in method_optlist:
        # options are key value pairs, separated by a colon ":"
        # allow white-spaces in input, but strip them here:
        option, dummysep, value = opt.partition(colon)
        option = option.strip()
        if option in opts:
            log.warning("Option '%s' specified more than once, using '%s'.",
                        option, value)
        opts[option] = value.strip()
    try:
        dyndnsclient.set_detector(klass(opts))
    except KeyError as exc:
        log.warning("Invalid change detector parameters: '%s'",
                    opts,
                    exc_info=exc)
        return None

    return dyndnsclient
Пример #3
0
def getDynDnsClientForConfig(config):
    """Factory method to instantiate and initialize a complete and working
    dyndns client

    @param config: a dictionary with configuration pairs
    """
    if config is None:
        return None
    if not 'hostname' in config:
        log.warning("No hostname configured")
        return None
    from .detector import dns
    dns_detector = dns.IPDetector_DNS(config['hostname'])
    from dyndnsc.updater.manager import get_updater_class
    try:
        klass = get_updater_class(config['protocol'])
    except KeyError:
        log.warning("Invalid update protocol: '%s'", config['protocol'])
        return None
    try:
        ip_updater = klass(config)
    except (AssertionError, KeyError) as exc:
        log.warning("Invalid update protocol configuration: '%s'", repr(config),
                 exc_info=exc)
        return None

    dyndnsclient = DynDnsClient(sleeptime=config['sleeptime'])
    dyndnsclient.add_updater(ip_updater)
    dyndnsclient.setDNSDetector(dns_detector)

    # allow config['method'] to be a list or a comma-separated string:
    if type(config['method']) != list:
        dummy = config['method'].split(',')
    else:
        dummy = config['method']
    method = dummy[0]
    if len(dummy) > 1:
        method_optlist = dummy[1:]
    else:
        method_optlist = []
    from .detector import manager
    try:
        klass = manager.get_detector_class(method)
    except (KeyError) as exc:
        log.warning("Invalid change detector configuration: '%s'", method,
                 exc_info=exc)
        return None

    # make a dictionary from method_optlist:
    opts = {}
    colon = ":"
    for opt in method_optlist:
        # options are key value pairs, separated by a colon ":"
        # allow white-spaces in input, but strip them here:
        option, dummysep, value = opt.partition(colon)
        option = option.strip()
        if option in opts:
            log.warning("Option '%s' specified more than once, using '%s'.",
                     option, value)
        opts[option] = value.strip()
    try:
        dyndnsclient.setChangeDetector(klass(opts))
    except KeyError as exc:
        log.warning("Invalid change detector parameters: '%s'", opts, exc_info=exc)
        return None

    return dyndnsclient