Created
February 26, 2024 11:06
-
-
Save wvengen/56fb225b14dc2a97e8134b6e7184a308 to your computer and use it in GitHub Desktop.
Adding a custom export command to python-ly's ly.server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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