Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

romanText write to file-like objects #1416

Merged
merged 1 commit into from
Sep 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions music21/converter/subConverters.py
Original file line number Diff line number Diff line change
Expand Up @@ -1323,9 +1323,14 @@ def write(self, obj, fmt, fp=None, subformats=None, **keywords): # pragma: no c
if fp is None:
fp = self.getTemporaryFile()

with open(fp, 'w', encoding='utf-8') as text_file:
if not hasattr(fp, 'write'):
with open(fp, 'w', encoding='utf-8') as text_file:
for entry in writeRoman.RnWriter(obj).combinedList:
text_file.write(entry + '\n')
else:
# file-like object
for entry in writeRoman.RnWriter(obj).combinedList:
text_file.write(entry + '\n')
fp.write(entry + '\n')

return fp

Expand Down Expand Up @@ -1687,6 +1692,19 @@ def testBrailleKeywords(self):
self.assertIn('<music21.braille.segment BrailleSegment>', f.read())
os.remove(out)

def testWriteRomanText(self):
import textwrap
from io import StringIO
from music21 import converter

rntxt = textwrap.dedent('''
Time Signature: 3/4
m1 C: I
''')
s = converter.parse(rntxt, format='romanText')
text_stream = StringIO()
s.write('romanText', text_stream)
self.assertTrue(text_stream.getvalue().endswith(rntxt))

class TestExternal(unittest.TestCase):
show = True
Expand Down