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

Commit 4f4f62d

Browse files
committed
Merge branch 'issue76'
2 parents 8f9ad18 + 991dcf8 commit 4f4f62d

File tree

2 files changed

+838
-23
lines changed

2 files changed

+838
-23
lines changed

pymarc/writer.py

Lines changed: 220 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,247 @@
1+
import pymarc
12
from pymarc import Record, WriteNeedsRecord
23

4+
try:
5+
import xml.etree.ElementTree as ET # builtin in Python 2.5
6+
except ImportError:
7+
import elementtree.ElementTree as ET
8+
9+
try:
10+
# the json module was included in the stdlib in python 2.6
11+
# http://docs.python.org/library/json.html
12+
import json
13+
except ImportError:
14+
# simplejson 2.0.9 is available for python 2.4+
15+
# http://pypi.python.org/pypi/simplejson/2.0.9
16+
# simplejson 1.7.3 is available for python 2.3+
17+
# http://pypi.python.org/pypi/simplejson/1.7.3
18+
import simplejson as json
19+
20+
321
class Writer(object):
422

23+
def __init__(self, file_handle, own_file_handle = True):
24+
self.file_handle = file_handle
25+
self.own_file_handle = own_file_handle
26+
527
def write(self, record):
6-
pass
28+
if not isinstance(record, Record):
29+
raise WriteNeedsRecord
30+
31+
def close(self):
32+
"""
33+
Closes the writer.
34+
35+
If own_file_handle is True, also closes the file handle.
36+
"""
37+
if self.own_file_handle:
38+
self.file_handle.close()
39+
self.file_handle = None
40+
41+
42+
class JSONWriter(Writer):
43+
"""
44+
A class for writing records as an array of MARC-in-JSON objects.
45+
46+
IMPORTANT: You must the close a JSONWriter,
47+
otherwise you will not get valid JSON.
48+
49+
Simple usage::
50+
51+
>>> from pymarc import JSONWriter
52+
53+
### writing to a file
54+
>>> writer = JSONWriter(open('file.json','wt'))
55+
>>> writer.write(record)
56+
>>> writer.close() # Important!
57+
58+
### writing to a string
59+
>>> string = StringIO()
60+
>>> writer = JSONWriter(string, own_file_handle = False)
61+
>>> writer.write(record)
62+
>>> writer.close() # Important!
63+
>>> print string
64+
"""
65+
66+
def __init__(self, file_handle, own_file_handle = True):
67+
"""
68+
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+
"""
74+
super(JSONWriter, self).__init__(file_handle, own_file_handle)
75+
self.write_count = 0
76+
self.file_handle.write('[')
77+
78+
def write(self, record):
79+
"""
80+
Writes a record.
81+
"""
82+
Writer.write(self, record)
83+
if self.write_count > 0:
84+
self.file_handle.write(',')
85+
json.dump(record.as_dict(), self.file_handle, separators=(',', ':'))
86+
self.write_count += 1
87+
88+
def close(self):
89+
"""
90+
Closes the writer.
91+
92+
If own_file_handle is True, also closes the file handle.
93+
"""
94+
self.file_handle.write(']')
95+
Writer.close(self)
96+
797

898
class MARCWriter(Writer):
999
"""
10100
A class for writing MARC21 records in transmission format.
11101
12-
Simple usage:
102+
Simple usage::
103+
104+
>>> from pymarc import MARCWriter
13105
14-
from pymarc import MARCWriter
106+
### writing to a file
107+
>>> writer = MARCWriter(open('file.dat','wb'))
108+
>>> writer.write(record)
109+
>>> writer.close()
15110
16-
## pass in a file
17-
writer = MARCWriter(file('file.dat','w'))
18-
writer.write(record)
111+
### writing to a string (Python 2 only)
112+
>>> string = StringIO()
113+
>>> writer = MARCWriter(string)
114+
>>> writer.write(record)
115+
>>> writer.close()
116+
>>> print string
19117
20-
## use StringIO if you want to write to a string
21-
string = StringIO()
22-
writer = MARCWriter(string)
23-
writer.write(record)
24-
print string
118+
### writing to memory (Python 3 only)
119+
>>> memory = BytesIO()
120+
>>> writer = MARCWriter(memory)
121+
>>> writer.write(record)
122+
>>> writer.close()
25123
"""
26124

