forked from ahupp/python-magic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonic.py
More file actions
163 lines (130 loc) · 4.01 KB
/
pythonic.py
File metadata and controls
163 lines (130 loc) · 4.01 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
"""
This is the more Pythonic / nicer version..
"""
import os
from magic.wrapper import MAGIC_NONE, magic_open, magic_load, magic_buffer, \
magic_file, magic_close, magic_setflags, MAGIC_MIME_TYPE, \
MAGIC_MIME_ENCODING, MAGIC_COMPRESS
## Internal use constants
FROM_FILE = 1
FROM_BUFFER = 2
class Magic(object):
"""
Object-oriented version of the library..
"""
_flags = None
_cookie = None
def __init__(self, flags=None, magic_file=None, mime=False,
mime_encoding=False, compress=False):
self._flags = flags or MAGIC_NONE
if mime:
self._flags |= MAGIC_MIME_TYPE
if mime_encoding:
self._flags |= MAGIC_MIME_ENCODING
if compress:
self._flags |= MAGIC_COMPRESS
self._cookie = magic_open(self.flags)
magic_load(self._cookie, magic_file)
@property
def flags(self):
return self._flags
@flags.setter
def flags(self, value):
self.setflags(value)
def setflags(self, flags):
self._flags = flags
magic_setflags(self._cookie, flags)
def from_buffer(self, buf):
return magic_buffer(self._cookie, buf)
def from_file(self, filename):
if not os.path.exists(filename):
raise IOError("File does not exist: " + filename)
return magic_file(self._cookie, filename)
def close(self):
magic_close(self._cookie)
def __del__(self):
## during shutdown magic_close may have been cleared already
if self._cookie and magic_close:
self.close()
self._cookie = None
class Magic2(object):
"""
More natural, yet less efficient implementation of the library.
"""
def __init__(self, from_type, from_arg, flags=None):
self._from = from_type
self._from_arg = from_arg
self._cookie = None
self._loaded = False
self.flags = flags or MAGIC_NONE
def _open(self, flags=MAGIC_NONE):
self._cookie = magic_open(flags)
def _close(self):
magic_close(self._cookie)
self._cookie = None
def __del__(self):
## during shutdown magic_close may have been cleared already
if self._cookie and magic_close:
self._close()
def _load(self, filename=None):
magic_load(self._cookie, filename)
self._loaded = True
def _setflags(self, flags):
magic_setflags(self._cookie, flags)
def _prepare(self):
if self._cookie is None:
self._open()
if not self._loaded:
self._load()
def _return(self):
if self._from == FROM_FILE:
return magic_file(self._cookie, self._from_arg)
if self._from == FROM_BUFFER:
return magic_buffer(self._cookie, self._from_arg)
raise ValueError
@classmethod
def from_file(cls, filename, flags=None):
return cls(from_type=FROM_FILE, from_arg=filename, flags=flags)
@classmethod
def from_buffer(cls, buf, flags=None):
return cls(from_type=FROM_BUFFER, from_arg=buf, flags=flags)
@property
def description(self):
self._prepare()
self._setflags(
self.flags
& ~MAGIC_MIME_TYPE
& ~MAGIC_MIME_ENCODING
)
return self._return()
@property
def mimetype(self):
self._prepare()
self._setflags(
self.flags
| MAGIC_MIME_TYPE
& ~MAGIC_MIME_ENCODING
)
return self._return()
@property
def encoding(self):
self._prepare()
self._setflags(
self.flags
& ~MAGIC_MIME_TYPE
| MAGIC_MIME_ENCODING
)
return self._return()
@property
def mime(self):
self._prepare()
self._setflags(
self.flags
| MAGIC_MIME_TYPE
| MAGIC_MIME_ENCODING
)
return self._return()
def __str__(self):
return self.description
def __unicode__(self):
return unicode(self.description)