Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importlib resources #185

Merged
merged 2 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 2 additions & 0 deletions devtools/conda-envs/mols.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ dependencies:
- pandas
- scipy
- libffi
- importlib_resources
- pip
- pip:
- pytest
- pytest-resource-path

# Optional
# GUI / plotting
Expand Down
2 changes: 2 additions & 0 deletions devtools/conda-envs/mols_minimal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ dependencies:
- pandas
- scipy
- libffi
- importlib_resources
- pip
- pip:
- pytest
- pytest-resource-path
41 changes: 14 additions & 27 deletions molSimplify/Classes/mGUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import glob
import tempfile
import re
from pkg_resources import resource_filename, Requirement
from importlib_resources import files as resource_files

Check warning on line 42 in molSimplify/Classes/mGUI.py

View check run for this annotation

Codecov / codecov/patch

molSimplify/Classes/mGUI.py#L42

Added line #L42 was not covered by tests
import xml.etree.ElementTree as ET

try:
Expand Down Expand Up @@ -142,8 +142,7 @@
helpAction.triggered.connect(self.qshowhelp)
menu2.addAction(helpAction)
# ## place title top ###
f = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/icons/logo.png")
f = resource_files("molSimplify").joinpath("icons/logo.png")

Check warning on line 145 in molSimplify/Classes/mGUI.py

View check run for this annotation

Codecov / codecov/patch

molSimplify/Classes/mGUI.py#L145

Added line #L145 was not covered by tests
clogo = mQPixmap(f)
self.grid.addWidget(clogo, 2, 8, 1, 10)
self.txtdev = mQLabel('Developed by Kulik group @ MIT', '', 'c', 16)
Expand Down Expand Up @@ -363,8 +362,7 @@
if globs.custom_path:
f = globs.custom_path + "/Ligands/ligands.dict"
else:
f = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/Ligands/ligands.dict")
f = str(resource_files("molSimplify").joinpath("Ligands/ligands.dict"))

Check warning on line 365 in molSimplify/Classes/mGUI.py

View check run for this annotation

Codecov / codecov/patch

molSimplify/Classes/mGUI.py#L365

Added line #L365 was not covered by tests
qcav0 = getligroups(readdict(f))
qcav = [_f for _f in qcav0.split(' ') if _f]
self.etliggrp = mQComboBox(qcav, ctip, 12)
Expand Down Expand Up @@ -613,8 +611,7 @@
self.qctWindow.setPalette(p) # set background color
self.qctWindow.setLayout(self.qctgrid) # set layout

f = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/icons/petachem.png")
f = resource_files("molSimplify").joinpath("icons/petachem.png")

Check warning on line 614 in molSimplify/Classes/mGUI.py

View check run for this annotation

Codecov / codecov/patch

molSimplify/Classes/mGUI.py#L614

Added line #L614 was not covered by tests
c0 = mQPixmap(f)
self.qctgrid.addWidget(c0, 0, 2, 1, 1)
# top text
Expand Down Expand Up @@ -876,10 +873,8 @@
self.sgrid.addWidget(self.jWindow) # add to stacked grid
self.jWindow.setPalette(p) # set background color
self.jWindow.setLayout(self.jgrid) # set layout
f1 = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/icons/sge.png")
f2 = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/icons/slurm.png")
f1 = resource_files("molSimplify").joinpath("icons/sge.png")
f2 = resource_files("molSimplify").joinpath("icons/slurm.png")

Check warning on line 877 in molSimplify/Classes/mGUI.py

View check run for this annotation

Codecov / codecov/patch

molSimplify/Classes/mGUI.py#L876-L877

Added lines #L876 - L877 were not covered by tests
c1 = mQPixmap(f1)
c2 = mQPixmap(f2)
self.jgrid.addWidget(c1, 1, 2, 1, 1)
Expand Down Expand Up @@ -1181,8 +1176,7 @@
self.etcDBmw1 = mQLineEdit('', ctip, 'l', 14)
self.cDBgrid.addWidget(self.etcDBmw1, 11, 8, 1, 1)
# aspirin icon
f = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/icons/chemdb.png")
f = resource_files("molSimplify").joinpath("icons/chemdb.png")

Check warning on line 1179 in molSimplify/Classes/mGUI.py

View check run for this annotation

Codecov / codecov/patch

molSimplify/Classes/mGUI.py#L1179

