File: async.py

package info (click to toggle)
gnome-python 2.28.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,288 kB
  • sloc: sh: 10,219; ansic: 7,997; xml: 2,464; python: 1,886; makefile: 392
file content (140 lines) | stat: -rw-r--r-- 4,333 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import pygtk; pygtk.require("2.0")
import gtk
import gnomevfs

def task_done():
    global counter
    counter -= 1
    if not counter:
        gtk.main_quit()

def write_callback(handle, bytes, exc_type, bytes_requested):
    print 'Write done:', handle, bytes, exc_type
    handle.close(lambda *args: None)
    task_done()
    
def read_callback(handle, buffer, exc_type, bytes_requested):
    print 'Read done:', handle, buffer, exc_type
    #handle.close(lambda *args: None)
    task_done()

def create_callback(handle, exc_type):
    print 'Create done', handle, exc_type
    if not exc_type:
        handle.write('Hello world!\n', write_callback)
    else:
        if issubclass(exc_type, gnomevfs.AccessDeniedError):
            print "Unable to create file: access denied"
        else:
            print "Unable to create file: unknown error"
        task_done()

def symlink_callback(handle, exc_type):
    print 'Symlink done:', handle, exc_type
    task_done()

def open_callback(handle, exc_type):
    print 'Open done:', handle, exc_type
    if not exc_type:
        handle.read(14, read_callback)
    else:
        task_done()


def info_callback(handle, results):
    for uri, exc_type, info in results:
        print '-'*len(str(uri))
        print uri
        print '-'*len(str(uri))
        if not exc_type:
            try:
                print 'mime_type:\t', info.mime_type
                print 'size (bytes):\t', info.size
                print 'permissions:\t%o' % (info.permissions,)
            except ValueError:
                pass
            print
        else:
            print 'Error:\t', exc_type
    task_done()

def dir_callback(handle, results, exc_type):
    if not exc_type or exc_type == gnomevfs.EOFError:
        for result in results:
            print result.name
            print '-'*len(str(result.name))
            try:
                print 'mime_type:\t', result.mime_type
                print 'size (bytes):\t', result.size
                print 'permissions:\t%o' % (result.permissions,)
            except ValueError:
                pass
            print
        if exc_type == gnomevfs.EOFError:
            task_done()
    else:
        print 'Error:\t', exc_type
        task_done()

counter = 0

print 'Current job limit:', gnomevfs.async.get_job_limit()

if 1:
    counter += 1
    print gnomevfs.async.get_file_info(('/etc/fstab',
                                         'http://www.gnome.org/index.html'),
                                        info_callback,
                                        options = gnomevfs.FILE_INFO_DEFAULT |
                                        gnomevfs.FILE_INFO_GET_MIME_TYPE)
if 1:
    counter += 1
    print gnomevfs.async.load_directory('/etc/skel',
                                         dir_callback,
                                         options =gnomevfs.FILE_INFO_DEFAULT |
                                         gnomevfs.FILE_INFO_GET_MIME_TYPE)


if 1:
    counter += 1
    print gnomevfs.async.open('/etc/fstab', open_callback)

if 1:
    counter += 1
    print gnomevfs.async.create('/tmp/test_file', create_callback)

if 1:
    counter += 1
    print gnomevfs.async.create_symbolic_link('/tmp/test_link',
                                               '/etc/fstab',
                                               symlink_callback)

def find_directory_cb(handle, results):
    print "Trash results:", results
    task_done()
    
if 1:
    counter += 1
    gnomevfs.async.find_directory(near_uri_list=[gnomevfs.URI("file:///home")],
                                  kind=gnomevfs.DIRECTORY_KIND_TRASH,
                                  create_if_needed=False,
                                  find_if_needed=True, permissions=0,
                                  callback=find_directory_cb)

def file_control_callback(handle, result, data):
    ## In this case, data contains a pointer to a string, not the
    ## string itself; quite useless from python.
    print "file control result: %r" % (result,)
    task_done()

def file_control(handle, exc_type):
    ## this is mostly useless, but I only discovered it after wrapping it.
    handle.control("file:test", ' '*256, file_control_callback)

if 1:
    counter += 1
    print gnomevfs.async.open('/etc/fstab', file_control)


if counter:
    gtk.main()