syncthing.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Permission is hereby granted, free of charge, to any person obtaining a copy
  4. # of this software and associated documentation files (the "Software"), to deal
  5. # in the Software without restriction, including without limitation the rights
  6. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. # copies of the Software, and to permit persons to whom the Software is
  8. # furnished to do so, subject to the following conditions:
  9. #
  10. # The above copyright notice and this permission notice shall be included in
  11. # all copies or substantial portions of the Software.
  12. #
  13. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. # SOFTWARE.
  20. import os
  21. import subprocess
  22. from libqtile.widget import base
  23. __all__ = ["Syncthing"]
  24. class Syncthing(base.ThreadPoolText):
  25. """Displays and change Syncthing status."""
  26. orientations = base.ORIENTATION_HORIZONTAL
  27. defaults = [
  28. ("format", "{label}{error}", "Formatting for field names."),
  29. ("error", "", "Label for Errors."),
  30. ("update_interval", 30.0, "Update interval for the Syncthing."),
  31. ("label", "Syncthing:  ", "Label for the output."),
  32. ("path_to_script", None, "Path to a script to get status."),
  33. ("active_color", "#00ff00", "Color for active status."),
  34. ("inactive_color", "#ff0000", "Color for inactive status."),
  35. ]
  36. def __init__(self, **config):
  37. super().__init__("", **config)
  38. self.add_defaults(Syncthing.defaults)
  39. self.add_callbacks({"Button1": self._change_status, "Button3": self._run_webUI})
  40. self._get_status()
  41. def _get_status(self):
  42. try:
  43. self.syncthing_status = subprocess.check_output([self.path_to_script]).decode("utf-8").rstrip()
  44. except Exception:
  45. self.error = "Error"
  46. self.label = "Unkonown path_to_script"
  47. self.foreground = "#ff0000"
  48. # self.syncthing_status = None
  49. else:
  50. self.error = ""
  51. self.foreground = self.active_color if self.syncthing_status == "active" else self.inactive_color
  52. def _run_webUI(self):
  53. """Starts Syncthing Web UI."""
  54. os.system("xdg-open 'http://127.0.0.1:8384/'")
  55. def _change_status(self):
  56. """Turns on/off Syncthing."""
  57. if self.syncthing_status == "active":
  58. os.system("systemctl --user stop syncthing.service")
  59. self.syncthing_status = "inactive"
  60. # os.system("notify-send -i dialog-information 'Syncthing service stopped'")
  61. else:
  62. os.system("systemctl --user start syncthing.service")
  63. self.syncthing_status = "active"
  64. # os.system("notify-send -i dialog-information 'Syncthing service started'")
  65. self._get_status()
  66. def poll(self):
  67. self._get_status()
  68. val = {}
  69. val["label"] = self.label
  70. val["error"] = self.error
  71. return self.format.format(**val)