74 def write(self, data): |
74 def write(self, data): |
75 assert isinstance(data, (str, buffer)), \ |
75 assert isinstance(data, (str, buffer)), \ |
76 "Binary objects must use raw strings, not %s" % data.__class__ |
76 "Binary objects must use raw strings, not %s" % data.__class__ |
77 StringIO.write(self, data) |
77 StringIO.write(self, data) |
78 |
78 |
|
79 def to_file(self, filename): |
|
80 """write a binary to disk |
|
81 |
|
82 the writing is performed in a safe way for files stored on |
|
83 Windows SMB shares |
|
84 """ |
|
85 pos = self.tell() |
|
86 with open(filename, 'wb') as fobj: |
|
87 self.seek(0) |
|
88 if sys.platform == 'win32': |
|
89 while True: |
|
90 # the 16kB chunksize comes from the shutil module |
|
91 # in stdlib |
|
92 chunk = self.read(16*1024) |
|
93 if not chunk: |
|
94 break |
|
95 fobj.write(chunk) |
|
96 else: |
|
97 fobj.write(self.read()) |
|
98 self.seek(pos) |
|
99 |
|
100 @staticmethod |
|
101 def from_file(filename): |
|
102 """read a file and returns its contents in a Binary |
|
103 |
|
104 the reading is performed in a safe way for files stored on |
|
105 Windows SMB shares |
|
106 """ |
|
107 binary = Binary() |
|
108 with open(filename, 'rb') as fobj: |
|
109 if sys.platform == 'win32': |
|
110 while True: |
|
111 # the 16kB chunksize comes from the shutil module |
|
112 # in stdlib |
|
113 chunk = fobj.read(16*1024) |
|
114 if not chunk: |
|
115 break |
|
116 binary.write(chunk) |
|
117 else: |
|
118 binary.write(fobj.read()) |
|
119 return binary |
|
120 |
|
121 |
79 # use this dictionary to rename entity types while keeping bw compat |
122 # use this dictionary to rename entity types while keeping bw compat |
80 ETYPE_NAME_MAP = {} |
123 ETYPE_NAME_MAP = {} |
81 |
124 |
82 # XXX cubic web cube migration map. See if it's worth keeping this mecanism |
125 # XXX cubic web cube migration map. See if it's worth keeping this mecanism |
83 # to help in cube renaming |
126 # to help in cube renaming |