Beispiel #1
0
def replace(source_name, target_name):
  source = Page(site, "")
  target = Page(site, "")
  old = get_name(source)
  new = get_name(target)

  # Move all references
  for reference in source.getReferences(follow_redirects=False):
    old_text = reference.text
    reference.text = old_text.replace(old, new)
    pywikibot.showDiff(old_text, reference.text)
    def test_self_thank(self):
        """Test that thanking oneself causes an error.

        This test is not in TestThankRevisionErrors because it may require
        making a successful edit in order to test the API call thanking the user
        running the test.
        """
        site = self.get_site()
        my_name = self.get_userpage().username
        data = site.usercontribs(user=my_name, total=1)
        for rev in data:
            revid = rev['revid']
            break
        else:
            test_page = Page(site, 'Pywikibot Thanks test')
            test_page.text += '* ~~~~\n'
            test_page.save('Pywikibot Thanks test')
            revid = test_page.latest_revision_id
        self.assertAPIError('invalidrecipient', None, Revision._thank,
                            revid, site, source='pywikibot test')
Beispiel #3
0
    def fromWikibase(cls, page_name, site):
        """
        Create a _WbDataPage from the JSON data given by the Wikibase API.

        @param page_name: page name from Wikibase value
        @type page_name: str
        @param site: The Wikibase site
        @type site: pywikibot.site.DataSite
        @rtype: pywikibot._WbDataPage
        """
        data_site = cls._get_data_site(site)
        page = Page(data_site, page_name)
        return cls(page, site)
Beispiel #4
0
    def test_self_thank(self):
        """Test that thanking oneself causes an error.

        This test is not in TestThankRevisionErrors because it may require
        making a successful edit in order to test the API call thanking
        the user running the test.
        """
        site = self.get_site()
        my_name = self.get_userpage().username
        data = site.usercontribs(user=my_name, total=1)
        for rev in data:
            revid = rev['revid']
            break
        else:
            test_page = Page(site, 'Pywikibot Thanks test')
            test_page.text += '* ~~~~\n'
            test_page.save('Pywikibot Thanks test')
            revid = test_page.latest_revision_id
        self.assertAPIError('invalidrecipient',
                            None,
                            site.thank_revision,
                            revid,
                            source='pywikibot test')
Beispiel #5
0
    def fromWikibase(cls: Type['_WbDataPage'], page_name: str,
                     site: Optional[DataSite]) -> '_WbDataPage':
        """
        Create a _WbDataPage from the JSON data given by the Wikibase API.

        :param page_name: page name from Wikibase value
        :param site: The Wikibase site
        """
        # TODO: This method signature does not match our parent class (which
        # takes a dictionary argument rather than a string). We should either
        # change this method's signature or rename this method.

        data_site = cls._get_data_site(site)
        page = Page(data_site, page_name)
        return cls(page, site)
