settings.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. "auth_token":"",
  54. "comment_api": "https://comments.odysee.com/api/v2",
  55. }
  56. def get_settings_folder(flbry="flbry/"):
  57. try:
  58. data_dir = os.environ["XDG_DATA_HOME"] + "/" + flbry
  59. except:
  60. data_dir = os.path.expanduser("~/.local/share/"+flbry)
  61. try:
  62. os.makedirs(data_dir)
  63. except:
  64. pass
  65. return data_dir
  66. def get_all_settings():
  67. with open(get_settings_folder()+"config.json") as f:
  68. settings_cache = json.load(f)
  69. return settings_cache
  70. def check_config():
  71. # This function checks whether config exists. If not makes a
  72. # default setting.
  73. default = default_key_value_pairs
  74. if not os.path.exists(get_settings_folder()+"config.json"):
  75. with open(get_settings_folder()+"config.json", 'w') as f:
  76. json.dump(default, f, indent=4, sort_keys=True)
  77. def get(key):
  78. # This function gets a setting from settings.
  79. data = get_all_settings()
  80. try:
  81. return data[key]
  82. except:
  83. return None
  84. def save(key, value):
  85. # This function will save a value into the settings file.
  86. data = get_all_settings()
  87. data[key] = value
  88. with open(get_settings_folder()+"config.json", 'w') as f:
  89. json.dump(data, f, indent=4, sort_keys=True)
  90. def set_theme(theme):
  91. # This will set a global theme
  92. user_themes = get_settings_folder()+"themes/"
  93. default_themes = "themes/"
  94. # First let's see if user has a theme folder in settings.
  95. try:
  96. os.makedirs(user_themes)
  97. except:
  98. pass
  99. # Trying to load the theme from user themes first
  100. try:
  101. with open(user_themes+theme+".json") as f:
  102. data = json.load(f)
  103. except Exception as e:
  104. try:
  105. with open(default_themes+theme+".json") as f:
  106. data = json.load(f)
  107. except Exception as e:
  108. return
  109. # Now let's actually apply the theme
  110. from flbry import variables
  111. for i in data:
  112. if data[i] in variables.clr:
  113. variables.clr[i] = clr[data[i]]
  114. else:
  115. variables.clr[i] = "\033["+data[i]+"m"
  116. def install_desktop(force=True):
  117. # This function will generate a .desktop file. And put it in
  118. # ~/.local/
  119. if force:
  120. EXEC = "sh force_terminal.sh"
  121. TERM = "false"
  122. else:
  123. EXEC = "python3 run.py"
  124. TERM = "true"
  125. desktop = """[Desktop Entry]
  126. Name=FastLBRY Terminal
  127. GenericName=LBRY client
  128. Path="""+os.getcwd()+"""
  129. Exec="""+EXEC+"""
  130. Icon="""+os.getcwd()+"""/icon.png
  131. Terminal="""+TERM+"""
  132. Type=Application
  133. Categories=Network;AudioVideo"""
  134. # Now we need to get and place it in the right place
  135. desktop_file = get_settings_folder("applications/")+"FastLRBY-terminal.desktop"
  136. try:
  137. o = open(desktop_file, "w")
  138. o.write(desktop)
  139. o.close()
  140. center("Installed in Applications Menu", "bdgr")
  141. except:
  142. center("Installing in Applications Menu failed", "bdrd")
  143. def theme_ui():
  144. # This is the ui for setting up themes.
  145. themes = []
  146. for i in os.listdir("themes"):
  147. if i.endswith(".json"):
  148. themes.append(i.replace(".json", ""))
  149. for i in os.listdir(get_settings_folder()+"themes"):
  150. if i.endswith(".json") and i.replace(".json", "") not in themes:
  151. themes.append(i.replace(".json", ""))
  152. d = {"categories":["Theme"],
  153. "size":[1],
  154. "data":[]}
  155. for i in themes:
  156. d["data"].append([i])
  157. table(d)
  158. center("Select Theme")
  159. # User selects a theme
  160. c = input(typing_dots())
  161. try:
  162. save("theme", themes[int(c)])
  163. except:
  164. save("theme", "default")
  165. center("Theme set to: "+get("theme"), "bdgr")
  166. set_theme(get("theme"))
  167. def ui():
  168. # This will be the user interface for setting up setting.
  169. # user inputs the number
  170. to_text = True
  171. while True:
  172. with open(get_settings_folder()+"config.json") as f:
  173. data = json.load(f)
  174. d = {"categories":["name","value"],
  175. "size":[1,1],
  176. "data":[]}
  177. for i in data:
  178. d["data"].append([i, data[i]])
  179. table(d)
  180. center("")
  181. c = input(typing_dots("Type 'help' for more info", to_text))
  182. to_text = False
  183. if c == "help":
  184. markdown.draw("help/settings.md", "Settings help")
  185. c = input(typing_dots())
  186. try:
  187. c = int(c)
  188. except:
  189. break
  190. # If editing theme
  191. if list(data.keys())[c] == "theme":
  192. theme_ui()
  193. elif type(data[list(data.keys())[c]]) == bool:
  194. save(list(data.keys())[c], not data[list(data.keys())[c]])
  195. else:
  196. value = input(typing_dots("New Value for '"+list(data.keys())[c]+"'", to_add_dots=True))
  197. # If the user inputs a special python value like True or None we want to treat it as that value, not as a string
  198. try:
  199. value = ast.literal_eval(value)
  200. except:
  201. pass
  202. try:
  203. save(list(data.keys())[c], value)
  204. except Exception as e:
  205. center("Error saving setting: "+str(e), "bdrd")
  206. def check_missing_keys():
  207. default = default_key_value_pairs
  208. for i in default:
  209. if not get(i):
  210. save(i, default[i])
  211. def initial_settings_stuff(hist_file):
  212. settings_cache = get_all_settings()
  213. set_theme(settings_cache["theme"])
  214. if settings_cache["save_history"]:
  215. try:
  216. readline.read_history_file(hist_file)
  217. except:
  218. file = open(hist_file, "a")
  219. file.close()
  220. if settings_cache["autoconnect"]:
  221. connect.start()
  222. # A reminder for the devs to check to devs file
  223. if settings_cache["dev_mode"]:
  224. donate.check_devs_file()