cli-frontend.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import sys
  2. import aggregator
  3. import shlex
  4. from backends.support import ResourceCategory, ResourceRawHTML
  5. from cmd import Cmd
  6. class PolyglotCmd(Cmd):
  7. def do_authenticate(self, args):
  8. """Authenticates with a backend. Args: backend, username, password, school"""
  9. args = shlex.split(args)
  10. if len(args) != 4:
  11. print("Wrong number of args")
  12. return
  13. name = args[0]
  14. self.backend = aggregator.load_backend(name)
  15. config = {
  16. "Email": args[1],
  17. "Username": args[1],
  18. "Password": args[2],
  19. "School": args[3]
  20. }
  21. if aggregator.authenticate(self.backend, config):
  22. print("OK")
  23. else:
  24. print("Authentication failed")
  25. return
  26. course_set = self.backend.courses
  27. self.courses = {}
  28. for course in course_set:
  29. print(course.title)
  30. self.courses[course.title] = course
  31. def dump_resource(self, rsrc, expand):
  32. if isinstance(rsrc, ResourceCategory):
  33. print(rsrc.name)
  34. if expand:
  35. for child in rsrc.children:
  36. print("..." + child.name)
  37. for child in rsrc.contents:
  38. print("")
  39. self.dump_resource(child, False)
  40. elif isinstance(rsrc, ResourceRawHTML):
  41. print(rsrc.name + ": " + rsrc.html)
  42. else:
  43. print(rsrc.name + " (" + type(rsrc).__name__ + ")")
  44. def find_resource(self, root, path):
  45. if len(path) == 0:
  46. return root
  47. if isinstance(root, ResourceCategory):
  48. for child in root.children:
  49. if child.name == path[0]:
  50. return self.find_resource(child, path[1:])
  51. print("Can't find " + path[0])
  52. return
  53. print("Wrong rsrc type with " + ",".join(path))
  54. def do_resources(self, args):
  55. args = shlex.split(args)
  56. if len(args) == 0:
  57. print("Must specify course")
  58. return
  59. self.dump_resource(self.find_resource(self.courses[args[0]].resources, args[1:]), True)
  60. def do_grades(self, _):
  61. for course in self.backend.courses:
  62. try:
  63. print(course.title + ": " + str(course.grade_summary * 100) + "%")
  64. except AttributeError:
  65. print(course.title + ": (N/A)")
  66. def tasks(self, course):
  67. try:
  68. for task in course.tasks:
  69. print("- " + task.name)
  70. except AttributeError:
  71. print("(tasks N/A)")
  72. def do_tasks(self, args):
  73. if args:
  74. self.tasks(self.courses[shlex.split(args)[0]])
  75. else:
  76. for course in self.backend.courses:
  77. print(course.title + ":")
  78. self.tasks(course)
  79. print()
  80. def do_wget(self, url):
  81. """
  82. Download file using loaded backend's session.
  83. Useful for ./export-gdoc, etc.
  84. """
  85. u = self.backend.session.get(url)
  86. with open("download.bin", "wb") as f:
  87. f.write(u.content)
  88. def do_quit(self, _):
  89. """Quits polyglot"""
  90. sys.exit(0)
  91. prompt = PolyglotCmd()
  92. prompt.prompt = "> "
  93. prompt.cmdloop()