Skip to content

Commit

Permalink
[Feature] Adding typing annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
PauAndrio committed May 13, 2024
1 parent b450565 commit 3d80e9a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 38 deletions.
15 changes: 8 additions & 7 deletions biobb_common/configuration/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import logging
from pathlib import Path
from biobb_common.tools import file_utils as fu
from typing import Optional
from typing import Dict, Any, Optional

GALAXY_CHARACTER_MAP = {
"__gt__": ">",
Expand All @@ -50,7 +50,7 @@
"__cn__": "\n",
"__cr__": "\r",
"__tc__": "\t",
"__pd__": "#",
"__pd__": "#"
}


Expand Down Expand Up @@ -114,9 +114,7 @@ def get_working_dir_path(self) -> str:

return self.properties.get("working_dir_path")

def get_prop_dic(
self, prefix: Optional[str] = None, global_log: Optional[logging.Logger] = None
) -> dict:
def get_prop_dic(self, prefix: Optional[str] = None, global_log: Optional[logging.Logger] = None) -> Dict[str, Any]:
"""get_prop_dic() returns the properties dictionary where keys are the
step names in the configuration YAML file and every value contains another
nested dictionary containing the keys and values of each step properties section.
Expand All @@ -136,7 +134,8 @@ def get_prop_dic(
Returns:
dict: Dictionary of properties.
"""
prop_dic = dict()

prop_dic: Dict[str, Any] = dict()
prefix = "" if prefix is None else prefix.strip()

# There is no step
Expand Down Expand Up @@ -311,7 +310,7 @@ def get_paths_dic(self, prefix: Optional[str] = None) -> dict:
prop_dic[key2] = str(
Path(
self.properties[self.system]["working_dir_path"]
).joinpath(prefix, key, value)
).joinpath(prefix, key2, value)
)
else:
prop_dic[key2] = str(
Expand All @@ -322,9 +321,11 @@ def get_paths_dic(self, prefix: Optional[str] = None) -> dict:

# Properties with step name
else:

for key in prop_dic:
for key2, value in prop_dic[key].items():
if isinstance(value, str) and value.startswith("dependency"):
dependency_step = value.split("/")[1]
while isinstance(value, str) and value.startswith("dependency"):
dependency_step = value.split("/")[1]
value = prop_dic[value.split("/")[1]][value.split("/")[2]]
Expand Down
62 changes: 32 additions & 30 deletions biobb_common/generic/biobb_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import importlib
import difflib
import typing
from typing import Optional, Mapping, Set, Union, Dict, List
from typing import Optional, Mapping, Set, Union, Dict, List, Any
import warnings
from pathlib import Path
from sys import platform
import shutil
from pydoc import locate
from biobb_common.tools import file_utils as fu
from biobb_common.command_wrapper import cmd_wrapper
from logging import Logger


class BiobbObject:
Expand All @@ -36,59 +37,57 @@ class BiobbObject:
* **container_generic_command** (*str*) - ("run") Which command typically run or exec will be used to execute your image.
"""

def __init__(self, properties: Optional[dict] = None, **kwargs) -> None:
def __init__(self, properties=None, **kwargs) -> None: # type: ignore
properties = properties or {}

# Input/Output files
self.io_dict: Dict[str, Union[str, Dict[str, Union[str, Path]]]] = {"in": {}, "out": {}}
self.io_dict: Dict[str, Dict] = {"in": {}, "out": {}}

# container Specific
self.container_path = properties.get("container_path")
self.container_path: Optional[str] = properties.get("container_path")
self.container_image: str = properties.get("container_image", '')
self.container_volume_path = properties.get(
self.container_volume_path: str = properties.get(
"container_volume_path", "/data")
self.container_working_dir = properties.get("container_working_dir")
self.container_user_id = properties.get("container_user_id")
self.container_shell_path = properties.get(
self.container_working_dir: Optional[str] = properties.get("container_working_dir")
self.container_user_id: Optional[str] = properties.get("container_user_id")
self.container_shell_path: str = properties.get(
"container_shell_path", "/bin/bash -c"
)
self.container_generic_command = properties.get(
self.container_generic_command: str = properties.get(
"container_generic_command", "run"
)

# stage
self.stage_io_dict: Dict[str, Union[str, Dict[str, Union[str, Path]]]] = {"in": {}, "out": {}}
self.stage_io_dict: Dict[str, Any] = {"in": {}, "out": {}}

# Properties common in all BB
self.disable_sandbox: bool = properties.get("disable_sandbox", False)
self.chdir_sandbox: bool = properties.get("chdir_sandbox", False)
self.binary_path = properties.get("binary_path")
self.can_write_console_log = properties.get(
self.binary_path: Optional[str] = properties.get("binary_path")
self.can_write_console_log: bool = properties.get(
"can_write_console_log", True)
self.global_log = properties.get("global_log", None)
self.out_log = None
self.err_log = None
self.prefix = properties.get("prefix", None)
self.step = properties.get("step", None)
self.path = properties.get("path", "")
self.remove_tmp = properties.get("remove_tmp", True)
self.restart = properties.get("restart", False)
self.global_log: Optional[Logger] = properties.get("global_log", None)
self.out_log: Optional[Logger] = None
self.err_log: Optional[Logger] = None
self.prefix: Optional[str] = properties.get("prefix", None)
self.step: Optional[str] = properties.get("step", None)
self.path: str = properties.get("path", "")
self.remove_tmp: bool = properties.get("remove_tmp", True)
self.restart: bool = properties.get("restart", False)
self.cmd: List[str] = []
self.return_code = None
self.return_code: Optional[int] = None
self.tmp_files: List[Union[str, Path]] = []
self.env_vars_dict: typing.Mapping = properties.get(
self.env_vars_dict: typing.Dict = properties.get(
"env_vars_dict", {})
self.shell_path: typing.Union[str, Path] = properties.get(
"shell_path", os.getenv("SHELL", "/bin/bash")
)

self.dev = properties.get("dev", None)
self.check_extensions = properties.get("check_extensions", True)
self.check_var_typing = properties.get("check_var_typing", True)
self.dev: Optional[str] = properties.get("dev", None)
self.check_extensions: bool = properties.get("check_extensions", True)
self.check_var_typing: bool = properties.get("check_var_typing", True)
self.locals_var_dict: Mapping[str, str] = dict()
self.doc_arguments_dict, self.doc_properties_dict = fu.get_doc_dicts(
self.__doc__
)
self.doc_arguments_dict, self.doc_properties_dict = fu.get_doc_dicts(self.__doc__)

try:
self.version = importlib.import_module(
Expand Down Expand Up @@ -135,7 +134,10 @@ def check_properties(
for prop, value in properties.items():
if self.doc_properties_dict.get(prop):
property_type = self.doc_properties_dict[prop].get("type")
if not isinstance(value, locate(property_type)):
classinfo: object = locate(property_type).__class__
if classinfo == type:
classinfo = locate(property_type)
if not isinstance(value, classinfo): # type: ignore
warnings.warn(
f"Warning: {prop} property type not recognized. Got {type(value)} Expected {locate(property_type)}"
)
Expand Down Expand Up @@ -163,7 +165,7 @@ def check_restart(self) -> bool:
)

if self.restart:
if fu.check_complete_files(self.io_dict["out"].values()):
if fu.check_complete_files(self.io_dict["out"].values()): # type: ignore
fu.log(
"Restart is enabled, this step: %s will the skipped" % self.step,
self.out_log,
Expand Down
Empty file added biobb_common/py.typed
Empty file.
3 changes: 3 additions & 0 deletions biobb_common/tools/test_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ def equal(file_a: str, file_b: str, ignore_list: Optional[typing.List[typing.Uni


def compare_line_by_line(file_a: str, file_b: str, ignore_list: typing.List[typing.Union[str, int]]) -> bool:
print(f"Comparing ignoring lines containing this words: {ignore_list}")
print(" FILE_A: "+file_a)
print(" FILE_B: "+file_b)
with open(file_a) as fa, open(file_b) as fb:
for index, (line_a, line_b) in enumerate(zip(fa, fb)):
if index in ignore_list or any(word in line_a for word in ignore_list if isinstance(word, str)):
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"Bioexcel": "https://bioexcel.eu/",
},
packages=setuptools.find_packages(exclude=["docs"]),
package_data={'biobb_common': ['py.typed']},
install_requires=["pyyaml", "requests", "biopython"],
python_requires='>=3.8',
classifiers=[
Expand All @@ -28,6 +29,6 @@
"License :: OSI Approved :: Apache Software License",
"Operating System :: MacOS :: MacOS X",
"Operating System :: POSIX",
"Operating System :: Unix",
"Operating System :: Unix"
],
)

0 comments on commit 3d80e9a

Please sign in to comment.