Stateful.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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).format(tableName = match_commands[2])
  56. con = sqlite3.connect(dbPath)
  57. cur = con.cursor()
  58. res = cur.execute(s)
  59. res.fetchone()
  60. # if res = None:
  61. con.close()
  62. # Markdown
  63. # csv
  64. # MongoDB
  65. # ----- Transit Command Handler -----
  66. def PackHandler(app_commands):
  67. dbType = app_commands[-1]
  68. dbPath = app_commands[-2]
  69. if dbType == "sqlite3":
  70. Operate_sqlite3(dbPath, app_commands)
  71. elif dbType == "csv":
  72. pass
  73. elif dbType == "mongodb":
  74. pass
  75. elif dbType == "toml":
  76. pass
  77. elif dbType == "md":
  78. pass
  79. else:
  80. input("error 1: could not found correct Data Base")
  81. exit()
  82. if __name__ == "__main__":
  83. a_c = ['/', 'test.db', 'sqlite3']
  84. a_c1 = ['add', 'board', 'testBoardName', 'test.db', 'sqlite3']
  85. a_c2 = ['edit', 'board', 't_boardName', 'to', 't_newBoardName', 'test.db', 'sqlite3']
  86. e_c = ['add', 'board']
  87. # Operate_sqlite3("test.db", a_c1)
  88. PackHandler(a_c2)