Skip to content

Commit

Permalink
python 3.12 does not have imp module
Browse files Browse the repository at this point in the history
  • Loading branch information
Acribbs committed Oct 25, 2024
1 parent e3ddb88 commit 5449530
Showing 1 changed file with 59 additions and 29 deletions.
88 changes: 59 additions & 29 deletions cgat/cgat.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,18 @@
cgat <tool> --help
'''

import os
import sys
import re
import glob
import imp
import importlib.util
import collections
import cgatcore.iotools as iotools
import cgat


def mapKeyword2Script(path):
'''collect keywords from scripts.'''
'''Collect keywords from scripts.'''

map_keyword2script = collections.defaultdict(list)

Expand All @@ -57,79 +56,110 @@ def mapKeyword2Script(path):


def printListInColumns(l, ncolumns):
'''output list *l* in *ncolumns*.'''
'''Output list *l* in *ncolumns*.'''
ll = len(l)

if ll == 0:
return

max_width = max([len(x) for x in l]) + 3
n = ll // ncolumns
if ll % 3 != 0:
if ll % ncolumns != 0:
n += 1

# build columns
# Build columns
columns = [l[x * n:x * n + n] for x in range(ncolumns)]

# add empty fields for missing columns in last row
# Add empty fields for missing columns in last row
for x in range(ncolumns - (len(l) % ncolumns)):
columns[-(x + 1)].append('')

# convert to rows
# Convert to rows
rows = list(zip(*columns))

# build pattern for a row
# Build pattern for a row
p = '%-' + str(max_width) + 's'
pattern = ' '.join([p for x in range(ncolumns)])
pattern = ' '.join([p for _ in range(ncolumns)])

# put it all together
# Put it all together
return '\n'.join([pattern % row for row in rows])


def main(argv=None):

argv = sys.argv

path = os.path.join(os.path.abspath(os.path.dirname(cgat.__file__)),
"tools")

if len(argv) == 1 or argv[1] == "--help" or argv[1] == "-h":
print((globals()["__doc__"]))
if len(argv) == 1 or argv[1] in ("--help", "-h"):
print(globals().get("__doc__", "No documentation available."))

map_keyword2script = mapKeyword2Script(path)

if len(argv) <= 2:

print('cgat tools are grouped by keywords. The following keywords')
print('are defined:\n')
print(("%s\n" % printListInColumns(list(map_keyword2script.keys()),
3)))
print("%s\n" % printListInColumns(list(map_keyword2script.keys()), 3))

if 'all' in argv[2:]:
print("The list of all available commands is:\n")
print(("%s\n" % printListInColumns(
print("%s\n" % printListInColumns(
sorted([os.path.basename(x)[:-3]
for x in glob.glob(os.path.join(path, "*.py"))]),
3)))

3))
else:
for arg in argv[2:]:
if arg in map_keyword2script:
print(("Tools matching the keyword '%s':\n" % arg))
print(('%s\n' % printListInColumns(
print("Tools matching the keyword '%s':\n" % arg)
print('%s\n' % printListInColumns(
sorted(map_keyword2script[arg]),
3)))
3))
return

if len(argv) < 2:
print("Error: No command provided.\nUse --help for more information.", file=sys.stderr)
sys.exit(1)

command = argv[1]

# Replace hyphens with underscores to match Python module naming conventions
command = re.sub("-", "_", command)

(file, pathname, description) = imp.find_module(command, [path, ])
module = imp.load_module(command, file, pathname, description)
# remove 'cgat' from sys.argv
del sys.argv[0]
module.main(sys.argv)

# Dynamically load the specified module using importlib
module_path = os.path.join(path, f"{command}.py")

if not os.path.isfile(module_path):
print(f"Error: Module '{command}' not found in path '{path}'.", file=sys.stderr)
sys.exit(1)

spec = importlib.util.spec_from_file_location(command, module_path)
if spec is None:
print(f"Error: Cannot create a module spec for '{command}'.", file=sys.stderr)
sys.exit(1)

module = importlib.util.module_from_spec(spec)
try:
spec.loader.exec_module(module)
except Exception as e:
print(f"Error: Failed to load module '{command}': {e}", file=sys.stderr)
sys.exit(1)

# Remove 'cgat' from sys.argv to pass the remaining arguments to the module's main function
if len(sys.argv) > 0:
del sys.argv[0]

# Ensure the module has a 'main' function
if not hasattr(module, 'main'):
print(f"Error: The module '{command}' does not have a 'main' function.", file=sys.stderr)
sys.exit(1)

# Call the module's main function with the remaining arguments
try:
module.main(sys.argv)
except Exception as e:
print(f"Error: An exception occurred while executing the 'main' function of '{command}': {e}", file=sys.stderr)
sys.exit(1)


if __name__ == "__main__":
sys.exit(main())

0 comments on commit 5449530

Please sign in to comment.