settings.py 2.7 KB

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