@@ -86,10 +86,12 @@ def _auto_adjust_mode(data, mode):
86
86
87
87
88
88
def _write_chunk (path , chunk , offset , logger = None , mode = 'r+b' ,
89
- make_parent = True , create_file = True ):
89
+ make_parent = True , create_file = True , force_string = False ):
90
90
"""Internal helper to wrap writing of chunks with offset to path.
91
91
The optional make_parent and create_file are used to decide if the parent
92
92
directory and the file should be created if it doesn't already exist.
93
+ The optional force_string is used to force contents to string even if it's
94
+ another type.
93
95
"""
94
96
# logger.debug("writing chunk to %r at offset %d" % (path, offset))
95
97
@@ -130,7 +132,11 @@ def _write_chunk(path, chunk, offset, logger=None, mode='r+b',
130
132
filehandle .write ('\0 ' )
131
133
# logger.debug("write %r chunk of size %d at position %d" %
132
134
# (path, len(chunk), filehandle.tell()))
133
- filehandle .write (chunk )
135
+ # NOTE: any value/type of chunk is forced to str if requested
136
+ if force_string :
137
+ filehandle .write (str (chunk ))
138
+ else :
139
+ filehandle .write (chunk )
134
140
# logger.debug("file %r chunk written at %d" % (path, offset))
135
141
return True
136
142
except Exception as err :
@@ -139,7 +145,7 @@ def _write_chunk(path, chunk, offset, logger=None, mode='r+b',
139
145
return False
140
146
141
147
142
- def write_chunk (path , chunk , offset , logger , mode = 'r+b' ):
148
+ def write_chunk (path , chunk , offset , logger , mode = 'r+b' , force_string = False ):
143
149
"""Wrapper to handle writing of chunks with offset to path.
144
150
Creates file first if it doesn't already exist.
145
151
"""
@@ -149,10 +155,12 @@ def write_chunk(path, chunk, offset, logger, mode='r+b'):
149
155
# TODO: enable this again once throuroughly tested and assured py2+3 safe
150
156
# mode = _auto_adjust_mode(chunk, mode)
151
157
152
- return _write_chunk (path , chunk , offset , logger , mode )
158
+ return _write_chunk (path , chunk , offset , logger , mode ,
159
+ force_string = force_string )
153
160
154
161
155
- def write_file (content , path , logger , mode = 'w' , make_parent = True , umask = None ):
162
+ def write_file (content , path , logger , mode = 'w' , make_parent = True , umask = None ,
163
+ force_string = False ):
156
164
"""Wrapper to handle writing of contents to path"""
157
165
if not logger :
158
166
logger = null_logger ("dummy" )
@@ -165,7 +173,8 @@ def write_file(content, path, logger, mode='w', make_parent=True, umask=None):
165
173
#mode = _auto_adjust_mode(content, mode)
166
174
167
175
retval = _write_chunk (path , content , offset = 0 , logger = logger , mode = mode ,
168
- make_parent = make_parent , create_file = False )
176
+ make_parent = make_parent , create_file = False ,
177
+ force_string = force_string )
169
178
170
179
if umask is not None :
171
180
os .umask (old_umask )
0 commit comments