File: shell.py

package info (click to toggle)
gnome-python 2.12.4-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,804 kB
  • ctags: 813
  • sloc: sh: 8,593; ansic: 7,747; python: 1,811; makefile: 333; xml: 54
file content (90 lines) | stat: -rw-r--r-- 2,666 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python

import os

_boldcode = os.popen('tput bold', 'r').read()
_normal = os.popen('tput rmso', 'r').read()

import pygtk; pygtk.require("2.0")
import gnomevfs

class Shell:
    def __init__(self, cwd=None):
        if cwd:
            self.cwd = gnomevfs.URI(cwd)
        else:
            self.cwd = gnomevfs.URI(os.getcwd())
        if str(self.cwd)[-1] != '/':
            self.cwd = self.cwd.append_string('/')

    def run(self):
        while 1:
            try:
                line = raw_input('%s%s$%s ' % (_boldcode, self.cwd, _normal))
                words = line.split()
                command = getattr(self, words[0])
                args = words[1:]
                command(*args)
            except KeyboardInterrupt:
                break
            except EOFError:
                break
            except Exception, e:
                print "Error: %s:%s" % (e.__class__.__name__, str(e))

    def cd(self, dir):
        new_cwd = self.cwd.resolve_relative(dir)
        if str(new_cwd)[-1] != '/':
            new_cwd = new_cwd.append_string('/')
        if gnomevfs.get_file_info(new_cwd).type != \
               gnomevfs.FILE_TYPE_DIRECTORY:
            raise gnomevfs.error('%s is not a directory' % dir)
        self.cwd = new_cwd

    def pwd(self):
        print str(self.cwd)

    def ls(self, dir=''):
        dir = self.cwd.resolve_relative(dir)

        dhandle = gnomevfs.open_directory(dir)
        for file_info in dhandle:
            print file_info.name

    def less(self, file):
        file = self.cwd.resolve_relative(file)
        file_info = gnomevfs.get_file_info(file)
        fp = gnomevfs.open(file, gnomevfs.OPEN_READ)
        less = os.popen('less -m -F -', 'w')
        buffer = fp.read(file_info.size)
        less.write(buffer)
        less.close()
    more = less
    cat = less

    def stat(self, file):
        file = self.cwd.resolve_relative(file)
        file_info = gnomevfs.get_file_info(file, gnomevfs.FILE_INFO_GET_MIME_TYPE)
        print 'Name:      ', file_info.name

        file_type = '(none)'
        try: file_type = ('unknown', 'regular', 'directory',
                          'fifo', 'socket', 'chardev', 'blockdev',
                          'symlink')[file_info.type]
        except: pass
        print 'Type:      ', file_type

        file_size = '(unknown)'
        try: file_size = file_info.size
        except: pass
        print 'Size:      ', file_size

        mime_type = '(none)'
        try: mime_type = file_info.mime_type
        except: pass
        print 'Mime type: ', mime_type

if __name__ == '__main__':
    shell = Shell()
    shell.run()
    print