linkchecker/linkcheck/configuration/__init__.py

408 lines
14 KiB
Python
Raw Normal View History

2014-01-08 21:33:04 +00:00
# Copyright (C) 2000-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.
#
2009-07-24 21:58:20 +00:00
# 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.
"""
Store metadata and options.
"""
import importlib.resources
import os
import re
2020-05-14 19:15:28 +00:00
import urllib.parse
2009-07-24 21:16:12 +00:00
import shutil
2012-10-10 08:53:52 +00:00
import socket
2021-12-30 19:27:04 +00:00
from .. import log, LOG_CHECK, PACKAGE_NAME, fileutil
from . import confparse
2021-12-30 19:27:04 +00:00
try:
from .. import _release
except ImportError:
raise SystemExit('Run "hatchling build --hooks-only" first')
Version = _release.__version__
ReleaseDate = _release.__release_date__
CopyrightYear = _release.__copyright_year__
AppName = _release.__app_name__
2020-05-30 16:01:36 +00:00
App = AppName + " " + Version
Author = _release.__author__
2020-04-30 19:11:59 +00:00
HtmlAuthor = Author.replace(' ', ' ')
Copyright = f"Copyright (C) 2000-2016 Bastian Kleineidam, 2010-{CopyrightYear} {Author}"
HtmlCopyright = (
"Copyright © 2000-2016 Bastian Kleineidam, "
f"2010-{CopyrightYear} {HtmlAuthor}")
2020-05-30 16:01:36 +00:00
HtmlAppInfo = App + ", " + HtmlCopyright
Url = _release.__url__
SupportUrl = _release.__support_url__
2022-11-08 19:21:29 +00:00
UserAgent = f"Mozilla/5.0 (compatible; {AppName}/{Version}; +{Url})"
2020-05-30 16:01:36 +00:00
Freeware = (
AppName
+ """ comes with ABSOLUTELY NO WARRANTY!
2020-08-30 17:40:39 +00:00
This is free software, and you are welcome to redistribute it under
2023-03-09 14:42:43 +00:00
certain conditions. Look at the file `COPYING' within this distribution."""
2020-05-30 16:01:36 +00:00
)
2020-05-30 16:01:36 +00:00
def normpath(path):
"""Norm given system path with all available norm or expand functions
in os.path."""
expanded = os.path.expanduser(os.path.expandvars(path))
return os.path.normcase(os.path.normpath(expanded))
2016-01-23 12:28:15 +00:00
# List Python modules in the form (module, name, version attribute)
2011-04-14 10:20:56 +00:00
Modules = (
2020-05-30 16:01:36 +00:00
# required modules
("bs4", "Beautiful Soup", "__version__"),
("dns.version", "dnspython", "version"),
2016-01-23 12:28:15 +00:00
("requests", "Requests", "__version__"),
2020-05-30 16:01:36 +00:00
# optional modules
2020-04-30 19:11:59 +00:00
("argcomplete", "Argcomplete", None),
2020-05-30 16:01:36 +00:00
("GeoIP", "GeoIP", 'lib_version'), # on Unix systems
("pygeoip", "GeoIP", 'lib_version'), # on Windows systems
("sqlite3", "SQLite", 'sqlite_version'),
2020-04-30 19:11:59 +00:00
("meliae", "Meliae", '__version__'),
2011-04-14 10:20:56 +00:00
)
2020-05-30 16:01:36 +00:00
2016-01-23 12:28:15 +00:00
def get_modules_info():
"""Return unicode string with detected module info."""
module_infos = []
for (mod, name, version_attr) in Modules:
try:
module = importlib.import_module(mod)
except ModuleNotFoundError:
2016-01-23 12:28:15 +00:00
continue
if version_attr and (attr := getattr(module, version_attr, None)):
2016-01-23 12:28:15 +00:00
version = attr() if callable(attr) else attr
2022-11-08 19:21:29 +00:00
module_infos.append(f"{name} {version}")
2016-01-23 12:28:15 +00:00
else:
# ignore attribute errors in case library developers
# change the version information attribute
module_infos.append(name)
2020-04-30 19:11:59 +00:00
return "Modules: %s" % (", ".join(module_infos))
2011-04-14 10:20:56 +00:00
2014-09-11 19:19:49 +00:00
def get_system_cert_file():
"""Try to find a system-wide SSL certificate file.
@return: the filename to the cert file
@raises: ValueError when no system cert file could be found
"""
if os.name == 'posix':
filename = "/etc/ssl/certs/ca-certificates.crt"
if os.path.isfile(filename):
return filename
msg = "no system certificate file found"
raise ValueError(msg)
def get_certifi_file():
"""Get the SSL certifications installed by the certifi package.
2020-07-25 15:35:48 +00:00
@return: the filename to the cert file
@rtype: string
@raises: ImportError when certifi is not installed or ValueError when
the file is not found
"""
import certifi
2020-05-30 16:01:36 +00:00
filename = certifi.where()
if os.path.isfile(filename):
return filename
msg = "%s not found; check your certifi installation" % filename
raise ValueError(msg)
# dynamic options
class Configuration(dict):
"""
Storage for configuration options. Options can both be given from
the command line as well as from configuration files.
"""
def __init__(self):
"""
Initialize the default options.
"""
2020-06-03 19:06:36 +00:00
super().__init__()
Fix remaining flake8 violations in linkcheck/ linkcheck/better_exchook2.py:28:89: E501 line too long (90 > 88 characters) linkcheck/better_exchook2.py:155:9: E722 do not use bare 'except' linkcheck/better_exchook2.py:166:9: E722 do not use bare 'except' linkcheck/better_exchook2.py:289:13: E741 ambiguous variable name 'l' linkcheck/better_exchook2.py:299:9: E722 do not use bare 'except' linkcheck/containers.py:48:13: E731 do not assign a lambda expression, use a def linkcheck/ftpparse.py:123:89: E501 line too long (93 > 88 characters) linkcheck/loader.py:46:47: E203 whitespace before ':' linkcheck/logconf.py:45:29: E231 missing whitespace after ',' linkcheck/robotparser2.py:157:89: E501 line too long (95 > 88 characters) linkcheck/robotparser2.py:182:89: E501 line too long (89 > 88 characters) linkcheck/strformat.py:181:16: E203 whitespace before ':' linkcheck/strformat.py:181:43: E203 whitespace before ':' linkcheck/strformat.py:253:9: E731 do not assign a lambda expression, use a def linkcheck/strformat.py:254:9: E731 do not assign a lambda expression, use a def linkcheck/strformat.py:341:89: E501 line too long (111 > 88 characters) linkcheck/url.py:102:32: E203 whitespace before ':' linkcheck/url.py:277:5: E741 ambiguous variable name 'l' linkcheck/url.py:402:5: E741 ambiguous variable name 'l' linkcheck/checker/__init__.py:203:1: E402 module level import not at top of file linkcheck/checker/fileurl.py:200:89: E501 line too long (103 > 88 characters) linkcheck/checker/mailtourl.py:122:60: E203 whitespace before ':' linkcheck/checker/mailtourl.py:157:89: E501 line too long (96 > 88 characters) linkcheck/checker/mailtourl.py:190:89: E501 line too long (109 > 88 characters) linkcheck/checker/mailtourl.py:200:89: E501 line too long (111 > 88 characters) linkcheck/checker/mailtourl.py:249:89: E501 line too long (106 > 88 characters) linkcheck/checker/unknownurl.py:226:23: W291 trailing whitespace linkcheck/checker/urlbase.py:245:89: E501 line too long (101 > 88 characters) linkcheck/configuration/confparse.py:236:89: E501 line too long (186 > 88 characters) linkcheck/configuration/confparse.py:247:89: E501 line too long (111 > 88 characters) linkcheck/configuration/__init__.py:164:9: E266 too many leading '#' for block comment linkcheck/configuration/__init__.py:184:9: E266 too many leading '#' for block comment linkcheck/configuration/__init__.py:190:9: E266 too many leading '#' for block comment linkcheck/configuration/__init__.py:195:9: E266 too many leading '#' for block comment linkcheck/configuration/__init__.py:198:9: E266 too many leading '#' for block comment linkcheck/configuration/__init__.py:435:89: E501 line too long (90 > 88 characters) linkcheck/director/aggregator.py:45:43: E231 missing whitespace after ',' linkcheck/director/aggregator.py:178:89: E501 line too long (106 > 88 characters) linkcheck/logger/__init__.py:29:1: E731 do not assign a lambda expression, use a def linkcheck/logger/__init__.py:108:13: E741 ambiguous variable name 'l' linkcheck/logger/__init__.py:275:19: F821 undefined name '_' linkcheck/logger/__init__.py:342:16: F821 undefined name '_' linkcheck/logger/__init__.py:380:13: F821 undefined name '_' linkcheck/logger/__init__.py:384:13: F821 undefined name '_' linkcheck/logger/__init__.py:387:13: F821 undefined name '_' linkcheck/logger/__init__.py:396:13: F821 undefined name '_' linkcheck/network/__init__.py:1:1: W391 blank line at end of file linkcheck/plugins/locationinfo.py:89:9: E731 do not assign a lambda expression, use a def linkcheck/plugins/locationinfo.py:91:9: E731 do not assign a lambda expression, use a def linkcheck/plugins/markdowncheck.py:112:89: E501 line too long (111 > 88 characters) linkcheck/plugins/markdowncheck.py:141:9: E741 ambiguous variable name 'l' linkcheck/plugins/markdowncheck.py:165:23: E203 whitespace before ':' linkcheck/plugins/viruscheck.py:95:42: E203 whitespace before ':'
2020-05-30 16:01:36 +00:00
# checking options
self["allowedschemes"] = []
self['cookiefile'] = None
self['robotstxt'] = True
self["debugmemory"] = False
self["localwebroot"] = None
2020-05-30 16:01:36 +00:00
self["maxfilesizeparse"] = 1 * 1024 * 1024
self["maxfilesizedownload"] = 5 * 1024 * 1024
self["maxnumurls"] = None
self["maxrunseconds"] = None
self["maxrequestspersecond"] = 10
2014-03-06 20:58:35 +00:00
self["maxhttpredirects"] = 10
self["sslverify"] = True
2014-03-01 19:49:06 +00:00
self["threads"] = 10
self["timeout"] = 60
self["aborttimeout"] = 300
self["recursionlevel"] = -1
self["useragent"] = UserAgent
self["resultcachesize"] = 100000
Fix remaining flake8 violations in linkcheck/ linkcheck/better_exchook2.py:28:89: E501 line too long (90 > 88 characters) linkcheck/better_exchook2.py:155:9: E722 do not use bare 'except' linkcheck/better_exchook2.py:166:9: E722 do not use bare 'except' linkcheck/better_exchook2.py:289:13: E741 ambiguous variable name 'l' linkcheck/better_exchook2.py:299:9: E722 do not use bare 'except' linkcheck/containers.py:48:13: E731 do not assign a lambda expression, use a def linkcheck/ftpparse.py:123:89: E501 line too long (93 > 88 characters) linkcheck/loader.py:46:47: E203 whitespace before ':' linkcheck/logconf.py:45:29: E231 missing whitespace after ',' linkcheck/robotparser2.py:157:89: E501 line too long (95 > 88 characters) linkcheck/robotparser2.py:182:89: E501 line too long (89 > 88 characters) linkcheck/strformat.py:181:16: E203 whitespace before ':' linkcheck/strformat.py:181:43: E203 whitespace before ':' linkcheck/strformat.py:253:9: E731 do not assign a lambda expression, use a def linkcheck/strformat.py:254:9: E731 do not assign a lambda expression, use a def linkcheck/strformat.py:341:89: E501 line too long (111 > 88 characters) linkcheck/url.py:102:32: E203 whitespace before ':' linkcheck/url.py:277:5: E741 ambiguous variable name 'l' linkcheck/url.py:402:5: E741 ambiguous variable name 'l' linkcheck/checker/__init__.py:203:1: E402 module level import not at top of file linkcheck/checker/fileurl.py:200:89: E501 line too long (103 > 88 characters) linkcheck/checker/mailtourl.py:122:60: E203 whitespace before ':' linkcheck/checker/mailtourl.py:157:89: E501 line too long (96 > 88 characters) linkcheck/checker/mailtourl.py:190:89: E501 line too long (109 > 88 characters) linkcheck/checker/mailtourl.py:200:89: E501 line too long (111 > 88 characters) linkcheck/checker/mailtourl.py:249:89: E501 line too long (106 > 88 characters) linkcheck/checker/unknownurl.py:226:23: W291 trailing whitespace linkcheck/checker/urlbase.py:245:89: E501 line too long (101 > 88 characters) linkcheck/configuration/confparse.py:236:89: E501 line too long (186 > 88 characters) linkcheck/configuration/confparse.py:247:89: E501 line too long (111 > 88 characters) linkcheck/configuration/__init__.py:164:9: E266 too many leading '#' for block comment linkcheck/configuration/__init__.py:184:9: E266 too many leading '#' for block comment linkcheck/configuration/__init__.py:190:9: E266 too many leading '#' for block comment linkcheck/configuration/__init__.py:195:9: E266 too many leading '#' for block comment linkcheck/configuration/__init__.py:198:9: E266 too many leading '#' for block comment linkcheck/configuration/__init__.py:435:89: E501 line too long (90 > 88 characters) linkcheck/director/aggregator.py:45:43: E231 missing whitespace after ',' linkcheck/director/aggregator.py:178:89: E501 line too long (106 > 88 characters) linkcheck/logger/__init__.py:29:1: E731 do not assign a lambda expression, use a def linkcheck/logger/__init__.py:108:13: E741 ambiguous variable name 'l' linkcheck/logger/__init__.py:275:19: F821 undefined name '_' linkcheck/logger/__init__.py:342:16: F821 undefined name '_' linkcheck/logger/__init__.py:380:13: F821 undefined name '_' linkcheck/logger/__init__.py:384:13: F821 undefined name '_' linkcheck/logger/__init__.py:387:13: F821 undefined name '_' linkcheck/logger/__init__.py:396:13: F821 undefined name '_' linkcheck/network/__init__.py:1:1: W391 blank line at end of file linkcheck/plugins/locationinfo.py:89:9: E731 do not assign a lambda expression, use a def linkcheck/plugins/locationinfo.py:91:9: E731 do not assign a lambda expression, use a def linkcheck/plugins/markdowncheck.py:112:89: E501 line too long (111 > 88 characters) linkcheck/plugins/markdowncheck.py:141:9: E741 ambiguous variable name 'l' linkcheck/plugins/markdowncheck.py:165:23: E203 whitespace before ':' linkcheck/plugins/viruscheck.py:95:42: E203 whitespace before ':'
2020-05-30 16:01:36 +00:00
# authentication
self["authentication"] = []
2010-10-14 16:36:11 +00:00
self["loginurl"] = None
self["loginuserfield"] = "login"
self["loginpasswordfield"] = "password"
self["loginextrafields"] = {}
Fix remaining flake8 violations in linkcheck/ linkcheck/better_exchook2.py:28:89: E501 line too long (90 > 88 characters) linkcheck/better_exchook2.py:155:9: E722 do not use bare 'except' linkcheck/better_exchook2.py:166:9: E722 do not use bare 'except' linkcheck/better_exchook2.py:289:13: E741 ambiguous variable name 'l' linkcheck/better_exchook2.py:299:9: E722 do not use bare 'except' linkcheck/containers.py:48:13: E731 do not assign a lambda expression, use a def linkcheck/ftpparse.py:123:89: E501 line too long (93 > 88 characters) linkcheck/loader.py:46:47: E203 whitespace before ':' linkcheck/logconf.py:45:29: E231 missing whitespace after ',' linkcheck/robotparser2.py:157:89: E501 line too long (95 > 88 characters) linkcheck/robotparser2.py:182:89: E501 line too long (89 > 88 characters) linkcheck/strformat.py:181:16: E203 whitespace before ':' linkcheck/strformat.py:181:43: E203 whitespace before ':' linkcheck/strformat.py:253:9: E731 do not assign a lambda expression, use a def linkcheck/strformat.py:254:9: E731 do not assign a lambda expression, use a def linkcheck/strformat.py:341:89: E501 line too long (111 > 88 characters) linkcheck/url.py:102:32: E203 whitespace before ':' linkcheck/url.py:277:5: E741 ambiguous variable name 'l' linkcheck/url.py:402:5: E741 ambiguous variable name 'l' linkcheck/checker/__init__.py:203:1: E402 module level import not at top of file linkcheck/checker/fileurl.py:200:89: E501 line too long (103 > 88 characters) linkcheck/checker/mailtourl.py:122:60: E203 whitespace before ':' linkcheck/checker/mailtourl.py:157:89: E501 line too long (96 > 88 characters) linkcheck/checker/mailtourl.py:190:89: E501 line too long (109 > 88 characters) linkcheck/checker/mailtourl.py:200:89: E501 line too long (111 > 88 characters) linkcheck/checker/mailtourl.py:249:89: E501 line too long (106 > 88 characters) linkcheck/checker/unknownurl.py:226:23: W291 trailing whitespace linkcheck/checker/urlbase.py:245:89: E501 line too long (101 > 88 characters) linkcheck/configuration/confparse.py:236:89: E501 line too long (186 > 88 characters) linkcheck/configuration/confparse.py:247:89: E501 line too long (111 > 88 characters) linkcheck/configuration/__init__.py:164:9: E266 too many leading '#' for block comment linkcheck/configuration/__init__.py:184:9: E266 too many leading '#' for block comment linkcheck/configuration/__init__.py:190:9: E266 too many leading '#' for block comment linkcheck/configuration/__init__.py:195:9: E266 too many leading '#' for block comment linkcheck/configuration/__init__.py:198:9: E266 too many leading '#' for block comment linkcheck/configuration/__init__.py:435:89: E501 line too long (90 > 88 characters) linkcheck/director/aggregator.py:45:43: E231 missing whitespace after ',' linkcheck/director/aggregator.py:178:89: E501 line too long (106 > 88 characters) linkcheck/logger/__init__.py:29:1: E731 do not assign a lambda expression, use a def linkcheck/logger/__init__.py:108:13: E741 ambiguous variable name 'l' linkcheck/logger/__init__.py:275:19: F821 undefined name '_' linkcheck/logger/__init__.py:342:16: F821 undefined name '_' linkcheck/logger/__init__.py:380:13: F821 undefined name '_' linkcheck/logger/__init__.py:384:13: F821 undefined name '_' linkcheck/logger/__init__.py:387:13: F821 undefined name '_' linkcheck/logger/__init__.py:396:13: F821 undefined name '_' linkcheck/network/__init__.py:1:1: W391 blank line at end of file linkcheck/plugins/locationinfo.py:89:9: E731 do not assign a lambda expression, use a def linkcheck/plugins/locationinfo.py:91:9: E731 do not assign a lambda expression, use a def linkcheck/plugins/markdowncheck.py:112:89: E501 line too long (111 > 88 characters) linkcheck/plugins/markdowncheck.py:141:9: E741 ambiguous variable name 'l' linkcheck/plugins/markdowncheck.py:165:23: E203 whitespace before ':' linkcheck/plugins/viruscheck.py:95:42: E203 whitespace before ':'
2020-05-30 16:01:36 +00:00
# filtering
self["externlinks"] = []
self["ignoreerrors"] = []
self["ignorewarnings"] = []
self["ignorewarningsforurls"] = []
self["internlinks"] = []
self["checkextern"] = False
Fix remaining flake8 violations in linkcheck/ linkcheck/better_exchook2.py:28:89: E501 line too long (90 > 88 characters) linkcheck/better_exchook2.py:155:9: E722 do not use bare 'except' linkcheck/better_exchook2.py:166:9: E722 do not use bare 'except' linkcheck/better_exchook2.py:289:13: E741 ambiguous variable name 'l' linkcheck/better_exchook2.py:299:9: E722 do not use bare 'except' linkcheck/containers.py:48:13: E731 do not assign a lambda expression, use a def linkcheck/ftpparse.py:123:89: E501 line too long (93 > 88 characters) linkcheck/loader.py:46:47: E203 whitespace before ':' linkcheck/logconf.py:45:29: E231 missing whitespace after ',' linkcheck/robotparser2.py:157:89: E501 line too long (95 > 88 characters) linkcheck/robotparser2.py:182:89: E501 line too long (89 > 88 characters) linkcheck/strformat.py:181:16: E203 whitespace before ':' linkcheck/strformat.py:181:43: E203 whitespace before ':' linkcheck/strformat.py:253:9: E731 do not assign a lambda expression, use a def linkcheck/strformat.py:254:9: E731 do not assign a lambda expression, use a def linkcheck/strformat.py:341:89: E501 line too long (111 > 88 characters) linkcheck/url.py:102:32: E203 whitespace before ':' linkcheck/url.py:277:5: E741 ambiguous variable name 'l' linkcheck/url.py:402:5: E741 ambiguous variable name 'l' linkcheck/checker/__init__.py:203:1: E402 module level import not at top of file linkcheck/checker/fileurl.py:200:89: E501 line too long (103 > 88 characters) linkcheck/checker/mailtourl.py:122:60: E203 whitespace before ':' linkcheck/checker/mailtourl.py:157:89: E501 line too long (96 > 88 characters) linkcheck/checker/mailtourl.py:190:89: E501 line too long (109 > 88 characters) linkcheck/checker/mailtourl.py:200:89: E501 line too long (111 > 88 characters) linkcheck/checker/mailtourl.py:249:89: E501 line too long (106 > 88 characters) linkcheck/checker/unknownurl.py:226:23: W291 trailing whitespace linkcheck/checker/urlbase.py:245:89: E501 line too long (101 > 88 characters) linkcheck/configuration/confparse.py:236:89: E501 line too long (186 > 88 characters) linkcheck/configuration/confparse.py:247:89: E501 line too long (111 > 88 characters) linkcheck/configuration/__init__.py:164:9: E266 too many leading '#' for block comment linkcheck/configuration/__init__.py:184:9: E266 too many leading '#' for block comment linkcheck/configuration/__init__.py:190:9: E266 too many leading '#' for block comment linkcheck/configuration/__init__.py:195:9: E266 too many leading '#' for block comment linkcheck/configuration/__init__.py:198:9: E266 too many leading '#' for block comment linkcheck/configuration/__init__.py:435:89: E501 line too long (90 > 88 characters) linkcheck/director/aggregator.py:45:43: E231 missing whitespace after ',' linkcheck/director/aggregator.py:178:89: E501 line too long (106 > 88 characters) linkcheck/logger/__init__.py:29:1: E731 do not assign a lambda expression, use a def linkcheck/logger/__init__.py:108:13: E741 ambiguous variable name 'l' linkcheck/logger/__init__.py:275:19: F821 undefined name '_' linkcheck/logger/__init__.py:342:16: F821 undefined name '_' linkcheck/logger/__init__.py:380:13: F821 undefined name '_' linkcheck/logger/__init__.py:384:13: F821 undefined name '_' linkcheck/logger/__init__.py:387:13: F821 undefined name '_' linkcheck/logger/__init__.py:396:13: F821 undefined name '_' linkcheck/network/__init__.py:1:1: W391 blank line at end of file linkcheck/plugins/locationinfo.py:89:9: E731 do not assign a lambda expression, use a def linkcheck/plugins/locationinfo.py:91:9: E731 do not assign a lambda expression, use a def linkcheck/plugins/markdowncheck.py:112:89: E501 line too long (111 > 88 characters) linkcheck/plugins/markdowncheck.py:141:9: E741 ambiguous variable name 'l' linkcheck/plugins/markdowncheck.py:165:23: E203 whitespace before ':' linkcheck/plugins/viruscheck.py:95:42: E203 whitespace before ':'
2020-05-30 16:01:36 +00:00
# plugins
self["pluginfolders"] = get_plugin_folders()
self["enabledplugins"] = []
Fix remaining flake8 violations in linkcheck/ linkcheck/better_exchook2.py:28:89: E501 line too long (90 > 88 characters) linkcheck/better_exchook2.py:155:9: E722 do not use bare 'except' linkcheck/better_exchook2.py:166:9: E722 do not use bare 'except' linkcheck/better_exchook2.py:289:13: E741 ambiguous variable name 'l' linkcheck/better_exchook2.py:299:9: E722 do not use bare 'except' linkcheck/containers.py:48:13: E731 do not assign a lambda expression, use a def linkcheck/ftpparse.py:123:89: E501 line too long (93 > 88 characters) linkcheck/loader.py:46:47: E203 whitespace before ':' linkcheck/logconf.py:45:29: E231 missing whitespace after ',' linkcheck/robotparser2.py:157:89: E501 line too long (95 > 88 characters) linkcheck/robotparser2.py:182:89: E501 line too long (89 > 88 characters) linkcheck/strformat.py:181:16: E203 whitespace before ':' linkcheck/strformat.py:181:43: E203 whitespace before ':' linkcheck/strformat.py:253:9: E731 do not assign a lambda expression, use a def linkcheck/strformat.py:254:9: E731 do not assign a lambda expression, use a def linkcheck/strformat.py:341:89: E501 line too long (111 > 88 characters) linkcheck/url.py:102:32: E203 whitespace before ':' linkcheck/url.py:277:5: E741 ambiguous variable name 'l' linkcheck/url.py:402:5: E741 ambiguous variable name 'l' linkcheck/checker/__init__.py:203:1: E402 module level import not at top of file linkcheck/checker/fileurl.py:200:89: E501 line too long (103 > 88 characters) linkcheck/checker/mailtourl.py:122:60: E203 whitespace before ':' linkcheck/checker/mailtourl.py:157:89: E501 line too long (96 > 88 characters) linkcheck/checker/mailtourl.py:190:89: E501 line too long (109 > 88 characters) linkcheck/checker/mailtourl.py:200:89: E501 line too long (111 > 88 characters) linkcheck/checker/mailtourl.py:249:89: E501 line too long (106 > 88 characters) linkcheck/checker/unknownurl.py:226:23: W291 trailing whitespace linkcheck/checker/urlbase.py:245:89: E501 line too long (101 > 88 characters) linkcheck/configuration/confparse.py:236:89: E501 line too long (186 > 88 characters) linkcheck/configuration/confparse.py:247:89: E501 line too long (111 > 88 characters) linkcheck/configuration/__init__.py:164:9: E266 too many leading '#' for block comment linkcheck/configuration/__init__.py:184:9: E266 too many leading '#' for block comment linkcheck/configuration/__init__.py:190:9: E266 too many leading '#' for block comment linkcheck/configuration/__init__.py:195:9: E266 too many leading '#' for block comment linkcheck/configuration/__init__.py:198:9: E266 too many leading '#' for block comment linkcheck/configuration/__init__.py:435:89: E501 line too long (90 > 88 characters) linkcheck/director/aggregator.py:45:43: E231 missing whitespace after ',' linkcheck/director/aggregator.py:178:89: E501 line too long (106 > 88 characters) linkcheck/logger/__init__.py:29:1: E731 do not assign a lambda expression, use a def linkcheck/logger/__init__.py:108:13: E741 ambiguous variable name 'l' linkcheck/logger/__init__.py:275:19: F821 undefined name '_' linkcheck/logger/__init__.py:342:16: F821 undefined name '_' linkcheck/logger/__init__.py:380:13: F821 undefined name '_' linkcheck/logger/__init__.py:384:13: F821 undefined name '_' linkcheck/logger/__init__.py:387:13: F821 undefined name '_' linkcheck/logger/__init__.py:396:13: F821 undefined name '_' linkcheck/network/__init__.py:1:1: W391 blank line at end of file linkcheck/plugins/locationinfo.py:89:9: E731 do not assign a lambda expression, use a def linkcheck/plugins/locationinfo.py:91:9: E731 do not assign a lambda expression, use a def linkcheck/plugins/markdowncheck.py:112:89: E501 line too long (111 > 88 characters) linkcheck/plugins/markdowncheck.py:141:9: E741 ambiguous variable name 'l' linkcheck/plugins/markdowncheck.py:165:23: E203 whitespace before ':' linkcheck/plugins/viruscheck.py:95:42: E203 whitespace before ':'
2020-05-30 16:01:36 +00:00
# output
self['trace'] = False
self['quiet'] = False
self["verbose"] = False
self["warnings"] = True
self["fileoutput"] = []
self['output'] = 'text'
self["status"] = True
self["status_wait_seconds"] = 5
self['logger'] = None
self.status_logger = None
2013-12-11 17:41:55 +00:00
self.loggers = {}
from ..logger import LoggerClasses
2020-05-30 16:01:36 +00:00
2013-12-11 17:41:55 +00:00
for c in LoggerClasses:
key = c.LoggerName
self[key] = {}
self.loggers[key] = c
2014-05-10 19:23:06 +00:00
def set_status_logger(self, status_logger):
"""Set the status logger."""
self.status_logger = status_logger
def logger_new(self, loggername, **kwargs):
2013-12-11 17:41:55 +00:00
"""Instantiate new logger and return it."""
2013-12-13 06:37:21 +00:00
args = self[loggername]
args.update(kwargs)
return self.loggers[loggername](**args)
def logger_add(self, loggerclass):
2013-12-11 17:41:55 +00:00
"""Add a new logger type to the known loggers."""
self.loggers[loggerclass.LoggerName] = loggerclass
2013-12-13 06:39:59 +00:00
self[loggerclass.LoggerName] = {}
def read(self, files=None):
"""
Read settings from given config files.
@raises: LinkCheckerError on syntax errors in the config file(s)
"""
if files is None:
cfiles = []
else:
cfiles = files[:]
if not cfiles:
userconf = get_user_config()
if os.path.isfile(userconf):
cfiles.append(userconf)
# filter invalid files
filtered_cfiles = []
for cfile in cfiles:
if not fileutil.is_valid_config_source(cfile):
log.warn(LOG_CHECK, _("Configuration file %r does not exist."), cfile)
elif not fileutil.is_readable(cfile):
log.warn(LOG_CHECK, _("Configuration file %r is not readable."), cfile)
else:
filtered_cfiles.append(cfile)
log.debug(LOG_CHECK, "reading configuration from %s", filtered_cfiles)
confparse.LCConfigParser(self).read(filtered_cfiles)
def add_auth(self, user=None, password=None, pattern=None):
2011-02-14 20:06:34 +00:00
"""Add given authentication data."""
if not user or not pattern:
2020-05-30 16:01:36 +00:00
log.warn(
LOG_CHECK, _("missing user or URL pattern in authentication data.")
)
return
entry = dict(user=user, password=password, pattern=re.compile(pattern))
self["authentication"].append(entry)
def get_user_password(self, url):
"""Get tuple (user, password) from configured authentication
that matches the given URL.
Both user and password can be None if not specified, or no
authentication matches the given URL.
"""
for auth in self["authentication"]:
if auth['pattern'].match(url):
return (auth['user'], auth['password'])
return (None, None)
def get_connectionlimits(self):
"""Get dict with limit per connection type."""
return {key: self['maxconnections%s' % key] for key in ('http', 'https', 'ftp')}
def sanitize(self):
"Make sure the configuration is consistent."
if self['logger'] is None:
self.sanitize_logger()
2010-10-14 16:36:11 +00:00
if self['loginurl']:
self.sanitize_loginurl()
self.sanitize_plugins()
self.sanitize_ssl()
2012-10-10 08:53:52 +00:00
# set default socket timeout
socket.setdefaulttimeout(self['timeout'])
def sanitize_logger(self):
2011-02-14 20:06:34 +00:00
"""Make logger configuration consistent."""
if not self['output']:
log.warn(LOG_CHECK, _("activating text logger output."))
self['output'] = 'text'
self['logger'] = self.logger_new(self['output'])
def sanitize_loginurl(self):
2011-02-14 20:06:34 +00:00
"""Make login configuration consistent."""
2010-10-14 16:36:11 +00:00
url = self["loginurl"]
disable = False
if self.get_user_password(url) == (None, None):
2020-05-30 16:01:36 +00:00
log.warn(
LOG_CHECK,
_("no user/password authentication data found for login URL."),
)
2010-10-14 16:36:11 +00:00
disable = True
if not url.lower().startswith(("http:", "https:")):
log.warn(LOG_CHECK, _("login URL is not a HTTP URL."))
2010-10-14 16:36:11 +00:00
disable = True
2020-05-14 19:15:28 +00:00
urlparts = urllib.parse.urlsplit(url)
2010-10-14 16:36:11 +00:00
if not urlparts[0] or not urlparts[1] or not urlparts[2]:
log.warn(LOG_CHECK, _("login URL is incomplete."))
2010-10-14 16:36:11 +00:00
disable = True
if disable:
2020-05-30 16:01:36 +00:00
log.warn(LOG_CHECK, _("disabling login URL %(url)s.") % {"url": url})
2010-10-14 16:36:11 +00:00
self["loginurl"] = None
def sanitize_plugins(self):
"""Ensure each plugin is configurable."""
for plugin in self["enabledplugins"]:
if plugin not in self:
self[plugin] = {}
def sanitize_ssl(self):
2014-09-11 19:19:49 +00:00
"""Use local installed certificate file if available.
Tries to get system, then certifi certificate file."""
if self["sslverify"] is True:
try:
2014-09-11 19:19:49 +00:00
self["sslverify"] = get_system_cert_file()
except ValueError:
try:
self["sslverify"] = get_certifi_file()
2014-09-11 19:19:49 +00:00
except (ValueError, ImportError):
pass
def get_user_data():
"""Get the user data folder.
Returns "~/.linkchecker/" if this folder exists,
"$XDG_DATA_HOME/linkchecker" if $XDG_DATA_HOME is set,
else "~/.local/share/linkchecker".
@rtype string
"""
homedotdir = normpath("~/.linkchecker/")
2020-05-30 16:01:36 +00:00
userdata = (
homedotdir
if os.path.isdir(homedotdir)
else os.path.join(
os.environ.get("XDG_DATA_HOME") or os.path.expanduser(
os.path.join("~", ".local", "share")),
"linkchecker")
2020-05-30 16:01:36 +00:00
)
return userdata
2020-05-30 16:01:36 +00:00
def get_plugin_folders():
"""Get linkchecker plugin folders. Default is
"$XDG_DATA_HOME/linkchecker/plugins/" if $XDG_DATA_HOME is set, else
"~/.local/share/linkchecker/plugins/".
"~/.linkchecker/plugins/" is also
supported for backwards compatibility, and is used if it exists."""
folders = []
defaultfolder = os.path.join(get_user_data(), "plugins")
if not os.path.exists(defaultfolder):
try:
make_userdir(defaultfolder)
2014-09-12 17:36:30 +00:00
except Exception as errmsg:
msg = _("could not create plugin directory %(dirname)r: %(errmsg)r")
args = dict(dirname=defaultfolder, errmsg=errmsg)
log.warn(LOG_CHECK, msg % args)
if os.path.exists(defaultfolder):
folders.append(defaultfolder)
return folders
def make_userdir(child):
"""Create a child directory."""
userdir = os.path.dirname(child)
if not os.path.isdir(userdir):
if os.name == 'nt':
# Windows forbids filenames with leading dot unless
# a trailing dot is added.
userdir += "."
2019-04-13 19:37:39 +00:00
os.makedirs(userdir, 0o700)
2009-07-24 21:16:12 +00:00
def get_user_config():
"""Get the user configuration filename.
If the user configuration file does not exist, copy it from the initial
configuration file.
Returns path to user config file (which might not exist due to copy
failures).
@return configuration filename
@rtype string
"""
# per user config settings
homedotfile = normpath("~/.linkchecker/linkcheckerrc")
2020-05-30 16:01:36 +00:00
userconf = (
homedotfile
if os.path.isfile(homedotfile)
else os.path.join(
os.environ.get("XDG_CONFIG_HOME") or os.path.expanduser(
os.path.join("~", ".config")),
"linkchecker", "linkcheckerrc")
2020-05-30 16:01:36 +00:00
)
if not os.path.exists(userconf):
# initial config (with all options explained)
with importlib.resources.path(
f"{PACKAGE_NAME}.data", "linkcheckerrc") as initialconf:
# copy the initial configuration to the user configuration
try:
make_userdir(userconf)
shutil.copy(initialconf, userconf)
except Exception as errmsg:
msg = _(
"could not copy initial configuration file %(src)r"
" to %(dst)r: %(errmsg)r"
)
args = dict(src=initialconf, dst=userconf, errmsg=errmsg)
log.warn(LOG_CHECK, msg % args)
return userconf
def split_hosts(value):
"""Split comma-separated host list."""
return [host for host in value.split(", ") if host]