settings.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # THIS FILE IS A PART OF VCStudio
  2. # PYTHON 3
  3. import os
  4. def read(setting):
  5. #opening the file
  6. data = open("settings/settings.data")
  7. data = data.read()
  8. data = data.split("\n")
  9. #finding the keyword
  10. for line in data:
  11. if line.startswith(setting):
  12. return convert(line.replace(setting+" = ", ""))
  13. return False
  14. def write(setting, value):
  15. # Making sure that the value is string'
  16. value = str(value)
  17. #opening the file
  18. data = open("settings/settings.data")
  19. data = data.read()
  20. data = data.split("\n")
  21. #making a new file
  22. ndata = open("settings/settings.data", "w")
  23. #finding the keyword
  24. found = False
  25. for line in data:
  26. if line.startswith(setting):
  27. line = setting+" = "+str(value)
  28. found = True
  29. if line != "":
  30. ndata.write(line+"\n")
  31. if not found:
  32. ndata.write(setting+" = "+str(value)+"\n")
  33. ndata.close()
  34. def list_languages():
  35. # Getting list of available languages
  36. all_langs = os.listdir("settings/languages/")
  37. # Filtering all the unnesesary garbage
  38. r = []
  39. for lang in all_langs:
  40. if lang.endswith(".data"):
  41. r.append(lang.replace(".data", ""))
  42. all_langs = sorted(r)
  43. return all_langs
  44. def load_all():
  45. # This function will preload everything for the settings file into the RAM
  46. # so if something has to be checked on every frame. I would not need to deal
  47. # with it constantly. But rather have a ddictionary in the RAM to which I
  48. # am going to refer at each frame / instance etc.
  49. ret = {}
  50. # Opening the file.
  51. data = open("settings/settings.data")
  52. data = data.read()
  53. data = data.split("\n")
  54. # Parsing the file.
  55. for d in data:
  56. if d:
  57. ret[d[:d.find(" = ")]] = convert(d[d.find(" = ")+3:])
  58. # Returning
  59. return ret
  60. def convert(string):
  61. # This function will convert a string of value. Into the value it self.
  62. # For exmple if it has float or boolean (True, False, None) data in the
  63. # settings file. So it's gonna be easier to parse later on.
  64. # Trying fload
  65. try:
  66. string = float(string)
  67. except:
  68. # Trying boolean
  69. if string == "True":
  70. string = True
  71. elif string == "False":
  72. string = False
  73. elif string == "None":
  74. string = None
  75. # That's it
  76. return string