This repository has been archived by the owner on Dec 24, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
mime.py
executable file
·139 lines (130 loc) · 3.71 KB
/
mime.py
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
#!/usr/bin/env python
"""
MIME type tests for python-xdg
>>> import os
>>> from xdg.mime import MimeType
>>> mime = MimeType.fromName("foo.txt")
>>> mime.name()
'text/plain'
>>> mime.type()
'text'
>>> mime.subtype()
'plain'
>>> mime.genericMime()
<MimeType: text/x-generic>
>>> mime.genericMime().name()
'text/x-generic'
>>> mime = MimeType("text/plain; charset=UTF-8")
>>> mime.name()
'text/plain'
>>> mime.type()
'text'
>>> mime.subtype()
'plain'
>>> MimeType.fromName("foo.TXT").name()
'text/plain'
>>> MimeType.fromName("foo.C").name()
'text/x-c++src'
>>> MimeType.fromName("foo.c").name()
'text/x-csrc'
>>> print(MimeType.fromName("no-such-file"))
None
>>> MimeType.fromInode("/dev/sda").name()
'inode/blockdevice'
>>> MimeType.fromInode("/dev/null").name()
'inode/chardevice'
>>> MimeType.fromInode("/").name()
'inode/mount-point'
>>> MimeType.fromInode(".").name()
'inode/directory'
>>> print(MimeType.fromInode("no-such-file"))
None
>>> MimeType(MimeType("inode/directory")).name()
'inode/directory'
# Localized attributes
>>> mime.comment()
'plain text document'
>>> mime.comment(lang="en") == mime.comment()
True
>>> mime.comment(lang="fr")
'document texte brut'
>>> MimeType("application/xml").comment()
'XML document'
>>> MimeType("application/xml").acronym()
'XML'
>>> MimeType("application/xml").expandedAcronym()
'eXtensible Markup Language'
# Non-existant mime types
>>> MimeType("application/x-does-not-exist")
<MimeType: application/x-does-not-exist>
>>> MimeType("application/x-does-not-exist").comment()
# Aliases / subclasses
>>> MimeType("application/javascript").aliases()
[<MimeType: application/x-javascript>, <MimeType: text/javascript>]
>>> MimeType("text/xml").aliasOf()
<MimeType: application/xml>
>>> MimeType("text/x-python").subClassOf()
[<MimeType: application/x-executable>, <MimeType: text/plain>]
>>> MimeType("text/x-python").isInstance("text/x-python")
True
>>> MimeType("text/x-python").isInstance("text/plain")
True
>>> MimeType("text/x-python").isInstance("application/x-executable")
True
>>> MimeType("text/plain").isInstance("application/zip")
False
>>> MimeType("application/vnd.android.package-archive").subClassOf()
[<MimeType: application/x-java-archive>]
>>> MimeType("application/vnd.android.package-archive").isInstance("application/x-java-archive")
True
>>> MimeType("application/vnd.android.package-archive").isInstance("application/zip")
True
# icons / extensions
>>> MimeType("application/zip").genericIcon()
'package-x-generic'
>>> MimeType("application/zip").icon()
'application-zip'
>>> MimeType("text/plain").genericIcon()
'text-x-generic'
>>> MimeType("application/x-bzip").extensions()
['.bz', '.bz2']
>>> MimeType("application/x-java-archive").extensions()
['.jar']
# test for globs weights
>>> MimeType.fromName("foo.png")
<MimeType: image/png>
# from* classmethods
>>> MimeType.fromName("foo.TXT").name()
'text/plain'
>>> MimeType.fromName("foo.C").name()
'text/x-c++src'
>>> MimeType.fromName("foo.c").name()
'text/x-csrc'
>>> MimeType.fromInode("/dev/sda").name()
'inode/blockdevice'
>>> MimeType.fromInode("/dev/null").name()
'inode/chardevice'
>>> MimeType.fromInode("/").name()
'inode/mount-point'
>>> MimeType.fromInode(".").name()
'inode/directory'
>>> MimeType.fromScheme("http://example.com").name()
'x-scheme-handler/http'
>>> MimeType.fromScheme("ftp://example.com").name()
'x-scheme-handler/ftp'
>>> MimeType.fromScheme("mailto:user@example.com").name()
'x-scheme-handler/mailto'
>>> MimeType.fromScheme("file:///").name()
'x-scheme-handler/file'
>>> f = open("test.tmp", "w")
>>> MimeType.fromContent(f.name).name()
'application/x-zerosize'
>>> _ = f.write("foo")
>>> f.close()
>>> MimeType.fromContent(f.name).name()
'text/plain'
>>> os.remove(f.name)
"""
if __name__ == "__main__":
import doctest
doctest.testmod()