-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbinfile.py
67 lines (53 loc) · 1.99 KB
/
binfile.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
#!/usr/bin/env python
# coding: utf-8
import os
import contentfile
class binfile(contentfile.contentfile):
'''
File class which is represented by a string containing its binary
contents. Good for files which must have their content exposed or be
manipulated but don't have their own specialized class.
'''
def __init__(self, path=None, content=None, dir=None, **kwargs):
'''Initialize a binfile object.'''
self.dir = None
self.setpath(path, **kwargs)
self.dir = dir
self.content = content
if self.content is None and self.path is not None and os.path.isfile(self.path): self.read(self.path)
self.kind = 'bin'
def __len__(self):
'''Get the length in bytes of the file's binary data string.'''
return len(self.content)
def __iadd__(self, content):
'''Add to the end of the file's content string.'''
self.add(content)
return self
def ref(self, **kwargs):
raise ValueError('Failed to cast binfile %s to a reffile because it is an invalid conversion.' % self)
def bin(self, **kwargs):
for key, value in kwargs.iteritems(): self.__dict__[key] = value
return self
def raw(self, **kwargs):
self.kind = 'raw'
self.__class__ = rawfile.rawfile
for key, value in kwargs.iteritems(): self.__dict__[key] = value
self.read(content=self.content)
return self
def copy(self):
'''Create a copy of the file.'''
copy = binfile()
copy.path = self.path
copy.rootpath = self.rootpath
copy.name = self.name
copy.ext = self.ext
copy.loc = self.loc
copy.content = self.content
return copy
def add(self, content):
'''Add to the end of the file's content string.'''
if self.content is None:
self.content = str(content)
else:
self.content += str(content)
import rawfile