digger.py 1.73 KB
#! /usr/bin/env python3

import locale
from dialog import Dialog
import glob
import os
import sys
import subprocess as sp

sys.path.append('modules')
sys.path.append('vars')

vars = {}

def getModules():
    choiceList = []
    modules = []
    for filename in glob.glob('./modules/*.py'):
        modules.append(os.path.splitext(os.path.basename(filename))[0])
    modules.remove('__init__')

    for modname in modules:
        try:
            module = __import__(modname)
            instance = module.getInstance(vars)
            choiceList.append((instance.name,instance.description,False))
            del instance
            del module
            sys.modules.pop(modname)
        except Exception as e:
            print("Exception raised while importing " + modname)

    choiceList.append(("all","Execute all modules",False))
    return choiceList

def prepareModule(moduleName):
    module = __import__(moduleName).getInstance(vars)
    module.getVars()
    return module

locale.setlocale(locale.LC_ALL, '')
d = Dialog(dialog="dialog",autowidgetsize=True)
d.set_background_title("My little program")

moduleList = getModules()
code, tags = d.checklist("Select modules to execute",
                         choices=moduleList,
                         title="Module selection")
if code == d.OK:
    runlist = []
    if "All" in tags:
        for module in moduleList:
            runlist.append(prepareModule(module))
    else:
        for tag in tags:
            runlist.append(prepareModule(tag))

    sp.call('clear', shell=True)

    for module in runlist:
        print("Running " + module.name + "...")
        try:
            module.run()
        except Exception as e:
            print("Exception raised while running " + module.name + ": " + str(e))