From 1efd8f521e70fc0c923e16409bd7e6164c01d118 Mon Sep 17 00:00:00 2001 From: Matthew Vliet Date: Mon, 24 Jan 2011 20:10:42 -0800 Subject: [PATCH] subcommands found in the directory --- .../repoman_client/subcommands/__init__.py | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 repoman-client/repoman_client/subcommands/__init__.py diff --git a/repoman-client/repoman_client/subcommands/__init__.py b/repoman-client/repoman_client/subcommands/__init__.py new file mode 100644 index 0000000..609d687 --- /dev/null +++ b/repoman-client/repoman_client/subcommands/__init__.py @@ -0,0 +1,41 @@ +""" +Automagically find all subcommands in files in this directory. + +Usage: + from subcommands import * + OR + from subcommands import subcommands + + In either case a list named `subcommands` will be imported. This list + contains each of the subcommands that was automagically found in the folder. +""" + +import os +import sys +from repoman_client.subcommand import SubCommand + +_excludes = ['__init__.py'] +subcommands = [] + + +# Search subcommands folder for any subclass of SubCommand +# Steps: +# 1. try to import each file in the subcommands directory +# 2. search each imported file for subclasses of SubCommand +# - if a subclass is found, it is added to the subcommands list +for f in os.listdir(__path__[0]): + if f.endswith('.py') and f not in _excludes: + toplevel = f.rsplit('.py')[0] + try: + module = __import__(toplevel, globals=globals()) + except: + continue + + for c in dir(module): + try: + cmd = getattr(module, c, None) + if issubclass(cmd, SubCommand) and cmd is not SubCommand: + subcommands.append(cmd) + except: + pass +