Exemplo n.º 1
0
 def test_attr_delimiter_cant_be_adjacent_to_cell_delimiter(self):
     """Couldn't find a logical explanation for MW's behaviour."""
     table = wtp.Table('{|class=wikitable\n' '!a| !!b|c\n' '|}')
     self.assertEqual(table.data(), [['', 'c']])
     # Remove one space and...
     table = wtp.Table('{|class=wikitable\n' '!a|!!b|c\n' '|}')
     self.assertEqual(table.data(), [['a', 'b|c']])
Exemplo n.º 2
0
    def edit_table(
        self,
        table: str,
        table_section: str,
        update_table_data: dict,
        table_row_identifier_column: str = "",
        columns_updated_in_one_row: list = [],
    ):
        """
        Edit table rows in a page text

        Keyword Arguments:
        table -- The text which the table content is being updated
        table_section_title -- The table section title
        update_table_data -- Dict with data with the update row
        table_row_identifier_column -- Identify table row that must be updated in case
                                       of there columns that must be updated only in the
                                      updated table row
        columns_updated_in_one_row -- List with columns that must be updated only in the
                                      updated table row
        Returns:
        str -- The page text with the updated table
        """
        table_string = "".join(table)
        updated_table = wtp.Table(table_string)

        table_data = updated_table.data(span=False)
        for row_number, _ in enumerate(table_data[1:], start=1):
            for edit_col in update_table_data:
                updated_table = wtp.Table(table_string)
                cell = updated_table.cells(row=row_number,
                                           column=edit_col).value
                if cell.strip(
                ) == update_table_data[edit_col]["current"].strip():
                    # Update data if column must be edited only in one row
                    if columns_updated_in_one_row:
                        if (edit_col in columns_updated_in_one_row
                                and table_row_identifier_column
                                in table_data[row_number]):
                            updated_table.cells(
                                row=row_number, column=edit_col
                            ).value = f" {update_table_data[edit_col]['update']}"
                            table_string = "".join(updated_table.string)
                        elif edit_col not in columns_updated_in_one_row:
                            updated_table.cells(
                                row=row_number, column=edit_col
                            ).value = f" {update_table_data[edit_col]['update']}"
                            table_string = "".join(updated_table.string)

                    # Update column data that can be edited in many rows
                    else:
                        updated_table.cells(
                            row=row_number, column=edit_col
                        ).value = f" {update_table_data[edit_col]['update']}"
                        table_string = "".join(updated_table.string)
        return table_section + updated_table.string
Exemplo n.º 3
0
 def test_only_pipes_can_seprate_attributes(self):
     """According to the note at mw:Help:Tables#Table_headers."""
     table = wtp.Table('{|class=wikitable\n! style="text-align:left;"! '
                       'Item\n! Amount\n! Cost\n|}')
     self.assertEqual(
         table.data(),
         [['style="text-align:left;"! Item', 'Amount', 'Cost']])
     table = wtp.Table('{|class=wikitable\n! style="text-align:left;"| '
                       'Item\n! Amount\n! Cost\n|}')
     self.assertEqual(table.data(), [['Item', 'Amount', 'Cost']])
Exemplo n.º 4
0
 def test_with_caption(self):
     table = wtp.Table('{|\n|+Food complements\n|-\n|Orange\n|Apple\n|-\n'
                       '|Bread\n|Pie\n|-\n|Butter\n|Ice cream \n|}')
     self.assertEqual(
         table.data(),
         [['Orange', 'Apple'], ['Bread', 'Pie'], ['Butter', 'Ice cream']],
     )
Exemplo n.º 5
0
 def test_longer_text_and_only_rstrip(self):
     table = wtp.Table('{|\n|multi\nline\ntext. \n\n2nd paragraph. \n|'
                       '\n* ulli1\n* ulli2\n* ulli3\n|}')
     self.assertEqual(table.data(), [[
         'multi\nline\ntext. \n\n2nd paragraph.',
         '\n* ulli1\n* ulli2\n* ulli3'
     ]])
