customAnchorCmd.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import subprocess
  2. class _CustomAnchorCmdItem:
  3. def __init__(self):
  4. self.__enabled = False
  5. self.__cmd = ""
  6. def serialize(self):
  7. return (self.enabled, self.cmd)
  8. def deserialize(self, data):
  9. if data:
  10. self.enabled = data[0]
  11. self.cmd = data[1]
  12. @property
  13. def enabled(self):
  14. return self.__enabled
  15. @enabled.setter
  16. def enabled(self, state):
  17. self.__enabled = state
  18. @property
  19. def cmd(self):
  20. return self.__cmd
  21. @cmd.setter
  22. def cmd(self, cmd):
  23. self.__cmd = cmd
  24. class AnchorCMD:
  25. http = _CustomAnchorCmdItem()
  26. ftp = _CustomAnchorCmdItem()
  27. magnet = _CustomAnchorCmdItem()
  28. @staticmethod
  29. def dict():
  30. return {
  31. 'http': AnchorCMD.http,
  32. 'https': AnchorCMD.http,
  33. 'ftp': AnchorCMD.ftp,
  34. 'magnet': AnchorCMD.magnet
  35. }
  36. @staticmethod
  37. def serialize():
  38. return {
  39. 'http': AnchorCMD.http.serialize(),
  40. 'ftp': AnchorCMD.ftp.serialize(),
  41. 'magnet': AnchorCMD.magnet.serialize()
  42. }
  43. @staticmethod
  44. def deserialize(data):
  45. if not data:
  46. return
  47. AnchorCMD.http.deserialize(data['http'])
  48. AnchorCMD.ftp.deserialize(data['ftp'])
  49. AnchorCMD.magnet.deserialize(data['magnet'])
  50. #@staticmethod
  51. #def serializeDefault():
  52. # return {}
  53. @staticmethod
  54. def handle(scheme, url):
  55. cmds = AnchorCMD.dict()
  56. if scheme in cmds and cmds[scheme].enabled:
  57. cmdSplit = cmds[scheme].cmd.split(" ")
  58. cmdSplit = [c.replace("%url", url) for c in cmdSplit]
  59. subprocess.Popen(cmdSplit)
  60. return True
  61. return False