dummy-frontend.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import sys
  2. from backends.support import ResourceCategory, ResourceRawHTML
  3. from backends.schoolloop import SchoolloopBackend
  4. from backends.haiku import HaikuBackend
  5. from backends.dummy import DummyBackend
  6. backends = [DummyBackend, SchoolloopBackend, HaikuBackend]
  7. backend = { x.__name__ : x for x in backends } [sys.argv[1]] ()
  8. from http.client import HTTPConnection
  9. import logging
  10. HTTPConnection.debuglevel = 1
  11. logging.basicConfig()
  12. logging.getLogger().setLevel(logging.DEBUG)
  13. requests_log = logging.getLogger("requests.packages.urllib3")
  14. requests_log.setLevel(logging.DEBUG)
  15. requests_log.propagate = True
  16. def authenticate():
  17. config = {
  18. "Email": sys.argv[2],
  19. "Username": sys.argv[2],
  20. "Password": sys.argv[3],
  21. "School": sys.argv[4]
  22. }
  23. if backend.login(config):
  24. backend.authenticated = True
  25. else:
  26. print("Bad credentials")
  27. sys.exit(1)
  28. def dump_resources(rsrc, count):
  29. if isinstance(rsrc, ResourceCategory):
  30. print((" " * count) + rsrc.name)
  31. for child in rsrc.children:
  32. dump_resources(child, count + 1)
  33. if rsrc.name == "Welcome":
  34. for child in rsrc.contents:
  35. dump_resources(child, count + 1)
  36. elif isinstance(rsrc, ResourceRawHTML):
  37. print((" " * count) + rsrc.name + ": " + rsrc.html)
  38. else:
  39. print((" " * count) + rsrc.name + " (" + type(rsrc).__name__ + ")")
  40. authenticate()
  41. for course in backend.courses:
  42. print(course.title + " by " + course.teacher + " (ID " + course.id + ")")
  43. try:
  44. print("Course grade: " + str(course.grade_summary))
  45. if course.title == "TODO: Don't waste bandwidth":
  46. for category in course.grades:
  47. print(category.name + ": " + str(round(category.mean * 100)) + "%")
  48. for grade in category.grades:
  49. print(".. " + grade.name + ": " + str(round(grade.grade * 100)) + "%")
  50. except AttributeError:
  51. print("Backend does not implement grades")
  52. try:
  53. dump_resources(course.resources, 0)
  54. except AttributeError:
  55. print("Backend does not implement resources")
  56. try:
  57. for task in course.tasks:
  58. print("- " + task.name)
  59. except AttributeError:
  60. print("Backend does not implement tasks")
  61. print("----")