Exemplo n.º 6
0
 def test_cell_extraction(self):
     table = wtp.Table('{|class=wikitable\n'
                       '|| 1 | 1 || a | 2\n'
                       '| 3 ||| 4\n'
                       '|| 5\n'
                       '! 6 !! a | 7\n'
                       '!| 8 || 9\n'
                       '|}')
     cells = table.cells()
     self.assertEqual(len(cells), 1)
     self.assertEqual(len(cells[0]), 9)
     cell_string = (
         '\n|| 1 | 1 ',
         '|| a | 2',
         '\n| 3 ',
         '||| 4',
         '\n|| 5',
         '\n! 6 ',
         '!! a | 7',
         '\n!| 8 ',
         '|| 9',
     )
     for r in cells:
         for i, c in enumerate(r):
             self.assertEqual(c.string, cell_string[i])
     # Single cell
     self.assertEqual(table.cells(row=0, column=4).string, cell_string[4])
     # Column only
     self.assertEqual(table.cells(column=4)[0].string, cell_string[4])
     # Row only
     self.assertEqual(table.cells(row=0)[4].string, cell_string[4])
def getZyklen():
    response = requests.get(api_endpoint,
                            params={
                                "action": "parse",
                                "section": 2,
                                "page": "Zyklen",
                                "prop": "wikitext",
                                "format": "json"
                            }).json()["parse"]["wikitext"]["*"]
    t = wtp.Table(response)

    book_index = 1
    for row in t.data()[1:]:
        print(wtp.parse(row[1]).wikilinks[0].text)
        num_books = int(html_filter.sub("", row[6]))
        if not db.session.query(Zyklus).filter(
                Zyklus.name == wtp.parse(row[1]).wikilinks[0].text).first():
            cycle = Zyklus(name=wtp.parse(row[1]).wikilinks[0].text,
                           num_books=num_books)
            db.session.add(cycle)
        else:
            cycle = db.session.query(Zyklus).filter(
                Zyklus.name == wtp.parse(row[1]).wikilinks[0].text).first()

        for book_index_relative in range(num_books):
            print(book_index + book_index_relative)
            main_characters = getMainCharacters(book_index +
                                                book_index_relative)
            adjustRelations(main_characters, cycle.id)

        book_index += num_books
        db.session.commit()
Exemplo n.º 8
0
 def test_no_caption(self):
     table = wtp.Table('{| class="wikitable"\n|a\n|+ ignore\n|}')
     self.assertEqual(table.caption, None)
     self.assertEqual(table.caption_attrs, None)
     table.caption = 'foo'
     self.assertEqual(table.string,
                      '{| class="wikitable"\n|+foo\n|a\n|+ ignore\n|}')
Exemplo n.º 9
0
 def test_no_attrs_but_caption(self):
     text = ('{|\n|+Food complements\n|-\n|Orange\n|Apple\n|-'
             '\n|Bread\n|Pie\n|-\n|Butter\n|Ice cream \n|}')
     table = wtp.Table(text)
     self.assertEqual(table.caption, 'Food complements')
     self.assertEqual(table.caption_attrs, None)
     table.caption = ' C '
     self.assertEqual(table.string, text.replace('Food complements', ' C '))
Exemplo n.º 10
0
 def test_inline_colspan_and_rowspan(self):
     table = wtp.Table('{| class=wikitable\n'
                       ' !a !! b !!  c !! rowspan = 2 | d \n'
                       ' |- \n'
                       ' | e || colspan = "2"| f\n'
                       '|}')
     self.assertEqual(table.data(span=True),
                      [['a', 'b', 'c', 'd'], ['e', 'f', 'f', 'd']])
Exemplo n.º 11
0
 def test_extra_spaces_have_no_effect(self):
     table = wtp.Table('{|\n|  Orange    ||   Apple   ||   more\n|-\n'
                       '|   Bread    ||   Pie     ||   more\n|-\n'
                       '|   Butter   || Ice cream ||  and more\n|}')
     self.assertEqual(
         table.data(),
         [['Orange', 'Apple', 'more'], ['Bread', 'Pie', 'more'],
          ['Butter', 'Ice cream', 'and more']],
     )
