Skip to content

Commit

Permalink
Update according to first review
Browse files Browse the repository at this point in the history
Better documentation and more readable function structrue
review: aboutcode-org#443 (review)

Signed-off-by: Hritik Vijay <hritikxx8@gmail.com>
  • Loading branch information
Hritik14 authored and Pushpit07 committed Jul 27, 2021
1 parent d872763 commit f4ca861
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
30 changes: 12 additions & 18 deletions vulnerabilities/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ def __lt__(self, other):
return affected_package_with_patched_package_objects


def split_markdown_front_matter(lines: str) -> Tuple[str, str]:
def split_markdown_front_matter(text: str) -> Tuple[str, str]:
r"""
Split text into markdown front matter and the markdown body
Return ("", text) for text with non existing front matter
Return a tuple of (front matter, markdown body) strings split from ``text``.
Each can be an empty string.
>>> text='''---
... title: DUMMY-SECURITY-2019-001
Expand All @@ -180,19 +180,13 @@ def split_markdown_front_matter(lines: str) -> Tuple[str, str]:
... # Markdown starts here
... '''
>>> split_markdown_front_matter(text)
('title: DUMMY-SECURITY-2019-001\ndescription: Incorrect access control.\ncves: [CVE-2042-1337]', '# Markdown starts here\n')
('title: DUMMY-SECURITY-2019-001\ndescription: Incorrect access control.\ncves: [CVE-2042-1337]', '# Markdown starts here')
"""
fmlines = []
mdlines = []
splitter = mdlines

lines = lines.replace("\r\n", "\n")
for index, line in enumerate(lines.split("\n")):
if index == 0 and line.strip().startswith("---"):
splitter = fmlines
elif line.strip().startswith("---"):
splitter = mdlines
else:
splitter.append(line)

return "\n".join(fmlines), "\n".join(mdlines)
lines = text.splitlines()
if lines[0] == "---":
lines = lines[1:]
text = "\n".join(lines)
frontmatter, _, markdown = text.partition("\n---\n")
return frontmatter, markdown

return "", text
8 changes: 3 additions & 5 deletions vulnerabilities/importers/istio.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,8 @@ def process_file(self, path):
return advisories

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

with open(path) as f:
yaml_lines, _ = split_markdown_front_matter(f.read())
return saneyaml.load(yaml_lines)
front_matter, _ = split_markdown_front_matter(f.read())
return saneyaml.load(front_matter)

0 comments on commit f4ca861

Please sign in to comment.