Beispiel #6
0
def check_file(root, filename, mod):
    file_path = os.path.join(root, *filename)
    # filename = (parent, part, part.cfg)
    if len(filename) == 2:
        filename = ("", filename[0], filename[1])
    elif len(filename) == 3:
        filename = tuple(filename)
    else:
        raise Exception("Number of filename parts is neither 3 nor 2.")
    if mod == "Squad":
        mod = ""
    page_name = join_parts(mod, *filename)
    if os.path.getsize(file_path) > 1 << 20:
        print("Cancelled reading '{}' because it is larger than 1 MiB.".format(
            filename[1]))
        return
    with open(file_path, 'r') as f:
        content = f.read()
    target = Page(site, page_name)
    root_node = confignodereader.read(content)
    if root_node.name != "PART":
        raise Exception("Root node is not PART")
    part_name = root_node.get("title")
    new_page_content = generate_content(part_name, content)
    try:
        infobox, infobox_page, root_element = ksp_util.get_part_infobox(
            site, part_name)
    except pywikibot.NoPage as e:
        infobox_page = e.getPage()
        infobox = None
    new_infobox_page = not infobox
    require_type_parent_update = False
    if new_infobox_page:
        # Create new box template
        if infobox_page.title()[-4] != "/Box":
            raise Exception("Tried to create a infobox not in a box page")
        infobox_page.text = "<noinclude>{{Data template used}}<noinclude>\n{{Infobox/Part\n}}"
        infobox, root_element = ksp_util.extract_from_page(infobox_page)
        infobox.add("since", version)
        require_type_parent_update = "The infobox page is newly created."
        old_infobox_content = ""
    else:
        old_infobox_content = infobox_page.text
    parent = ksp_util.get_parent(infobox) or ""
    if target.exists():
        old_page_content = target.get()
        pywikibot.showDiff(old_page_content, new_page_content)
        if old_page_content != new_page_content:
            if pywikibot.inputChoice(
                    "Do you want to upload the new version of '{}'?".format(
                        target.title()), ['Yes', 'No'], ['y', 'n'],
                    'y') == 'y':
                target.text = new_page_content
                target.save(comment=comment_update)
    else:
        if infobox.has("part"):
            part = infobox.get("part")
        else:
            part = filename[1]
        source = Page(site, join_parts(mod, parent, part, "part.cfg"))
        if source.exists():
            if pywikibot.inputChoice(
                    "Move and update '{}' to '{}'?".format(
                        source.title(), target.title()), ['Yes', 'No'],
                ['y', 'n'], 'y') == 'y':
                source.move(
                    newtitle=target.title(),
                    reason="Renamed part configuration file after update.",
                    deleteAndMove=True)
                source.text = new_page_content
                source.save(comment=comment_update)
                require_type_parent_update = (
                    "The part.cfg has been moved, "
                    "but the infobox doesn't link to "
                    "it anymore.")
        else:
            if pywikibot.inputChoice(
                    "Create new page '{}'?".format(target.title()),
                ['Yes', 'No'], ['y', 'n'], 'y') == 'y':
                target.text = new_page_content
                target.save(comment=comment_new)
                require_type_parent_update = (
                    "The part.cfg has been created, "
                    "but the infobox doesn't link to "
                    "it yet.")

    if require_type_parent_update:
        # new probable type
        options = [("Manually", "m")]
        valid_types = None
        if filename[0]:
            types = ksp_util.reverse_maplist(ksp_util.type_map).get(
                filename[0])
            options += [("Parent", "p"), ("Type", "t")]
        print("The infobox for '{}' requires attention.".format(part_name))
        if len(options) == 1:
            print(require_type_parent_update)
            print(
                "The infobox can't be configured to point to the "
                "part.cfg. Please fix this manually.")
        else:
            #infobox must be updated: Either type or parent and maybe part
            answers, hotkeys = zip(*options)
            choice = pywikibot.inputChoice(
                require_type_parent_update + " What should be "
                "set?", answers, hotkeys, "m")
        if choice == "t":
            types += ["Other"]
            hotkeys = ["o"] + range(1, len(types))
            # which type?
            if len(types) > 1:
                type_idx = pywikibot.inputChoice(
                    "Choose the new type or 'other' if none "
                    "applies to this part.", types, hotkeys)
            else:
                type_idx = "o"
            if type_idx == "o":
                types = ["None"] + ksp_util.type_map.keys()
                hotkeys = ["n"] + range(1, len(all_types))
                type_idx = pywikibot.inputChoice(
                    "Choose the new type. It'll set the parent "
                    "directly if 'none' is chosen.", types, hotkeys)
            if type_idx != "n":
                new_type = types[int(type_idx)]
                infobox.add("type", new_type)
            else:
                choice = "p"

        if choice == "p":
            infobox.add("parent", filename[0])
        infobox.add("part", filename[1])
        if mod:
            mod_short = ksp_util.reverse_maplist(ksp_util.MOD_MAP)[mod][0]
            infobox.add("mod", mod_short)
    # default infobox values

    # manufacturer= requires manufacturer map

    # name pair: (infobox, config)
    default_pairs = [("costs", "cost"), ("mass", "mass"),
                     ("tolerance", "crashTolerance"), ("temp", "maxTemp")]
    for infobox_name, config_name in default_pairs:
        value = root_node.get(config_name)
        if value:
            infobox.add(infobox_name, value)
        else:
            print("Part definition doesn't contain a value for '{}'".format(
                config_name))
    # special: drag (maximum_drag and minimum_drag)
    min_drag = root_node.get("minimum_drag")
    max_drag = root_node.get("maximum_drag")
    if not min_drag and not max_drag:
        print("Part definition doesn't contain drag values.")
    else:
        min_drag = float((min_drag or max_drag).strip())
        max_drag = float((max_drag or min_drag).strip())
        if abs(min_drag - max_drag) < 0.001:
            infobox.add("drag", "{}".format(min_drag))
        else:
            infobox.add("drag", "{}-{}".format(min_drag, max_drag))
    # special: research
    research = root_node.get("TechRequired")
    if research:
        research_name = ksp_util.RESEARCH_MAP.get(research)
        if research_name:
            infobox.add("research", research_name)
        elif research != "Unresearcheable":  # ignore the "special value"
            print("Unknown research definition: {}".format(research))
    else:
        print("Part definition doesn't contain a research value.")
    infobox_page.text = unicode(root_element)
    if old_infobox_content != infobox_page.text:
        pywikibot.showDiff(old_infobox_content, infobox_page.text)
        if pywikibot.inputChoice(
                "Do you want to upload the new version of '{}'?".format(
                    infobox_page.title()), ['Yes', 'No'], ['y', 'n'],
                'y') == 'y':
            infobox_page.save(
                comment=comment_new if new_infobox_page else comment_update)
