Skip to content

Commit

Permalink
Add type hints for Bugzilla and FuzzManager connectors
Browse files Browse the repository at this point in the history
  • Loading branch information
tysmith committed May 9, 2024
1 parent e3c1042 commit 89cea94
Show file tree
Hide file tree
Showing 6 changed files with 257 additions and 231 deletions.
29 changes: 16 additions & 13 deletions grizzly/common/bugzilla.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
from pathlib import Path
from shutil import rmtree
from tempfile import mkdtemp
from typing import Any, Generator, List, Optional, Tuple
from zipfile import ZipFile

from bugsy import Bugsy
from bugsy import Bug, Bugsy
from bugsy.errors import BugsyException
from requests.exceptions import ConnectionError as RequestsConnectionError

Expand All @@ -26,15 +27,15 @@
class BugzillaBug:
__slots__ = ("_bug", "_data")

def __init__(self, bug):
def __init__(self, bug: Bug) -> None:
self._bug = bug
self._data = Path(mkdtemp(prefix=f"bug{bug.id}-", dir=grz_tmp("bugzilla")))
self._fetch_attachments()

def __enter__(self):
def __enter__(self) -> "BugzillaBug":
return self

def __exit__(self, *exc):
def __exit__(self, *exc: Any) -> None:
self.cleanup()

def _fetch_attachments(self) -> None:
Expand Down Expand Up @@ -62,7 +63,7 @@ def _fetch_attachments(self) -> None:
continue
(self._data / attachment.file_name).write_bytes(data)

def _unpack_archives(self):
def _unpack_archives(self) -> None:
"""Unpack and remove archives.
Arguments:
Expand All @@ -80,22 +81,24 @@ def _unpack_archives(self):
entry.unlink()
# TODO: add support for other archive types

def assets(self, ignore=None):
def assets(
self, ignore: Optional[Tuple[str]] = None
) -> Generator[Tuple[str, Path], None, None]:
"""Scan files for assets.
Arguments:
ignore (list(str)): Assets not to include in output.
ignore: Assets not to include in output.
Yields:
tuple(str, Path): Name and path to asset.
Asset name and path.
"""
for asset, file in KNOWN_ASSETS.items():
if not ignore or asset not in ignore:
asset_path = self._data / file
if asset_path.is_file():
yield asset, asset_path

def cleanup(self):
def cleanup(self) -> None:
"""Remove attachment data.
Arguments:
Expand All @@ -107,11 +110,11 @@ def cleanup(self):
rmtree(self._data)

@classmethod
def load(cls, bug_id):
def load(cls, bug_id: int) -> Optional["BugzillaBug"]:
"""Load bug information from a Bugzilla instance.
Arguments:
bug_id (int): Bug to load.
bug_id: Bug to load.
Returns:
BugzillaBug
Expand All @@ -128,14 +131,14 @@ def load(cls, bug_id):
LOG.error("Unable to connect to %r (%s)", bugzilla.bugzilla_url, exc)
return None

def testcases(self):
def testcases(self) -> List[Path]:
"""Create a list of potential test cases.
Arguments:
None
Returns:
list(Path): Files and directories that could potentially be test cases.
Files and directories that could potentially be test cases.
"""
# unpack archives
self._unpack_archives()
Expand Down
Loading

0 comments on commit 89cea94

Please sign in to comment.