Skip to content

Commit

Permalink
Don't raise gemmi ModuleNotFoundError unless gemmi is used (dials#2592
Browse files Browse the repository at this point in the history
)

* Hide gemmi imports so `ImportError` is not raised unless gemmi is used

* News fragment

* Removed whitespace introduced by accident

* ... and add missing whitespace

* Make gemmi import "Up-front but nullable", re-raise if needed

* Make is specific: `ModuleNotFoundError`
  • Loading branch information
Baharis authored Jan 31, 2024
1 parent e36f939 commit bf73681
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions newsfragments/2592.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Reorganize imports in ``export_mtz.py`` so that its gemmi-less objects can be used by other gemmi-less software
10 changes: 9 additions & 1 deletion src/dials/util/export_mtz.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
from math import isclose
from typing import List, Optional

import gemmi
import numpy as np
import pandas as pd
try:
import gemmi
except ModuleNotFoundError as e:
gemmi = None
gemmi_import_error = e

from cctbx import uctbx
from dxtbx import flumpy
Expand Down Expand Up @@ -310,6 +314,8 @@ def add_batch_list(
if max_batch_number > batch_offset:
batch_offset = max_batch_number

if gemmi is None:
raise gemmi_import_error
batch = gemmi.Mtz.Batch()

# Setting fields that are the same for all batches
Expand Down Expand Up @@ -670,6 +676,8 @@ def export_mtz(
)

# Create the mtz file
if gemmi is None:
raise gemmi_import_error
mtz = gemmi.Mtz(with_base=True)
mtz.title = f"From {env.dispatcher_name}"
date_str = time.strftime("%Y-%m-%d at %H:%M:%S %Z")
Expand Down

0 comments on commit bf73681

Please sign in to comment.