Exemplo n.º 12
0
 def test_attrs_and_caption(self):
     text = ('{| class="wikitable"\n'
             '|+ style="caption-side:bottom; color:#e76700;"|'
             '\'\'Food complements\'\'\n|-\n|Orange\n|Apple\n|-'
             '\n|Bread\n|Pie\n|-\n|Butter\n|Ice cream \n|}')
     table = wtp.Table(text)
     self.assertEqual(table.caption, "''Food complements''")
     self.assertEqual(table.caption_attrs,
                      ' style="caption-side:bottom; color:#e76700;"')
Exemplo n.º 13
0
 def test_second_caption_is_ignored(self):
     table = wtp.Table('{|\n'
                       '  |+ c1\n'
                       '  |+ c2\n'
                       '|-\n'
                       '|1\n'
                       '|2\n'
                       '|}')
     self.assertEqual(table.data(), [['1', '2']])
Exemplo n.º 14
0
 def test_colspan_and_rowspan_and_span_true(self):
     table = wtp.Table(
         '{| class="wikitable"\n!colspan= 6 |11\n|-\n'
         '|rowspan="2"|21\n|22\n|23\n|24\n  |colspan="2"|25\n|-\n'
         '|31\n|colspan="2"|32\n|33\n|34\n|}')
     self.assertEqual(table.data(span=True), [
         ['11', '11', '11', '11', '11', '11'],
         ['21', '22', '23', '24', '25', '25'],
         ['21', '31', '32', '32', '33', '34'],
     ])
Exemplo n.º 15
0
 def test_growing_downward_growing_cells(self):
     table = wtp.Table('{|class=wikitable\n'
                       '| a || rowspan=0 | b\n'
                       '|-\n'
                       '| c\n'
                       '|}')
     self.assertEqual(table.data(span=True), [
         ['a', 'b'],
         ['c', 'b'],
     ])
Exemplo n.º 16
0
 def test_colspan_0(self):
     table = wtp.Table('{|class=wikitable\n'
                       '| colspan=0 | a || b\n'
                       '|-\n'
                       '| c || d\n'
                       '|}')
     self.assertEqual(table.data(span=True), [
         ['a', 'b'],
         ['c', 'd'],
     ])
Exemplo n.º 17
0
 def test_with_optional_rowseprator_on_first_row(self):
     table = wtp.Table('{| class=wikitable | g\n'
                       ' |- 132131 |||\n'
                       '  | a | b\n'
                       ' |-\n'
                       '  | c\n'
                       '|}')
     self.assertEqual(
         table.data(),
         [['b'], ['c']],
     )
Exemplo n.º 18
0
 def test_changing_cell_should_effect_the_table(self):
     t = wtp.Table('{|class=wikitable\n|a=b|c\n|}')
     c = t.cells(0, 0)
     c.value = 'v'
     self.assertEqual(c.value, 'v')
     c.set('a', 'b2')
     self.assertEqual(t.string, '{|class=wikitable\n|a="b2"|v\n|}')
     c.delete('a')
     self.assertEqual(t.string, '{|class=wikitable\n||v\n|}')
     c.set('c', 'd')
     self.assertEqual(t.string, '{|class=wikitable\n| c="d"|v\n|}')
Exemplo n.º 19
0
 def test_ending_row_group(self):
     table = wtp.Table('{|class=wikitable\n'
                       '| rowspan = 3 | a || b\n'
                       '|-\n'
                       '| c\n'
                       '|}')
     self.assertEqual(table.data(span=True), [
         ['a', 'b'],
         ['a', 'c'],
         ['a', None],
     ])
Exemplo n.º 20
0
 def test_ending_row_group_and_rowspan_0(self):
     table = wtp.Table('{|class=wikitable\n'
                       '| rowspan = 3 | a || rowspan = 0 | b || c\n'
                       '|-\n'
                       '| d\n'
                       '|}')
     self.assertEqual(table.getdata(span=True), [
         ['a', 'b', 'c'],
         ['a', 'b', 'd'],
         ['a', 'b', None],
     ])
