def gradebook_header(contents): result = [] for div in [ cell.strip() for cell in queryHTML('//th/div[1]', contents) if cell.strip() ]: links = [ cell.strip() for cell in queryHTML('//a/text()', div) if cell.strip() ] if links: # <th><div><a>...</a></div></th> result.extend(links) else: spans = [ cell.strip() for cell in queryHTML('//span/text()', div) if cell.strip() ] if spans: # <th><div><span>...</span></div></th> (e.g. linked column activities) result.extend(spans) else: labels = [ cell.strip() for cell in queryHTML('//div/text()', div) if cell.strip() ] if labels: # <th><div>...</div></th> (e.g. Name, Total, Ave.) result.extend(labels) return result
def format_weekly_calendar(contents): table = [] for n, row in enumerate(queryHTML('//table[@id="calendar-view-week"]//tr', contents)): if n == 0: header = [] for cell in queryHTML('//tr//th/a/text()', str(row)): header.append(cell) table.append(header) else: block = [] for cell in queryHTML('//tr//td', str(row)): events = [] for event in queryHTML('//td//a//span/text()', str(cell)): events.append(event) block.append(", ".join(events)) table.append(block) return format_table(table, header_rows=1)
def worksheet_tabs(contents): result = [] cells = [ cell.strip() for cell in queryHTML( '//table[@class="schooltool_gradebook"][1]/tr[1]/td', contents) ] for cell in cells: row = [] row.extend([ '*%s*' % text.strip() for text in queryHTML('//td/span/text()', cell) if text.strip() ]) row.extend([ '%s' % text.strip() for text in queryHTML('//td/a/text()', cell) if text.strip() ]) result.extend(row) return result
def format_weekly_calendar(contents): table = [] for n, row in enumerate( queryHTML('//table[@id="calendar-view-week"]//tr', contents)): if n == 0: header = [] for cell in queryHTML('//tr//th/a/text()', str(row)): header.append(cell) table.append(header) else: block = [] for cell in queryHTML('//tr//td', str(row)): events = [] for event in queryHTML('//td//a//span/text()', str(cell)): events.append(event) block.append(", ".join(events)) table.append(block) return format_table(table, header_rows=1)
def printQuery(self, query, skip_inner_blank=False): for item in queryHTML(query, self.contents): if item.strip(): if skip_inner_blank: result = str(item.strip()).splitlines() for line in result: line = line.strip() if line: print line else: print item.strip()
def printGradebook(contents): contents = contents.replace('<br />', ' ') tabs_table_rows = [] tabs_table_rows.append(worksheet_tabs(contents)) grades_table_rows = [] gradebook_rows = queryHTML('//table[@class="schooltool_gradebook"][2]//tr', contents) for row_number, row in enumerate(gradebook_rows): # we don't care about these rows # first (0): activity description # third (2): 'Apply a grade for all students' if row_number == 1: grades_table_rows.append(gradebook_header(row)) if row_number > 2: columns = [] cells = [cell for cell in queryHTML('//tr/td', row)] for cell in cells: # Student's name text = queryHTML('//td/a[1]/text()', cell) if not text: # Activity inputs text_input_value = queryHTML( '//td//input[@type="text"]/@value', cell) if text_input_value: text = [ "[%s]" % str(text_input_value[0]).ljust(5, '_') ] if not text: # Total and Ave. text = queryHTML('//td/b/text()', cell) if text: text = [text[0].strip()] if not text: # Linked column activities text = queryHTML('//td/span/text()', cell) if text: text = [text[0].strip()] if not text: text = [''] columns.append(text[0].strip().encode('utf-8')) grades_table_rows.append(columns) print format_table(tabs_table_rows) print format_table(grades_table_rows, header_rows=1)
def queryHTML(self, query): return queryHTML(query, self.contents)