Skip to content

Commit

Permalink
Add Database typing
Browse files Browse the repository at this point in the history
  • Loading branch information
Javagedes committed Jan 11, 2024
1 parent d2b55a9 commit 0982ca8
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 23 deletions.
12 changes: 8 additions & 4 deletions edk2toollib/database/edk2_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion edk2toollib/database/tables/base_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand Down
5 changes: 3 additions & 2 deletions edk2toollib/database/tables/environment_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -33,15 +34,15 @@

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:
"""Create the tables necessary for this parser."""
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()

Expand Down
5 changes: 3 additions & 2 deletions edk2toollib/database/tables/inf_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import time
from pathlib import Path
from sqlite3 import Cursor
from typing import Any

from joblib import Parallel, delayed

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)

Expand Down
5 changes: 3 additions & 2 deletions edk2toollib/database/tables/instanced_fv_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -40,15 +41,15 @@ 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."""


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)
Expand Down
20 changes: 14 additions & 6 deletions edk2toollib/database/tables/instanced_inf_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = {}

Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion edk2toollib/database/tables/package_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand Down
5 changes: 3 additions & 2 deletions edk2toollib/database/tables/source_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import time
from pathlib import Path
from sqlite3 import Cursor
from typing import Any

from joblib import Parallel, delayed

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion edk2toollib/uefi/edk2/build_objects/dsc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
4 changes: 2 additions & 2 deletions edk2toollib/uefi/edk2/path_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down

0 comments on commit 0982ca8

Please sign in to comment.