|
1 | 1 | import os |
| 2 | +from pathlib import Path |
2 | 3 | from PyQt5.QtGui import QImage |
3 | 4 | import matplotlib.pyplot as plt |
4 | 5 |
|
5 | | -from planvec.common import DATA_DIR_PATH |
| 6 | +from planvec.common import DATA_REPOSITORY_DIR_PATH |
| 7 | +from planvec.common import DATA_DESKTOP_DIR_PATH |
6 | 8 | from planvec.utils import date_utils |
7 | 9 |
|
8 | 10 | from typing import List |
|
13 | 15 |
|
14 | 16 |
|
15 | 17 | class DataManager: |
16 | | - def __init__(self, date_tag: str = date_utils.get_date_tag()): |
| 18 | + def __init__(self, output_location: str, date_tag: str = date_utils.get_date_tag()): |
17 | 19 | self.date_tag = date_tag |
18 | | - self.out_dir_path = os.path.join(DATA_DIR_PATH, date_tag) |
| 20 | + self.out_dir_path = str(self.output_root_dir_path_from_output_location(output_location) / date_tag) |
19 | 21 | self.try_create_date_output_folder() |
20 | 22 |
|
| 23 | + @staticmethod |
| 24 | + def output_root_dir_path_from_output_location(output_location: str) -> Path: |
| 25 | + if output_location == 'desktop': |
| 26 | + return DATA_DESKTOP_DIR_PATH |
| 27 | + elif output_location == 'repository': |
| 28 | + return DATA_REPOSITORY_DIR_PATH |
| 29 | + else: |
| 30 | + raise ValueError(f'output_location {output_location} not supported.') |
| 31 | + |
21 | 32 | def try_create_date_output_folder(self) -> None: |
22 | 33 | if not os.path.exists(self.out_dir_path): |
23 | 34 | os.makedirs(self.out_dir_path) |
24 | 35 |
|
25 | | - def team_dir_exists(self, team_name: str) -> bool: |
| 36 | + def team_dir_exists(self, school_name: str, team_name: str) -> bool: |
26 | 37 | assert os.path.exists(self.out_dir_path), 'Output folder for this \ |
27 | 38 | date does not exist.' |
28 | 39 |
|
29 | | - return os.path.exists(os.path.join(self.out_dir_path, team_name)) |
| 40 | + return os.path.exists(os.path.join(self.out_dir_path, school_name, team_name)) |
30 | 41 |
|
31 | | - def create_team_folder(self, team_name: str) -> None: |
32 | | - if not self.team_dir_exists(team_name): |
33 | | - os.makedirs(os.path.join(self.out_dir_path, team_name)) |
| 42 | + def create_team_folder(self, school_name, team_name: str) -> None: |
| 43 | + if not self.team_dir_exists(school_name, team_name): |
| 44 | + os.makedirs(os.path.join(self.out_dir_path, school_name, team_name)) |
34 | 45 |
|
35 | | - def _create_save_img_path(self, team_name: str, file_type: str, suffix: str = '', idx: int = None) -> str: |
36 | | - assert self.team_dir_exists(team_name), 'Cannot save. Team directory ' \ |
37 | | - 'does not exist.' |
| 46 | + def _create_save_img_path(self, school_name: str, team_name: str, file_type: str, suffix: str = '', |
| 47 | + idx: int = None) -> str: |
| 48 | + assert self.team_dir_exists(school_name, team_name), 'Cannot save. Team directory ' \ |
| 49 | + 'does not exist.' |
38 | 50 | if idx is None: |
39 | | - idx = self.get_next_team_img_idx(team_name) |
| 51 | + idx = self.get_next_team_img_idx(school_name, team_name) |
40 | 52 | img_name = IMG_NAME_FORMAT.format(idx=idx, |
41 | 53 | team=team_name, |
42 | 54 | date_tag=date_utils.get_date_time_tag(), |
43 | 55 | suffix=suffix, |
44 | 56 | file_type=file_type) |
45 | | - team_dir = os.path.join(self.out_dir_path, team_name) |
| 57 | + team_dir = os.path.join(self.out_dir_path, school_name, team_name) |
46 | 58 | return os.path.join(team_dir, img_name) |
47 | 59 |
|
48 | | - def save_qt_image(self, team_name: str, qt_img: QImage, suffix: str = '', idx: int = None) -> None: |
49 | | - file_path = self._create_save_img_path(team_name, 'jpeg', suffix, idx) |
| 60 | + def save_qt_image(self, school_name: str, team_name: str, qt_img: QImage, suffix: str = '', idx: int = None) -> None: |
| 61 | + file_path = self._create_save_img_path(school_name, team_name, 'jpeg', suffix, idx) |
50 | 62 | qt_img.save(file_path) |
51 | 63 |
|
52 | | - def save_pdf(self, team_name: str, fig: plt.Figure, suffix: str = '', idx: int = None) -> None: |
53 | | - file_path = self._create_save_img_path(team_name, 'pdf', suffix, idx) |
| 64 | + def save_pdf(self, school_name: str, team_name: str, fig: plt.Figure, suffix: str = '', idx: int = None) -> None: |
| 65 | + file_path = self._create_save_img_path(school_name, team_name, 'pdf', suffix, idx) |
54 | 66 | save_output_fig(fig, file_path) |
55 | 67 |
|
56 | | - def load_team_output_file_names(self, team_name: str, endswith: str) -> List[str]: |
| 68 | + def load_team_output_file_names(self, school_name, team_name: str, endswith: str) -> List[str]: |
57 | 69 | """Load all the output file names of a team, e.g. .jpeg or .pdf files.""" |
58 | | - if self.team_dir_exists(team_name): |
59 | | - all_files = os.listdir(os.path.join(self.out_dir_path, team_name)) |
| 70 | + if self.team_dir_exists(school_name, team_name): |
| 71 | + all_files = os.listdir(os.path.join(self.out_dir_path, school_name, team_name)) |
60 | 72 | return [f for f in all_files if f.endswith(endswith)] |
61 | 73 | else: |
62 | 74 | return [] |
63 | 75 |
|
64 | | - def get_next_team_img_idx(self, team_name: str) -> int: |
65 | | - team_img_names = self.load_team_output_file_names(team_name, endswith='jpeg') |
| 76 | + def get_next_team_img_idx(self, school_name, team_name: str) -> int: |
| 77 | + team_img_names = self.load_team_output_file_names(school_name, team_name, endswith='jpeg') |
66 | 78 | img_indices = sorted([int(name.split('_')[0]) for name in team_img_names]) |
67 | 79 | if len(img_indices) == 0: |
68 | 80 | return 0 |
69 | 81 | return img_indices[-1] + 1 |
70 | 82 |
|
71 | | - def load_all_team_names(self) -> List[str]: |
72 | | - items = os.listdir(self.out_dir_path) |
73 | | - return [item for item in items |
74 | | - if os.path.isdir(os.path.join(self.out_dir_path, item))] |
| 83 | + def load_all_team_names(self, school_name: str) -> List[str]: |
| 84 | + if os.path.exists(os.path.join(self.out_dir_path, school_name)): |
| 85 | + items = os.listdir(os.path.join(self.out_dir_path, school_name)) |
| 86 | + return [item for item in items |
| 87 | + if os.path.isdir(os.path.join(self.out_dir_path, school_name, item))] |
| 88 | + else: |
| 89 | + return [] |
75 | 90 |
|
76 | | - def delete_all_team_images_and_pdfs(self, team_name: str): |
| 91 | + def delete_all_team_images_and_pdfs(self, school_name, team_name: str): |
77 | 92 | """Caution: This method deletes all output images stored for a team.""" |
78 | | - team_img_names = self.load_team_output_file_names(team_name, endswith='jpeg') |
79 | | - team_pdf_names = self.load_team_output_file_names(team_name, endswith='pdf') |
| 93 | + team_img_names = self.load_team_output_file_names(school_name, team_name, endswith='jpeg') |
| 94 | + team_pdf_names = self.load_team_output_file_names(school_name, team_name, endswith='pdf') |
80 | 95 | for file_name in team_img_names + team_pdf_names: |
81 | | - os.remove(os.path.join(os.path.join(self.out_dir_path, team_name, file_name))) |
| 96 | + os.remove(os.path.join(os.path.join(self.out_dir_path, school_name, team_name, file_name))) |
82 | 97 |
|
83 | 98 |
|
84 | | -def save_output_fig(out_fig, path=os.path.join(DATA_DIR_PATH, 'default_out_name.pdf')): |
| 99 | +def save_output_fig(out_fig, path=os.path.join(DATA_REPOSITORY_DIR_PATH, 'default_out_name.pdf')): |
85 | 100 | """Stores the output figure of the GUI processing without padding such that |
86 | 101 | the resulting size on paper equals the size of the drawing canvas.""" |
87 | 102 | out_fig.tight_layout(pad=0) |
|
0 commit comments