Skip to content
This repository was archived by the owner on Feb 4, 2020. It is now read-only.

Commit 21e6f93

Browse files
committed
Merge pull request #82 from jimnicholls/issue76
Updated Writer API
2 parents d4d449e + 4d4af1b commit 21e6f93

File tree

2 files changed

+78
-102
lines changed

2 files changed

+78
-102
lines changed

pymarc/writer.py

+26-43
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,20 @@
2020

2121
class Writer(object):
2222

23-
def __init__(self, file_handle, own_file_handle = True):
23+
def __init__(self, file_handle):
2424
self.file_handle = file_handle
25-
self.own_file_handle = own_file_handle
2625

2726
def write(self, record):
2827
if not isinstance(record, Record):
2928
raise WriteNeedsRecord
3029

31-
def close(self):
30+
def close(self, also_close_file_handle = True):
3231
"""
3332
Closes the writer.
3433
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.
3635
"""
37-
if self.own_file_handle:
36+
if also_close_file_handle:
3837
self.file_handle.close()
3938
self.file_handle = None
4039

@@ -53,25 +52,21 @@ class JSONWriter(Writer):
5352
### writing to a file
5453
>>> writer = JSONWriter(open('file.json','wt'))
5554
>>> writer.write(record)
56-
>>> writer.close() # Important!
55+
>>> writer.close() # Important!
5756
5857
### writing to a string
5958
>>> string = StringIO()
6059
>>> writer = JSONWriter(string, own_file_handle = False)
6160
>>> writer.write(record)
62-
>>> writer.close() # Important!
61+
>>> writer.close(also_close_file_handle = False) # Important!
6362
>>> print string
6463
"""
6564

66-
def __init__(self, file_handle, own_file_handle = True):
65+
def __init__(self, file_handle):
6766
"""
6867
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.
7368
"""
74-
super(JSONWriter, self).__init__(file_handle, own_file_handle)
69+
super(JSONWriter, self).__init__(file_handle)
7570
self.write_count = 0
7671
self.file_handle.write('[')
7772

@@ -85,14 +80,14 @@ def write(self, record):
8580
json.dump(record.as_dict(), self.file_handle, separators=(',', ':'))
8681
self.write_count += 1
8782

88-
def close(self):
83+
def close(self, also_close_file_handle = True):
8984
"""
9085
Closes the writer.
9186
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.
9388
"""
9489
self.file_handle.write(']')
95-
Writer.close(self)
90+
Writer.close(self, also_close_file_handle)
9691

9792

9893
class MARCWriter(Writer):
@@ -112,25 +107,21 @@ class MARCWriter(Writer):
112107
>>> string = StringIO()
113108
>>> writer = MARCWriter(string)
114109
>>> writer.write(record)
115-
>>> writer.close()
110+
>>> writer.close(also_close_file_handle = False)
116111
>>> print string
117112
118113
### writing to memory (Python 3 only)
119114
>>> memory = BytesIO()
120115
>>> writer = MARCWriter(memory)
121116
>>> writer.write(record)
122-
>>> writer.close()
117+
>>> writer.close(also_close_file_handle = False)
123118
"""
124119

125-
def __init__(self, file_handle, own_file_handle = True):
120+
def __init__(self, file_handle):
126121
"""
127122
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.
132123
"""
133-
super(MARCWriter, self).__init__(file_handle, own_file_handle)
124+
super(MARCWriter, self).__init__(file_handle)
134125

135126
def write(self, record):
136127
"""
@@ -159,19 +150,15 @@ class TextWriter(Writer):
159150
>>> string = StringIO()
160151
>>> writer = TextWriter(string, own_file_handle = False)
161152
>>> writer.write(record)
162-
>>> writer.close()
153+
>>> writer.close(also_close_file_handle = False)
163154
>>> print string
164155
"""
165156

166-
def __init__(self, file_handle, own_file_handle = True):
157+
def __init__(self, file_handle):
167158
"""
168159
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.
173160
"""
174-
super(TextWriter, self).__init__(file_handle, own_file_handle)
161+
super(TextWriter, self).__init__(file_handle)
175162
self.write_count = 0
176163

177164
def write(self, record):
@@ -199,31 +186,27 @@ class XMLWriter(Writer):
199186
### writing to a file
200187
>>> writer = XMLWriter(open('file.xml','wb'))
201188
>>> writer.write(record)
202-
>>> writer.close() # Important!
189+
>>> writer.close() # Important!
203190
204191
### writing to a string (Python 2 only)
205192
>>> string = StringIO()
206193
>>> writer = XMLWriter(string)
207194
>>> writer.write(record)
208-
>>> writer.close() # Important!
195+
>>> writer.close(also_close_file_handle = False) # Important!
209196
>>> print string
210197
211198
### writing to memory (Python 3 only)
212199
>>> memory = BytesIO()
213200
>>> writer = XMLWriter(memory)
214201
>>> writer.write(record)
215-
>>> writer.close() # Important!
202+
>>> writer.close(also_close_file_handle = False) # Important!
216203
"""
217204

218-
def __init__(self, file_handle, own_file_handle = True):
205+
def __init__(self, file_handle):
219206
"""
220207
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.
225208
"""
226-
super(XMLWriter, self).__init__(file_handle, own_file_handle)
209+
super(XMLWriter, self).__init__(file_handle)
227210
self.file_handle.write(
228211
b'<?xml version="1.0" encoding="UTF-8"?>')
229212
self.file_handle.write(
@@ -237,11 +220,11 @@ def write(self, record):
237220
node = pymarc.record_to_xml_node(record)
238221
self.file_handle.write(ET.tostring(node, encoding='utf-8'))
239222

240-
def close(self):
223+
def close(self, also_close_file_handle = True):
241224
"""
242225
Closes the writer.
243226
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.
245228
"""
246229
self.file_handle.write(b'</collection>')
247-
Writer.close(self)
230+
Writer.close(self, also_close_file_handle)

0 commit comments

Comments
 (0)