def test_get_sections(self): text = "=Section=\nSection text" section_service = WikiSectionService() text_sections = section_service.get_sections(text) # check if all list items are of Section type section_list_instance = all( isinstance(section, wtp.Section) for section in text_sections ) self.assertTrue(section_list_instance)
def add_table_row( self, page_text: str, new_row: str, table_section_title: str, table_template: str, ) -> str: """ Add a table with a new row to the page text Keyword Arguments: page_text -- The text which the table content is being added new_row -- The table row which will be added to the page text table_section_title -- The table section title table_template -- The template with the table header Returns: str -- The page text with the new table row """ page_text += f"{table_template}" table = WikiSectionService().get_section_table(page_text, table_section_title) table_string = str(table) str_index_new_row = self.get_new_row_index(table) updated_table = (table_string[:str_index_new_row] + new_row + table_string[str_index_new_row:]) text_before_table_index = page_text.find(table_string) wtp_page_text = wtp.parse(page_text) wtp_page_text.string = page_text[ 0:text_before_table_index] + updated_table return wtp_page_text.string
def test_get_section_title_str_index_fails_with_no_existing_section(self): template_text = "=Section=\n" "Section text" section = wtp.Section("=Different Section title=\n" "Section text") section_level = section.level with self.assertRaises(ValueError): WikiSectionService().get_section_title_str_index( section, template_text, section_level )
def generate_text_from_dict( self, template_text: str, page_initial_section: str, page_data: dict ): """ Generates text from dict Keyword Arguments: template_text -- Text used as template page_initial_section -- The first section of the page page_data -- Dict containing data of all page sections Returns: updated_text -- Text formatted with wikitext syntax """ section_obj = WikiSectionService() sections = section_obj.get_sections(template_text) updated_text = f"{page_initial_section}\n" for section in sections: if section_obj.is_section_being_updated(section, page_data): start_index, end_index = section_obj.get_section_title_str_index( section, template_text, section.level ) page_section_data = page_data[section.title] if section_obj.parent_section_contains_child_section(page_section_data): for (children_section_number, child_section) in enumerate( page_section_data ): child_section_title = section_obj.add_child_section_markers( section, child_section ) # If the section has more than one child section # this child's section data must be placed after # the predecessor child section if children_section_number > 0: end_index = section_obj.update_child_section_string_position( page_data, section, children_section_number ) # Update page text updated_text += ( template_text[start_index:end_index] + child_section_title + page_section_data[child_section] ) else: # Update page text updated_text += ( template_text[start_index:end_index] + page_section_data ) return updated_text
def test_section_being_updated(self): SectionStub = namedtuple("Section", ["title"]) section_title = "section title" section = SectionStub(section_title) page_data = {section_title: "section text"} updated_section = WikiSectionService().is_section_being_updated( section, page_data ) self.assertTrue(updated_section)
def generate_sections_dict(self, sections): page_sections_dict = {} for section in sections: if section.title is not None: try: parent_section_level = 2 ( start_index, end_index, ) = WikiSectionService().get_section_title_str_index( section, section.string, parent_section_level) except ValueError: continue parent_section_text = section.string[ end_index:len(section.string) # noqa ] children_sections = WikiSectionService().get_sections( parent_section_text) children_dict = {} for child_section in children_sections: if child_section.title is not None: ( child_start_index, child_end_index, ) = WikiSectionService().get_section_title_str_index( child_section, child_section.string, parent_section_level + 1, ) children_dict[ child_section.title] = child_section.string[ child_end_index:len(child_section.string ) # noqa ] page_sections_dict[section.title] = children_dict return page_sections_dict
def test_get_section_table(self): col_header = "Column header" col_data = "Column data" text = ( "=Section=\n" "{|class='wikitable sortable'\n" "|-\n" f'! scope="col" | {col_header}\n' "|-\n" f"| {col_data}\n" "|-\n" "|}" ) section_service = WikiSectionService() section_table = section_service.get_section_table(text, "Section") self.assertIsInstance(section_table, wtp.Table) # Assert data in table rows table_header_data = section_table.data()[0][0] table_col_data = section_table.data()[1][0] self.assertEqual(col_header, table_header_data) self.assertEqual(col_data, table_col_data)
def wikitext_to_dict(self, page_title: str): mediawiki = MediaWikiService() text = mediawiki.get_page_text(page_title) redirect_page = MediaWikiService().is_redirect_page(text) if redirect_page: raise ValueError( f"Error getting text from the page '{page_title}'." f" Page was moved from '{page_title}' to '{redirect_page}'") else: sections = WikiSectionService().get_sections(text) page_sections_dict = self.generate_sections_dict(sections) if not page_sections_dict: raise ValueError(f"Error parsing page '{page_title}' to dict") else: return page_sections_dict
def edit_page_text(self, update_fields: dict, overview_page_data: dict, document_data: dict): page_text = MediaWikiService().get_page_text(self.templates.oeg_page) updated_table_fields = self.get_update_table_fields( update_fields, overview_page_data) if updated_table_fields: overview_page_table = WikiSectionService().get_section_table( page_text, self.templates.activities_list_section_title) project_list_section_title = ( f"\n=={self.templates.page_initial_section}==\n" f"==={self.templates.activities_list_section_title}===\n") updated_text = WikiTableService().edit_table( overview_page_table.string, project_list_section_title, updated_table_fields, ) return updated_text else: return page_text
def test_add_child_section_markers(self): SectionStub = namedtuple("Section", ["level"]) parent_section_level = 1 parent_section = SectionStub(parent_section_level) child_section_markers = "=" * (parent_section_level + 1) child_section_text = "text" expected_child_section_title = ( f"\n{child_section_markers}" f"{child_section_text}" f"{child_section_markers}" ) child_section_title = WikiSectionService().add_child_section_markers( parent_section, child_section_text ) self.assertEqual(expected_child_section_title, child_section_title)
def create_page(self, document_data: dict) -> None: """ Creates a wiki page Keyword arguments: document_data -- All required data for a project using Organised Editing Guidelines """ mediawiki = MediaWikiService() wikitext = WikiTextService() token = mediawiki.get_token() page_title = self.templates.oeg_page overview_page_sections = self.document_to_page_sections(document_data) sections_text = wikitext.generate_text_from_dict( self.templates.page_template, f"=={self.templates.page_initial_section}==", overview_page_sections, ) updated_text = WikiTableService().add_table_row( page_text=sections_text, new_row=self.generate_activities_list_table_row(document_data), table_section_title=self.templates.activities_list_section_title, table_template=self.templates.table_template, ) if mediawiki.is_existing_page(page_title): page_text = MediaWikiService().get_page_text( self.templates.oeg_page) overview_page_table = (WikiSectionService().get_section_table( page_text, self.templates.activities_list_section_title).string) updated_text = WikiTableService().add_table_row( page_text=page_text, new_row=self.generate_activities_list_table_row(document_data), table_section_title=self.templates. activities_list_section_title, table_template=overview_page_table, ) mediawiki.edit_page(token, self.templates.oeg_page, updated_text) else: mediawiki.create_page(token, page_title, updated_text)
def create_page(self, document_data: dict): """ Creates a wiki page Keyword arguments: document_data -- All required data for a project using Organised Editing Guidelines """ mediawiki = MediaWikiService() organisation_page_sections = self.document_to_page_sections( document_data) sections_text = WikiTextService().generate_text_from_dict( self.templates.page_template, self.templates.page_initial_section, organisation_page_sections, ) updated_text = WikiTableService().add_table_row( page_text=sections_text, new_row=self.generate_projects_list_table_row(document_data), table_section_title=self.templates.projects_section, table_template=self.templates.table_template, ) page_title = f"{self.templates.oeg_page}/{document_data['organisation']['name'].capitalize()}" token = mediawiki.get_token() if mediawiki.is_existing_page(page_title): page_text = MediaWikiService().get_page_text(page_title) organisation_page_table = (WikiSectionService().get_section_table( page_text, self.templates.projects_list_section).string) updated_text = WikiTableService().add_table_row( page_text=page_text, new_row=self.generate_projects_list_table_row(document_data), table_section_title=self.templates.projects_list_section, table_template=organisation_page_table, ) mediawiki.edit_page(token, page_title, updated_text) else: mediawiki.create_page(token, page_title, updated_text)
def test_get_section_index_fails_with_no_existing_section(self): text = "=Section=\nSection text\n=Second section=" section_service = WikiSectionService() with self.assertRaises(ValueError): section_service.get_section_index(text, "Non existing section")
def test_get_section_table_fails_without_table_in_text(self): text_without_table = "=Section title=\nText without table" with self.assertRaises(ValueError): WikiSectionService().get_section_table(text_without_table, "Section title")
def test_section_parent_section_contains_child_section(self): page_section_data = {"child section": "child section text"} contains_child_section = WikiSectionService().parent_section_contains_child_section( page_section_data ) self.assertTrue(contains_child_section)
def test_section_parent_section_not_contains_child_section(self): page_section_data = "parent section text" contains_child_section = WikiSectionService().parent_section_contains_child_section( page_section_data ) self.assertFalse(contains_child_section)
def test_get_section_index(self): text = "=Section=\nSection text\n=Second section=" expected_section_index = 2 section_service = WikiSectionService() section_index = section_service.get_section_index(text, "Second section") self.assertEqual(expected_section_index, section_index)
def get_edit_page_text( self, update_fields: dict, current_organisation_page: dict, update_organisation_page: dict, ) -> str: """ Get the text for a updated organisation page Keyword Arguments: update_fields -- Fields that are being updated current_organisation_page -- Dict with the current organisation page content that is being updated update_organisation_page -- Dict with the organisation page updated content Returns: updated_text -- Text for the updated organisation page """ # Get text of a organisation page page_title = ( f"{self.templates.oeg_page}/" f"{current_organisation_page['organisation']['name'].capitalize()}" ) page_text = MediaWikiService().get_page_text(page_title) # Generate updated text for organisation page update_current_organisation_page = self.document_to_page_sections( update_organisation_page) sections_text = WikiTextService().generate_text_from_dict( page_text, self.templates.page_initial_section, update_current_organisation_page, ) updated_table_fields = self.get_update_table_fields( update_fields, current_organisation_page) # Update organisation page text if none table field needs updated project_list_table = WikiSectionService().get_section_table( page_text, self.templates.projects_list_section) project_list_section_title = ( f"\n=={self.templates.projects_section}==\n" f"==={self.templates.projects_list_section}===\n") updated_text = (sections_text + project_list_section_title + project_list_table.string) if updated_table_fields: # Update organisation page text if any table field needs updated forbidden_fields = [ self.templates.projects_list_project_author_column, self.templates.projects_list_project_status_column, ] project_wiki_page = WikiTextService().hyperlink_wiki_page( wiki_page= (f"{self.templates.oeg_page}/Projects/" f'{current_organisation_page["project"]["name"].capitalize()}' ), text=current_organisation_page["project"]["name"].capitalize(), ) updated_project_list_table = WikiTableService().edit_table( project_list_table.string, project_list_section_title, updated_table_fields, project_wiki_page, forbidden_fields, ) updated_text = sections_text + updated_project_list_table return updated_text