Skip to content

Commit bbd5d33

Browse files
committed
InvalidLinkBear: Add context
Add context enum to every link parsed containing additional useful information. Related to #1239
1 parent fe7b2f4 commit bbd5d33

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

bears/general/InvalidLinkBear.py

+27-14
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from coalib.results.Diff import Diff
88
from coalib.bears.LocalBear import LocalBear
9+
from coalib.misc.Enum import enum
910
from dependency_management.requirements.PipRequirement import PipRequirement
1011
from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY
1112
from coalib.results.Result import Result
@@ -50,8 +51,7 @@ def parse_pip_vcs_url(link):
5051
return splitted_schema
5152

5253
@staticmethod
53-
def find_links_in_file(file, network_timeout, link_ignore_regex,
54-
link_ignore_list):
54+
def extract_links_from_file(file, link_ignore_regex, link_ignore_list):
5555
link_ignore_regex = re.compile(link_ignore_regex)
5656
regex = re.compile(
5757
r"""
@@ -80,22 +80,35 @@ def find_links_in_file(file, network_timeout, link_ignore_regex,
8080
# Unbalanced parenthesis
8181
(?<!\.)(?<!,) # Exclude trailing `.` or `,` from URL
8282
""", re.VERBOSE)
83+
8384
for line_number, line in enumerate(file):
8485
for match in re.findall(regex, line):
8586
link = match[0]
87+
context = enum(
88+
pip_vcs_url=False)
89+
if link.startswith(('hg+', 'bzr+', 'git+', 'svn+')):
90+
context.pip_vcs_url = True
8691
if not (link_ignore_regex.search(link) or
8792
fnmatch(link, link_ignore_list)):
88-
if link.startswith(('hg+', 'bzr+', 'git+', 'svn+')):
89-
link = InvalidLinkBear.parse_pip_vcs_url(link)
90-
host = urlparse(link).netloc
91-
code = InvalidLinkBear.get_status_code(
92-
link,
93-
network_timeout.get(host)
94-
if host in network_timeout
95-
else network_timeout.get('*')
96-
if '*' in network_timeout
97-
else InvalidLinkBear.DEFAULT_TIMEOUT)
98-
yield line_number + 1, link, code
93+
yield link, line_number, context
94+
95+
def analyze_links_in_file(self, file, network_timeout, link_ignore_regex,
96+
link_ignore_list):
97+
for link, line_number, link_context in self.extract_links_from_file(
98+
file, link_ignore_regex, link_ignore_list):
99+
100+
if link_context.pip_vcs_url:
101+
link = InvalidLinkBear.parse_pip_vcs_url(link)
102+
103+
host = urlparse(link).netloc
104+
code = InvalidLinkBear.get_status_code(
105+
link,
106+
network_timeout.get(host)
107+
if host in network_timeout
108+
else network_timeout.get('*')
109+
if '*' in network_timeout
110+
else InvalidLinkBear.DEFAULT_TIMEOUT)
111+
yield line_number + 1, link, code, link_context
99112

100113
@deprecate_settings(link_ignore_regex='ignore_regex',
101114
network_timeout=('timeout', lambda t: {'*': t}))
@@ -134,7 +147,7 @@ def run(self, filename, file,
134147
if not url == '*' else '*': timeout
135148
for url, timeout in network_timeout.items()}
136149

137-
for line_number, link, code in InvalidLinkBear.find_links_in_file(
150+
for line_number, link, code, context in self.analyze_links_in_file(
138151
file, network_timeout, link_ignore_regex, link_ignore_list):
139152
if code is None:
140153
yield Result.from_values(

0 commit comments

Comments
 (0)