import importlib import logging import os import pkgutil import sys from functools import total_ordering import ee.tools def eprint(*args, **kwargs): print(*args, file=sys.stderr, **kwargs) @total_ordering class Tool(object): def __init__(self, module_name, name): self.module_name = module_name self.name = name def __eq__(self, other): return self.name == other.name def __lt__(self, other): return self.name < other.name def find_tools(): prefix = ee.tools.__name__ + '.' ps = pkgutil.walk_packages(ee.tools.__path__, prefix) tools = [] for (module_loader, module_name, ispkg) in ps: name = module_name.replace(prefix, '').replace('_', '-') tools.append(Tool(module_name, name)) return sorted(tools) def main(): logging.basicConfig() # you need to initialize logging, otherwise you will not see anything from requests logging.getLogger().setLevel(logging.DEBUG) tools = find_tools() if len(sys.argv) <= 1: eprint("Available tools:") for t in tools: print(" {}".format(t.name)) exit(1) name = sys.argv[1] del sys.argv[1] if os.path.isdir("py"): sys.path.append("py") for t in tools: if t.name != name: continue sys.argv[0] = t.name importlib.import_module(t.module_name) exit(0) eprint("No such tool: {}".format(name)) exit(1) if __name__ == "__main__": main()