Added line #L1179 was not covered by tests
c = mQPixmap(f)
relresize(c, c, 0.4)
self.cDBgrid.addWidget(c, 7, 0, 4, 2)
Expand Down Expand Up @@ -1271,8 +1265,7 @@
self.butpret.clicked.connect(self.qretmain)
self.pgrid.addWidget(self.butpret, 7, 3, 1, 2)
# c1p = mPic(self.pWindow,globs.installdir+'/icons/wft1.png',0.04,0.7,0.2)
f = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/icons/wft3.png")
f = resource_files("molSimplify").joinpath("icons/wft3.png")

Check warning on line 1268 in molSimplify/Classes/mGUI.py

View check run for this annotation

Codecov / codecov/patch

molSimplify/Classes/mGUI.py#L1268

Added line #L1268 was not covered by tests
c3p = mQPixmap(f)
self.pgrid.addWidget(c3p, 3, 0, 4, 1)
# c2p = mPic(self.pWindow,globs.installdir+'/icons/wft2.png',0.04,0.035,0.2)
Expand Down Expand Up @@ -1746,8 +1739,7 @@
if globs.custom_path:
f = globs.custom_path + "/Data/coordinations.dict"
else:
f = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/Data/coordinations.dict")
f = str(resource_files("molSimplify").joinpath("Data/coordinations.dict"))

Check warning on line 1742 in molSimplify/Classes/mGUI.py

View check run for this annotation

Codecov / codecov/patch

molSimplify/Classes/mGUI.py#L1742

Added line #L1742 was not covered by tests
with open(f, 'r') as fl:
s = fl.read().splitlines()
if gname.lower() in s:
Expand Down Expand Up @@ -1790,8 +1782,7 @@
fl.write(xyzl)
# write png file
if glob.glob(cfile):
f = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/icons/geoms/")
f = str(resource_files("molSimplify").joinpath("icons/geoms/"))

Check warning on line 1785 in molSimplify/Classes/mGUI.py

View check run for this annotation

Codecov / codecov/patch

molSimplify/Classes/mGUI.py#L1785

Added line #L1785 was not covered by tests
shutil.copy2(cfile, f+gshort+'.png')
choice = QMessageBox.information(
self.geWindow, 'Add', 'Successfully added to the database!')
Expand Down Expand Up @@ -1827,8 +1818,7 @@
if globs.custom_path:
f = globs.custom_path + "/Data/coordinations.dict"
else:
f = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/Data/coordinations.dict")
f = str(resource_files("molSimplify").joinpath("Data/coordinations.dict"))

Check warning on line 1821 in molSimplify/Classes/mGUI.py

View check run for this annotation

Codecov / codecov/patch

molSimplify/Classes/mGUI.py#L1821

Added line #L1821 was not covered by tests
with open(f, 'r') as fl:
s = fl.read()
if gname.lower() not in s and gshort.lower() not in s:
Expand All @@ -1847,8 +1837,7 @@
if globs.custom_path:
f = globs.custom_path + "/Data/coordinations.dict"
else:
f = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/Data/coordinations.dict")
f = str(resource_files("molSimplify").joinpath("Data/coordinations.dict"))

Check warning on line 1840 in molSimplify/Classes/mGUI.py

View check run for this annotation

Codecov / codecov/patch

molSimplify/Classes/mGUI.py#L1840

Added line #L1840 was not covered by tests
with open(f, 'w') as fl:
fl.write(snew)
# remove file
Expand Down Expand Up @@ -2076,8 +2065,7 @@
def viewgeom(self):
# get geometry
geom = self.dcoordg.currentText()
gfname = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/icons/geoms/" + geom + ".png")
gfname = str(resource_files("molSimplify").joinpath(f"icons/geoms/{geom}.png"))

Check warning on line 2068 in molSimplify/Classes/mGUI.py

View check run for this annotation

Codecov / codecov/patch

molSimplify/Classes/mGUI.py#L2068

Added line #L2068 was not covered by tests
if glob.glob(gfname):
rows = self.lgrid.rowCount()
# Clear existing widgets in layout
Expand Down Expand Up @@ -2268,8 +2256,7 @@
# add to box
for i, t in enumerate(qc):
# File f is never actually used? RM 2022/02/17
f = resource_filename(Requirement.parse( # noqa F841
"molSimplify"), "molSimplify/icons/geoms/" + t + ".png")
f = str(resource_files("molSimplify").joinpath(f"icons/geoms/{t}.png")) # noqa F841

Check warning on line 2259 in molSimplify/Classes/mGUI.py

