WithToml.py 1.2 KB

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