You are not logged in Log in Join
You are here: Home » Members » andym » ZShellCLI » ZShellCLI.py » View File

Log in
Name

Password

 

ZShellCLI.py

File details
Size
2 K
File type
text/plain

File contents

import cmd, sys, os, string, tempfile

# I have to insert this to get access to xmlrpclib, you may not
sys.path.insert(0, r'e:\Zope23\lib\python')

import xmlrpclibBasicAuth

default_prompt = '> '

class ZShellCLI(cmd.Cmd):
    def __init__(self, *args):
        ''' setup '''
        self.prompt = default_prompt
        self.path = ''
        self.connect = None

    def do_open(self, line):
        '''
        means typing password in clear text
        need to look at Cmd module to see if we
        can type this in sep. 
        maybe config file to load this in?
        '''
        # syntax: open http://127.0.0.1 user pwd
        (url, user, pwd) = string.split(line, ' ')
        self.base_prompt = string.split(url, '/')[-1]
        self.prompt = self.base_prompt + '> '
        self.connect = xmlrpclibBasicAuth.Server(url, user, pwd)
        print "Connected to %s" % url

    def do_close(self, line):
        self.connect = None
        print "Disconnected from %s" % self.base_prompt
        self.prompt = default_prompt

    def do_exit(self, line):
        sys.exit() # bye

    def do_quit(self, line):
        sys.exit() # bye

    def show_browser(self, data):
        ''' creates a make file and shoves it in '''
        filename = tempfile.mktemp()
        file = open(filename, 'w')
        data = string.join(string.split(data, '\n')[1:], '\n')
        file.write(data)
        file.close()
        os.system('explorer %s' % filename)

    def set_path(self, path):
        ''' change path, set a nice prompt '''
        self.path = path
        self.prompt = "%s%s> " % (self.base_prompt, self.path)

    def run_cmd(self, command):
        ''' runs a command, changes path '''
        if self.connect is not None:
            info = { 'command': command, 'result_type': 'text', 'path': self.path }
            output = self.connect.zshell('', info)
            self.set_path(output['path'])
            return output['data']
        else:
            return "Not Connected"

    def default(self, line):
        print self.run_cmd(command=line)

    def emptyline(self):
        pass

    def preloop(self):
        ''' hello '''
        print
        print "ZShellCLI: A CLI to ZShell"

if __name__ == '__main__':
    shell = ZShellCLI()
    try:
        # go!
        shell.cmdloop()
    except KeyboardInterrupt:
        sys.exit()