Esempio n. 1
0
    def format_protoclaims(self):
        """Create a preview table for the protoclaims."""
        table_head = (
            "{| class='wikitable'\n"
            "|-\n"
            "! Property\n"
            "! Value\n"
            "! Qualifiers\n"
        )
        table_end = '|}'
        table_row = (
            '|-\n'
            '| {prop} \n'
            '| {value} \n'
            '| {quals} \n'
        )

        # format each table row
        rows = []
        for prop, statements in self.protoclaims.items():
            if not statements:
                continue
            prop = PreviewItem.make_wikidata_template(prop)
            for statement in helpers.listify(statements):
                if (statement is None) or (statement.is_none()):
                    continue
                quals = ''
                if statement.quals:
                    if len(statement.quals) > 1:
                        quals = ['* {}'.format(PreviewItem.format_qual(qual))
                                 for qual in statement.quals]
                    else:
                        quals = [PreviewItem.format_qual(statement.quals[0])]

                ref = ''
                if statement.ref:
                    ref = '\n{}'.format(
                        PreviewItem.format_reference(statement.ref))

                rows.append(
                    {
                        'prop': prop,
                        'value': PreviewItem.format_itis(statement),
                        'quals': ' \n'.join(quals),
                        'references': ref
                    }
                )

        # if any statement has a reference then add the reference column
        if any(row.get('references') for row in rows):
            default_ref = PreviewItem.make_text_italics('default reference')
            table_head += '! References\n'
            table_row += '| {references} \n'
            if self.ref:
                for row in rows:
                    row['references'] = row['references'] or default_ref

        # start table construction
        table = table_head
        for row in rows:
            table += table_row.format(**row)
        table += table_end

        return table
    def add_multiple_label_or_alias(self, data, item, case_sensitive=False,
                                    summary=None):
        """
        Add multiple labels or aliases to the item in one edit.

        Adds the name as either a label (if none) or an alias in the
        given language.

        If adding both a label and an alias in the same language (or multiple
        aliases) supply a list of names where the first will be used as label.

        @param data: dictionary of language-name pairs. The name can be either
            a single name or a list of names.
        @param item: the item to which the label/alias should be added
        @param summary: summary to append to auto-generated edit summary
        @param case_sensitive: if the comparison is case sensitive
        """
        item.exists()  # load contents

        summary = summary or self.edit_summary
        edit_summary = 'Added [{{lang}}] {{typ}} to [[{qid}]]'.format(
            qid=item.title())
        if summary:
            edit_summary = u'{0}, {1}'.format(edit_summary, summary)

        new_label_langs = []
        labels = item.labels or dict()
        new_alias_langs = []
        aliases = item.aliases or dict()

        for lang, names in data.items():
            for name in helpers.listify(names):
                if lang not in labels:
                    labels[lang] = name
                    new_label_langs.append(lang)
                elif (
                        (case_sensitive and name != labels[lang]) or
                        (not case_sensitive and
                            name.lower() != labels[lang].lower())):
                    if lang not in aliases:
                        aliases[lang] = [name, ]
                        new_alias_langs.append(lang)
                    elif (
                            (case_sensitive and name not in aliases[lang]) or
                            (not case_sensitive and
                                name.lower() not in helpers.list_to_lower(
                                    aliases[lang]))):
                        aliases[lang].append(name)
                        new_alias_langs.append(lang)

        new_label_langs = sorted(list(set(new_label_langs)))
        new_alias_langs = sorted(list(set(new_alias_langs)))

        if new_label_langs:
            label_summary = edit_summary.format(
                lang=', '.join(new_label_langs), typ='label')
            item.editLabels(labels, summary=label_summary)
            pywikibot.output(label_summary)

        if new_alias_langs:
            alias_summary = edit_summary.format(
                lang=', '.join(new_alias_langs), typ='alias')
            item.editAliases(aliases, summary=alias_summary)
            pywikibot.output(alias_summary)
 def test_listify_list(self):
     input_value = ['a', 'c']
     expected = ['a', 'c']
     self.assertEqual(listify(input_value), expected)
 def test_listify_string(self):
     input_value = 'a string'
     expected = ['a string']
     self.assertEqual(listify(input_value), expected)
 def test_listify_none(self):
     self.assertEqual(listify(None), None)
 def test_listify_empty_list(self):
     self.assertEqual(listify([]), [])
Esempio n. 7
0
    def add_multiple_label_or_alias(self,
                                    data,
                                    item,
                                    case_sensitive=False,
                                    summary=None):
        """
        Add multiple labels or aliases to the item in one edit.

        Adds the name as either a label (if none) or an alias in the
        given language.

        If adding both a label and an alias in the same language (or multiple
        aliases) supply a list of names where the first will be used as label.

        @param data: dictionary of language-name pairs. The name can be either
            a single name or a list of names.
        @param item: the item to which the label/alias should be added
        @param summary: summary to append to auto-generated edit summary
        @param case_sensitive: if the comparison is case sensitive
        """
        item.exists()  # load contents

        summary = summary or self.edit_summary
        edit_summary = 'Added [{{lang}}] {{typ}} to [[{qid}]]'.format(
            qid=item.title())
        if summary:
            edit_summary = u'{0}, {1}'.format(edit_summary, summary)

        new_label_langs = []
        labels = item.labels or dict()
        new_alias_langs = []
        aliases = item.aliases or dict()

        for lang, names in data.items():
            for name in helpers.listify(names):
                if lang not in labels:
                    labels[lang] = name
                    new_label_langs.append(lang)
                elif ((case_sensitive and name != labels[lang])
                      or (not case_sensitive
                          and name.lower() != labels[lang].lower())):
                    if lang not in aliases:
                        aliases[lang] = [
                            name,
                        ]
                        new_alias_langs.append(lang)
                    elif ((case_sensitive and name not in aliases[lang])
                          or (not case_sensitive and name.lower()
                              not in helpers.list_to_lower(aliases[lang]))):
                        aliases[lang].append(name)
                        new_alias_langs.append(lang)

        new_label_langs = sorted(list(set(new_label_langs)))
        new_alias_langs = sorted(list(set(new_alias_langs)))

        if new_label_langs:
            label_summary = edit_summary.format(
                lang=', '.join(new_label_langs), typ='label')
            item.editLabels(labels, summary=label_summary)
            pywikibot.output(label_summary)

        if new_alias_langs:
            alias_summary = edit_summary.format(
                lang=', '.join(new_alias_langs), typ='alias')
            item.editAliases(aliases, summary=alias_summary)
            pywikibot.output(alias_summary)
Esempio n. 8
0
 def test_listify_string(self):
     input_value = 'a string'
     expected = ['a string']
     self.assertEqual(listify(input_value), expected)
Esempio n. 9
0
 def test_listify_list(self):
     input_value = ['a', 'c']
     expected = ['a', 'c']
     self.assertEqual(listify(input_value), expected)
Esempio n. 10
0
 def test_listify_empty_list(self):
     self.assertEqual(listify([]), [])
Esempio n. 11
0
 def test_listify_none(self):
     self.assertEqual(listify(None), None)