From a4158bc3597e26f0a38d553a3ec2fbbb0dae60ef Mon Sep 17 00:00:00 2001 From: Jim Nicholls Date: Tue, 8 Sep 2015 08:01:26 +1000 Subject: [PATCH] Replace own_file_handle on Writer __init__ with also_close_file_handle on Writer close. --- pymarc/writer.py | 69 +++++++++++------------------ test/writer.py | 111 ++++++++++++++++++++++------------------------- 2 files changed, 78 insertions(+), 102 deletions(-) diff --git a/pymarc/writer.py b/pymarc/writer.py index 7368859..86f7a65 100644 --- a/pymarc/writer.py +++ b/pymarc/writer.py @@ -20,21 +20,20 @@ class Writer(object): - def __init__(self, file_handle, own_file_handle = True): + def __init__(self, file_handle): self.file_handle = file_handle - self.own_file_handle = own_file_handle def write(self, record): if not isinstance(record, Record): raise WriteNeedsRecord - def close(self): + def close(self, also_close_file_handle = True): """ Closes the writer. - If own_file_handle is True, also closes the file handle. + If also_close_file_handle is True, also closes the file handle. """ - if self.own_file_handle: + if also_close_file_handle: self.file_handle.close() self.file_handle = None @@ -53,25 +52,21 @@ class JSONWriter(Writer): ### writing to a file >>> writer = JSONWriter(open('file.json','wt')) >>> writer.write(record) - >>> writer.close() # Important! + >>> writer.close() # Important! ### writing to a string >>> string = StringIO() >>> writer = JSONWriter(string, own_file_handle = False) >>> writer.write(record) - >>> writer.close() # Important! + >>> writer.close(also_close_file_handle = False) # Important! >>> print string """ - def __init__(self, file_handle, own_file_handle = True): + def __init__(self, file_handle): """ You need to pass in a text file like object. - - If own_file_handle is True (the default) then the file handle will be - closed when the writer is closed. Otherwise the file handle will be - left open. """ - super(JSONWriter, self).__init__(file_handle, own_file_handle) + super(JSONWriter, self).__init__(file_handle) self.write_count = 0 self.file_handle.write('[') @@ -85,14 +80,14 @@ def write(self, record): json.dump(record.as_dict(), self.file_handle, separators=(',', ':')) self.write_count += 1 - def close(self): + def close(self, also_close_file_handle = True): """ Closes the writer. - If own_file_handle is True, also closes the file handle. + If also_close_file_handle is True, also closes the file handle. """ self.file_handle.write(']') - Writer.close(self) + Writer.close(self, also_close_file_handle) class MARCWriter(Writer): @@ -112,25 +107,21 @@ class MARCWriter(Writer): >>> string = StringIO() >>> writer = MARCWriter(string) >>> writer.write(record) - >>> writer.close() + >>> writer.close(also_close_file_handle = False) >>> print string ### writing to memory (Python 3 only) >>> memory = BytesIO() >>> writer = MARCWriter(memory) >>> writer.write(record) - >>> writer.close() + >>> writer.close(also_close_file_handle = False) """ - def __init__(self, file_handle, own_file_handle = True): + def __init__(self, file_handle): """ You need to pass in a byte file like object. - - If own_file_handle is True (the default) then the file handle will be - closed when the writer is closed. Otherwise the file handle will be - left open. """ - super(MARCWriter, self).__init__(file_handle, own_file_handle) + super(MARCWriter, self).__init__(file_handle) def write(self, record): """ @@ -159,19 +150,15 @@ class TextWriter(Writer): >>> string = StringIO() >>> writer = TextWriter(string, own_file_handle = False) >>> writer.write(record) - >>> writer.close() + >>> writer.close(also_close_file_handle = False) >>> print string """ - def __init__(self, file_handle, own_file_handle = True): + def __init__(self, file_handle): """ You need to pass in a text file like object. - - If own_file_handle is True (the default) then the file handle will be - closed when the writer is closed. Otherwise the file handle will be - left open. """ - super(TextWriter, self).__init__(file_handle, own_file_handle) + super(TextWriter, self).__init__(file_handle) self.write_count = 0 def write(self, record): @@ -199,31 +186,27 @@ class XMLWriter(Writer): ### writing to a file >>> writer = XMLWriter(open('file.xml','wb')) >>> writer.write(record) - >>> writer.close() # Important! + >>> writer.close() # Important! ### writing to a string (Python 2 only) >>> string = StringIO() >>> writer = XMLWriter(string) >>> writer.write(record) - >>> writer.close() # Important! + >>> writer.close(also_close_file_handle = False) # Important! >>> print string ### writing to memory (Python 3 only) >>> memory = BytesIO() >>> writer = XMLWriter(memory) >>> writer.write(record) - >>> writer.close() # Important! + >>> writer.close(also_close_file_handle = False) # Important! """ - def __init__(self, file_handle, own_file_handle = True): + def __init__(self, file_handle): """ You need to pass in a binary file like object. - - If own_file_handle is True (the default) then the file handle will be - closed when the writer is closed. Otherwise the file handle will be - left open. """ - super(XMLWriter, self).__init__(file_handle, own_file_handle) + super(XMLWriter, self).__init__(file_handle) self.file_handle.write( b'') self.file_handle.write( @@ -237,11 +220,11 @@ def write(self, record): node = pymarc.record_to_xml_node(record) self.file_handle.write(ET.tostring(node, encoding='utf-8')) - def close(self): + def close(self, also_close_file_handle = True): """ Closes the writer. - If own_file_handle is True, also closes the file handle. + If also_close_file_handle is True, also closes the file handle. """ self.file_handle.write(b'') - Writer.close(self) + Writer.close(self, also_close_file_handle) diff --git a/test/writer.py b/test/writer.py index 576196c..83cca1d 100644 --- a/test/writer.py +++ b/test/writer.py @@ -18,16 +18,15 @@ class JSONWriterTest(unittest.TestCase): - def test_own_file_handle_true(self): + def test_close_true(self): """ - If a JSONWriter is created with own_file_handle = True, then when the - JSONWriter is closed the file handle is also closed. + If also_close_file_handle is true, then the file handle is also closed. """ file_handle = StringIO() self.assertFalse( file_handle.closed, 'The file handle should be open') - writer = pymarc.JSONWriter(file_handle, own_file_handle = True) + writer = pymarc.JSONWriter(file_handle) self.assertFalse( file_handle.closed, 'The file handle should still be open') @@ -36,20 +35,19 @@ def test_own_file_handle_true(self): file_handle.closed, 'The file handle should close when the writer closes') - def test_own_file_handle_false(self): + def test_close_false(self): """ - If a JSONWriter is created with own_file_handle = False, then when the - JSONWriter is closed the file handle is NOT also closed. + If also_close_file_handle is false, then the file handle is NOT closed. """ file_handle = StringIO() self.assertFalse( file_handle.closed, 'The file handle should be open') - writer = pymarc.JSONWriter(file_handle, own_file_handle = False) + writer = pymarc.JSONWriter(file_handle) self.assertFalse( file_handle.closed, 'The file handle should still be open') - writer.close() + writer.close(also_close_file_handle = False) self.assertFalse( file_handle.closed, 'The file handle should NOT close when the writer closes') @@ -60,8 +58,8 @@ def test_writing_0_records(self): """) file_handle = StringIO() try: - writer = pymarc.JSONWriter(file_handle, own_file_handle = False) - writer.close() + writer = pymarc.JSONWriter(file_handle) + writer.close(also_close_file_handle = False) actual = json.loads(file_handle.getvalue()) self.assertEquals(actual, expected) finally: @@ -78,10 +76,10 @@ def test_writing_empty_record(self): """) file_handle = StringIO() try: - writer = pymarc.JSONWriter(file_handle, own_file_handle = False) + writer = pymarc.JSONWriter(file_handle) record = pymarc.Record() writer.write(record) - writer.close() + writer.close(also_close_file_handle = False) actual = json.loads(file_handle.getvalue()) self.assertEquals(actual, expected) finally: @@ -118,7 +116,7 @@ def test_writing_1_record(self): """) file_handle = StringIO() try: - writer = pymarc.JSONWriter(file_handle, own_file_handle = False) + writer = pymarc.JSONWriter(file_handle) record = pymarc.Record() record.add_field( pymarc.Field('100', ['0', '0'], ['a', u('me')])) @@ -128,7 +126,7 @@ def test_writing_1_record(self): ['0', '0'], ['a', u('Foo /'), 'c', u('by me.')])) writer.write(record) - writer.close() + writer.close(also_close_file_handle = False) actual = json.loads(file_handle.getvalue()) self.assertEquals(actual, expected) finally: @@ -207,7 +205,7 @@ def test_writing_3_records(self): """) file_handle = StringIO() try: - writer = pymarc.JSONWriter(file_handle, own_file_handle = False) + writer = pymarc.JSONWriter(file_handle) record = pymarc.Record() record.add_field( pymarc.Field( @@ -237,7 +235,7 @@ def test_writing_3_records(self): ['0', '0'], ['a', u('Foo /'), 'c', u('by me.')])) writer.write(record) - writer.close() + writer.close(also_close_file_handle = False) actual = json.loads(file_handle.getvalue()) self.assertEquals(actual, expected) finally: @@ -268,16 +266,15 @@ def test_write(self): # remove it os.remove('test/writer-test.dat') - def test_own_file_handle_true(self): + def test_close_true(self): """ - If a MARCWriter is created with own_file_handle = True, then when the - MARCWriter is closed the file handle is also closed. + If also_close_file_handle is true, then the file handle is also closed. """ file_handle = BytesIO() self.assertFalse( file_handle.closed, 'The file handle should be open') - writer = pymarc.MARCWriter(file_handle, own_file_handle = True) + writer = pymarc.MARCWriter(file_handle) self.assertFalse( file_handle.closed, 'The file handle should still be open') @@ -286,20 +283,19 @@ def test_own_file_handle_true(self): file_handle.closed, 'The file handle should close when the writer closes') - def test_own_file_handle_false(self): + def test_close_false(self): """ - If a MARCWriter is created with own_file_handle = False, then when the - MARCWriter is closed the file handle is NOT also closed. + If also_close_file_handle is false, then the file handle is NOT closed. """ file_handle = BytesIO() self.assertFalse( file_handle.closed, 'The file handle should be open') - writer = pymarc.MARCWriter(file_handle, own_file_handle = False) + writer = pymarc.MARCWriter(file_handle) self.assertFalse( file_handle.closed, 'The file handle should still be open') - writer.close() + writer.close(also_close_file_handle = False) self.assertFalse( file_handle.closed, 'The file handle should NOT close when the writer closes') @@ -310,8 +306,8 @@ class TextWriterTest(unittest.TestCase): def test_writing_0_records(self): file_handle = StringIO() try: - writer = pymarc.TextWriter(file_handle, own_file_handle = False) - writer.close() + writer = pymarc.TextWriter(file_handle) + writer.close(also_close_file_handle = False) self.assertEqual( file_handle.getvalue(), '', @@ -328,7 +324,7 @@ def test_writing_1_record(self): expected = textwrap.dedent(expected[1:]) file_handle = StringIO() try: - writer = pymarc.TextWriter(file_handle, own_file_handle = False) + writer = pymarc.TextWriter(file_handle) record = pymarc.Record() record.add_field( pymarc.Field('100', ['0', '0'], ['a', u('me')])) @@ -338,7 +334,7 @@ def test_writing_1_record(self): ['0', '0'], ['a', u('Foo /'), 'c', u('by me.')])) writer.write(record) - writer.close() + writer.close(also_close_file_handle = False) self.assertEquals(file_handle.getvalue(), expected) finally: file_handle.close() @@ -360,7 +356,7 @@ def test_writing_3_records(self): expected = textwrap.dedent(expected[1:]) file_handle = StringIO() try: - writer = pymarc.TextWriter(file_handle, own_file_handle = False) + writer = pymarc.TextWriter(file_handle) record = pymarc.Record() record.add_field( pymarc.Field( @@ -390,7 +386,7 @@ def test_writing_3_records(self): ['0', '0'], ['a', u('Foo /'), 'c', u('by me.')])) writer.write(record) - writer.close() + writer.close(also_close_file_handle = False) self.assertEquals(file_handle.getvalue(), expected) finally: file_handle.close() @@ -402,23 +398,23 @@ def test_writing_empty_record(self): expected = textwrap.dedent(expected[1:]) file_handle = StringIO() try: - writer = pymarc.TextWriter(file_handle, own_file_handle = False) + writer = pymarc.TextWriter(file_handle) record = pymarc.Record() writer.write(record) + writer.close(also_close_file_handle = False) self.assertEquals(file_handle.getvalue(), expected) finally: file_handle.close() - def test_own_file_handle_true(self): + def test_close_true(self): """ - If a TextWriter is created with own_file_handle = True, then when the - TextWriter is closed the file handle is also closed. + If also_close_file_handle is true, then the file handle is also closed. """ file_handle = StringIO() self.assertFalse( file_handle.closed, 'The file handle should be open') - writer = pymarc.TextWriter(file_handle, own_file_handle = True) + writer = pymarc.TextWriter(file_handle) self.assertFalse( file_handle.closed, 'The file handle should still be open') @@ -427,20 +423,19 @@ def test_own_file_handle_true(self): file_handle.closed, 'The file handle should close when the writer closes') - def test_own_file_handle_false(self): + def test_close_false(self): """ - If a TextWriter is created with own_file_handle = False, then when the - TextWriter is closed the file handle is NOT also closed. + If also_close_file_handle is false, then the file handle is NOT closed. """ file_handle = StringIO() self.assertFalse( file_handle.closed, 'The file handle should be open') - writer = pymarc.TextWriter(file_handle, own_file_handle = False) + writer = pymarc.TextWriter(file_handle) self.assertFalse( file_handle.closed, 'The file handle should still be open') - writer.close() + writer.close(also_close_file_handle = False) self.assertFalse( file_handle.closed, 'The file handle should NOT close when the writer closes') @@ -459,8 +454,8 @@ def test_writing_0_records(self): expected = expected.encode() file_handle = BytesIO() try: - writer = pymarc.XMLWriter(file_handle, own_file_handle = False) - writer.close() + writer = pymarc.XMLWriter(file_handle) + writer.close(also_close_file_handle = False) self.assertEquals(file_handle.getvalue(), expected) finally: file_handle.close() @@ -479,10 +474,10 @@ def test_writing_empty_record(self): expected = expected.encode() file_handle = BytesIO() try: - writer = pymarc.XMLWriter(file_handle, own_file_handle = False) + writer = pymarc.XMLWriter(file_handle) record = pymarc.Record() writer.write(record) - writer.close() + writer.close(also_close_file_handle = False) self.assertEquals(file_handle.getvalue(), expected) finally: file_handle.close() @@ -508,7 +503,7 @@ def test_writing_1_record(self): expected = expected.encode() file_handle = BytesIO() try: - writer = pymarc.XMLWriter(file_handle, own_file_handle = False) + writer = pymarc.XMLWriter(file_handle) record = pymarc.Record() record.add_field( pymarc.Field('100', ['0', '0'], ['a', u('me')])) @@ -518,7 +513,7 @@ def test_writing_1_record(self): ['0', '0'], ['a', u('Foo /'), 'c', u('by me.')])) writer.write(record) - writer.close() + writer.close(also_close_file_handle = False) self.assertEquals(file_handle.getvalue(), expected) finally: file_handle.close() @@ -562,7 +557,7 @@ def test_writing_3_records(self): expected = expected.encode() file_handle = BytesIO() try: - writer = pymarc.XMLWriter(file_handle, own_file_handle = False) + writer = pymarc.XMLWriter(file_handle) record = pymarc.Record() record.add_field( pymarc.Field( @@ -592,21 +587,20 @@ def test_writing_3_records(self): ['0', '0'], ['a', u('Foo /'), 'c', u('by me.')])) writer.write(record) - writer.close() + writer.close(also_close_file_handle = False) self.assertEquals(file_handle.getvalue(), expected) finally: file_handle.close() - def test_own_file_handle_true(self): + def test_close_true(self): """ - If a XMLWriter is created with own_file_handle = True, then when the - XMLWriter is closed the file handle is also closed. + If also_close_file_handle is true, then the file handle is also closed. """ file_handle = BytesIO() self.assertFalse( file_handle.closed, 'The file handle should be open') - writer = pymarc.XMLWriter(file_handle, own_file_handle = True) + writer = pymarc.XMLWriter(file_handle) self.assertFalse( file_handle.closed, 'The file handle should still be open') @@ -615,20 +609,19 @@ def test_own_file_handle_true(self): file_handle.closed, 'The file handle should close when the writer closes') - def test_own_file_handle_false(self): + def test_close_false(self): """ - If a XMLWriter is created with own_file_handle = False, then when the - XMLWriter is closed the file handle is NOT also closed. + If also_close_file_handle is false, then the file handle is NOT closed. """ file_handle = BytesIO() self.assertFalse( file_handle.closed, 'The file handle should be open') - writer = pymarc.XMLWriter(file_handle, own_file_handle = False) + writer = pymarc.XMLWriter(file_handle) self.assertFalse( file_handle.closed, 'The file handle should still be open') - writer.close() + writer.close(also_close_file_handle = False) self.assertFalse( file_handle.closed, 'The file handle should NOT close when the writer closes')