allmaps.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import sqlite3 as sql
  2. import subprocess, traceback
  3. # get all maps in database
  4. def getmaps(database):
  5. output = []
  6. con = sql.connect(database)
  7. with con:
  8. cursor = con.cursor()
  9. try:
  10. cursor.execute("select distinct mapid from Cts_times;")
  11. output = cursor.fetchall()
  12. except sql.Error:
  13. print("Shit is fucked.")
  14. return output
  15. # if there is no query then it outputs the index file.
  16. def getcontent(query=None):
  17. cmd = [("./cts")]
  18. proc = subprocess.Popen(cmd, env=query, stdout=subprocess.PIPE, shell=True)
  19. # communicate returns 'bytes' class with function 'decode'
  20. return proc.communicate()[0].decode('utf-8')
  21. def renderindex(template):
  22. # no env variable
  23. table = getcontent()
  24. filename = "./output/index.html"
  25. with open(filename, 'w+') as fout:
  26. fout.write(template % (table))
  27. fout.close
  28. pass
  29. def main():
  30. template = ""
  31. with open("overview.html", 'r') as fin:
  32. template = fin.read()
  33. renderindex(template)
  34. maps = getmaps("db/cts.db")
  35. with open("map.html", 'r') as fin:
  36. template = fin.read()
  37. # for each map generate an html file.
  38. for game_map in maps:
  39. # game_map is a tuple obj.
  40. map_name = game_map[0]
  41. query = {"QUERY_STRING" : ("map=%s" % map_name)}
  42. table = getcontent(query)
  43. filename = ("./output/maps/%s.html" % map_name)
  44. with open(filename, 'w+') as fout:
  45. title = map_name
  46. fout.write(template.format(
  47. title=title,
  48. map_name=map_name,
  49. table=table)
  50. )
  51. # fout.write(template % (title, map_name, table))
  52. return True
  53. if __name__ == "__main__":
  54. success = False
  55. try:
  56. success = main()
  57. except FileNotFoundError:
  58. traceback.print_exc()
  59. print("\n\t The script probably didn't find the page templates needed to generate a page. You can copy minimal working examples from the repository at templates/.")
  60. if success:
  61. print("allmaps.py - Generated pages for all maps.")
  62. pass