Beispiel #7
0
for arg in args:
    if arg == "--version" or arg == "-V":
        version = next(args, None)
        if version is None:
            print "Version without number"
    elif arg == "--directory" or arg == "-d":
        root_directory = next(args, None)
        if root_directory is None:
            print "Root directory not given"

if version is None:
    print("Version is not given.")
    #get version from Template:Check version/Cur and then ++
    current_version_match = re.search(
        r"<(only)?include(only)?>((?:[0-9]+\.)+[0-9]+)</(only)?include(only)?>.*",
        Page(site, "Template:Check version/Cur").get())
    if current_version_match:
        if current_version_match.group(1) != current_version_match.group(
                2) or current_version_match.group(
                    4) != current_version_match.group(5):
            parts = [str(x) for x in range(3)]
            increment_index = int(
                pywikibot.inputChoice(
                    "Which part of the version number was incremented?", parts,
                    parts, '1'))
            if increment_index in [int(x) for x in parts]:
                version = guess_next_version(current_version_match.group(3),
                                             increment_index)
                if pywikibot.inputChoice(
                        "Guessing next version number: {}?".format(version),
                    ['Yes', 'No'], ['y', 'n'], 'n') == 'n':
Beispiel #8
0
    if language_code is None:
      print "Found language code parameter but no definition"

if language_code is None:
  print("Language code is not given.")
  sys.exit(1)
elif not re.search("^[A-Za-z]{2}(-[A-Za-z]{2})?$", language_code):
  print("Invalid language code format. Must be XX or XX-XX!")
  sys.exit(1)
elif language_code.lower() != language_code:
  language_code = language_code.lower()
  print("Found capital letters in language code. Changed it to '{}'".format(language_code))

comment_new = "+added {}".format(language_code)

mw_langlink = Page(site, "MediaWiki:langlink-{}".format(language_code))
if mw_langlink.exists():
  print("'{}' already exists. Skipped creation.".format(mw_langlink.title()))
else:
  mw_langlink.text = "{{{{mw-langlink|{}}}}}".format(language_code)
  if pywikibot.inputChoice("Create '{}'?".format(mw_langlink.title()), ['Yes', 'No'], ['y', 'n'], 'n') == 'y':
    mw_langlink.save(comment=comment_new)
insert_alphabetically(Page(site, "MediaWiki:Sidebar"), r"^\*\* langlink-$code\|.*$", language_code, "** langlink-$code|{{subst:#language:$code}}", comment_new)

