Skip to content

Commit

Permalink
feat(rm): pkg_resource (#3431)
Browse files Browse the repository at this point in the history
### Changed

- Remove pkg_resource use for invoice and delivery report
  • Loading branch information
henrikstranneheim authored Sep 2, 2024
1 parent fcdde31 commit 004814e
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 32 deletions.
17 changes: 13 additions & 4 deletions cg/apps/invoice/render.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions cg/constants/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
6 changes: 4 additions & 2 deletions cg/constants/report.py
Original file line number Diff line number Diff line change
@@ -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"]
Expand Down
21 changes: 7 additions & 14 deletions cg/resources/__init__.py
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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)
2 changes: 1 addition & 1 deletion cg/server/invoices/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
26 changes: 15 additions & 11 deletions cg/utils/files.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
"""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:
FileNotFoundError: If the directory does not exist.
"""
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}")


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


Expand Down
9 changes: 9 additions & 0 deletions tests/utils/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 004814e

Please sign in to comment.