diff --git a/edk2toollib/database/edk2_db.py b/edk2toollib/database/edk2_db.py index 5fe5d409..00d3560e 100644 --- a/edk2toollib/database/edk2_db.py +++ b/edk2toollib/database/edk2_db.py @@ -10,7 +10,7 @@ import sqlite3 import time import uuid -from typing import Any +from typing import Any, Optional, Type from edk2toollib.database.tables import EnvironmentTable from edk2toollib.database.tables.base_table import TableGenerator @@ -60,7 +60,7 @@ class Edk2DB: db.connection.execute("SELECT * FROM ?", table) """ - def __init__(self, db_path: str, pathobj: Edk2Path = None, **kwargs: dict[str,Any]): + def __init__(self: 'Edk2DB', db_path: str, pathobj: Edk2Path = None, **kwargs: dict[str,Any]) -> 'Edk2DB': """Initializes the database. Args: @@ -72,11 +72,15 @@ def __init__(self, db_path: str, pathobj: Edk2Path = None, **kwargs: dict[str,An self.clear_parsers() self.connection = sqlite3.connect(db_path) - def __enter__(self): + def __enter__(self: 'Edk2DB') -> 'Edk2DB': """Enables the use of the `with` statement.""" return self - def __exit__(self, exc_type, exc_value, traceback): + def __exit__( + self, exc_type: Optional[Type[BaseException]], + exc_value: Optional[BaseException], + traceback: Optional[Any] # noqa: ANN401 + ) -> None: """Enables the use of the `with` statement.""" self.connection.commit() self.connection.close() diff --git a/edk2toollib/database/tables/base_table.py b/edk2toollib/database/tables/base_table.py index 6c8448d7..cbd1416a 100644 --- a/edk2toollib/database/tables/base_table.py +++ b/edk2toollib/database/tables/base_table.py @@ -6,6 +6,7 @@ ## """An interface for a parser that generates a sqlite3 table maintained by Edk2DB.""" import sqlite3 +from typing import Any from edk2toollib.uefi.edk2.path_utilities import Edk2Path @@ -19,7 +20,7 @@ class TableGenerator: the parser has finished executing and has returned. Review sqlite3 documentation for more information on how to interact with the database. """ - def __init__(self, *args, **kwargs): + def __init__(self, *args: Any, **kwargs: Any) -> 'TableGenerator': """Initialize the query with the specific settings.""" def create_tables(self, db_cursor: sqlite3.Cursor) -> None: diff --git a/edk2toollib/database/tables/environment_table.py b/edk2toollib/database/tables/environment_table.py index c7854074..b0036645 100644 --- a/edk2toollib/database/tables/environment_table.py +++ b/edk2toollib/database/tables/environment_table.py @@ -8,6 +8,7 @@ """A module to run a table generator that creates or appends to a table with environment information.""" import datetime import sqlite3 +from typing import Any import git @@ -33,7 +34,7 @@ class EnvironmentTable(TableGenerator): """A Workspace parser that records import environment information for a given parsing execution.""" # noqa: E501 - def __init__(self, *args, **kwargs): + def __init__(self, *args: Any, **kwargs: Any) -> 'EnvironmentTable': """Initialize the query with the specific settings.""" def create_tables(self, db_cursor: sqlite3.Cursor) -> None: @@ -41,7 +42,7 @@ def create_tables(self, db_cursor: sqlite3.Cursor) -> None: db_cursor.execute(CREATE_ENV_VALUES_TABLE_COMMAND) db_cursor.execute(CREATE_ENV_TABLE_COMMAND) - def parse(self, db_cursor: sqlite3.Cursor, pathobj: Edk2Path, id, env) -> None: + def parse(self, db_cursor: sqlite3.Cursor, pathobj: Edk2Path, id: str, env: dict) -> None: """Parses the environment and adds the data to the table.""" dtime = datetime.datetime.now() diff --git a/edk2toollib/database/tables/inf_table.py b/edk2toollib/database/tables/inf_table.py index 4a01cac6..67ba1640 100644 --- a/edk2toollib/database/tables/inf_table.py +++ b/edk2toollib/database/tables/inf_table.py @@ -11,6 +11,7 @@ import time from pathlib import Path from sqlite3 import Cursor +from typing import Any from joblib import Parallel, delayed @@ -41,7 +42,7 @@ class InfTable(TableGenerator): """A Table Generator that parses all INF files in the workspace and generates a table.""" # TODO: Add phase, protocol, guid, ppi, pcd tables and associations once necessary - def __init__(self, *args, **kwargs): + def __init__(self, *args: Any, **kwargs: Any) -> 'InfTable': """Initializes the INF Table Parser. Args: @@ -83,7 +84,7 @@ def parse(self, db_cursor: Cursor, pathobj: Edk2Path, env_id: str, env: dict) -> row = (env_id, "inf", inf["PATH"], "source", source) db_cursor.execute(INSERT_JUNCTION_ROW, row) - def _parse_file(self, filename, pathobj) -> dict: + def _parse_file(self, filename: str, pathobj: Edk2Path) -> dict: inf_parser = InfP().SetEdk2Path(pathobj) inf_parser.ParseFile(filename) diff --git a/edk2toollib/database/tables/instanced_fv_table.py b/edk2toollib/database/tables/instanced_fv_table.py index 40b29b5c..92280fb2 100644 --- a/edk2toollib/database/tables/instanced_fv_table.py +++ b/edk2toollib/database/tables/instanced_fv_table.py @@ -11,6 +11,7 @@ import re import sqlite3 from pathlib import Path +from typing import Any from edk2toollib.database.tables.base_table import TableGenerator from edk2toollib.uefi.edk2.parsers.fdf_parser import FdfParser as FdfP @@ -40,7 +41,7 @@ class InstancedFvTable(TableGenerator): RULEOVERRIDE = re.compile(r'RuleOverride\s*=.+\s+(.+\.inf)', re.IGNORECASE) - def __init__(self, *args, **kwargs): + def __init__(self, *args: Any, **kwargs: Any) -> 'InstancedFvTable': """Initialize the query with the specific settings.""" @@ -48,7 +49,7 @@ def create_tables(self, db_cursor: sqlite3.Cursor) -> None: """Create the tables necessary for this parser.""" db_cursor.execute(CREATE_INSTANCED_FV_TABLE) - def parse(self, db_cursor: sqlite3.Cursor, pathobj: Edk2Path, env_id, env) -> None: + def parse(self, db_cursor: sqlite3.Cursor, pathobj: Edk2Path, env_id: str, env: dict) -> None: """Parse the workspace and update the database.""" self.pathobj = pathobj self.ws = Path(self.pathobj.WorkspacePath) diff --git a/edk2toollib/database/tables/instanced_inf_table.py b/edk2toollib/database/tables/instanced_inf_table.py index 8910f031..129f0b8e 100644 --- a/edk2toollib/database/tables/instanced_inf_table.py +++ b/edk2toollib/database/tables/instanced_inf_table.py @@ -9,8 +9,10 @@ """A module to run the InstancedInf table generator against a dsc, adding instanced inf information to the database.""" import logging import re +import sqlite3 from pathlib import Path from sqlite3 import Cursor +from typing import Any from edk2toollib.database.tables.base_table import TableGenerator from edk2toollib.uefi.edk2.parsers.dsc_parser import DscParser as DscP @@ -78,7 +80,7 @@ class InstancedInfTable(TableGenerator): SECTION_REGEX = re.compile(r"\[(.*)\]") OVERRIDE_REGEX = re.compile(r"\<(.*)\>") - def __init__(self, *args, **kwargs): + def __init__(self, *args: Any, **kwargs: Any) -> 'InstancedInfTable': """Initialize the query with the specific settings.""" self._parsed_infs = {} @@ -131,7 +133,7 @@ def parse(self, db_cursor: Cursor, pathobj: Edk2Path, env_id: str, env: dict) -> inf_entries = self._build_inf_table(dscp) return self._insert_db_rows(db_cursor, env_id, inf_entries) - def _insert_db_rows(self, db_cursor, env_id, inf_entries) -> int: + def _insert_db_rows(self, db_cursor: sqlite3.Cursor, env_id: str, inf_entries: list) -> int: """Inserts data into the database. Inserts all inf's into the instanced_inf table and links source files and used libraries via the junction @@ -156,7 +158,7 @@ def _insert_db_rows(self, db_cursor, env_id, inf_entries) -> int: rows += [(env_id, e["COMPONENT"], e["PATH"], library) for library in e["LIBRARIES_USED"]] db_cursor.executemany(INSERT_INF_TABLE_JUNCTION_ROW, rows) - def _build_inf_table(self, dscp: DscP): + def _build_inf_table(self, dscp: DscP) -> list: """Create the instanced inf entries, including components and libraries. Multiple entries of the same library will exist if multiple components use it. @@ -199,7 +201,7 @@ def _parse_inf_recursively( override_dict: dict, scope: str, visited: list[str] - ): + ) -> str: """Recurses down all libraries starting from a single INF. Will immediately return if the INF has already been visited. @@ -246,7 +248,7 @@ def _parse_inf_recursively( instance, cls, component, library_dict, override_dict, scope, visited ) # Transform path to edk2 relative form (POSIX) - def to_posix(path): + def to_posix(path: str) -> str: if path is None: return None return Path(path).as_posix() @@ -281,7 +283,13 @@ def to_posix(path): }) return to_return - def _lib_to_instance(self, library_class_name, scope, library_dict, override_dict): + def _lib_to_instance( + self, + library_class_name: str, + scope: str, + library_dict: dict, + override_dict: dict + ) -> str: """Converts a library name to the actual instance of the library. This conversion is based off the library section definitions in the DSC. diff --git a/edk2toollib/database/tables/package_table.py b/edk2toollib/database/tables/package_table.py index 4db3bfe6..2687e039 100644 --- a/edk2toollib/database/tables/package_table.py +++ b/edk2toollib/database/tables/package_table.py @@ -8,6 +8,7 @@ """A module to generate a table containing information about a package.""" from pathlib import Path from sqlite3 import Cursor +from typing import Any import git @@ -27,7 +28,7 @@ """ class PackageTable(TableGenerator): """A Table Generator that associates packages with their repositories.""" - def __init__(self, *args, **kwargs): + def __init__(self, *args: Any, **kwargs: Any) -> 'PackageTable': """Initializes the Repository Table Parser. Args: diff --git a/edk2toollib/database/tables/source_table.py b/edk2toollib/database/tables/source_table.py index 05e680f5..d8d23fe4 100644 --- a/edk2toollib/database/tables/source_table.py +++ b/edk2toollib/database/tables/source_table.py @@ -11,6 +11,7 @@ import time from pathlib import Path from sqlite3 import Cursor +from typing import Any from joblib import Parallel, delayed @@ -38,7 +39,7 @@ class SourceTable(TableGenerator): """A Table Generator that parses all c and h files in the workspace.""" - def __init__(self, *args, **kwargs): + def __init__(self, *args: Any, **kwargs: Any) -> 'SourceTable': """Initializes the Source Table Parser. Args: @@ -71,7 +72,7 @@ def parse(self, db_cursor: Cursor, pathobj: Edk2Path, id: str, env: dict) -> Non db_cursor.executemany(INSERT_SOURCE_ROW, src_entries) - def _parse_file(self, ws, filename: Path) -> dict: + def _parse_file(self, _ws: str, filename: Path) -> dict: """Parse a C file and return the results.""" license = "" with open(filename, 'r', encoding='cp850') as f: diff --git a/edk2toollib/uefi/edk2/build_objects/dsc.py b/edk2toollib/uefi/edk2/build_objects/dsc.py index 9c104726..1a508b98 100644 --- a/edk2toollib/uefi/edk2/build_objects/dsc.py +++ b/edk2toollib/uefi/edk2/build_objects/dsc.py @@ -38,7 +38,7 @@ def add(self, item: Any) -> None: # noqa: ANN401 class dsc_dict(collections.OrderedDict): """A dictionary that allows specific classes as headers and sections.""" - def __init__(self, allowed_key_classes: Optional[str]=None, allowed_value_classes: Optional[Any]=None) -> None: + def __init__(self, allowed_key_classes: Optional[str]=None, allowed_value_classes: Optional[Any]=None) -> None: # noqa: ANN401 """Initializes a dsc_dict.""" if allowed_key_classes is None: allowed_key_classes = [] diff --git a/edk2toollib/uefi/edk2/path_utilities.py b/edk2toollib/uefi/edk2/path_utilities.py index ca4bd522..7c6e2efc 100644 --- a/edk2toollib/uefi/edk2/path_utilities.py +++ b/edk2toollib/uefi/edk2/path_utilities.py @@ -133,12 +133,12 @@ def __init__(self, ws: os.PathLike, package_path_list: Iterable[os.PathLike], ) @property - def WorkspacePath(self): + def WorkspacePath(self: 'Edk2Path') -> str: """Workspace Path as a string.""" return str(self._workspace_path) @property - def PackagePathList(self): + def PackagePathList(self: 'Edk2Path') -> list[str]: """List of package paths as strings.""" return [str(p) for p in self._package_path_list]