Stateful.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import Alt
  2. import base64, datetime
  3. import tomlkit, sqlite3
  4. # kw == 'k'ey 'w'ord; dt_now = date&&time in UTC
  5. # Please consider adding more salt to prevent uid duplication(specially under multi-user use case)
  6. def generate_uid(kw):
  7. dt_now = str(datetime.datetime.now(datetime.timezone.utc))
  8. genText = str.encode(kw + "_" + dt_now)
  9. uid = base64.b64encode(genText)
  10. return(uid)
  11. # ----- Methods binding app command to model -----
  12. # Toml
  13. def GetTomlDoc(tomlName):
  14. try:
  15. with open(tomlName, "rb") as t:
  16. doc = tomlkit.load(t)
  17. if doc == {}:
  18. input("error 0: could not found correct config file")
  19. exit()
  20. return doc
  21. except:
  22. input("error 0: could not found correct config file")
  23. exit()
  24. def MatchTomlKey(tomlName, key, table=None) -> str:
  25. doc = GetTomlDoc(tomlName)
  26. if table == None:
  27. return str(doc.item(key))
  28. elif table != None:
  29. d = doc.unwrap()
  30. return str(d[table][key])
  31. # no differernt between ↑ MatchTomlKey() ↑ except receives and returns in list
  32. def MatchTomlKeys(tomlName, keys, table=None) -> list:
  33. doc = GetTomlDoc(tomlName)
  34. if table == None:
  35. # rl == 'r'eturn 'l'ist
  36. rl = []
  37. for key in keys:
  38. try:
  39. rl.append(doc.item(key))
  40. except:
  41. pass
  42. return rl
  43. elif table != None:
  44. rl = []
  45. d = doc.unwrap()
  46. for key in keys:
  47. try:
  48. rl.append(d[table][key])
  49. except:
  50. pass
  51. return rl
  52. # Sqlite3
  53. def Operate_sqlite3(dbPath, match_commands):
  54. matchedSyntax = MatchTomlKeys("dev_config.toml", match_commands, "sqlite3")
  55. s = " ".join(matchedSyntax)
  56. con = sqlite3.connect(dbPath)
  57. cur = con.cursor()
  58. con.close()
  59. # Markdown
  60. # csv
  61. # MongoDB
  62. # ----- Transit Command Handler -----
  63. def PackHandler(app_commands):
  64. dbType = app_commands[-1]
  65. dbPath = app_commands[-2]
  66. if dbType == "sqlite3":
  67. Operate_sqlite3(dbPath, app_commands)
  68. elif dbType == "csv":
  69. pass
  70. elif dbType == "mongodb":
  71. pass
  72. elif dbType == "toml":
  73. pass
  74. elif dbType == "md":
  75. pass
  76. else:
  77. input("error 1: could not found correct Data Base")
  78. exit()
  79. if __name__ == "__main__":
  80. a_c = ['add', 'board', 'testBoardName', 'test.db', 'sqlite3']
  81. a_c1 = ['edit', 'board', 't_boardName', 'to', 't_newBoardName', 'test.db', 'sqlite3']
  82. e_c = ['add', 'board']
  83. # Operate_sqlite3("test.db", a_c1)
  84. PackHandler(a_c)