File: __init__.py

package info (click to toggle)
linkchecker 9.3-1
  • links: PTS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 4,392 kB
  • sloc: python: 27,603; lex: 1,141; yacc: 821; makefile: 408; sh: 138; ansic: 95; sql: 20; awk: 4
file content (128 lines) | stat: -rw-r--r-- 4,552 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# -*- coding: iso-8859-1 -*-
# Copyright (C) 2014 Bastian Kleineidam
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""
Module for plugin management.
"""
from .. import loader, log, LOG_PLUGIN
from ..decorators import notimplemented


class _PluginBase(object):
    """Basic plugin class featuring plugin identification and
    helper functions."""

    def __init__(self, config):
        """Add plugin-specific configuration."""
        pass

    @notimplemented
    def applies_to(self, url_data, **kwargs):
        """See if url_data should be handled by this plugin."""
        pass

    @notimplemented
    def check(self, url_data):
        """Common check method run for all plugins."""
        pass

    @classmethod
    def read_config(cls, configparser):
        """Read configuration file options."""
        pass


class _ConnectionPlugin(_PluginBase):
    """Plugins run after connection checks."""
    pass


class _ContentPlugin(_PluginBase):
    """Plugins run for valid URLs with content."""
    pass


class _ParserPlugin(_PluginBase):
    """Plugins run for valid URLs to parse their contents."""
    pass


def get_plugin_modules(folders, package='plugins',
                       parentpackage='linkcheck.dummy'):
    """Get plugin modules for given folders."""
    for folder in folders:
        for module in loader.get_folder_modules(folder, parentpackage):
            yield module
    for module in loader.get_package_modules(package):
        yield module


def get_plugin_classes(modules):
    """Get plugin classes for given modules."""
    classes = (_ConnectionPlugin, _ContentPlugin, _ParserPlugin)
    return loader.get_plugins(modules, classes)


class PluginManager(object):
    """Manage all connection and content plugins."""

    def __init__(self, config):
        """Load enabled plugins."""
        self.connection_plugins = []
        self.content_plugins = []
        self.parser_plugins = []
        folders = config["pluginfolders"]
        modules = get_plugin_modules(folders)
        self.load_modules(modules, config)

    def load_modules(self, modules, config):
        """Load plugin modules."""
        for pluginclass in get_plugin_classes(modules):
            name = pluginclass.__name__
            if name in config["enabledplugins"]:
                if issubclass(pluginclass, _ConnectionPlugin):
                    log.debug(LOG_PLUGIN, "Enable connection plugin %s", name)
                    self.connection_plugins.append(pluginclass(config[name]))
                elif issubclass(pluginclass, _ContentPlugin):
                    log.debug(LOG_PLUGIN, "Enable content plugin %s", name)
                    self.content_plugins.append(pluginclass(config[name]))
                elif issubclass(pluginclass, _ParserPlugin):
                    log.debug(LOG_PLUGIN, "Enable parser plugin %s", name)
                    self.parser_plugins.append(pluginclass(config[name]))
                else:
                    raise ValueError("Invalid plugin class %s" % pluginclass)

    def run_connection_plugins(self, url_data):
        """Run all connection plugins."""
        run_plugins(self.connection_plugins, url_data)

    def run_content_plugins(self, url_data):
        """Run all content plugins."""
        run_plugins(self.content_plugins, url_data)

    def run_parser_plugins(self, url_data, pagetype):
        """Run parser plugins for given pagetype."""
        run_plugins(self.parser_plugins, url_data, stop_after_match=True, pagetype=pagetype)


def run_plugins(plugins, url_data, stop_after_match=False, **kwargs):
    """Run the check(url_data) method of given plugins."""
    for plugin in plugins:
        log.debug(LOG_PLUGIN, "Run plugin %s", plugin.__class__.__name__)
        if plugin.applies_to(url_data, **kwargs):
            plugin.check(url_data)
            if stop_after_match:
                break