#!/usr/bin/env python # # Administrate kforge domain objects # import os import sys import re from optparse import OptionParser import kforge.command from kforge.exceptions import * # would like this to be more reflection .... # todo complete ... requiredVariables = { 'project' : ['name'], 'person' : ['name'], 'member' : ['project', 'person'], 'service' : ['project', 'plugin', 'name'], 'license' : ['id'], 'role' : ['id'], 'plugin' : ['name'] } def domainObjectDisplay(domainObjectName, domainObject, showAll = False): """ [[TODO: tabbing so that we get nice output when recursing e.g.displaying a member should give: person: username: xxx project: unixname: yyy ]] """ attributeList = domainObject.__dict__ if showAll == False: variablesToShow = requiredVariables[domainObjectName] else: variablesToShow = domainObject.__dict__.keys() result = '' for attributeKey in variablesToShow: value = '' if attributeKey in requiredVariables.keys(): # let's do another lookup value += '\n' value += domainObjectDisplay(attributeKey, attributeList[attributeKey], showAll) else: value = str(attributeList[attributeKey]) + '\n' result += attributeKey + ': ' + value return result def errorMsg(errorMsg = 'incorrect arguments'): print 'ERROR: ' + errorMsg + '\n' print 'Type -h for help' sys.exit(1) print 'end of errorMsg' """ [[TODO: refactor to a proper subcommand setup as set out in the thread: http://sourceforge.net/mailarchive/message.php?msg_id=2522579 To have a decent setup [[TODO: Command tab-completion. See http://furius.ca/optcomplete/]] """ if __name__ == "__main__": usage = 'usage: %prog [options] [attribute]' usage += '\n\n' usage += 'Administer kforge domain objects\n\n' usage += 'Domain objects require the following extra attributes:\n' for dobj in requiredVariables.keys(): usage += '\t' + dobj + ': ' + str(requiredVariables[dobj]) + '\n' parser = OptionParser(usage) parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False, help='Be verbose in printing status messages') (options, args) = parser.parse_args() # TODO: doesn't work at present if options.verbose == True: import logging x = logging.getLogger('') x.setLevel(logging.DEBUG) # Get subcommands if len(args) < 2: errorMsg() action = args[0] domainObjectName = args[1] attributeList = [] if len(args) > 2: attributeList = args[2:] # do pre processing checks if not requiredVariables.has_key(domainObjectName): errorMsg("domain object name '%s' not in list" % domainObjectName) elif action != 'list' and len(attributeList) != len(requiredVariables[domainObjectName]): errorMsg('incorrect attributes supplied for chosen action and domain object') # where a domainObjectName requires more than 1 variable for command the first # variable is needed for listing elif action == 'list' and len(requiredVariables[domainObjectName]) > 1 and len(attributeList) == 0: errorMsg('not enough attributes supplied to perform this action') className = domainObjectName.capitalize() + action.capitalize() classInstance = apply(kforge.command.__dict__[className], attributeList) try: if options.verbose: print 'Executing command ...' classInstance.execute() except KforgeCommandError, inst: errorMsg("Error executing command: " + str(inst)) raise # Now do post execute stuff if action == 'create': pass if action == 'list': for result in classInstance.results: print domainObjectDisplay(domainObjectName, result) if action == 'read': # unfortunately each command to read a domain object calls the member # variable in which the retrieved object is stored by its own name print domainObjectDisplay(domainObjectName, classInstance.__dict__[domainObjectName]) if options.verbose: print 'SUCCESS'