View check run for this annotation

Codecov / codecov/patch

molSimplify/Classes/mGUI.py#L2259

Added line #L2259 was not covered by tests

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable f is not used.
self.dcoordg.addItem(QIcon(t), t)
self.dcoordg.setIconSize(QSize(60, 60))
# set default geometry
Expand Down
5 changes: 2 additions & 3 deletions molSimplify/Classes/mol3D.py
Original file line number Diff line number Diff line change
Expand Up @@ -2672,7 +2672,7 @@ def returnxyz(self):
ss += "%s \t%f\t%f\t%f\n" % (atom.sym, xyz[0], xyz[1], xyz[2])
return (ss)

def readfromxyz(self, filename, ligand_unique_id=False, read_final_optim_step=False):
def readfromxyz(self, filename: str, ligand_unique_id=False, read_final_optim_step=False):
"""
Read XYZ into a mol3D class instance.

Expand All @@ -2693,8 +2693,7 @@ def readfromxyz(self, filename, ligand_unique_id=False, read_final_optim_step=Fa
amassdict = globs.amass()
self.graph = []
self.xyzfile = filename
fname = filename.split('.xyz')[0]
with open(fname + '.xyz', 'r') as f:
with open(filename, 'r') as f:
s = f.read().splitlines()
try:
atom_count = int(s[0])
Expand Down
5 changes: 2 additions & 3 deletions molSimplify/Informatics/MOF/MOF_functionalizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
XYZ_connected,
write_cif,
)
from pkg_resources import resource_filename, Requirement
from importlib_resources import files as resource_files

Check warning on line 19 in molSimplify/Informatics/MOF/MOF_functionalizer.py

View check run for this annotation

Codecov / codecov/patch

molSimplify/Informatics/MOF/MOF_functionalizer.py#L19

Added line #L19 was not covered by tests
import numpy as np
import scipy
import networkx as nx
Expand Down Expand Up @@ -1257,8 +1257,7 @@

# Load in the mol3D from the folder molSimplify folder monofunctionalized_BDC.
functional_group_template = mol3D()
func_group_xyz_path = resource_filename(Requirement.parse(
"molSimplify"), f"molSimplify/Informatics/MOF/monofunctionalized_BDC/{functional_group}.xyz")
func_group_xyz_path = str(resource_files("molSimplify").joinpath(f"Informatics/MOF/monofunctionalized_BDC/{functional_group}.xyz"))

Check warning on line 1260 in molSimplify/Informatics/MOF/MOF_functionalizer.py

View check run for this annotation

Codecov / codecov/patch

molSimplify/Informatics/MOF/MOF_functionalizer.py#L1260

Added line #L1260 was not covered by tests
functional_group_template.readfromxyz(func_group_xyz_path) # This is a whole BDC linker with the requested functional group on it.

# Read information about the important indices of the functional_group_template.
Expand Down
4 changes: 2 additions & 2 deletions molSimplify/Informatics/geometrics.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import numpy as np
import json
from pkg_resources import resource_filename, Requirement
from importlib_resources import files as resource_files

Check warning on line 4 in molSimplify/Informatics/geometrics.py

View check run for this annotation

Codecov / codecov/patch

molSimplify/Informatics/geometrics.py#L4

Added line #L4 was not covered by tests
from molSimplify.Classes.mol3D import mol3D


Expand All @@ -17,7 +17,7 @@
metrics: list, a list of geometric considered.
path2metric: str, the molSimplify path to load geometrics_csd if geometrics_csd is not specified
'''
jsonpath = resource_filename(Requirement.parse("molSimplify"), path2metric)
jsonpath = resource_files("molSimplify").joinpath(path2metric)

Check warning on line 20 in molSimplify/Informatics/geometrics.py

View check run for this annotation

Codecov / codecov/patch

molSimplify/Informatics/geometrics.py#L20

Added line #L20 was not covered by tests
if not isinstance(geometrics_csd, dict):
print("loading csd geometrics...")
with open(jsonpath, "r") as f:
Expand Down
63 changes: 21 additions & 42 deletions molSimplify/Scripts/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
except ImportError:
import openbabel # fallback to version 2
from typing import Any, List, Dict, Tuple, Union, Optional
from pkg_resources import resource_filename, Requirement
from importlib_resources import files as resource_files

from molSimplify.Classes.globalvars import (globalvars,
romans)
Expand All @@ -34,8 +34,7 @@
if globs.custom_path:
f = globs.custom_path + "/Data/coordinations.dict"
else:
f = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/Data/coordinations.dict")
f = resource_files("molSimplify").joinpath("Data/coordinations.dict")

Check warning on line 37 in molSimplify/Scripts/io.py

View check run for this annotation

Codecov / codecov/patch

molSimplify/Scripts/io.py#L37

Added line #L37 was not covered by tests
with open(f, 'r') as f:
s = f.read().splitlines()
s = [_f for _f in s if _f]
Expand Down Expand Up @@ -64,8 +63,7 @@
if globs.custom_path:
f = globs.custom_path + "/Data/coordinations.dict"
else:
f = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/Data/coordinations.dict")
f = resource_files("molSimplify").joinpath("Data/coordinations.dict")
with open(f, 'r') as f:
s = f.read().splitlines()
s = [_f for _f in s if _f]
Expand Down Expand Up @@ -164,8 +162,7 @@
if globs.custom_path: # test if a custom path is used:
licores_path = str(globs.custom_path).rstrip('/') + "/Ligands/ligands.dict"
else:
licores_path = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/Ligands/ligands.dict")
licores_path = resource_files("molSimplify").joinpath("Ligands/ligands.dict")
licores = readdict(licores_path)
if flip:
for ligand in list(licores.keys()):
Expand Down Expand Up @@ -195,8 +192,7 @@
slicores_path = str(globs.custom_path).rstrip(
'/') + "/Ligands/simple_ligands.dict"
else:
slicores_path = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/Ligands/simple_ligands.dict")
slicores_path = resource_files("molSimplify").joinpath("Ligands/simple_ligands.dict")
slicores = readdict(slicores_path)
return slicores

Expand Down Expand Up @@ -245,8 +241,7 @@
if globs.custom_path: # test if a custom path is used:
bcores_path = str(globs.custom_path).rstrip('/') + "/Bind/bind.dict"
else:
bcores_path = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/Bind/bind.dict")
bcores_path = resource_files("molSimplify").joinpath("Bind/bind.dict")
bcores = readdict(bcores_path)
return bcores

Expand Down Expand Up @@ -275,8 +270,7 @@
if globs.custom_path: # test if a custom path is used:
mcores = str(globs.custom_path).rstrip('/') + "/Cores/cores.dict"
else:
mcores = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/Cores/cores.dict")
mcores = resource_files("molSimplify").joinpath("Cores/cores.dict")
mcores = readdict(mcores)
return mcores

Expand Down Expand Up @@ -306,8 +300,7 @@
subcores = str(globs.custom_path).rstrip(
'/') + "/Substrates/substrates.dict"
else:
subcores = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/Substrates/substrates.dict")
subcores = resource_files("molSimplify").joinpath("Substrates/substrates.dict")

Check warning on line 303 in molSimplify/Scripts/io.py

View check run for this annotation

Codecov / codecov/patch

molSimplify/Scripts/io.py#L303

Added line #L303 was not covered by tests
subcores = readdict_sub(subcores)
return subcores

Expand All @@ -323,8 +316,7 @@
if globs.custom_path: # test if a custom path is used:
fname = str(globs.custom_path).rstrip('/') + path
else:
fname = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify"+path)
fname = resource_files("molSimplify").joinpath(path.strip('/'))
d = dict()

with open(fname) as f:
Expand All @@ -348,8 +340,7 @@
if globs.custom_path: # test if a custom path is used:
fname = str(globs.custom_path).rstrip('/') + path
else:
fname = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify"+path)
fname = resource_files("molSimplify").joinpath(path.strip('/'))

Check warning on line 343 in molSimplify/Scripts/io.py

View check run for this annotation

Codecov / codecov/patch

molSimplify/Scripts/io.py#L343

Added line #L343 was not covered by tests
d = dict()

with open(fname) as f:
Expand Down Expand Up @@ -452,8 +443,7 @@
if globs.custom_path:
f = globs.custom_path + "/Data/" + coord + ".dat"
else:
f = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/Data/" + coord + ".dat")
f = resource_files("molSimplify").joinpath(f"Data/{coord}.dat")
with open(f) as f:
txt = [_f for _f in f.read().splitlines() if _f]
b = []
Expand Down Expand Up @@ -485,8 +475,7 @@
if globs.custom_path:
fcore = globs.custom_path + "/Cores/" + dbentry[0]
else:
fcore = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/Cores/" + dbentry[0])
fcore = str(resource_files("molSimplify").joinpath(f"Cores/{dbentry[0]}"))
# check if core xyz/mol file exists
if not glob.glob(fcore):
emsg = "We can't find the core structure file %s right now! Something is amiss. Exiting..\n" % fcore
Expand Down Expand Up @@ -582,8 +571,7 @@
if globs.custom_path:
fsubst = globs.custom_path + "/Substrates/" + var_list_sub_i[0]
else:
fsubst = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/Substrates/" + var_list_sub_i[0])
fsubst = str(resource_files("molSimplify").joinpath(f"Substrates/{var_list_sub_i[0]}"))

Check warning on line 574 in molSimplify/Scripts/io.py

View check run for this annotation

Codecov / codecov/patch

molSimplify/Scripts/io.py#L574

Added line #L574 was not covered by tests
# check if substrate xyz/mol file exists
if not glob.glob(fsubst):
emsg = "We can't find the substrate structure file %s right now! Something is amiss. Exiting..\n" % fsubst
Expand Down Expand Up @@ -706,8 +694,7 @@
if globs.custom_path:
flig = globs.custom_path + "/Ligands/" + dbentry[0]
else:
flig = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/Ligands/" + dbentry[0])
flig = str(resource_files("molSimplify").joinpath(f"Ligands/{dbentry[0]}"))
# check if ligand xyz/mol file exists
print(('looking for '+flig))
if not os.path.isfile(flig):
Expand Down Expand Up @@ -753,7 +740,6 @@
lig.ffopt = dbentry[4][0]
# load from file
elif ('.mol' in userligand or '.xyz' in userligand or '.smi' in userligand or '.sdf' in userligand):
# flig = resource_filename(Requirement.parse("molSimplify"),"molSimplify/" +userligand)
if glob.glob(userligand):
ftype = userligand.split('.')[-1]
# try and catch error if conversion doesn't work
Expand Down Expand Up @@ -818,8 +804,7 @@
if globs.custom_path:
fbind = globs.custom_path + "/Bind/" + bindcores[userbind][0]
else:
fbind = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/Bind/" + bindcores[userbind][0])
fbind = str(resource_files("molSimplify").joinpath(f"Bind/{bindcores[userbind][0]}"))

Check warning on line 807 in molSimplify/Scripts/io.py

View check run for this annotation

Codecov / codecov/patch

molSimplify/Scripts/io.py#L807

Added line #L807 was not covered by tests
# check if bind xyz/mol file exists
if not glob.glob(fbind):
emsg = "We can't find the binding species structure file %s right now! Something is amiss. Exiting..\n" % fbind
Expand Down Expand Up @@ -903,8 +888,7 @@


def plugin_defs() -> str:
plugin_path = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/plugindefines_reference.txt")
plugin_path = str(resource_files("molSimplify").joinpath("plugindefines_reference.txt"))

Check warning on line 891 in molSimplify/Scripts/io.py

View check run for this annotation

Codecov / codecov/patch

molSimplify/Scripts/io.py#L891

Added line #L891 was not covered by tests
return plugin_path

# def get_name(args,rootdir,core,ligname,bind = False,bsmi = False):
Expand Down Expand Up @@ -1144,16 +1128,11 @@
if not os.path.exists(globs.custom_path):
os.makedirs(globs.custom_path)
# copytree cannot overwrite, need to enusre directory does not exist already
core_dir = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/Cores")
li_dir = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/Ligands")
bind_dir = (resource_filename(
Requirement.parse("molSimplify"), "molSimplify/Bind"))
data_dir = (resource_filename(
Requirement.parse("molSimplify"), "molSimplify/Data"))
subs_dir = (resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/Substrates"))
core_dir = resource_files("molSimplify").joinpath("Cores")
li_dir = resource_files("molSimplify").joinpath("Ligands")
bind_dir = resource_files("molSimplify").joinpath("Bind")
data_dir = resource_files("molSimplify").joinpath("Data")
subs_dir = resource_files("molSimplify").joinpath("Substrates")

Check warning on line 1135 in molSimplify/Scripts/io.py

View check run for this annotation

Codecov / codecov/patch

molSimplify/Scripts/io.py#L1131-L1135

Added lines #L1131 - L1135 were not covered by tests
if os.path.exists(str(globs.custom_path).rstrip("/")+"/Cores"):
print('Note: removing old molSimplify data')
shutil.rmtree(str(globs.custom_path).rstrip("/")+"/Cores")
Expand Down
Loading
Loading