settings.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. #####################################################################
  2. # #
  3. # THIS IS A SOURCE CODE FILE FROM A PROGRAM TO INTERACT WITH THE #
  4. # LBRY PROTOCOL ( lbry.com ). IT WILL USE THE LBRY SDK ( lbrynet ) #
  5. # FROM THEIR REPOSITORY ( https://github.com/lbryio/lbry-sdk ) #
  6. # WHICH I GONNA PRESENT TO YOU AS A BINARY. SINCE I DID NOT DEVELOP #
  7. # IT AND I'M LAZY TO INTEGRATE IN A MORE SMART WAY. THE SOURCE CODE #
  8. # OF THE SDK IS AVAILABLE IN THE REPOSITORY MENTIONED ABOVE. #
  9. # #
  10. # ALL THE CODE IN THIS REPOSITORY INCLUDING THIS FILE IS #
  11. # (C) J.Y.Amihud and Other Contributors 2021. EXCEPT THE LBRY SDK. #
  12. # YOU CAN USE THIS FILE AND ANY OTHER FILE IN THIS REPOSITORY UNDER #
  13. # THE TERMS OF GNU GENERAL PUBLIC LICENSE VERSION 3 OR ANY LATER #
  14. # VERSION. TO FIND THE FULL TEXT OF THE LICENSE GO TO THE GNU.ORG #
  15. # WEBSITE AT ( https://www.gnu.org/licenses/gpl-3.0.html ). #
  16. # #
  17. # THE LBRY SDK IS UNFORTUNATELY UNDER THE MIT LICENSE. IF YOU ARE #
  18. # NOT INTENDING TO USE MY CODE AND JUST THE SDK. YOU CAN FIND IT ON #
  19. # THEIR OFFICIAL REPOSITORY ABOVE. THEIR LICENSE CHOICE DOES NOT #
  20. # SPREAD ONTO THIS PROJECT. DON'T GET A FALSE ASSUMPTION THAT SINCE #
  21. # THEY USE A PUSH-OVER LICENSE, I GONNA DO THE SAME. I'M NOT. #
  22. # #
  23. # THE LICENSE CHOSEN FOR THIS PROJECT WILL PROTECT THE 4 ESSENTIAL #
  24. # FREEDOMS OF THE USER FURTHER, BY NOT ALLOWING ANY WHO TO CHANGE #
  25. # THE LICENSE AT WILL. SO NO PROPRIETARY SOFTWARE DEVELOPER COULD #
  26. # TAKE THIS CODE AND MAKE THEIR USER-SUBJUGATING SOFTWARE FROM IT. #
  27. # #
  28. #####################################################################
  29. import os
  30. import json
  31. from flbry.variables import *
  32. import ast
  33. from flbry import markdown
  34. from flbry import connect
  35. from flbry import donate
  36. import readline
  37. # This file will manage settings / installation and stuff like this.
  38. default_key_value_pairs = {
  39. "theme":"default",
  40. "markdown_reader":None,
  41. "save_history":False,
  42. "default_opener":"xdg-open",
  43. "autoconnect":False,
  44. "player":"xdg-open",
  45. "music_player":"xdg-open",
  46. "default_tip":0.01,
  47. "dev_mode":False,
  48. "graph_force_ASCII":False,
  49. "ignore_width_forcing":False,
  50. "lbrynet_binary":"flbry/lbrynet",
  51. "default_editor":None,
  52. "librarian_instance": "https://librarian.bcow.xyz/"
  53. }
  54. def get_settings_folder(flbry="flbry/"):
  55. try:
  56. data_dir = os.environ["XDG_DATA_HOME"] + "/" + flbry
  57. except:
  58. data_dir = os.path.expanduser("~/.local/share/"+flbry)
  59. try:
  60. os.makedirs(data_dir)
  61. except:
  62. pass
  63. return data_dir
  64. def get_all_settings():
  65. with open(get_settings_folder()+"config.json") as f:
  66. settings_cache = json.load(f)
  67. return settings_cache
  68. def check_config():
  69. # This function checks whether config exists. If not makes a
  70. # default setting.
  71. default = default_key_value_pairs
  72. if not os.path.exists(get_settings_folder()+"config.json"):
  73. with open(get_settings_folder()+"config.json", 'w') as f:
  74. json.dump(default, f, indent=4, sort_keys=True)
  75. def get(key):
  76. # This function gets a setting from settings.
  77. data = get_all_settings()
  78. try:
  79. return data[key]
  80. except:
  81. return None
  82. def save(key, value):
  83. # This function will save a value into the settings file.
  84. data = get_all_settings()
  85. data[key] = value
  86. with open(get_settings_folder()+"config.json", 'w') as f:
  87. json.dump(data, f, indent=4, sort_keys=True)
  88. def set_theme(theme):
  89. # This will set a global theme
  90. user_themes = get_settings_folder()+"themes/"
  91. default_themes = "themes/"
  92. # First let's see if user has a theme folder in settings.
  93. try:
  94. os.makedirs(user_themes)
  95. except:
  96. pass
  97. # Trying to load the theme from user themes first
  98. try:
  99. with open(user_themes+theme+".json") as f:
  100. data = json.load(f)
  101. except Exception as e:
  102. try:
  103. with open(default_themes+theme+".json") as f:
  104. data = json.load(f)
  105. except Exception as e:
  106. return
  107. # Now let's actually apply the theme
  108. from flbry import variables
  109. for i in data:
  110. if data[i] in variables.clr:
  111. variables.clr[i] = clr[data[i]]
  112. else:
  113. variables.clr[i] = "\033["+data[i]+"m"
  114. def install_desktop(force=True):
  115. # This function will generate a .desktop file. And put it in
  116. # ~/.local/
  117. if force:
  118. EXEC = "sh force_terminal.sh"
  119. TERM = "false"
  120. else:
  121. EXEC = "python3 run.py"
  122. TERM = "true"
  123. desktop = """[Desktop Entry]
  124. Name=FastLBRY Terminal
  125. GenericName=LBRY client
  126. Path="""+os.getcwd()+"""
  127. Exec="""+EXEC+"""
  128. Icon="""+os.getcwd()+"""/icon.png
  129. Terminal="""+TERM+"""
  130. Type=Application
  131. Categories=Network;AudioVideo"""
  132. # Now we need to get and place it in the right place
  133. desktop_file = get_settings_folder("applications/")+"FastLRBY-terminal.desktop"
  134. try:
  135. o = open(desktop_file, "w")
  136. o.write(desktop)
  137. o.close()
  138. center("Installed in Applications Menu", "bdgr")
  139. except:
  140. center("Installing in Applications Menu failed", "bdrd")
  141. def theme_ui():
  142. # This is the ui for setting up themes.
  143. themes = []
  144. for i in os.listdir("themes"):
  145. if i.endswith(".json"):
  146. themes.append(i.replace(".json", ""))
  147. for i in os.listdir(get_settings_folder()+"themes"):
  148. if i.endswith(".json") and i.replace(".json", "") not in themes:
  149. themes.append(i.replace(".json", ""))
  150. d = {"categories":["Theme"],
  151. "size":[1],
  152. "data":[]}
  153. for i in themes:
  154. d["data"].append([i])
  155. table(d)
  156. center("Select Theme")
  157. # User selects a theme
  158. c = input(typing_dots())
  159. try:
  160. save("theme", themes[int(c)])
  161. except:
  162. save("theme", "default")
  163. center("Theme set to: "+get("theme"), "bdgr")
  164. set_theme(get("theme"))
  165. def ui():
  166. # This will be the user interface for setting up setting.
  167. # user inputs the number
  168. to_text = True
  169. while True:
  170. with open(get_settings_folder()+"config.json") as f:
  171. data = json.load(f)
  172. d = {"categories":["name","value"],
  173. "size":[1,1],
  174. "data":[]}
  175. for i in data:
  176. d["data"].append([i, data[i]])
  177. table(d)
  178. center("")
  179. c = input(typing_dots("Type 'help' for more info", to_text))
  180. to_text = False
  181. if c == "help":
  182. markdown.draw("help/settings.md", "Settings help")
  183. c = input(typing_dots())
  184. try:
  185. c = int(c)
  186. except:
  187. break
  188. # If editing theme
  189. if list(data.keys())[c] == "theme":
  190. theme_ui()
  191. elif type(data[list(data.keys())[c]]) == bool:
  192. save(list(data.keys())[c], not data[list(data.keys())[c]])
  193. else:
  194. value = input(typing_dots("New Value for '"+list(data.keys())[c]+"'", to_add_dots=True))
  195. # If the user inputs a special python value like True or None we want to treat it as that value, not as a string
  196. try:
  197. value = ast.literal_eval(value)
  198. except:
  199. pass
  200. try:
  201. save(list(data.keys())[c], value)
  202. except Exception as e:
  203. center("Error saving setting: "+str(e), "bdrd")
  204. def check_missing_keys():
  205. default = default_key_value_pairs
  206. for i in default:
  207. if not get(i):
  208. save(i, default[i])
  209. def initial_settings_stuff(hist_file):
  210. settings_cache = get_all_settings()
  211. set_theme(settings_cache["theme"])
  212. if settings_cache["save_history"]:
  213. try:
  214. readline.read_history_file(hist_file)
  215. except:
  216. file = open(hist_file, "a")
  217. file.close()
  218. if settings_cache["autoconnect"]:
  219. connect.start()
  220. # A reminder for the devs to check to devs file
  221. if settings_cache["dev_mode"]:
  222. donate.check_devs_file()