Refactor deprecated pkg_resources usage
- Locate resources relative to `__file__` - Support egg/zip with `open_file_for_reading`
This commit is contained in:
@@ -8,7 +8,7 @@ PROPKA representation of bonds.
|
|||||||
import logging
|
import logging
|
||||||
import math
|
import math
|
||||||
import json
|
import json
|
||||||
import pkg_resources
|
from pathlib import Path
|
||||||
import propka.calculations
|
import propka.calculations
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
@@ -35,6 +35,8 @@ class BondMaker:
|
|||||||
TODO - the documentation for this class needs to be improved.
|
TODO - the documentation for this class needs to be improved.
|
||||||
"""
|
"""
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
from propka.input import open_file_for_reading
|
||||||
|
|
||||||
# predefined bonding distances
|
# predefined bonding distances
|
||||||
self.distances = {'S-S': DISULFIDE_DISTANCE,
|
self.distances = {'S-S': DISULFIDE_DISTANCE,
|
||||||
'F-F': FLUORIDE_DISTANCE}
|
'F-F': FLUORIDE_DISTANCE}
|
||||||
@@ -51,9 +53,8 @@ class BondMaker:
|
|||||||
+ [self.default_dist_squared])
|
+ [self.default_dist_squared])
|
||||||
self.max_sq_distance = max(distances)
|
self.max_sq_distance = max(distances)
|
||||||
# protein bonding data
|
# protein bonding data
|
||||||
self.data_file_name = (
|
self.data_file_name = Path(__file__).parent / 'protein_bonds.json'
|
||||||
pkg_resources.resource_filename(__name__, 'protein_bonds.json'))
|
with open_file_for_reading(self.data_file_name) as json_file:
|
||||||
with open(self.data_file_name, 'rt') as json_file:
|
|
||||||
self.protein_bonds = json.load(json_file)
|
self.protein_bonds = json.load(json_file)
|
||||||
self.intra_residue_backbone_bonds = {'N': ['CA'], 'CA': ['N', 'C'],
|
self.intra_residue_backbone_bonds = {'N': ['CA'], 'CA': ['N', 'C'],
|
||||||
'C': ['CA', 'O'], 'O': ['C']}
|
'C': ['CA', 'O'], 'O': ['C']}
|
||||||
|
|||||||
@@ -10,10 +10,11 @@ Input routines.
|
|||||||
:func:`get_atom_lines_from_input`) have been removed.
|
:func:`get_atom_lines_from_input`) have been removed.
|
||||||
"""
|
"""
|
||||||
import typing
|
import typing
|
||||||
from typing import Iterator, Tuple
|
from typing import Iterator, Tuple, Union
|
||||||
import contextlib
|
import contextlib
|
||||||
|
import io
|
||||||
|
import zipfile
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from pkg_resources import resource_filename
|
|
||||||
from propka.lib import protein_precheck
|
from propka.lib import protein_precheck
|
||||||
from propka.atom import Atom
|
from propka.atom import Atom
|
||||||
from propka.conformation_container import ConformationContainer
|
from propka.conformation_container import ConformationContainer
|
||||||
@@ -34,6 +35,16 @@ def open_file_for_reading(
|
|||||||
input_file.seek(0)
|
input_file.seek(0)
|
||||||
return contextlib.nullcontext(input_file)
|
return contextlib.nullcontext(input_file)
|
||||||
|
|
||||||
|
input_file = Path(input_file)
|
||||||
|
|
||||||
|
if not input_file.is_file():
|
||||||
|
for p in input_file.parents:
|
||||||
|
if not zipfile.is_zipfile(p):
|
||||||
|
continue
|
||||||
|
zf = zipfile.ZipFile(p)
|
||||||
|
stream = zf.open(str(input_file.relative_to(p)))
|
||||||
|
return io.TextIOWrapper(stream)
|
||||||
|
|
||||||
return contextlib.closing(open(input_file, 'rt'))
|
return contextlib.closing(open(input_file, 'rt'))
|
||||||
|
|
||||||
|
|
||||||
@@ -122,7 +133,7 @@ def read_molecule_file(filename: str, mol_container: MolecularContainer, stream=
|
|||||||
return mol_container
|
return mol_container
|
||||||
|
|
||||||
|
|
||||||
def read_parameter_file(input_file, parameters: Parameters) -> Parameters:
|
def read_parameter_file(input_file: Union[Path, str], parameters: Parameters) -> Parameters:
|
||||||
"""Read a parameter file.
|
"""Read a parameter file.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@@ -133,7 +144,7 @@ def read_parameter_file(input_file, parameters: Parameters) -> Parameters:
|
|||||||
"""
|
"""
|
||||||
# try to locate the parameter file
|
# try to locate the parameter file
|
||||||
try:
|
try:
|
||||||
ifile = resource_filename(__name__, input_file)
|
ifile = Path(__file__).parent / input_file
|
||||||
input_ = open_file_for_reading(ifile)
|
input_ = open_file_for_reading(ifile)
|
||||||
except (IOError, FileNotFoundError, ValueError, KeyError):
|
except (IOError, FileNotFoundError, ValueError, KeyError):
|
||||||
input_ = open_file_for_reading(input_file)
|
input_ = open_file_for_reading(input_file)
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ Implements many of the main functions used to call PROPKA.
|
|||||||
import sys
|
import sys
|
||||||
import logging
|
import logging
|
||||||
import argparse
|
import argparse
|
||||||
import pkg_resources
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@@ -246,7 +246,7 @@ def build_parser(parser=None):
|
|||||||
"--version", action="version", version=f"%(prog)s {propka.__version__}")
|
"--version", action="version", version=f"%(prog)s {propka.__version__}")
|
||||||
group.add_argument(
|
group.add_argument(
|
||||||
"-p", "--parameters", dest="parameters",
|
"-p", "--parameters", dest="parameters",
|
||||||
default=pkg_resources.resource_filename(__name__, "propka.cfg"),
|
default=str(Path(__file__).parent / "propka.cfg"),
|
||||||
help="set the parameter file [{default:s}]")
|
help="set the parameter file [{default:s}]")
|
||||||
try:
|
try:
|
||||||
group.add_argument(
|
group.add_argument(
|
||||||
|
|||||||
29
tests/test_input.py
Normal file
29
tests/test_input.py
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import propka.input as m
|
||||||
|
import zipfile
|
||||||
|
|
||||||
|
|
||||||
|
def test_open_file_for_reading(tmp_path):
|
||||||
|
path = tmp_path / "tmp.txt"
|
||||||
|
path.write_text("One\nTwo\nThree\n")
|
||||||
|
# str
|
||||||
|
with m.open_file_for_reading(str(path)) as outer:
|
||||||
|
assert outer.read() == "One\nTwo\nThree\n"
|
||||||
|
assert outer.closed
|
||||||
|
# Path
|
||||||
|
with m.open_file_for_reading(path) as outer:
|
||||||
|
# TextIO
|
||||||
|
with m.open_file_for_reading(outer) as inner:
|
||||||
|
assert inner.readline() == "One\n"
|
||||||
|
assert not outer.closed
|
||||||
|
assert outer.readline() == "Two\n"
|
||||||
|
assert outer.closed
|
||||||
|
|
||||||
|
|
||||||
|
def test_open_file_for_reading__zipfile(tmp_path):
|
||||||
|
zippath = tmp_path / "tmp.zip"
|
||||||
|
arcname = "foo/bar.txt"
|
||||||
|
with zipfile.ZipFile(zippath, "w") as ziphandle:
|
||||||
|
ziphandle.writestr(arcname, "One\nTwo\nThree\n")
|
||||||
|
with m.open_file_for_reading(zippath / arcname) as outer:
|
||||||
|
assert outer.readline() == "One\n"
|
||||||
|
assert outer.closed
|
||||||
Reference in New Issue
Block a user