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

.show('flat'), support for http://flat.io #846

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
59 changes: 59 additions & 0 deletions music21/converter/subConverters.py
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,65 @@ def show(self, obj, fmt, app=None, subformats=None, **keywords): # pragma: no c
self.launch(returnedFilePath, fmt=fmt, app=app)


# ------------------------------------------------------------------------------
class ConverterFlatio(SubConverter):
'''
Converter for Flatio. Sends XML to http://flat.io and then opens it in a browser tab.

You must have a flat.io authentication token, and set it like
>>> us = environment.UserSettings()
>>> us['flatioAuthToken'] = '[MY FLAT.IO AUTH TOKEN]'

See https://flat.io/developers/docs/api/authentication.html#personal-access-tokens
'''
registerFormats = ('flat',)
registerOutputExtensions = ('flat',)

def parseData(self, strData, number=None):
'''
Not implemented yet.
'''
# Pull MusicXML from a flat.io url?

def write(self, obj, fmt, fp=None, subformats=None,
compress=False, **keywords): # pragma: no cover
'''
Uploads MusicXML to flat.io via their API.
'''
import requests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

requests isn't in the requirements.txt. Is it possible to do this with the python standard library? If not, it needs to be added to requirements.txt

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, was already mentioned above. I think that given that the Python std library docs already suggest using requests instead, and it's only not in the std library because it gets security fixes faster than other libraries, it's probably okay. So we just need the format name changed and a mechanism for helping users get their API codes, etc.

from music21.musicxml import m21ToXml

if not environLocal['flatioAuthToken']:
raise SubConverterException("Trying to use flat.io subconverter, but no flatioAuthToken is set. See https://flat.io/developers/docs/api/authentication.html#personal-access-tokens to get a token.")

generalExporter = m21ToXml.GeneralObjectExporter(obj)
dataBytes: bytes = generalExporter.parse()

body = {
'title': defaults.title,
'privacy': 'private',
'data': dataBytes.decode("utf-8")
}

headers = {
'Authorization': 'Bearer ' + environLocal['flatioAuthToken'],
'Content-Type': 'application/json'
}
r = requests.post("https://api.flat.io/v2/scores", json=body, headers=headers)
if r.status_code != 200:
print("Could not upload XML to flat.io")
print(r.status_code, r.reason)

return r.json()['htmlUrl']

def show(self, obj, fmt, app=None, subformats=None, **keywords): # pragma: no cover
'''
Uploads MusixXML to flat.io and opens the result in a browser tab.
'''
import webbrowser
url = self.write(obj, fmt, subformats=subformats, **keywords)
webbrowser.open(url, new=2)

# ------------------------------------------------------------------------------
class ConverterMidi(SubConverter):
'''
Expand Down
3 changes: 3 additions & 0 deletions music21/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,9 @@ def _loadDefaults(self, forcePlatform=None):
self._ref['autoDownload'] = 'ask'
self._ref['debug'] = 0

# authentication token for http://flat.io
self._ref['flatioAuthToken'] = None

# printing of missing import warnings
# default/non-zero is on
self._ref['warnings'] = 1
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ jsonpickle
matplotlib
more_itertools
numpy
requests
webcolors>=1.5