| 9 | from middle import __VERSION__ |
| 10 | |
| 11 | class SaveManager: |
| 12 | def __init__(self) -> None: |
| 13 | self._results = ResultSetting() #Settings.setting["Results"] |
| 14 | self._output = self._results.output |
| 15 | |
| 16 | if self._output is None: |
| 17 | home = os.path.expanduser("~") |
| 18 | self._output = os.path.join(home,'.local','share','mapXplore') |
| 19 | self._results.output = self._output |
| 20 | |
| 21 | def _create_directory(self, directory:str): |
| 22 | if not os.path.exists(directory): |
| 23 | os.makedirs(directory) |
| 24 | |
| 25 | def _write_content(self, content, name): |
| 26 | |
| 27 | if isinstance(content, bytes): |
| 28 | with(open(name,'wb')) as fb: |
| 29 | fb.write(content) |
| 30 | else: |
| 31 | with(open(name,'w')) as fb: |
| 32 | fb.write(content) |
| 33 | |
| 34 | def save(self, content, format, name=None, directory:str=None) -> str: |
| 35 | output_directory = directory if directory is not None else self._output |
| 36 | self._create_directory(output_directory) |
| 37 | if name is None: |
| 38 | name = Hashing.get_md5(content) |
| 39 | if format is not None: |
| 40 | name+=f".{format}" |
| 41 | |
| 42 | path = os.path.join(output_directory, f"{name}.{format}") |
| 43 | |
| 44 | self._write_content(content, path) |
| 45 | return path |
| 46 | |
| 47 | def convert_content_to_plain(self, items:QueryResult)->str: |
| 48 | self._output_dir = ResultSetting().get_folder_output(ResultSetting().format) |
| 49 | fileType = ResultSetting().get_FileType |
| 50 | if fileType == ResultSetting.ExportFileType.Excel: |
| 51 | return self.convert_to_csv(items) |
| 52 | elif fileType == ResultSetting.ExportFileType.JSON: |
| 53 | return self.convert_to_json(items) |
| 54 | else: |
| 55 | files = [] |
| 56 | for item in items: |
| 57 | html = self.convert_to_html(item.results) |
| 58 | file = self.save(html, 'html', directory=self._output_dir) |
| 59 | files.append({"name":item.value, "file":os.path.basename(file)}) |
| 60 | return self._create_index(files) |
| 61 | |
| 62 | def _create_index(self, files:list[str]) ->str: |
| 63 | env = Environment(loader=FileSystemLoader(".")) |
| 64 | index_template = env.get_template('/templates/index.html') |
| 65 | html = index_template.render(files=files) |
| 66 | output = self.save(html, 'html', 'index', self._output_dir) |
| 67 | return output |
| 68 |
no outgoing calls
no test coverage detected