Advertisement
Guest User

cmd

a guest
Jun 13th, 2012
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.85 KB | None | 0 0
  1. import os
  2. import cmd
  3. import readline
  4.  
  5. class Shell(cmd.Cmd, object):
  6.  
  7.     def __init__(self):
  8.         cmd.Cmd.__init__(self)
  9.         self.__setCanExit(True)
  10.        
  11.     def __canExit(self):
  12.         return self.canExit
  13.    
  14.     def __setCanExit(self, value):
  15.         self.canExit = value
  16.  
  17.     ## Override methods in Cmd object ##
  18.     def preloop(self):
  19.         """Initialization before prompting user for commands.
  20.           Despite the claims in the Cmd documentaion, Cmd.preloop() is not a stub.
  21.        """
  22.         cmd.Cmd.preloop(self)   ## sets up command completion
  23.         self._hist = []      ## No history yet
  24.         self._locals = {}      ## Initialize execution namespace for user
  25.         self._globals = {}
  26.  
  27.     def postloop(self):
  28.         """Take care of any unfinished business.
  29.           Despite the claims in the Cmd documentaion, Cmd.postloop() is not a stub.
  30.        """
  31.         cmd.Cmd.postloop(self)   ## Clean up command completion
  32.         print
  33.         print "Bye!"
  34.        
  35.     def emptyline(self):    
  36.         """Do nothing on empty input line"""
  37.         pass
  38.    
  39.     def __listdir(self, root):
  40.         "List directory 'root' appending the path separator to subdirs."
  41.         res = []
  42.         for name in os.listdir(root):
  43.             path = os.path.join(root, name)
  44.             if os.path.isdir(path):
  45.                 name += os.sep
  46.             res.append(name)
  47.         return res
  48.  
  49.     def __complete_path(self, path=None):
  50.         "Perform completion of filesystem path."
  51.         if not path:
  52.             return self.__listdir('.')
  53.        
  54.         dirname, rest = os.path.split(path)
  55.         tmp = dirname if dirname else '.'
  56.         res = [os.path.join(dirname, p)
  57.                 for p in self.__listdir(tmp) if p.startswith(rest)]
  58.        
  59.        
  60.         # more than one match, or single match which does not exist (typo)
  61.         if len(res) > 1 or not os.path.exists(path):
  62.             return res
  63.         # resolved to a single directory, so return list of files below it
  64.         if os.path.isdir(path):
  65.             return [os.path.join(path, p) for p in self.__listdir(path)]
  66.         # exact file match terminates this completion
  67.         return [path + ' ']
  68.  
  69.     def do_exit(self, args):
  70.         """Exits from the console"""
  71.         if self.__canExit(): return True
  72.         print "Please, wait until all operations end"
  73.         return False
  74.  
  75.     ## Command definitions to support Cmd object functionality ##
  76.     def do_EOF(self, args):
  77.         """Exit on system end of file character"""
  78.         return self.do_exit(args)
  79.  
  80.     def do_shell(self, args):
  81.         """Pass command to a system shell when line begins with '!'"""
  82.         os.system(args)
  83.  
  84.     def do_help(self, args):
  85.         """Get help on commands
  86.           'help' or '?' with no arguments prints a list of commands for which help is available
  87.           'help <command>' or '? <command>' gives help on <command>
  88.        """
  89.         ## The only reason to define this method is for the help text in the doc string
  90.         cmd.Cmd.do_help(self, args)
  91.        
  92.     def do_put(self,args):
  93.         """Uploads a file to a remote path
  94.        """
  95.         print "fichero subido"
  96.         print args
  97.  
  98.        
  99.     def complete_put(self, text, line, begidx, endidx):
  100.         "Completions for the 'extra' command."
  101.         if not text:
  102.             return self.__complete_path()
  103.         # treat the last arg as a path and complete it
  104.         return self.__complete_path(text)
  105.        
  106.         #=======================================================================
  107.         # "Completions for the 'extra' command."
  108.         # if not args:
  109.         #    return self.__complete_path('.')
  110.         # # treat the last arg as a path and complete it
  111.         # return self.__complete_path(args[-1])
  112.         #=======================================================================
  113.    
  114. #    def precmd(self, line):
  115. #        """ This method is called after the line has been input but before
  116. #            it has been interpreted. If you want to modifdy the input line
  117. #            before execution (for example, variable substitution) do it here.
  118. #        """
  119. #        self._hist += [ line.strip() ]
  120. #        return line
  121.  
  122. #    def postcmd(self, stop, line):
  123. #        """If you want to stop the console, return something that evaluates to true.
  124. #           If you want to do some post command processing, do it here.
  125. #        """
  126. #        return stop
  127.  
  128.  
  129.  
  130. #    def default(self, line):      
  131. #        """Called on an input line when the command prefix is not recognized.
  132. #           In that case we execute the line as Python code.
  133. #        """
  134. #        try:
  135. #            exec(line) in self._locals, self._globals
  136. #        except Exception, e:
  137. #            print e.__class__, ":", e
  138.  
  139.  
  140. if __name__ == '__main__':
  141.     console = Shell()
  142.     console . cmdloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement