From 004814e951059dd471e24950d12b908c8371832f Mon Sep 17 00:00:00 2001 From: Henrik Stranneheim Date: Mon, 2 Sep 2024 10:26:02 +0200 Subject: [PATCH] feat(rm): pkg_resource (#3431) ### Changed - Remove pkg_resource use for invoice and delivery report --- cg/apps/invoice/render.py | 17 +++++++++++++---- cg/constants/constants.py | 1 + cg/constants/report.py | 6 ++++-- cg/resources/__init__.py | 21 +++++++-------------- cg/server/invoices/views.py | 2 +- cg/utils/files.py | 26 +++++++++++++++----------- tests/utils/test_files.py | 9 +++++++++ 7 files changed, 50 insertions(+), 32 deletions(-) diff --git a/cg/apps/invoice/render.py b/cg/apps/invoice/render.py index f6a835bb66..e73278f9db 100644 --- a/cg/apps/invoice/render.py +++ b/cg/apps/invoice/render.py @@ -1,8 +1,11 @@ import datetime as dt +from pathlib import Path from openpyxl import Workbook, load_workbook from openpyxl.styles import Border, Font, PatternFill, Side -from pkg_resources import resource_filename + +from cg.constants import FileExtensions +from cg.utils.files import get_project_root_dir def render_xlsx(data: dict) -> Workbook: @@ -34,11 +37,17 @@ def render_xlsx(data: dict) -> Workbook: }] } """ - pkg_dir = __name__.rpartition(".")[0] + project_root_dir = get_project_root_dir() sample_type = "pool" if data["pooled_samples"] else "sample" costcenter = data["cost_center"] - template_path = resource_filename(pkg_dir, f"templates/{costcenter}_{sample_type}_invoice.xlsx") - workbook = load_workbook(template_path) + template_path = Path( + project_root_dir, + "apps", + "invoice", + "templates", + f"{costcenter}_{sample_type}_invoice{FileExtensions.XLSX}", + ) + workbook = load_workbook(template_path.as_posix()) if data["pooled_samples"]: worksheet = workbook["Bilaga Prover"] worksheet["C1"] = costcenter.upper() diff --git a/cg/constants/constants.py b/cg/constants/constants.py index 5e1f7170dc..c7ae23d812 100644 --- a/cg/constants/constants.py +++ b/cg/constants/constants.py @@ -223,6 +223,7 @@ class FileExtensions(StrEnum): TSV: str = ".tsv" TXT: str = ".txt" VCF: str = ".vcf" + XLSX: str = ".xlsx" XML: str = ".xml" YAML: str = ".yaml" diff --git a/cg/constants/report.py b/cg/constants/report.py index 86a8890bdb..48716dbc1f 100644 --- a/cg/constants/report.py +++ b/cg/constants/report.py @@ -1,15 +1,17 @@ """Delivery report constants.""" -from importlib.resources import files from pathlib import Path from cg.constants import DataDelivery from cg.constants.constants import CancerAnalysisType, FileExtensions, Workflow from cg.constants.subject import Sex +from cg.utils.files import get_project_root_dir + +project_root_dir: Path = get_project_root_dir() DELIVERY_REPORT_FILE_NAME: str = f"delivery-report{FileExtensions.HTML}" SWEDAC_LOGO_PATH = Path( - files("cg"), "meta", "report", "templates", "static", "images", "SWEDAC_logo.png" + project_root_dir, "meta", "report", "templates", "static", "images", "SWEDAC_logo.png" ) BALSAMIC_REPORT_ACCREDITED_PANELS: list[str] = ["gmsmyeloid"] diff --git a/cg/resources/__init__.py b/cg/resources/__init__.py index f203a60e1a..36f2723f09 100644 --- a/cg/resources/__init__.py +++ b/cg/resources/__init__.py @@ -1,8 +1,9 @@ from pathlib import Path -import pkg_resources - from cg.constants import FileExtensions +from cg.utils.files import get_project_root_dir + +project_root_dir: Path = get_project_root_dir() RAREDISEASE_BUNDLE_FILENAMES: str = ( Path("resources", "raredisease_bundle_filenames").with_suffix(FileExtensions.YAML).as_posix() @@ -20,18 +21,10 @@ Path("resources", "tomte_bundle_filenames").with_suffix(FileExtensions.YAML).as_posix() ) -RAREDISEASE_BUNDLE_FILENAMES_PATH = Path( - pkg_resources.resource_filename("cg", RAREDISEASE_BUNDLE_FILENAMES) -) +RAREDISEASE_BUNDLE_FILENAMES_PATH = Path(project_root_dir, RAREDISEASE_BUNDLE_FILENAMES) -RNAFUSION_BUNDLE_FILENAMES_PATH: Path = Path( - pkg_resources.resource_filename("cg", RNAFUSION_BUNDLE_FILENAMES) -) +RNAFUSION_BUNDLE_FILENAMES_PATH = Path(project_root_dir, RNAFUSION_BUNDLE_FILENAMES) -TAXPROFILER_BUNDLE_FILENAMES_PATH: Path = Path( - pkg_resources.resource_filename("cg", TAXPROFILER_BUNDLE_FILENAMES) -) +TAXPROFILER_BUNDLE_FILENAMES_PATH = Path(project_root_dir, TAXPROFILER_BUNDLE_FILENAMES) -TOMTE_BUNDLE_FILENAMES_PATH: Path = Path( - pkg_resources.resource_filename("cg", TOMTE_BUNDLE_FILENAMES) -) +TOMTE_BUNDLE_FILENAMES_PATH = Path(project_root_dir, TOMTE_BUNDLE_FILENAMES) diff --git a/cg/server/invoices/views.py b/cg/server/invoices/views.py index 0c50526e95..32c2554b22 100644 --- a/cg/server/invoices/views.py +++ b/cg/server/invoices/views.py @@ -197,7 +197,7 @@ def invoice_template(invoice_id): workbook = render_xlsx(invoice_dict) temp_dir = tempfile.gettempdir() - filename = "Invoice_{}_{}.xlsx".format(invoice_obj.id, cost_center) + filename = f"Invoice_{invoice_obj.id}_{cost_center}.xlsx" excel_path = os.path.join(temp_dir, filename) workbook.save(excel_path) diff --git a/cg/utils/files.py b/cg/utils/files.py index de66999e14..9f8249eba8 100644 --- a/cg/utils/files.py +++ b/cg/utils/files.py @@ -1,13 +1,18 @@ -"""Some helper functions for working with files""" +"""Some helper functions for working with files.""" import logging import os import shutil +from importlib.resources import files from pathlib import Path LOG = logging.getLogger(__name__) +def get_project_root_dir() -> Path: + return Path(files("cg")) + + def get_file_in_directory(directory: Path, file_name: str) -> Path: """Get a file in a directory and subdirectories. Raises: @@ -15,11 +20,10 @@ def get_file_in_directory(directory: Path, file_name: str) -> Path: """ if not directory.is_dir() or not directory.exists(): raise FileNotFoundError(f"Directory {directory} does not exist") - for directory_path, _, files in os.walk(directory): - for file in files: + for directory_path, _, dir_files in os.walk(directory): + for file in dir_files: if file_name == file: - path_to_file = Path(directory_path, file) - return path_to_file + return Path(directory_path, file) raise FileNotFoundError(f"File {file_name} not found in {directory}") @@ -43,10 +47,10 @@ def get_files_in_directory_with_pattern(directory: Path, pattern: str) -> list[P files_with_pattern: list[Path] = [] if not directory.is_dir() or not directory.exists(): raise FileNotFoundError(f"Directory {directory} does not exist") - for directory_path, _, files in os.walk(directory): - for file in files: - if pattern in file: - files_with_pattern.append(Path(directory_path, file)) + for directory_path, _, dir_files in os.walk(directory): + files_with_pattern.extend( + Path(directory_path, file) for file in dir_files if pattern in file + ) if not files_with_pattern: raise FileNotFoundError(f"No files with pattern {pattern} found in {directory}") return files_with_pattern @@ -117,9 +121,9 @@ def link_or_overwrite_file(src: Path, dst: Path) -> None: def get_all_files_in_directory_tree(directory: Path) -> list[Path]: """Get the relative paths of all files in a directory and its subdirectories.""" files_in_directory: list[Path] = [] - for subdir, _, files in os.walk(directory): + for subdir, _, dir_files in os.walk(directory): subdir = Path(subdir).relative_to(directory) - files_in_directory.extend([Path(subdir, file) for file in files]) + files_in_directory.extend([Path(subdir, file) for file in dir_files]) return files_in_directory diff --git a/tests/utils/test_files.py b/tests/utils/test_files.py index 7ef3e7972f..59572a5051 100644 --- a/tests/utils/test_files.py +++ b/tests/utils/test_files.py @@ -9,12 +9,21 @@ get_file_with_pattern_from_list, get_files_in_directory_with_pattern, get_files_matching_pattern, + get_project_root_dir, get_source_creation_time_stamp, remove_directory_and_contents, rename_file, ) +def test_get_project_root_dir(): + # WHEN getting the project root dir + root_dir: Path = get_project_root_dir() + + # THEN return the dir path + assert root_dir.name == "cg" + + def test_get_file_in_directory(nested_directory_with_file: Path, some_file: str): """Test function to get a file in a directory and subdirectories.""" # GIVEN a directory with subdirectories with a file