home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / doc / python-gnome2 / examples / vfs / shell.py < prev    next >
Encoding:
Python Source  |  2004-11-21  |  2.6 KB  |  91 lines

  1. #!/usr/bin/env python
  2.  
  3. import os
  4.  
  5. _boldcode = os.popen('tput bold', 'r').read()
  6. _normal = os.popen('tput rmso', 'r').read()
  7.  
  8. import pygtk; pygtk.require("2.0")
  9. import gnomevfs
  10.  
  11. class Shell:
  12.     def __init__(self, cwd=None):
  13.         if cwd:
  14.             self.cwd = gnomevfs.URI(cwd)
  15.         else:
  16.             self.cwd = gnomevfs.URI(os.getcwd())
  17.         if str(self.cwd)[-1] != '/':
  18.             self.cwd = self.cwd.append_string('/')
  19.  
  20.     def run(self):
  21.         while 1:
  22.             try:
  23.                 line = raw_input('%s%s$%s ' % (_boldcode, self.cwd, _normal))
  24.                 words = line.split()
  25.                 command = getattr(self, words[0])
  26.                 args = words[1:]
  27.                 command(*args)
  28.             except KeyboardInterrupt:
  29.                 break
  30.             except EOFError:
  31.                 break
  32.             except Exception, e:
  33.                 print "Error: %s:%s" % (e.__class__.__name__, str(e))
  34.  
  35.     def cd(self, dir):
  36.         new_cwd = self.cwd.resolve_relative(dir)
  37.         if str(new_cwd)[-1] != '/':
  38.             new_cwd = new_cwd.append_string('/')
  39.         if gnomevfs.get_file_info(new_cwd).type != \
  40.                gnomevfs.FILE_TYPE_DIRECTORY:
  41.             raise gnomevfs.error('%s is not a directory' % dir)
  42.         self.cwd = new_cwd
  43.  
  44.     def pwd(self):
  45.         print str(self.cwd)
  46.  
  47.     def ls(self, dir=''):
  48.         dir = self.cwd.resolve_relative(dir)
  49.  
  50.         dhandle = gnomevfs.open_directory(dir)
  51.         for file_info in dhandle:
  52.             print file_info.name
  53.  
  54.     def less(self, file):
  55.         file = self.cwd.resolve_relative(file)
  56.         file_info = gnomevfs.get_file_info(file)
  57.         fp = gnomevfs.open(file, gnomevfs.OPEN_READ)
  58.         less = os.popen('less -m -F -', 'w')
  59.         buffer = fp.read(file_info.size)
  60.         less.write(buffer)
  61.         less.close()
  62.     more = less
  63.     cat = less
  64.  
  65.     def stat(self, file):
  66.         file = self.cwd.resolve_relative(file)
  67.         file_info = gnomevfs.get_file_info(file, gnomevfs.FILE_INFO_GET_MIME_TYPE)
  68.         print 'Name:      ', file_info.name
  69.  
  70.         file_type = '(none)'
  71.         try: file_type = ('unknown', 'regular', 'directory',
  72.                           'fifo', 'socket', 'chardev', 'blockdev',
  73.                           'symlink')[file_info.type]
  74.         except: pass
  75.         print 'Type:      ', file_type
  76.  
  77.         file_size = '(unknown)'
  78.         try: file_size = file_info.size
  79.         except: pass
  80.         print 'Size:      ', file_size
  81.  
  82.         mime_type = '(none)'
  83.         try: mime_type = file_info.mime_type
  84.         except: pass
  85.         print 'Mime type: ', mime_type
  86.  
  87. if __name__ == '__main__':
  88.     shell = Shell()
  89.     shell.run()
  90.     print
  91.