from PIPRes.service_impl.rec_i_dcm import * from PIPRes.util.cipres import getCipresRegistry from PIPRes.util.ui_xml import UICmdParamFromXML, UICmdParamInterface import cmd, os class ConfigCmd(cmd.Cmd): def __init__(self, rootCmdParam): cmd.Cmd.__init__(self) self.prompt = '%_> ' self.rootCmdParam = rootCmdParam self.currParam = self.rootCmdParam def preloop(self): self.exiting = False self.postcmd(0, '') def postcmd(self, flag, s): if self.exiting and flag: return flag subCmds = self.currParam.cmdParams print '\n' + '\n'.join(self.currParam.linesForDisplay()) return 0 def do_help(self, verb): if verb == '': print 'my do help with no arg' else: cmd.Cmd.do_help(self, verb) def do_exit(self, rest): self.exiting = True return 1 def help_exit(self): print "exit: halts the configuration process" return 1 def do_cd(self, rest): rc, newPath = self.interpretPathToCmdParam(rest, self.currParam, True) if rc == 0: self.currParam = newPath return rc def interpretPathToCmdParam(self, pathSpec, currParam, printErrors): sp = os.path.split(pathSpec) if sp[0] == '': next = sp[1] if next == '..': if currParam is not self.rootCmdParam: currParam = currParam.parent else: if printErrors: print 'Cannot descend lower in the configuration tree' return 1, None else: try: currParam = currParam.getChild(next) if currParam is None: raise ValueError except ValueError: if printErrors: if next.isdigit(): print '%s is out of range.' % next else: print 'child parameter %s not found.' %next return 1, None return 0, currParam else: rc, newPath= self.interpretPathToCmdParam(sp[0], currParam, printErrors) if rc: return rc, None return self.interpretPathToCmdParam(sp[1], newPath, printErrors) def help_exit(self): print "exit: halts the configuration process" return 1 def dummyCmd(self, rest): print "just entered: %s." % (self.lastcmd) def leafCommand(self, rest): try: # if we are at a leaf, the value suffices (so the value will be the entire lastcmd # otherwise we assume that the args (rest) hold the value val = rest or self.lastcmd self.currParam.takeText(val) except ValueError, e: print e def activeChildCommand(self, rest): if not rest: self.do_cd(self.lastcmd) else: if rest[0] == '=': storedCurrParam, self.currParam = self.currParam, self.activeChild self.leafCommand(rest[1:].strip()) self.currParam = storedCurrParam else: print 'activeChildCommand=%s.' % self.lastcmd print 'rest =%s.'% rest def __getattr__(self, attrName): if attrName.startswith('do_'): tag = attrName[3:] if tag == 'EOF': return self.do_exit if self.currParam.isLeaf(): return self.leafCommand else: rc, child = self.interpretPathToCmdParam(tag, self.currParam, False) if rc == 0: self.activeChild = child return self.activeChildCommand #return self.dummyCmd elif attrName.startswith('help_'): tag = attrName[3:] return self.dummyCmd raise AttributeError, '%s object has not attribute %s.' % (self.__class__.__name__, attrName) def cipresConfigure(uiObject, registry = None): if not isinstance(uiObject, UICmdParamInterface): uiContent = UICmdParamFromXML('cmd-params', {}) if not uiContent.parseFileObj(uiObject): return False if registry is None: registry = getCipresRegistry() uiContent.setRegistry(registry) uiObject = uiContent cmd = ConfigCmd(uiObject) cmd.cmdloop(uiObject.label or None) return uiContent if __name__ == '__main__': from cStringIO import StringIO xmlContent = StringIO(RecIDCM.ui_xml) configuredUI = cipresConfigure(xmlContent) while not configuredUI.isReady(): print 'Configuration is not complete - more input needed' configuredUI = cipresConfigure(configuredUI) cDict = configuredUI.getConfigurationDict() print cDict