Skip to content

Commit

Permalink
Check for write permissions prior to save file
Browse files Browse the repository at this point in the history
  • Loading branch information
dkratzert committed Nov 19, 2024
1 parent 8846e65 commit 14d7892
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion finalcif/appwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1113,7 +1113,7 @@ def make_report_tables(self) -> None:
"""
Generates a report document.
"""
from finalcif.report.tables import make_report_from, make_multi_tables
from finalcif.report.tables import make_multi_tables
from finalcif.report.templated_report import TemplatedReport
current_block = self.ui.datanameComboBox.currentIndex()
if self.cif.doc[current_block].name == 'global':
Expand Down
18 changes: 16 additions & 2 deletions finalcif/cif/cif_file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ def save(self, filename: Union[Path, None] = None) -> None:
if self.is_empty():
print(f'File {filename} is empty.')
return
if not self.is_writable(filename):
raise PermissionError(f'Failed to open {filename.resolve()} for writing: Operation not permitted')
self.order_cif_keys()
print('Saving CIF to', Path(filename).resolve())
if Version(gemmi.__version__) > Version('0.6.3'):
Expand All @@ -285,6 +287,18 @@ def save(self, filename: Union[Path, None] = None) -> None:
else:
self.doc.write_file(str(filename), gemmi.cif.Style.Indent35)

def is_writable(self, filepath: Union[Path, str]) -> bool:
filepath = Path(filepath)
try:
exist = filepath.exists()
with open(filepath, 'wb+') as f:
f.write(b'')
if not exist:
Path(filepath).unlink(missing_ok=True)
return True
except Exception:
return False

def order_cif_keys(self) -> None:
"""
Brings the current CIF in the specific order of the order list.
Expand Down Expand Up @@ -829,7 +843,7 @@ def angles(self, without_H: bool = False) -> Generator:
publ_loop = self.block.find_loop('_geom_angle_publ_flag')
angle = namedtuple('angle', ('label1', 'label2', 'label3', 'angle_val', 'symm1', 'symm2'))
for label1, label2, label3, angle_val, symm1, symm2, publ in \
zip(label1, label2, label3, angle_val, symm1, symm2, publ_loop):
zip(label1, label2, label3, angle_val, symm1, symm2, publ_loop):
if (without_H and (self.ishydrogen(label1) or self.ishydrogen(label2) or
self.ishydrogen(label3)) or (self.yes_not_set(publ))):
continue
Expand Down Expand Up @@ -901,7 +915,7 @@ def hydrogen_bonds(self) -> Generator:
hydr = namedtuple('HydrogenBond', ('label_d', 'label_h', 'label_a', 'dist_dh', 'dist_ha', 'dist_da',
'angle_dha', 'symm'))
for label_d, label_h, label_a, dist_dh, dist_ha, dist_da, angle_dha, symm, publ in (
zip(label_d, label_h, label_a, dist_dh, dist_ha, dist_da, angle_dha, symm, publ_loop)):
zip(label_d, label_h, label_a, dist_dh, dist_ha, dist_da, angle_dha, symm, publ_loop)):
if self.yes_not_set(publ):
continue
if self.picometer:
Expand Down

0 comments on commit 14d7892

Please sign in to comment.