20
20
21
21
class Writer (object ):
22
22
23
- def __init__ (self , file_handle , own_file_handle = True ):
23
+ def __init__ (self , file_handle ):
24
24
self .file_handle = file_handle
25
- self .own_file_handle = own_file_handle
26
25
27
26
def write (self , record ):
28
27
if not isinstance (record , Record ):
29
28
raise WriteNeedsRecord
30
29
31
- def close (self ):
30
+ def close (self , also_close_file_handle = True ):
32
31
"""
33
32
Closes the writer.
34
33
35
- If own_file_handle is True, also closes the file handle.
34
+ If also_close_file_handle is True, also closes the file handle.
36
35
"""
37
- if self . own_file_handle :
36
+ if also_close_file_handle :
38
37
self .file_handle .close ()
39
38
self .file_handle = None
40
39
@@ -53,25 +52,21 @@ class JSONWriter(Writer):
53
52
### writing to a file
54
53
>>> writer = JSONWriter(open('file.json','wt'))
55
54
>>> writer.write(record)
56
- >>> writer.close() # Important!
55
+ >>> writer.close() # Important!
57
56
58
57
### writing to a string
59
58
>>> string = StringIO()
60
59
>>> writer = JSONWriter(string, own_file_handle = False)
61
60
>>> writer.write(record)
62
- >>> writer.close() # Important!
61
+ >>> writer.close(also_close_file_handle = False) # Important!
63
62
>>> print string
64
63
"""
65
64
66
- def __init__ (self , file_handle , own_file_handle = True ):
65
+ def __init__ (self , file_handle ):
67
66
"""
68
67
You need to pass in a text file like object.
69
-
70
- If own_file_handle is True (the default) then the file handle will be
71
- closed when the writer is closed. Otherwise the file handle will be
72
- left open.
73
68
"""
74
- super (JSONWriter , self ).__init__ (file_handle , own_file_handle )
69
+ super (JSONWriter , self ).__init__ (file_handle )
75
70
self .write_count = 0
76
71
self .file_handle .write ('[' )
77
72
@@ -85,14 +80,14 @@ def write(self, record):
85
80
json .dump (record .as_dict (), self .file_handle , separators = (',' , ':' ))
86
81
self .write_count += 1
87
82
88
- def close (self ):
83
+ def close (self , also_close_file_handle = True ):
89
84
"""
90
85
Closes the writer.
91
86
92
- If own_file_handle is True, also closes the file handle.
87
+ If also_close_file_handle is True, also closes the file handle.
93
88
"""
94
89
self .file_handle .write (']' )
95
- Writer .close (self )
90
+ Writer .close (self , also_close_file_handle )
96
91
97
92
98
93
class MARCWriter (Writer ):
@@ -112,25 +107,21 @@ class MARCWriter(Writer):
112
107
>>> string = StringIO()
113
108
>>> writer = MARCWriter(string)
114
109
>>> writer.write(record)
115
- >>> writer.close()
110
+ >>> writer.close(also_close_file_handle = False )
116
111
>>> print string
117
112
118
113
### writing to memory (Python 3 only)
119
114
>>> memory = BytesIO()
120
115
>>> writer = MARCWriter(memory)
121
116
>>> writer.write(record)
122
- >>> writer.close()
117
+ >>> writer.close(also_close_file_handle = False )
123
118
"""
124
119
125
- def __init__ (self , file_handle , own_file_handle = True ):
120
+ def __init__ (self , file_handle ):
126
121
"""
127
122
You need to pass in a byte file like object.
128
-
129
- If own_file_handle is True (the default) then the file handle will be
130
- closed when the writer is closed. Otherwise the file handle will be
131
- left open.
132
123
"""
133
- super (MARCWriter , self ).__init__ (file_handle , own_file_handle )
124
+ super (MARCWriter , self ).__init__ (file_handle )
134
125
135
126
def write (self , record ):
136
127
"""
@@ -159,19 +150,15 @@ class TextWriter(Writer):
159
150
>>> string = StringIO()
160
151
>>> writer = TextWriter(string, own_file_handle = False)
161
152
>>> writer.write(record)
162
- >>> writer.close()
153
+ >>> writer.close(also_close_file_handle = False )
163
154
>>> print string
164
155
"""
165
156
166
- def __init__ (self , file_handle , own_file_handle = True ):
157
+ def __init__ (self , file_handle ):
167
158
"""
168
159
You need to pass in a text file like object.
169
-
170
- If own_file_handle is True (the default) then the file handle will be
171
- closed when the writer is closed. Otherwise the file handle will be
172
- left open.
173
160
"""
174
- super (TextWriter , self ).__init__ (file_handle , own_file_handle )
161
+ super (TextWriter , self ).__init__ (file_handle )
175
162
self .write_count = 0
176
163
177
164
def write (self , record ):
@@ -199,31 +186,27 @@ class XMLWriter(Writer):
199
186
### writing to a file
200
187
>>> writer = XMLWriter(open('file.xml','wb'))
201
188
>>> writer.write(record)
202
- >>> writer.close() # Important!
189
+ >>> writer.close() # Important!
203
190
204
191
### writing to a string (Python 2 only)
205
192
>>> string = StringIO()
206
193
>>> writer = XMLWriter(string)
207
194
>>> writer.write(record)
208
- >>> writer.close() # Important!
195
+ >>> writer.close(also_close_file_handle = False) # Important!
209
196
>>> print string
210
197
211
198
### writing to memory (Python 3 only)
212
199
>>> memory = BytesIO()
213
200
>>> writer = XMLWriter(memory)
214
201
>>> writer.write(record)
215
- >>> writer.close() # Important!
202
+ >>> writer.close(also_close_file_handle = False) # Important!
216
203
"""
217
204
218
- def __init__ (self , file_handle , own_file_handle = True ):
205
+ def __init__ (self , file_handle ):
219
206
"""
220
207
You need to pass in a binary file like object.
221
-
222
- If own_file_handle is True (the default) then the file handle will be
223
- closed when the writer is closed. Otherwise the file handle will be
224
- left open.
225
208
"""
226
- super (XMLWriter , self ).__init__ (file_handle , own_file_handle )
209
+ super (XMLWriter , self ).__init__ (file_handle )
227
210
self .file_handle .write (
228
211
b'<?xml version="1.0" encoding="UTF-8"?>' )
229
212
self .file_handle .write (
@@ -237,11 +220,11 @@ def write(self, record):
237
220
node = pymarc .record_to_xml_node (record )
238
221
self .file_handle .write (ET .tostring (node , encoding = 'utf-8' ))
239
222
240
- def close (self ):
223
+ def close (self , also_close_file_handle = True ):
241
224
"""
242
225
Closes the writer.
243
226
244
- If own_file_handle is True, also closes the file handle.
227
+ If also_close_file_handle is True, also closes the file handle.
245
228
"""
246
229
self .file_handle .write (b'</collection>' )
247
- Writer .close (self )
230
+ Writer .close (self , also_close_file_handle )
0 commit comments