Skip to content

Commit

Permalink
fix apps for imp
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmkrieger committed Sep 19, 2024
1 parent 4648ee5 commit ad74568
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 24 deletions.
14 changes: 8 additions & 6 deletions docs/apps/docapps.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#!/usr/bin/python

import os
from prody.utilities.misctools import importImp
imp = importImp()
import sys
import importlib
from subprocess import Popen, PIPE

path = [importlib.util.find_spec("prody").submodule_search_locations[0]]
apps = imp.load_module('prody.apps',
*imp.find_module('apps', path))
from prody.utilities.misctools import impLoadModule

for cmd, subcmds in [('prody', apps.PRODY_APPS), ('evol', apps.EVOL_APPS)]:
path_apps = importlib.util.find_spec("prody.apps").submodule_search_locations[0]

prody_apps = impLoadModule('prody.apps.prody_apps', path_apps + '/prody_apps/', '__init__')
evol_apps = impLoadModule('prody.apps.evol_apps', path_apps + '/evol_apps/', '__init__')

for cmd, subcmds in [('prody', prody_apps.PRODY_APPS), ('evol', evol_apps.EVOL_APPS)]:

pipe = Popen([cmd, '-h'], stdout=PIPE, stderr=PIPE)
with open(os.path.join(cmd, cmd + '.txt'), 'w') as rst:
Expand Down
21 changes: 14 additions & 7 deletions prody/apps/evol_apps/__init__.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
"""This module defines some sequence evolution applications."""

from prody.utilities.misctools import importImp
imp = importImp()
import importlib
import importlib.machinery
import importlib.resources
import sys

try:
import argparse
except ImportError:
from .. import argparse

from ..apptools import *
from prody.apps.apptools import *
from prody.utilities.misctools import impLoadModule

if sys.version_info[0] == 2:
import imp
path_prody = imp.find_module('prody')[1]
else:
path_prody = importlib.util.find_spec("prody").submodule_search_locations[0]
path_apps = imp.find_module('apps', [path_prody])[1]
path_apps = imp.find_module('evol_apps', [path_apps])[1]

try:
import imp
path_apps = imp.find_module('apps', [path_prody])[1]
path_apps = imp.find_module('evol_apps', [path_apps])[1]
except ModuleNotFoundError:
path_apps = importlib.util.find_spec("prody.apps").submodule_search_locations[0]
path_apps += '/evol_apps/'

EVOL_APPS = ['search', 'fetch', 'filter', 'refine', 'merge', 'occupancy',
'conserv', 'coevol', 'rankorder']
Expand Down Expand Up @@ -82,8 +90,7 @@

for cmd in EVOL_APPS:
cmd = 'evol_' + cmd
mod = imp.load_module('prody.apps.evol_apps.' + cmd,
*imp.find_module(cmd, [path_apps]))
mod = impLoadModule('prody.apps.evol_apps.', cmd, path_apps)
mod.APP.addApplication(evol_commands)


Expand Down
17 changes: 11 additions & 6 deletions prody/apps/prody_apps/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""This module defines structure and dynamics analysis applications."""

from prody.utilities.misctools import importImp
imp = importImp()
import importlib
import sys

Expand All @@ -10,14 +8,22 @@
except ImportError:
from .. import argparse

from ..apptools import *
from prody.apps.apptools import *
from prody.utilities.misctools import impLoadModule

if sys.version_info[0] == 2:
import imp
path_prody = imp.find_module('prody')[1]
else:
path_prody = importlib.util.find_spec("prody").submodule_search_locations[0]
path_apps = imp.find_module('apps', [path_prody])[1]
path_apps = imp.find_module('prody_apps', [path_apps])[1]

try:
import imp
path_apps = imp.find_module('apps', [path_prody])[1]
path_apps = imp.find_module('prody_apps', [path_apps])[1]
except ModuleNotFoundError:
path_apps = importlib.util.find_spec("prody.apps").submodule_search_locations[0]
path_apps += '/prody_apps/'

PRODY_APPS = ['anm', 'gnm', 'pca', 'eda', 'align', 'blast', 'biomol',
'catdcd', 'contacts', 'fetch', 'select', 'energy',
Expand Down Expand Up @@ -49,7 +55,6 @@
*imp.find_module(cmd, [path_apps]))
mod.addCommand(prody_commands)


def prody_main():

if len(sys.argv) == 1:
Expand Down
21 changes: 16 additions & 5 deletions prody/utilities/misctools.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
'getValue', 'indentElement', 'isPDB', 'isURL', 'isListLike', 'isSymmetric', 'makeSymmetric',
'getDistance', 'fastin', 'createStringIO', 'div0', 'wmean', 'bin2dec', 'wrapModes',
'fixArraySize', 'decToHybrid36', 'hybrid36ToDec', 'DTYPE', 'checkIdentifiers', 'split', 'mad',
'importDec']
'importDec', 'impLoadModule']

DTYPE = array(['a']).dtype.char # 'S' for PY2K and 'U' for PY3K
CURSORS = []
Expand Down Expand Up @@ -803,11 +803,22 @@ def importDec():

return dec

def importImp():

def impLoadModule(name, cmd, path):
"""Returns an an imported module equivalent to imp."""

if not name.endswith('.'):
name += '.'

try:
import imp
except ModuleNotFoundError:
from importlib import import_module as imp
mod = imp.load_module(name + cmd,
*imp.find_module(cmd, [path]))
except ImportError:
import importlib
loader = importlib.machinery.SourceFileLoader(name + cmd,
path + cmd + '.py')
mod = importlib.util.types.ModuleType(loader.name)
loader.exec_module(mod)

return imp
return mod

0 comments on commit ad74568

Please sign in to comment.