Skip to content

Commit

Permalink
Add: Add documentation for CVEManger
Browse files Browse the repository at this point in the history
Explain the CVEManager public constructor and methods.
  • Loading branch information
bjoernricks committed Jun 12, 2024
1 parent 4e9303d commit 1d9ba6b
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions greenbone/scap/cve/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@


class CVEManager(AsyncContextManager):
"""
Manager for the CVE database.
Read and write CVEs from and to the database.
"""

def __init__(
self,
db: Database,
Expand All @@ -52,6 +58,17 @@ def __init__(
yield_per: int = DEFAULT_YIELD_PER,
update: bool = True,
) -> None:
"""
Create a new CVEManager.
Args:
db: The database to use.
insert_threshold: The number of CVEs to insert before committing.
Use only when adding single CVEs with add.
yield_per: The number of CVEs to yield per transaction when querying.
update: Whether to update existing CVEs when adding new ones.
Defaults to True.
"""
self._db = db
self._cves: list[CVE] = []
self._insert_threshold = insert_threshold
Expand All @@ -74,12 +91,24 @@ async def __aexit__(
return

async def add(self, cve: CVE) -> None:
"""
Add a CVE to the database.
The CVE will be added to the database when the number of CVEs reaches
the threshold set in the constructor.
"""
self._cves.append(cve)

if len(self._cves) > self._insert_threshold:
await self.add_cves(self._cves)
self._cves = []

async def add_cves(self, cves: Sequence[CVE]) -> None:
"""
Add a sequence of CVEs to the database.
All CVEs will be added to the database in a single transaction.
"""
if not cves:
return

Expand Down Expand Up @@ -130,8 +159,6 @@ async def add_cves(self, cves: Sequence[CVE]) -> None:

await self._insert_foreign_data(transaction, cves)

self._cves = []

async def _insert_foreign_data(
self, connection: AsyncConnection, cves: Sequence[CVE]
) -> None:
Expand Down

0 comments on commit 1d9ba6b

Please sign in to comment.