import sys
import argparse

from ceterach.api import MediaWiki

from difflib import Differ

from parse_equipment import AUTOGEN_HEADER, AUTOGEN_FOOTER

parser = argparse.ArgumentParser()
parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), default=sys.stdin,
                    help='The file to read the new autogenerated contents from. Defaults to stdin.')
parser.add_argument('page', help='The page to update.')
parser.add_argument('-u', '--user', help='Your username on the wiki.')
parser.add_argument('-p', '--password', help='Your password on the wiki.')
parser.add_argument('-s', '--summary', default='', help='A summary of the edit.')
parser.add_argument('-m', '--minor', help='Mark this as a minor edit.', action='store_true')
parser.add_argument('--confirm', help='Confirm the page edit.', action='store_true')
args = parser.parse_args()

API_URL = 'http://dragonsdogma.wikia.com/api.php'

if args.confirm and not (args.user and args.password):
    print('\nError: Must provide username and password in order to confirm an edit to a page.\n')
    parser.print_help()
    exit(1)

api = MediaWiki(API_URL)
page = api.page(args.page)

current_revision = page.content
if not (AUTOGEN_HEADER in current_revision and AUTOGEN_FOOTER in current_revision):
    # It's safest to only update sections that have been previously autogenerated.
    print('Error: This page does not have the autogen header or footer.')
    exit(1)

header_index = current_revision.index(AUTOGEN_HEADER)
footer_index = current_revision.index(AUTOGEN_FOOTER) + len(AUTOGEN_FOOTER)
before = current_revision[:header_index]
after = current_revision[footer_index+1:]

updated = args.infile.read()
new_revision = before + updated + after

if current_revision == new_revision:
    print('Error: No edits have been made.')
    exit(1)

if not args.confirm:
    diff = Differ()
    result = diff.compare(current_revision.splitlines(keepends=True), new_revision.splitlines(keepends=True))
    sys.stdout.writelines(result)
    print('\n===========\nSummary:', args.summary)
    print('Run this command with --confirm to confirm this page edit.')
    exit(1)
else:
    api.login(args.user, args.password)
    page.edit(new_revision, args.summary or '', minor=args.minor)
    print('%s successfully updated.' % args.page)

Add a script to automatically update an autogenerated table.