/src/config.py

https://github.com/daehruoydeef/Yin-Yang · Python · 273 lines · 168 code · 83 blank · 22 comment · 8 complexity · 8f3b9b1ee1e272efb1d681354fc11cf1 MD5 · raw file

  1. import json
  2. import pwd
  3. import os
  4. import pathlib
  5. import re
  6. from suntime import Sun, SunTimeException
  7. # aliases for path to use later on
  8. home = os.getenv("HOME")
  9. path = home + "/.config"
  10. def exists():
  11. """returns True or False wether Config exists or note"""
  12. return os.path.isfile(path+"/yin_yang/yin_yang.json")
  13. def get_desktop():
  14. """Return the current desktop's name or 'unkown' if can't determine it"""
  15. # just to get all possible implementations of dekstop variables
  16. env = str(os.getenv("GDMSESSION")).lower()
  17. second_env = str(os.getenv("XDG_CURRENT_DESKTOP")).lower()
  18. third_env = str(os.getenv("XDG_CURRENT_DESKTOP")).lower()
  19. # these are the envs I will look for
  20. # feel free to add your Desktop and see if it works
  21. gnome_re = re.compile(r'gnome')
  22. budgie_re = re.compile(r'budgie')
  23. kde_re = re.compile(r'kde')
  24. plasma_re = re.compile(r'plasma')
  25. plasma5_re = re.compile(r'plasma5')
  26. if gnome_re.search(env) or gnome_re.search(second_env) or gnome_re.search(third_env):
  27. return "gtk"
  28. if budgie_re.search(env) or budgie_re.search(second_env) or budgie_re.search(third_env):
  29. return "gtk"
  30. if kde_re.search(env) or kde_re.search(second_env) or kde_re.search(third_env):
  31. return "kde"
  32. if plasma_re.search(env) or plasma_re.search(second_env) or plasma_re.search(third_env):
  33. return "kde"
  34. if plasma5_re.search(env) or plasma5_re.search(second_env) or plasma5_re.search(third_env):
  35. return "kde"
  36. return "unknown"
  37. def set_sun_time():
  38. latitude: float = float(get("latitude"))
  39. longitude: float = float(get("latitude"))
  40. sun = Sun(latitude, longitude)
  41. try:
  42. today_sr = sun.get_local_sunrise_time()
  43. today_ss = sun.get_local_sunset_time()
  44. print('Today the sun raised at {} and get down at {}'.
  45. format(today_sr.strftime('%H:%M'), today_ss.strftime('%H:%M')))
  46. # Get today's sunrise and sunset in UTC
  47. update("switchToLight", today_sr.strftime('%H:%M'))
  48. update("switchToDark", today_ss.strftime('%H:%M'))
  49. except SunTimeException as e:
  50. print("Error: {0}.".format(e))
  51. # generate path for yin-yang if there is none this will be skipped
  52. pathlib.Path(path+"/yin_yang").mkdir(parents=True, exist_ok=True)
  53. # if there is no config generate a generic one
  54. config = {}
  55. config["version"] = "2.0"
  56. config["desktop"] = get_desktop()
  57. config["followSun"] = False
  58. config["latitude"] = ""
  59. config["longitude"] = ""
  60. config["schedule"] = False
  61. config["switchToDark"] = "20:00"
  62. config["switchToLight"] = "07:00"
  63. config["running"] = False
  64. config["theme"] = ""
  65. config["codeLightTheme"] = "Default Light+"
  66. config["codeDarkTheme"] = "Default Dark+"
  67. config["codeEnabled"] = False
  68. config["kdeLightTheme"] = "org.kde.breeze.desktop"
  69. config["kdeDarkTheme"] = "org.kde.breezedark.desktop"
  70. config["kdeEnabled"] = False
  71. config["gtkLightTheme"] = ""
  72. config["gtkDarkTheme"] = ""
  73. config["atomLightTheme"] = ""
  74. config["atomDarkTheme"] = ""
  75. config["atomEnabled"] = False
  76. config["gtkEnabled"] = False
  77. config["wallpaperLightTheme"] = ""
  78. config["wallpaperDarkTheme"] = ""
  79. config["wallpaperEnabled"] = False
  80. config["firefoxEnabled"] = False
  81. config["firefoxDarkTheme"] = "firefox-compact-dark@mozilla.org"
  82. config["firefoxLightTheme"] = "firefox-compact-light@mozilla.org"
  83. config["firefoxActiveTheme"] = "firefox-compact-light@mozilla.org"
  84. config["gnomeEnabled"] = False
  85. config["gnomeLightTheme"] = ""
  86. config["gnomeDarkTheme"] = ""
  87. config["kvantumEnabled"] = False
  88. config["kvantumLightTheme"] = ""
  89. config["kvantumDarkTheme"] = ""
  90. if exists():
  91. # making config global for this module
  92. with open(path+"/yin_yang/yin_yang.json", "r") as conf:
  93. config = json.load(conf)
  94. config["desktop"] = get_desktop()
  95. def get_config():
  96. """returns the config"""
  97. return config
  98. def update(key, value):
  99. """Update the value of a key in configuration"""
  100. config[key] = value
  101. write_config()
  102. def write_config(config=config):
  103. """Write configuration"""
  104. with open(path+"/yin_yang/yin_yang.json", 'w') as conf:
  105. json.dump(config, conf, indent=4)
  106. def gtk_exists():
  107. return os.path.isfile(path+"/gtk-3.0/settings.ini")
  108. def get_enabled_plugins():
  109. """returns a list of plugins which are activated"""
  110. pass
  111. def get_light_time():
  112. """returns the time which should toggle the lightMode"""
  113. pass
  114. def get_dark_time():
  115. """returns the time which should toggle the lightMode"""
  116. pass
  117. def get_theme():
  118. return config["theme"]
  119. def get_kde_light_theme():
  120. return config["kdeLightTheme"]
  121. def get_kde_dark_theme():
  122. return config["kdeDarkTheme"]
  123. def get_kde_enabled():
  124. return config["kdeEnabled"]
  125. def get_code_light_theme():
  126. return config["codeLightTheme"]
  127. def get_code_dark_theme():
  128. return config["codeDarkTheme"]
  129. def get_code_enabled():
  130. return config["codeEnabled"]
  131. def get_gtk_light_theme():
  132. return config["gtkLightTheme"]
  133. def get_gtk_dark_theme():
  134. return config["gtkDarkTheme"]
  135. def get_gtk_enabled():
  136. return config["gtkEnabled"]
  137. def get(key):
  138. """Return the given key from the config"""
  139. return config[key]
  140. def is_scheduled():
  141. return config["schedule"]
  142. def get_version():
  143. return config["version"]
  144. def kde_get_light_theme():
  145. """Return the KDE light theme specified in the yin-yang config"""
  146. return config["kdeLightTheme"]
  147. def kde_get_dark_theme():
  148. """Return the KDE dark theme specified in the yin-yang config"""
  149. return config["kdeDarkTheme"]
  150. def kde_get_checkbox():
  151. return config["kdeEnabled"]
  152. def gtk_get_light_theme():
  153. """Return the GTK Light theme specified in the yin-yang config"""
  154. return config["gtkLightTheme"]
  155. def gtk_get_dark_theme():
  156. """Return the GTK dark theme specified in the yin-yang config"""
  157. return config["gtkDarkTheme"]
  158. def gtk_get_checkbox():
  159. return config["gtkEnabled"]
  160. def code_get_light_theme():
  161. """Return the code light theme specified in the yin-yang config"""
  162. return config["codeLightTheme"]
  163. def code_get_dark_theme():
  164. """Return the code dark theme specified in the yin-yang config"""
  165. return config["codeDarkTheme"]
  166. def code_get_checkbox():
  167. return config["codeEnabled"]
  168. def gnome_get_light_theme():
  169. """Return the Gnome Shell Light theme specified in the yin-yang config"""
  170. return config["gnomeLightTheme"]
  171. def gnome_get_dark_theme():
  172. """Return the Gnome Shell dark theme specified in the yin-yang config"""
  173. return config["gnomeDarkTheme"]
  174. def gnome_get_checkbox():
  175. return config["gnomeEnabled"]
  176. def kvantum_get_light_theme():
  177. """Return the Kvantum Light theme specified in the yin-yang config"""
  178. return config["kvantumLightTheme"]
  179. def kvantum_get_dark_theme():
  180. """Return the Kvantum dark theme specified in the yin-yang config"""
  181. return config["kvantumDarkTheme"]
  182. def kvantum_get_checkbox():
  183. return config["kvantumEnabled"]