This is the main version of the one-file python New Session Manager client library. There exists a github version, but that is just an archive.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
4.1KB

  1. #! /usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. PyNSMClient - A New Session Manager Client-Library in one file.
  5. The Non-Session-Manager by Jonathan Moore Liles <male@tuxfamily.org>: http://non.tuxfamily.org/nsm/
  6. New Session Manager, by LinuxAudio.org: https://github.com/linuxaudio/new-session-manager
  7. With help from code fragments from https://github.com/attwad/python-osc ( DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE v2 )
  8. MIT License
  9. Copyright 2014-2020 Nils Hilbricht https://www.laborejo.org
  10. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  11. associated documentation files (the "Software"), to deal in the Software without restriction,
  12. including without limitation the rights to use, copy, modify, merge, publish, distribute,
  13. sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
  14. furnished to do so, subject to the following conditions:
  15. The above copyright notice and this permission notice shall be included in all copies or
  16. substantial portions of the Software.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
  18. NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  20. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
  21. OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. """
  23. #/nsm/server/announce #A hack to get this into Agordejo launcher discovery
  24. from nsmclient import NSMClient
  25. import sys
  26. from time import sleep
  27. import os
  28. from threading import Timer
  29. sys.path.append(os.getcwd())
  30. class BaseClient(object):
  31. def saveCallbackFunction(self, ourPath, sessionName, ourClientNameUnderNSM):
  32. print (__file__, "save")
  33. def openOrNewCallbackFunction(self, ourPath, sessionName, ourClientNameUnderNSM):
  34. print (__file__,"open/new")
  35. def exitCallbackFunction(self, ourPath, sessionName, ourClientNameUnderNSM):
  36. print (__file__, "quit")
  37. sys.exit()
  38. def broadcastCallbackFunction(self, ourPath, sessionName, ourClientNameUnderNSM, messagePath, listOfArguments):
  39. print (__file__, "broadcast")
  40. def event(self, nsmClient):
  41. pass
  42. def __init__(self, name, delayedFunctions=[], eventFunction=None):
  43. """delayedFunctions are a (timer delay in seconds, function call) list of tuples. They will
  44. be executed once.
  45. If the function is a string instead it will be evaluated in the BaseClient context,
  46. providing self. Do not give a lambda!
  47. Give eventFunction for repeated execution."""
  48. self.nsmClient = NSMClient(prettyName = name, #will raise an error and exit if this example is not run from NSM.
  49. saveCallback = self.saveCallbackFunction,
  50. openOrNewCallback = self.openOrNewCallbackFunction,
  51. supportsSaveStatus = False, # Change this to True if your program announces it's save status to NSM
  52. exitProgramCallback = self.exitCallbackFunction,
  53. broadcastCallback = self.broadcastCallbackFunction,
  54. hideGUICallback = None, #replace with your hiding function. You need to answer in your function with nsmClient.announceGuiVisibility(False)
  55. showGUICallback = None, #replace with your showing function. You need to answer in your function with nsmClient.announceGuiVisibility(True)
  56. loggingLevel = "info", #"info" for development or debugging, "error" for production. default is error.
  57. )
  58. if eventFunction:
  59. self.event = eventFunction
  60. for delay, func in delayedFunctions:
  61. if type(func) is str:
  62. func = eval('lambda self=self: ' + func )
  63. t = Timer(interval=delay, function=func, args=())
  64. t.start()
  65. while True:
  66. self.nsmClient.reactToMessage()
  67. self.event(self.nsmClient)
  68. sleep(0.05)
  69. if __name__ == '__main__':
  70. """This is the most minimal nsm client in existence"""
  71. BaseClient(name="testclient_base") #this never returns an object.