#!/usr/bin/env python import cmd, sys import kforge.system import kforge.dom from kforge.core.command import * class KforgeAdmin(cmd.Cmd): sysdict = kforge.system.SystemDictionary() registry = kforge.dom.DomainRegistry() prompt = '%s: ' % sysdict['service_name'] nohelp = 'No help page yet written for %s' lastClassName = 'Person' cwdo = registry lastNonListCwdo = '' def do_create(self, *args, **kwds): if args and args[0]: className = args[0] else: lastClassName = self.lastClassName message = "Create object of which type? [%s]: " % lastClassName className = raw_input(message) or lastClassName if not self.registry.isDomainClassRegistered(className): print "Sorry, '%s' isn't a registered class." % className else: domainClass = self.registry.getDomainClass(className) self.lastClassName = className keyName = domainClass.registerKeyName objectKwds = {} objectRegister = domainClass.createRegister() if not keyName == 'id': message = "Create %s with which %s? []: " % (className, keyName) domainObjectKey = raw_input(message) or '' objectKwds[keyName] = domainObjectKey try: readCmd = DomainObjectRead(className, objectKwds=objectKwds) readCmd.execute() print "That %s already exists." % className except KforgeCommandError: createCmd = DomainObjectCreate(className, objectKwds=objectKwds) createCmd.execute() self.cwdo = createCmd.object promptKeyValue = getattr(self.cwdo, keyName) self.prompt = '%s: ' % promptKeyValue print self.cwdo def help_create(self, *args, **kwds): print "Creates a new object." def do_read(self, *args, **kwds): if args and args[0]: className = args[0] else: lastClassName = self.lastClassName message = "Read which type of object? [%s]: " % lastClassName className = raw_input(message) or lastClassName if not self.registry.isDomainClassRegistered(className): print "Sorry, '%s' isn't a registered class." % className else: self.lastClassName = className domainClass = self.registry.getDomainClass(className) keyName = domainClass.registerKeyName objectKwds = {} objectRegister = domainClass.createRegister() message = "Read %s with which %s? []: " % (className, keyName) domainObjectKey = raw_input(message) or '' objectKwds[keyName] = domainObjectKey try: readCmd = DomainObjectRead(className, objectKwds=objectKwds) readCmd.execute() self.lastCwdo = self.cwdo self.cwdo = readCmd.object if not hasattr(self.cwdo, 'keys'): self.lastNonListCwdo = self.lastCwdo promptKeyValue = getattr(self.cwdo, keyName) self.prompt = '%s: ' % promptKeyValue print self.cwdo except KforgeCommandError: print "That %s doesn't exist." % className def do_list(self, *args, **kwds): register = None className = '' if args and args[0]: className = args[0] elif hasattr(self.cwdo, 'keys'): register = self.cwdo else: lastClassName = self.lastClassName message = "List which type of objects? [%s]: " % lastClassName className = raw_input(message) or lastClassName if register == None: if self.registry.isDomainClassRegistered(className): self.lastClassName = className domainClass = self.registry.getDomainClass(className) register = domainClass.createRegister() else: print "Sorry, '%s' isn't a registered class." % className return print [i for i in register.keys()] def help_list(self, *args, **kwds): print "Lists the objects of a class register." def do_use(self, *args, **kwds): attrName = args[0] or self.lastNonListCwdo if hasattr(self.cwdo, 'keys'): if attrName in self.cwdo.keys(): newCwdo = self.cwdo[attrName] else: print "'%s' is not a key of current object list." % attrName return elif hasattr(self.cwdo, attrName): newCwdo = getattr(self.cwdo, attrName) else: print "'%s' is not an attribute of current object." % attrName return self.lastPrompt = self.prompt self.prompt = '%s: ' % (attrName) self.lastCwdo = self.cwdo self.cwdo = newCwdo if not hasattr(self.cwdo, 'keys'): self.lastNonListCwdo = self.lastCwdo print self.cwdo def help_use(self, *args, **kwds): print "Changes current object to attribute of current object." def do_back(self, *args, **kwds): self.cwdo = self.lastCwdo self.prompt = self.lastPrompt print self.cwdo def help_back(self, *args, **kwds): print "Changes current object to last current object." def do_print(self, *args, **kwds): print self.cwdo def help_print(self, *args, **kwds): print "Prints the last working object." def do_update(self, *args, **kwds): print "update" def do_delete(self, *args, **kwds): print "delete" def do_quit(self, *args, **kwds): sys.exit() def help_quit(self): print "Exit." def do_exit(self, *args, **kwds): sys.exit() def help_exit(self): print "Exit." def do_EOF(self, *args, **kwds): print "" sys.exit() def help_EOF(self): print "Exit." print "" print "Welcome to the KForge interactive command line!" print "" print "Type 'help' for list of available commands" print "Type 'help command' for information about a specific command" print "" c = KforgeAdmin() c.cmdloop()