forked from ursinus-cs271-f2023/Modules
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgithubclassroomasmt.py
More file actions
148 lines (119 loc) · 5.41 KB
/
githubclassroomasmt.py
File metadata and controls
148 lines (119 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
import time
import sys
import pyperclip as pc
import os
import frontmatter
class GHClassroomAsmt(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "https://www.katalon.com/"
self.verificationErrors = []
self.accept_next_alert = True
# https://stackoverflow.com/questions/3663450/remove-substring-only-at-the-end-of-string
def rchop(self, s, suffix):
if suffix and s.endswith(suffix):
return s[:-len(suffix)]
return s
def getasmts(self):
result = []
f = open(MDFILE, 'r')
mdcontents = f.read()
post = frontmatter.loads(mdcontents)
postdict = post.to_dict()
for item in postdict['schedule']:
if 'deliverables' in item:
for deliverable in item['deliverables']:
dtitle = deliverable['dtitle']
if not 'handed out' in dtitle.lower():
dtitle = self.rchop(dtitle, 'Handed Out')
dtitle = self.rchop(dtitle, "Due")
result.append(dtitle)
return result
def createasmt(self, title, driver):
courseidx = 1
coursesearchdone = False
while not coursesearchdone:
try:
if driver.find_element_by_xpath("(.//*[normalize-space(text()) and normalize-space(.)='Archive'])[" + str(courseidx) + "]/following::h1[1]").text == COURSE:
coursesearchdone = True
else:
courseidx = courseidx + 1
except Exception as e:
print(e)
break
if not coursesearchdone:
print("Course not found: might already be on the course page; otherwise, this will fail")
else:
print("Found course index " + str(courseidx))
driver.find_element_by_xpath("(.//*[normalize-space(text()) and normalize-space(.)='Archive'])[" + str(courseidx) + "]/following::h1[1]").click()
try:
driver.find_element_by_link_text("New assignment").click() # if an assignment exists
except:
try:
driver.find_element_by_link_text("Create an assignment").click() # if this is the first assignment
except Exception as e:
print(e)
sys.exit(-1)
driver.find_element_by_id("assignment_title").click()
driver.find_element_by_id("assignment_title").clear()
driver.find_element_by_id("assignment_title").send_keys(title)
driver.find_element_by_xpath("(.//*[normalize-space(text()) and normalize-space(.)='/'])[2]/following::div[2]").click()
driver.find_element_by_id("new-assignment-submit").click()
driver.find_element_by_id("new-assignment-submit").click()
driver.find_element_by_xpath("(.//*[normalize-space(text()) and normalize-space(.)='Run C++'])[1]/following::input[2]").click()
driver.find_element_by_name("commit").click()
# Copy the link address
driver.find_element_by_css_selector("svg.octicon.octicon-clippy").click()
print(title)
print(pc.paste())
driver.find_element_by_link_text("View my classroom").click()
def test_createassignment(self):
asmts = self.getasmts()
driver = self.driver
driver.get("https://classroom.github.com/")
driver.find_element_by_link_text("Sign in").click()
print("Log in now!")
time.sleep(30)
for asmt in asmts:
title = ASMTTITLE + asmt
title = title.strip()
self.createasmt(title[0:59], driver)
driver.close()
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException as e: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException as e: return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
COURSE = os.getenv('COURSE') # billmongan-CS173-Fall2020
ASMTTITLE = os.getenv('ASMTTITLE') # just the prefix CS173-Fall2020-
MDFILE = os.getenv('MDFILE') # pages/_syllabus.md
if COURSE is None:
print("Usage Environment Variables: COURSE=<GitHub Classroom Class Name> ASMTTITLE=<Assignment title prefix> MDFILE=_pages/syllabus.md")
sys.exit(-1)
unittest.main()