Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import cmd
- import readline
- class Shell(cmd.Cmd, object):
- def __init__(self):
- cmd.Cmd.__init__(self)
- self.__setCanExit(True)
- def __canExit(self):
- return self.canExit
- def __setCanExit(self, value):
- self.canExit = value
- ## Override methods in Cmd object ##
- def preloop(self):
- """Initialization before prompting user for commands.
- Despite the claims in the Cmd documentaion, Cmd.preloop() is not a stub.
- """
- cmd.Cmd.preloop(self) ## sets up command completion
- self._hist = [] ## No history yet
- self._locals = {} ## Initialize execution namespace for user
- self._globals = {}
- def postloop(self):
- """Take care of any unfinished business.
- Despite the claims in the Cmd documentaion, Cmd.postloop() is not a stub.
- """
- cmd.Cmd.postloop(self) ## Clean up command completion
- print
- print "Bye!"
- def emptyline(self):
- """Do nothing on empty input line"""
- pass
- def __listdir(self, root):
- "List directory 'root' appending the path separator to subdirs."
- res = []
- for name in os.listdir(root):
- path = os.path.join(root, name)
- if os.path.isdir(path):
- name += os.sep
- res.append(name)
- return res
- def __complete_path(self, path=None):
- "Perform completion of filesystem path."
- if not path:
- return self.__listdir('.')
- dirname, rest = os.path.split(path)
- tmp = dirname if dirname else '.'
- res = [os.path.join(dirname, p)
- for p in self.__listdir(tmp) if p.startswith(rest)]
- # more than one match, or single match which does not exist (typo)
- if len(res) > 1 or not os.path.exists(path):
- return res
- # resolved to a single directory, so return list of files below it
- if os.path.isdir(path):
- return [os.path.join(path, p) for p in self.__listdir(path)]
- # exact file match terminates this completion
- return [path + ' ']
- def do_exit(self, args):
- """Exits from the console"""
- if self.__canExit(): return True
- print "Please, wait until all operations end"
- return False
- ## Command definitions to support Cmd object functionality ##
- def do_EOF(self, args):
- """Exit on system end of file character"""
- return self.do_exit(args)
- def do_shell(self, args):
- """Pass command to a system shell when line begins with '!'"""
- os.system(args)
- def do_help(self, args):
- """Get help on commands
- 'help' or '?' with no arguments prints a list of commands for which help is available
- 'help <command>' or '? <command>' gives help on <command>
- """
- ## The only reason to define this method is for the help text in the doc string
- cmd.Cmd.do_help(self, args)
- def do_put(self,args):
- """Uploads a file to a remote path
- """
- print "fichero subido"
- print args
- def complete_put(self, text, line, begidx, endidx):
- "Completions for the 'extra' command."
- if not text:
- return self.__complete_path()
- # treat the last arg as a path and complete it
- return self.__complete_path(text)
- #=======================================================================
- # "Completions for the 'extra' command."
- # if not args:
- # return self.__complete_path('.')
- # # treat the last arg as a path and complete it
- # return self.__complete_path(args[-1])
- #=======================================================================
- # def precmd(self, line):
- # """ This method is called after the line has been input but before
- # it has been interpreted. If you want to modifdy the input line
- # before execution (for example, variable substitution) do it here.
- # """
- # self._hist += [ line.strip() ]
- # return line
- # def postcmd(self, stop, line):
- # """If you want to stop the console, return something that evaluates to true.
- # If you want to do some post command processing, do it here.
- # """
- # return stop
- # def default(self, line):
- # """Called on an input line when the command prefix is not recognized.
- # In that case we execute the line as Python code.
- # """
- # try:
- # exec(line) in self._locals, self._globals
- # except Exception, e:
- # print e.__class__, ":", e
- if __name__ == '__main__':
- console = Shell()
- console . cmdloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement