Skip to content

Instantly share code, notes, and snippets.

@wvengen
Created February 26, 2024 11:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wvengen/56fb225b14dc2a97e8134b6e7184a308 to your computer and use it in GitHub Desktop.
Save wvengen/56fb225b14dc2a97e8134b6e7184a308 to your computer and use it in GitHub Desktop.
Adding a custom export command to python-ly's ly.server
#!/usr/bin/env python3
#
# python-ly server that adds an a custom export command
#
# tested with python-ly version 0.9.8
# https://github.com/frescobaldi/python-ly
#
import copy
from http.server import HTTPServer
from ly.server.main import parse_command_line
from ly.server.command import _export_command
import ly.server.handler as handler
class export_custom(_export_command):
def export(self, opts, cursor, exports):
# Yse cursor.document as Lilypond document to work with
# here we return a dummy value, later serialized as JSON.
return "Hi there"
class CustomRequestHandler(handler.RequestHandler):
def create_command(self, cmd):
if cmd.get('command') == 'customexport':
# note that this doesn't handle variables or args
return [export_custom()]
else:
return super().create_command(cmd)
def main():
server_opts, cmd_opts = parse_command_line()
handler.default_opts = copy.deepcopy(cmd_opts)
exit_code = 0
try:
server = HTTPServer(('', server_opts.port), CustomRequestHandler)
print("Welcome to the python-ly HTTP server, with custom export")
print("Listening on port {port}".format(port=server_opts.port))
server.serve_forever()
except KeyboardInterrupt:
print("\nKeyboard interrupt received. Shutting down server")
server.socket.close()
print("Successfully closed. Bye...")
return exit_code
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment