Stateful.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # @author:hyf
  2. import os
  3. import base64, datetime
  4. import tomlkit, sqlite3
  5. # kw == 'k'ey 'w'ord; dt_now = date&&time in UTC
  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 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. print("Error 0: Could not find correct 'config.toml'")
  19. exit()
  20. return doc
  21. except:
  22. print("Error 0: Could not find correct 'config.toml'")
  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. def matchTomlKeys(tomlName, keys, table=None) -> list:
  32. doc = getTomlDoc(tomlName)
  33. if table == None:
  34. # rl == 'r'eturn 'l'ist
  35. rl = []
  36. for key in keys:
  37. rl.append(doc.item(key))
  38. return rl
  39. elif table != None:
  40. rl = []
  41. d = doc.unwrap()
  42. for key in keys:
  43. rl.append(d[table][key])
  44. return rl
  45. # Sqlite3
  46. # Markdown
  47. # csv
  48. # MongoDB