Skip to content

Commit 6f6d16a

Browse files
committed
Update according to first review
Better documentation and more readable function structrue review: #443 (review) Signed-off-by: Hritik Vijay <hritikxx8@gmail.com>
1 parent 41ff78c commit 6f6d16a

2 files changed

Lines changed: 15 additions & 23 deletions

File tree

vulnerabilities/helpers.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,10 @@ def __lt__(self, other):
167167
return affected_package_with_patched_package_objects
168168

169169

170-
def split_markdown_front_matter(lines: str) -> Tuple[str, str]:
170+
def split_markdown_front_matter(text: str) -> Tuple[str, str]:
171171
r"""
172-
Split text into markdown front matter and the markdown body
173-
Return ("", text) for text with non existing front matter
172+
Return a tuple of (front matter, markdown body) strings split from ``text``.
173+
Each can be an empty string.
174174
175175
>>> text='''---
176176
... title: DUMMY-SECURITY-2019-001
@@ -180,19 +180,13 @@ def split_markdown_front_matter(lines: str) -> Tuple[str, str]:
180180
... # Markdown starts here
181181
... '''
182182
>>> split_markdown_front_matter(text)
183-
('title: DUMMY-SECURITY-2019-001\ndescription: Incorrect access control.\ncves: [CVE-2042-1337]', '# Markdown starts here\n')
183+
('title: DUMMY-SECURITY-2019-001\ndescription: Incorrect access control.\ncves: [CVE-2042-1337]', '# Markdown starts here')
184184
"""
185-
fmlines = []
186-
mdlines = []
187-
splitter = mdlines
188-
189-
lines = lines.replace("\r\n", "\n")
190-
for index, line in enumerate(lines.split("\n")):
191-
if index == 0 and line.strip().startswith("---"):
192-
splitter = fmlines
193-
elif line.strip().startswith("---"):
194-
splitter = mdlines
195-
else:
196-
splitter.append(line)
197-
198-
return "\n".join(fmlines), "\n".join(mdlines)
185+
lines = text.splitlines()
186+
if lines[0] == "---":
187+
lines = lines[1:]
188+
text = "\n".join(lines)
189+
frontmatter, _, markdown = text.partition("\n---\n")
190+
return frontmatter, markdown
191+
192+
return "", text

vulnerabilities/importers/istio.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,8 @@ def process_file(self, path):
174174
return advisories
175175

176176
def get_data_from_md(self, path):
177-
"""Return a mapping of vulnerability data from istio. The data is
178-
in the form of yaml objects found inside front matter of the .md file.
179-
"""
177+
"""Return a mapping of vulnerability data extracted from an advisory."""
180178

181179
with open(path) as f:
182-
yaml_lines, _ = split_markdown_front_matter(f.read())
183-
return yaml.safe_load(yaml_lines)
180+
front_matter, _ = split_markdown_front_matter(f.read())
181+
return yaml.safe_load(front_matter)

0 commit comments

Comments
 (0)