def optional_prefix(code, match=None):
  ret = Template("{{mp-lang|$code}}").substitute(code=code) + "\n"
  if match is None:
    return ret
  elif match.start() == 0:
    return ret + "&middot; "
  else:
Beispiel #9
0
 if m:
     print("==========================================")
     print("Working on page: '{}'".format(page.title()))
     part_name = m.group(1)
     parsed = mwparserfromhell.parse(page.text)
     templates = parsed.filter_templates()
     text_name = []
     for template in templates:
         if template.name == "Part config":
             if template.has("1"):
                 text_name += [template.get("1")]
             else:
                 text_name += [None]
     if len(text_name) == 1:
         text_name = str(text_name[0])
         box_page = Page(site, text_name + "/Box")
         if not box_page.exists():
             print("WARNING: The infobox page {} does not exist.".format(
                 box_page.title()))
         box_parent = extract_from_page(box_page)
         part_page = Page(site, text_name)
         part_parent = extract_from_page(part_page)
         if not box_parent and not part_parent:
             print("ERROR: Unable to determine part parent")
         elif bool(box_parent) != bool(
                 part_parent) or box_parent == part_parent:
             parent = box_parent or part_parent
             target = "Parts/{}/{}/part.cfg".format(parent, part_name)
             print("Parent: {}; Target: {}".format(parent, target))
             if Page(site, target).exists():
                 print(
Beispiel #10
0
def check_file(root, filename, mod):
    file_path = os.path.join(root, *filename)
    # filename = (parent, part, part.cfg)
    if len(filename) == 2:
        filename = ("", filename[0], filename[1])
    elif len(filename) == 3:
        filename = tuple(filename)
    else:
        raise Exception("Number of filename parts is neither 3 nor 2.")
    if mod == "Squad":
        mod = ""
    page_name = join_parts(mod, *filename)
    if os.path.getsize(file_path) > 1<<20:
        print("Cancelled reading '{}' because it is larger than 1 MiB.".format(filename[1]))
        return
    with open(file_path, 'r') as f:
        content = f.read()
    target = Page(site, page_name)
    root_node = confignodereader.read(content)
    if root_node.name != "PART":
        raise Exception("Root node is not PART")
    part_name = root_node.get("title")
    new_page_content = generate_content(part_name, content)
    try:
        infobox, infobox_page, root_element = ksp_util.get_part_infobox(site, part_name)
    except pywikibot.NoPage as e:
        infobox_page = e.getPage()
        infobox = None
    new_infobox_page = not infobox
    require_type_parent_update = False
    if new_infobox_page:
        # Create new box template
        if infobox_page.title()[-4] != "/Box":
            raise Exception("Tried to create a infobox not in a box page")
        infobox_page.text = "<noinclude>{{Data template used}}<noinclude>\n{{Infobox/Part\n}}"
        infobox, root_element = ksp_util.extract_from_page(infobox_page)
        infobox.add("since", version)
        require_type_parent_update = "The infobox page is newly created."
        old_infobox_content = ""
    else:
        old_infobox_content = infobox_page.text
    parent = ksp_util.get_parent(infobox) or ""
    if target.exists():
        old_page_content = target.get()
        pywikibot.showDiff(old_page_content, new_page_content)
        if old_page_content != new_page_content:
            if pywikibot.inputChoice("Do you want to upload the new version of '{}'?".format(target.title()), ['Yes', 'No'], ['y', 'n'], 'y') == 'y':
                target.text = new_page_content
                target.save(comment=comment_update)
    else:
        if infobox.has("part"):
            part = infobox.get("part")
        else:
            part = filename[1]
        source = Page(site, join_parts(mod, parent, part, "part.cfg"))
        if source.exists():
            if pywikibot.inputChoice("Move and update '{}' to '{}'?".format(source.title(), target.title()), ['Yes', 'No'], ['y', 'n'], 'y') == 'y':
                source.move(newtitle=target.title(), reason="Renamed part configuration file after update.", deleteAndMove=True)
                source.text = new_page_content
                source.save(comment=comment_update)
                require_type_parent_update =("The part.cfg has been moved, "
                                             "but the infobox doesn't link to "
                                             "it anymore.")
        else:
            if pywikibot.inputChoice("Create new page '{}'?".format(target.title()), ['Yes', 'No'], ['y', 'n'], 'y') == 'y':
                target.text = new_page_content
                target.save(comment=comment_new)
                require_type_parent_update =("The part.cfg has been created, "
                                             "but the infobox doesn't link to "
                                             "it yet.")

    if require_type_parent_update:
        # new probable type
        options = [("Manually", "m")]
        valid_types = None
        if filename[0]:
            types = ksp_util.reverse_maplist(ksp_util.type_map).get(filename[0])
            options += [("Parent", "p"), ("Type", "t")]
        print("The infobox for '{}' requires attention.".format(part_name))
        if len(options) == 1:
            print(require_type_parent_update)
            print("The infobox can't be configured to point to the "
                  "part.cfg. Please fix this manually.")
        else:
            #infobox must be updated: Either type or parent and maybe part
            answers, hotkeys = zip(*options)
            choice = pywikibot.inputChoice(
                        require_type_parent_update + " What should be "
                        "set?", answers, hotkeys, "m")
        if choice == "t":
            types += ["Other"]
            hotkeys = ["o"] + range(1, len(types))
            # which type?
            if len(types) > 1:
                type_idx = pywikibot.inputChoice(
                            "Choose the new type or 'other' if none "
                            "applies to this part.", types, hotkeys)
            else:
                type_idx = "o"
            if type_idx == "o":
                types = ["None"] + ksp_util.type_map.keys()
                hotkeys = ["n"] + range(1, len(all_types))
                type_idx = pywikibot.inputChoice(
                            "Choose the new type. It'll set the parent "
                            "directly if 'none' is chosen.", types, hotkeys)
            if type_idx != "n":    
                new_type = types[int(type_idx)]
                infobox.add("type", new_type)
            else:
                choice = "p"

        if choice == "p":
            infobox.add("parent", filename[0])
        infobox.add("part", filename[1])
        if mod:
            mod_short = ksp_util.reverse_maplist(ksp_util.MOD_MAP)[mod][0]
            infobox.add("mod", mod_short)
    # default infobox values

    # manufacturer= requires manufacturer map

    # name pair: (infobox, config)
    default_pairs = [("costs", "cost"), ("mass", "mass"),
                     ("tolerance", "crashTolerance"), ("temp", "maxTemp")]
    for infobox_name, config_name in default_pairs:
        value = root_node.get(config_name)
        if value:
            infobox.add(infobox_name, value)
        else:
            print("Part definition doesn't contain a value for '{}'".format(config_name))
    # special: drag (maximum_drag and minimum_drag)
    min_drag = root_node.get("minimum_drag")
    max_drag = root_node.get("maximum_drag")
    if not min_drag and not max_drag:
        print("Part definition doesn't contain drag values.")
    else:
        min_drag = float((min_drag or max_drag).strip())
        max_drag = float((max_drag or min_drag).strip())
        if abs(min_drag - max_drag) < 0.001:
            infobox.add("drag", "{}".format(min_drag))
        else:
            infobox.add("drag", "{}-{}".format(min_drag, max_drag))
    # special: research
    research = root_node.get("TechRequired")
    if research:
        research_name = ksp_util.RESEARCH_MAP.get(research)
        if research_name:
            infobox.add("research", research_name)
        elif research != "Unresearcheable":  # ignore the "special value"
            print("Unknown research definition: {}".format(research))
    else:
        print("Part definition doesn't contain a research value.")
    infobox_page.text = unicode(root_element)
    if old_infobox_content != infobox_page.text:
        pywikibot.showDiff(old_infobox_content, infobox_page.text)
        if pywikibot.inputChoice("Do you want to upload the new version of '{}'?".format(infobox_page.title()), ['Yes', 'No'], ['y', 'n'], 'y') == 'y':
            infobox_page.save(comment=comment_new if new_infobox_page else comment_update)
Beispiel #11
0
            else:
                print("Didn't checked '{}' because the subdirectory depth is already {}.".format(complete_path, len(sub)))

if root_directory is None:
    print("Parts directory is not given, don't update part configurations.")
else:
    for mod in ["Squad", "NASAmission"]:
        parts_directory = os.path.join(root_directory, mod, "Parts")
        if os.path.isdir(parts_directory):
            check_directory(parts_directory, mod, [])
        else:
            print("Parts director '{}' does not exists/is not a directory!".format(parts_directory))

# Update check version
if pywikibot.inputChoice("Should 'Template:Check version/Cur' be updated?", ['Yes', 'No'], ['y', 'n'], 'n') == 'y':
    check_version_cur = Page(site, "Template:Check version/Cur")
    check_version_cur.text = "<onlyinclude>{}</onlyinclude><noinclude>: Newest version available to buy. Needs to be updated when a new version gets released.</noinclude>".format(version)
    check_version_cur.save(comment=comment_update)
new_version = guess_next_version(version)
if pywikibot.inputChoice("Should 'Template:Check version/Rev' be updated?", ['Yes', 'No'], ['y', 'n'], 'n') == 'y':
    check_version_rev = Page(site, "Template:Check version/Rev")
    if check_version_rev.exists():
        if check_version_rev.get().find(new_version) < 0:
            print("Template:Check version/Rev already contains the version {}. Skipped.".format(new_version))
        else:
            lines = check_version_rev.get()
            match = re.compile(r"^\|{}=([1-9][0-9]*)$".format(version), re.M).search(lines)
            if match:
                index = match.end()
                check_version_rev.text = lines[:index] + "\n|{}={}".format(new_version, int(match.group(1)) + 1) + lines[index:]
                check_version_rev.save(comment=comment_update)
Beispiel #12
0
    sections = []
    for match in section_regex.finditer(content):
        depth = 0 if match.group('es') is None else len(match.group('es'))
        while len(super_sections) > 0 and peek(super_sections).depth > depth:
            super_section.pop()
        super_section = peek(super_sections)
        section = Section(match.group('title'), match.group('content'),
                          super_section, [], depth)
        if super_section is not None:
            super_section.parent += [section]
        sections += [section]
    return sections


# Automatically read the requests
requests = Page(site, "Project:Request Move#Requests")
#TODO: More general regex? Or manuall reading?
row_regex = re.compile(
    r"(?:\|\||\n\|)[ \t]*((?:\[\[.*\]\]|(?:\[\[.*\]\]|[^-}])(?:\[\[.*\]\]|[^|\n])*(?:\[\[.*\]\]|[^|\n ])))?"
)
sections = split_sections(requests.text)
print sections
for tested_section in sections:
    if tested_section.title == "Completed":
        section = tested_section.content
# Get line by line
for row in section.split("|-"):
    elements = [match.group(1) for match in row_regex.finditer(row)]
    if len(elements) == 2:
        elements += ['No reason given.']
    if len(elements) == 3:
Beispiel #13
0
 if m:
   print("==========================================")
   print("Working on page: '{}'".format(page.title()))
   part_name = m.group(1)
   parsed = mwparserfromhell.parse(page.text)
   templates = parsed.filter_templates()
   text_name = []
   for template in templates:
       if template.name == "Part config":
           if template.has("1"):
               text_name += [template.get("1")]
           else:
               text_name += [None]
   if len(text_name) == 1:
       text_name = str(text_name[0])
       box_page = Page(site, text_name + "/Box")
       if not box_page.exists():
           print("WARNING: The infobox page {} does not exist.".format(box_page.title()))
       box_parent = extract_from_page(box_page)
       part_page = Page(site, text_name)
       part_parent = extract_from_page(part_page)
       if not box_parent and not part_parent:
           print("ERROR: Unable to determine part parent")
       elif bool(box_parent) != bool(part_parent) or box_parent == part_parent:
           parent = box_parent or part_parent
           target = "Parts/{}/{}/part.cfg".format(parent, part_name)
           print("Parent: {}; Target: {}".format(parent, target))
           if Page(site, target).exists():
               print("Didn't moved '{}' to '{}' because it already exists".format(page.title(), target))
           else:
               print("Move '{}' to '{}'".format(page.title(), target))