Stateful.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import Alt
  2. import tomlkit, sqlite3
  3. # uid related
  4. # ----- Toml Methods -----
  5. def GetTomlDoc(tomlName):
  6. try:
  7. with open(tomlName, "rb") as t:
  8. doc = tomlkit.load(t)
  9. if doc == {}:
  10. input("error 0: could not found correct config file")
  11. exit()
  12. return doc
  13. except:
  14. input("error 0: could not found correct config file")
  15. exit()
  16. def MatchTomlKey(tomlName, key, table=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, keys, table=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. # ----- Sqlite -----
  45. def Exec_one(dbPath, commands):
  46. con = sqlite3.connect(dbPath)
  47. cur = con.cursor()
  48. cur.execute(commands)
  49. con.commit()
  50. re = cur.fetchall()
  51. con.close()
  52. return re
  53. def IsExist(exec_commands, returnBool=True):
  54. tableName = str(exec_commands[1]).capitalize()
  55. ItemName = str(exec_commands[2])
  56. sqls = "SELECT name FROM {table} WHERE name='{name}'".format(table=tableName, name=ItemName)
  57. ie = Exec_one(dbPath, sqls)
  58. if ie != [] and returnBool == False:
  59. return ie
  60. elif ie != [] and returnBool == True:
  61. return True
  62. elif ie == []:
  63. return False
  64. else:
  65. # Alt.Err(errCode)
  66. print("err <Code>: unexpected error in existence check")
  67. class command():
  68. pass
  69. class objBoard():
  70. pass
  71. class objClass():
  72. pass
  73. class objEvent():
  74. pass