~gdesklets-desklet-team/gdesklets/0.36

« back to all changes in this revision

Viewing changes to utils/vfs.py

  • Committer: Robert Pastierovic
  • Date: 2007-10-07 10:08:42 UTC
  • Revision ID: pastierovic@gmail.com-20071007100842-fdvp2vzmqgh1j87k
merged 0.3x branch and basic documentation and some other changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Abstraction layer for VFS operations to move gnomevfs dependency out of the
 
3
# core.
 
4
#
 
5
 
 
6
USE_GNOMEVFS = True
 
7
 
 
8
import urllib
 
9
 
 
10
try:
 
11
    import gnomevfs
 
12
 
 
13
    OPEN_READ = gnomevfs.OPEN_READ
 
14
    OPEN_WRITE = gnomevfs.OPEN_WRITE
 
15
 
 
16
except ImportError:
 
17
    try:
 
18
        import gnome.vfs as gnomevfs
 
19
 
 
20
        OPEN_READ = gnomevfs.OPEN_READ
 
21
        OPEN_WRITE = gnomevfs.OPEN_WRITE
 
22
 
 
23
    except ImportError:
 
24
        log("Using urllib, because gnomevfs isn't available")
 
25
        OPEN_READ = "r"
 
26
        OPEN_WRITE = "w"
 
27
 
 
28
        USE_GNOMEVFS = False
 
29
 
 
30
 
 
31
 
 
32
def escape_path(uri):
 
33
 
 
34
    try:
 
35
        return gnomevfs.escape_string(uri)
 
36
    except:
 
37
        return escape_path_urllib(uri)
 
38
 
 
39
def escape_path_urllib(uri):
 
40
 
 
41
    return urllib.quote(uri)
 
42
 
 
43
 
 
44
 
 
45
def unescape_path(uri):
 
46
 
 
47
    return urllib.unquote(uri)
 
48
 
 
49
 
 
50
 
 
51
#
 
52
# Reads the entire file and returns its contents.
 
53
#
 
54
def read_entire_file(uri):
 
55
 
 
56
    try:
 
57
        uri = gnomevfs.read_entire_file(uri)
 
58
    except:
 
59
        log("Warning: Couldn't read file \"%s\"." % (uri,))
 
60
        raise
 
61
 
 
62
    return uri
 
63
 
 
64
def read_entire_file_urllib(uri):
 
65
 
 
66
    if (not "://" in uri): uri = "file://" + uri
 
67
 
 
68
    try:
 
69
        uri = urllib.urlopen(uri).read()
 
70
    except:
 
71
        log("Warning: Couldn't read file \"%s\"." % (uri,))
 
72
        raise
 
73
 
 
74
    return uri
 
75
 
 
76
 
 
77
 
 
78
#
 
79
# Opens the given URI and returns a file descriptor.
 
80
#
 
81
def open(uri, mode = OPEN_READ):
 
82
 
 
83
    try:
 
84
        fd = gnomevfs.open(uri, mode)
 
85
    except:
 
86
        log("Warning: Couldn't open file \"%s\"." % (uri,))
 
87
        return
 
88
 
 
89
    return fd
 
90
 
 
91
def open_urllib(uri, mode = OPEN_READ):
 
92
 
 
93
    if (not "://" in uri): uri = "file://" + uri
 
94
 
 
95
    try:
 
96
        fd = urllib.urlopen(uri)
 
97
    except:
 
98
        log("Warning: Couldn't open file \"%s\"." % (uri,))
 
99
        return
 
100
 
 
101
    return fd
 
102
 
 
103
 
 
104
 
 
105
#
 
106
# Returns whether the given URI exists.
 
107
#
 
108
def exists(uri):
 
109
 
 
110
    return gnomevfs.exists(gnomevfs.URI(uri))
 
111
 
 
112
def exists_urllib(uri):
 
113
 
 
114
    if (not "://" in uri): uri = "file://" + uri
 
115
 
 
116
    try:
 
117
        urllib.urlopen(uri)
 
118
    except:
 
119
        return False
 
120
    else:
 
121
        return True
 
122
 
 
123
 
 
124
 
 
125
if (not USE_GNOMEVFS):
 
126
    read_entire_file = read_entire_file_urllib
 
127
    open = open_urllib
 
128
    exists = exists_urllib
 
129
    escape_path = escape_path_urllib