kbc_toml.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import tomlkit
  2. import kbc_alt
  3. # ----- Toml Methods -----
  4. def GetTomlDoc(tomlName:str):
  5. try:
  6. with open(tomlName, "rb") as t:
  7. doc = tomlkit.load(t)
  8. if doc == {}:
  9. input("error 0: could not found correct config file")
  10. exit()
  11. return doc
  12. except FileNotFoundError:
  13. input("error 0: could not found correct config file")
  14. # sec_respond?
  15. exit()
  16. def MatchTomlKey(tomlName:str, key:str, table:str="none") -> str:
  17. doc = GetTomlDoc(tomlName)
  18. if table == "none":
  19. return str(doc.item(key))
  20. elif table != "none":
  21. d = doc.unwrap()
  22. return str(d[table][key])
  23. # no differernt between ↑ MatchTomlKey() ↑ except receives and returns in list
  24. def MatchTomlKeys(tomlName:str, keys:list, table:str="none") -> list:
  25. doc = GetTomlDoc(tomlName)
  26. if table == "none":
  27. # rl == 'r'eturn 'l'ist
  28. rl = []
  29. for key in keys:
  30. try:
  31. rl.append(doc.item(key))
  32. except:
  33. pass
  34. return rl
  35. elif table != "none":
  36. rl = []
  37. d = doc.unwrap()
  38. for key in keys:
  39. try:
  40. rl.append(d[table][key])
  41. except:
  42. pass
  43. return rl
  44. def MatchTomlTable(tomlName:str, tableName:str, returnType:str="list"):
  45. d = GetTomlDoc(tomlName).unwrap()
  46. if returnType == "list":
  47. return list(d.get(tableName).values())
  48. elif returnType == "dict":
  49. return d