From b98090ff3f9f2390ec76817254808233d96ef24c Mon Sep 17 00:00:00 2001 From: Daniel Kratzert Date: Thu, 22 Feb 2024 11:50:15 +0100 Subject: [PATCH] Small refactoring and imports improved --- .../equip_property/author_loop_templates.py | 4 +-- finalcif/equip_property/equipment.py | 3 ++- finalcif/equip_property/properties.py | 4 +-- finalcif/equip_property/tools.py | 1 - finalcif/finalcif_start.py | 4 +-- finalcif/gui/custom_classes.py | 1 - finalcif/gui/dialogs.py | 3 ++- finalcif/report/report_text.py | 4 +-- finalcif/report/templated_report.py | 27 ++++++++++++------- finalcif/tools/settings.py | 4 ++- tests/test_loops.py | 4 +-- tests/test_main_table.py | 1 - tests/test_saint.py | 2 +- tests/test_templated_report.py | 2 +- tests/test_workfolder.py | 2 +- 15 files changed, 35 insertions(+), 31 deletions(-) diff --git a/finalcif/equip_property/author_loop_templates.py b/finalcif/equip_property/author_loop_templates.py index e31afab5..c471cb6f 100644 --- a/finalcif/equip_property/author_loop_templates.py +++ b/finalcif/equip_property/author_loop_templates.py @@ -1,3 +1,4 @@ +from __future__ import annotations # ---------------------------------------------------------------------------- # "THE BEER-WARE LICENSE" (Revision 42): # Daniel Kratzert wrote this file. As long as you retain @@ -5,7 +6,6 @@ # and you think this stuff is worth it, you can buy me a beer in return. # ---------------------------------------------------------------------------- import re -from contextlib import suppress from dataclasses import dataclass from enum import Enum from pathlib import Path @@ -51,7 +51,7 @@ class Author: class AuthorLoops(): - def __init__(self, ui: Ui_FinalCifWindow, cif: CifContainer, app: 'AppWindow'): + def __init__(self, ui: Ui_FinalCifWindow, cif: CifContainer, app: AppWindow): self.ui = ui self.cif = cif self.app = app diff --git a/finalcif/equip_property/equipment.py b/finalcif/equip_property/equipment.py index 328c15e8..fe022b7a 100644 --- a/finalcif/equip_property/equipment.py +++ b/finalcif/equip_property/equipment.py @@ -1,3 +1,4 @@ +from __future__ import annotations from bisect import bisect from pathlib import Path from typing import List, Dict @@ -22,7 +23,7 @@ class Equipment: - def __init__(self, app: 'AppWindow', settings: FinalCifSettings): + def __init__(self, app: AppWindow, settings: FinalCifSettings): self.app = app self.settings = settings if app: diff --git a/finalcif/equip_property/properties.py b/finalcif/equip_property/properties.py index 94878fbd..5b61846a 100644 --- a/finalcif/equip_property/properties.py +++ b/finalcif/equip_property/properties.py @@ -1,3 +1,4 @@ +from __future__ import annotations import threading import time from contextlib import suppress @@ -15,7 +16,6 @@ from finalcif.tools.settings import FinalCifSettings from finalcif.tools import misc - from typing import TYPE_CHECKING if TYPE_CHECKING: @@ -23,7 +23,7 @@ class Properties(QtCore.QObject): - def __init__(self, parent: 'AppWindow', settings: FinalCifSettings): + def __init__(self, parent: AppWindow, settings: FinalCifSettings): super().__init__(parent=parent) self.app = parent self.settings = settings diff --git a/finalcif/equip_property/tools.py b/finalcif/equip_property/tools.py index e37ff90d..392e1471 100644 --- a/finalcif/equip_property/tools.py +++ b/finalcif/equip_property/tools.py @@ -1,4 +1,3 @@ -from pathlib import Path from typing import Union import gemmi.cif diff --git a/finalcif/finalcif_start.py b/finalcif/finalcif_start.py index f7527e2d..2fbd4bfa 100644 --- a/finalcif/finalcif_start.py +++ b/finalcif/finalcif_start.py @@ -5,15 +5,12 @@ # and you think this stuff is worth it, you can buy me a beer in return. # Dr. Daniel Kratzert # ---------------------------------------------------------------------------- -import os import sys import time import traceback from pathlib import Path from typing import Type -from PyQt5.QtGui import QIcon - from finalcif import VERSION from finalcif.app_path import application_path from finalcif.appwindow import AppWindow, DEBUG, app @@ -62,6 +59,7 @@ def main(): file = None app.setQuitOnLastWindowClosed(True) w = AppWindow(file=file) + from PyQt5.QtGui import QIcon app.setWindowIcon(QIcon(os.path.join(application_path, r'icon/finalcif2.png'))) w.setWindowTitle('FinalCif v{}'.format(VERSION)) # w.showMaximized() # For full screen view diff --git a/finalcif/gui/custom_classes.py b/finalcif/gui/custom_classes.py index 5609944e..28f38c9f 100644 --- a/finalcif/gui/custom_classes.py +++ b/finalcif/gui/custom_classes.py @@ -11,7 +11,6 @@ from finalcif.cif.text import retranslate_delimiter from finalcif.gui.combobox import MyComboBox from finalcif.gui.dialogs import show_keyword_help -from finalcif.gui.edit_button import FloatingButtonWidget from finalcif.gui.mixins import ItemTextMixin from finalcif.gui.plaintextedit import MyQPlainTextEdit diff --git a/finalcif/gui/dialogs.py b/finalcif/gui/dialogs.py index 335cec8e..59d2e8c9 100644 --- a/finalcif/gui/dialogs.py +++ b/finalcif/gui/dialogs.py @@ -5,7 +5,7 @@ from PyQt5.QtCore import Qt from PyQt5.QtWidgets import QMessageBox, QMainWindow, QFileDialog, QVBoxLayout, QTextEdit, \ - QPushButton, QFrame, QApplication + QPushButton, QFrame from finalcif import VERSION @@ -226,6 +226,7 @@ def cif_file_save_dialog(filename: str) -> str: if __name__ == '__main__': from PyQt5 import QtWidgets + from PyQt5.QtWidgets import QApplication app = QApplication.instance() if app is None: diff --git a/finalcif/report/report_text.py b/finalcif/report/report_text.py index dc319359..22df9233 100644 --- a/finalcif/report/report_text.py +++ b/finalcif/report/report_text.py @@ -268,8 +268,8 @@ def __init__(self, cif: CifContainer, paragraph: Paragraph, ref: ReferenceList): refineref = SHELXLReference() if 'OLEX' in refined.upper(): refineref = Olex2Reference() - if 'NOSPHERA2' in solution_prog.upper() or 'NOSPHERA2' in cif['_refine_special_details'].upper() \ - or 'NOSPHERA2' in cif['_olex2_refine_details'].upper(): + if ('NOSPHERA2' in solution_prog.upper() or 'NOSPHERA2' in cif['_refine_special_details'].upper() + or 'NOSPHERA2' in cif['_olex2_refine_details'].upper()): refineref = [Olex2Reference(), Nosphera2Reference()] refine_coef = gstr(cif['_refine_ls_structure_factor_coef']) if manual_method: diff --git a/finalcif/report/templated_report.py b/finalcif/report/templated_report.py index 4f777a34..fce90244 100644 --- a/finalcif/report/templated_report.py +++ b/finalcif/report/templated_report.py @@ -444,6 +444,21 @@ def get_integration_program(self, cif: CifContainer) -> str: def get_absortion_correction_program(self, cif: CifContainer) -> str: absdetails = cif['_exptl_absorpt_process_details'].replace('-', ' ').replace(':', '') + bruker_scaling = cif['_computing_bruker_data_scaling'].replace('-', ' ').replace(':', '') + scale_prog, version = self._get_scaling_program(absdetails) + if not scale_prog: + scale_prog, version = self._get_scaling_program(bruker_scaling) + if 'SORTAV' in absdetails.upper(): + scale_prog = 'SORTAV' + self.literature['absorption'] = SORTAVReference() + if 'crysalis' in absdetails.lower(): + scale_prog = 'SCALE3 ABSPACK' + # see above also + scale_prog += " " + version + + return scale_prog + + def _get_scaling_program(self, absdetails: str) -> tuple[str, str]: scale_prog = '' version = '' if 'SADABS' in absdetails.upper() or 'TWINABS' in absdetails.upper(): @@ -456,15 +471,7 @@ def get_absortion_correction_program(self, cif: CifContainer) -> str: else: scale_prog = 'TWINABS' self.literature['absorption'] = SadabsTwinabsReference() - if 'SORTAV' in absdetails.upper(): - scale_prog = 'SORTAV' - self.literature['absorption'] = SORTAVReference() - if 'crysalis' in absdetails.lower(): - scale_prog = 'SCALE3 ABSPACK' - # see above also - scale_prog += " " + version - - return scale_prog + return scale_prog, version def solution_method(self, cif: CifContainer) -> str: solution_method = gstr(cif['_atom_sites_solution_primary']) or '??' @@ -487,7 +494,7 @@ def refinement_prog(self, cif: CifContainer) -> str: if 'OLEX' in refined.upper(): self.literature['refinement'] = Olex2Reference() if ('NOSPHERA2' in refined.upper() or 'NOSPHERA2' in cif['_refine_special_details'].upper() or - 'NOSPHERAT2' in cif['_olex2_refine_details'].upper()): + 'NOSPHERAT2' in cif['_olex2_refine_details'].upper()): self.literature['refinement'] = Nosphera2Reference() return refined.split()[0] diff --git a/finalcif/tools/settings.py b/finalcif/tools/settings.py index e555afc1..a9088835 100644 --- a/finalcif/tools/settings.py +++ b/finalcif/tools/settings.py @@ -191,7 +191,9 @@ def _load_settings(self, property: str, item_name: str): self.settings.beginGroup(property) try: v = self.settings.value(item_name) - except TypeError: + except TypeError as e: + print(f"DBG> Item name '{item_name}' could not be loaded from settings.") + print(e) v = None self.settings.endGroup() return v diff --git a/tests/test_loops.py b/tests/test_loops.py index 5b62f4ef..b02b5297 100644 --- a/tests/test_loops.py +++ b/tests/test_loops.py @@ -6,8 +6,6 @@ # ---------------------------------------------------------------------------- import os -import gemmi.cif - os.environ["RUNNING_TEST"] = 'True' import unittest from pathlib import Path @@ -244,4 +242,4 @@ def test_move_row_up_from_start_save_and_load_again(self): except AttributeError: self.assertEqual('C', as_string(c.loops[3][0, 0])) self.assertEqual('H', as_string(c.loops[3][1, 0])) - self.assertEqual('O', as_string(c.loops[3][2, 0])) \ No newline at end of file + self.assertEqual('O', as_string(c.loops[3][2, 0])) diff --git a/tests/test_main_table.py b/tests/test_main_table.py index 40469d59..44b6f9f7 100644 --- a/tests/test_main_table.py +++ b/tests/test_main_table.py @@ -10,7 +10,6 @@ import unittest from pathlib import Path -import pytest from PyQt5.QtCore import Qt from PyQt5.QtGui import QColor diff --git a/tests/test_saint.py b/tests/test_saint.py index 472de1cc..f2e88069 100644 --- a/tests/test_saint.py +++ b/tests/test_saint.py @@ -4,7 +4,6 @@ # this notice you can do whatever you want with this stuff. If we meet some day, # and you think this stuff is worth it, you can buy me a beer in return. # ---------------------------------------------------------------------------- -import os import unittest from pathlib import Path @@ -13,6 +12,7 @@ data = Path('.') + class MyTestCase(unittest.TestCase): def test_saint_repr(self): diff --git a/tests/test_templated_report.py b/tests/test_templated_report.py index 6601caa7..934df89e 100644 --- a/tests/test_templated_report.py +++ b/tests/test_templated_report.py @@ -95,7 +95,7 @@ def test_ccdc_num_in_table(self): t = TemplatedReport() ok = t.make_templated_report(options=self.options, cif=CifContainer(self.testcif), - output_filename=self.reportdoc, + output_filename=str(self.reportdoc), picfile=self.report_pic, template_path=self.text_template) self.assertTrue(ok) diff --git a/tests/test_workfolder.py b/tests/test_workfolder.py index 7d8567f8..8de88802 100644 --- a/tests/test_workfolder.py +++ b/tests/test_workfolder.py @@ -34,12 +34,12 @@ def setUp(self) -> None: def tearDown(self) -> None: Path('foo.cif').unlink(missing_ok=True) Path('tests/examples/work/cu_BruecknerJK_153F40_0m-finalcif.cif').unlink(missing_ok=True) + self.myapp.ui.trackChangesCifCheckBox.setChecked(False) self.myapp.close() def test_save_action(self): self.myapp.save_current_cif_file() self.assertEqual(True, (data / 'examples/work/cu_BruecknerJK_153F40_0m-finalcif.cif').exists()) - self.myapp.ui.trackChangesCifCheckBox.setChecked(False) class TestWorkfolder(unittest.TestCase):