Exemplo n.º 21
0
 def test_with_headers(self):
     table = wtp.Table(
         '{|\n! style="text-align:left;"| Item\n! Amount\n! Cost\n|-\n'
         '|Orange\n|10\n|7.00\n|-\n|Bread\n|4\n|3.00\n|-\n'
         '|Butter\n|1\n|5.00\n|-\n!Total\n|\n|15.00\n|}')
     self.assertEqual(table.data(), [
         ['Item', 'Amount', 'Cost'],
         ['Orange', '10', '7.00'],
         ['Bread', '4', '3.00'],
         ['Butter', '1', '5.00'],
         ['Total', '', '15.00'],
     ])
Exemplo n.º 22
0
 def test_all_rows_are_on_a_single_line(self):
     table = wtp.Table('{|\n'
                       '|a||b||c\n'
                       '|-\n'
                       '|d||e||f\n'
                       '|-\n'
                       '|g||h||i\n'
                       '|}')
     self.assertEqual(
         table.data(),
         [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']],
     )
Exemplo n.º 23
0
 def test_multiline_table(self):
     table = wtp.Table('{|s\n|a\n|}')
     self.assertEqual(table.table_attrs, 's')
     self.assertEqual(table.attrs, {'s': ''})
     self.assertEqual(table.has_attr('s'), True)
     self.assertEqual(table.has_attr('n'), False)
     self.assertEqual(table.get_attr('s'), '')
     table.table_attrs = 'class="wikitable"'
     self.assertEqual(repr(table),
                      "Table('{|class=\"wikitable\"\\n|a\\n|}')")
     self.assertEqual(table.get_attr('class'), 'wikitable')
     table.set_attr('class', 'sortable')
     self.assertEqual(table.attrs, {'class': 'sortable'})
     table.del_attr('class')
     self.assertEqual(table.attrs, {})
Exemplo n.º 24
0
 def test_each_row_on_a_newline(self):
     table = wtp.Table('{|\n'
                       '|Orange\n'
                       '|Apple\n'
                       '|-\n'
                       '|Bread\n'
                       '|Pie\n'
                       '|-\n'
                       '|Butter\n'
                       '|Ice cream \n'
                       '|}')
     self.assertEqual(
         table.data(),
         [['Orange', 'Apple'], ['Bread', 'Pie'], ['Butter', 'Ice cream']],
     )
Exemplo n.º 25
0
 def test_odd_case1(self):
     table = wtp.Table('{|class=wikitable\n'
                       '  [[a]]\n'
                       ' |+ cp1\n'
                       'cp1\n'
                       '! h1 ||+ h2\n'
                       '|-\n'
                       '! h3 !|+ h4\n'
                       '|-\n'
                       '! h5 |!+ h6\n'
                       '|-\n'
                       '|c1\n'
                       '|+t [[w]]\n\n'
                       'text\n'
                       '|c2\n'
                       '|}')
     self.assertEqual(table.data(span=False),
                      [['h1', '+ h2'], ['+ h4'], ['!+ h6'], ['c1', 'c2']])
Exemplo n.º 26
0
 def test_set_caption_attrs_before_cap(self):
     table = wtp.Table('{| class="wikitable"\n|a\n|+ ignore\n|}')
     table.caption_attrs = 'style=""'
     self.assertEqual(table.caption_attrs, 'style=""')
Exemplo n.º 27
0
 def test_replace_caption_attrs(self):
     table = wtp.Table('{|class="wikitable"\n|+old|cap\n|}')
     table.caption_attrs = 'new'
     self.assertEqual(table.caption_attrs, 'new')
Exemplo n.º 28
0
 def test_doublepipe_multiline(self):
     table = wtp.Table('{|\n|| multi\nline\n||\n 1\n|}')
     self.assertEqual(table.data(), [['multi\nline', '\n 1']])
Exemplo n.º 29
0
 def test_strip_is_false(self):
     table = wtp.Table('{|class=wikitable\n| a || b \n|}')
     self.assertEqual(table.data(strip=False), [[' a ', ' b ']])
Exemplo n.º 30
0
 def test_cell_span_false(self):
     t = wtp.Table('{|class=wikitable\n|a=b|c\n|}')
     c = t.cells(span=False)
     self.assertEqual(len(c), 1)