27-
def __init__(self, file_handle):
125+
def __init__(self, file_handle, own_file_handle = True):
28126
"""
29-
You need to pass in a file like object.
127+
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.
30132
"""
31-
super(MARCWriter, self).__init__()
32-
self.file_handle = file_handle
133+
super(MARCWriter, self).__init__(file_handle, own_file_handle)
33134

34135
def write(self, record):
35136
"""
36137
Writes a record.
37138
"""
38-
if not isinstance(record, Record):
39-
raise WriteNeedsRecord
139+
Writer.write(self, record)
40140
self.file_handle.write(record.as_marc())
41141

42-
def close(self):
142+
143+
class TextWriter(Writer):
144+
"""
145+
A class for writing records in prettified text MARCMaker format.
146+
147+
A blank line separates each record.
148+
149+
Simple usage::
150+
151+
>>> from pymarc import TextWriter
152+
153+
### writing to a file
154+
>>> writer = TextWriter(open('file.txt','wt'))
155+
>>> writer.write(record)
156+
>>> writer.close()
157+
158+
### writing to a string
159+
>>> string = StringIO()
160+
>>> writer = TextWriter(string, own_file_handle = False)
161+
>>> writer.write(record)
162+
>>> writer.close()
163+
>>> print string
164+
"""
165+
166+
def __init__(self, file_handle, own_file_handle = True):
43167
"""
44-
Closes the file.
168+
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.
45173
"""
46-
self.file_handle.close()
47-
self.file_handle = None
174+
super(TextWriter, self).__init__(file_handle, own_file_handle)
175+
self.write_count = 0
176+
177+
def write(self, record):
178+
"""
179+
Writes a record.
180+
"""
181+
Writer.write(self, record)
182+
if self.write_count > 0:
183+
self.file_handle.write('\n')
184+
self.file_handle.write(str(record))
185+
self.write_count += 1
186+
187+
188+
class XMLWriter(Writer):
189+
"""
190+
A class for writing records as a MARCXML collection.
48191
192+
IMPORTANT: You must the close an XMLWriter, otherwise you will not get
193+
a valid XML document.
194+
195+
Simple usage::
196+
197+
>>> from pymarc import XMLWriter
198+
199+
### writing to a file
200+
>>> writer = XMLWriter(open('file.xml','wb'))
201+
>>> writer.write(record)
202+
>>> writer.close() # Important!
203+
204+
### writing to a string (Python 2 only)
205+
>>> string = StringIO()
206+
>>> writer = XMLWriter(string)
207+
>>> writer.write(record)
208+
>>> writer.close() # Important!
209+
>>> print string
210+
211+
### writing to memory (Python 3 only)
212+
>>> memory = BytesIO()
213+
>>> writer = XMLWriter(memory)
214+
>>> writer.write(record)
215+
>>> writer.close() # Important!
216+
"""
217+
218+
def __init__(self, file_handle, own_file_handle = True):
219+
"""
220+
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+
"""
226+
super(XMLWriter, self).__init__(file_handle, own_file_handle)
227+
self.file_handle.write(
228+
b'<?xml version="1.0" encoding="UTF-8"?>')
229+
self.file_handle.write(
230+
b'<collection xmlns="http://www.loc.gov/MARC21/slim">')
231+
232+
def write(self, record):
233+
"""
234+
Writes a record.
235+
"""
236+
Writer.write(self, record)
237+
node = pymarc.record_to_xml_node(record)
238+
self.file_handle.write(ET.tostring(node, encoding='utf-8'))
239+
240+
def close(self):
241+
"""
242+
Closes the writer.
243+
244+
If own_file_handle is True, also closes the file handle.
245+
"""
246+
self.file_handle.write(b'</collection>')
247+
Writer.close(self)

0 commit comments

Comments
 (0)