Example #1
0
        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)
            else:
Example #2
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)
Example #3
0
      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:
    return "&middot; " + ret
Example #4
0
 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".
Example #5
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)
Example #6
0
    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)
            else:
                print("Template:Check version/Rev doesn't contain the current version. Skipped.")
    else:
        print("Template:Check version/Rev doesn't exists. Skipped.")
if pywikibot.inputChoice("Should a check version category for version {} be created?".format(new_version), ['Yes', 'No'], ['y', 'n'], 'n') == 'y':
    check_version_cat = Category(site, "Category:Check version/{}".format(new_version))
Example #7
0
 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))
             page.move(newtitle=target, reason="Update to new directory system", deleteAndMove=True)