<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/python

"""
"""
import xmlrpclib, re, sys, time
from getopt import getopt

def help():
    print """\
    -h,--help    This help text.
    -u,--url     URL of the wiki to inject into
    -f,--file    supply a file to read page(s) from.
    -B,--batch   pages are separated by '-'*79 and the first line is the name
    -n,--dry-run actually do the updates
    -F,--force   do the updates even if it's already there (and equal)
    -v,--verbose verbose operation
    -b,--base    base page from which all pages are made subpages
    -r,--region  name to use in the delimiter regex
    [ pages ]
    """
    sys.exit(0)

def error(s):
    print &gt;&gt;sys.stderr, s
    sys.exit(0)

opt = {'u:': 'url=', 'r:': 'region=', 'b:': 'base=', 'f:': 'file=',
       'h': 'help', 'F': 'force', 'u': 'update', 'v': 'verbose',
       'B': 'batch'}

try:    opts, pages = getopt(sys.argv[1:], ''.join(opt.keys()), opt.values())
except: help()
#opts = [ (o[1] != '-' and o[1:] or opt[o[2:]], v or 1) for o,v in opts ]

batch, force, dry_run = False, False, False
region, base, fn, url, verb = '','','wiki.wok','http://localhost:8000/wiki', 0

for k,v in opts:
    if   k in ('--help',   '-h'): help()  # does not return
    elif k in ('--verbose','-v'): verb    = verb + 1
    elif k in ('--force',  '-F'): force   = True
    elif k in ('--dry-run','-n'): dry_run = True
    elif k in ('--batch',  '-B'): batch   = True
    elif k in ('--url',    '-u'): url     = v
    elif k in ('--region', '-r'): region  = v+' '
    elif k in ('--base',   '-b'): base    = v
    elif k in ('--file',   '-f'): fn      = v

if verb &gt; 2: print opts

if '?' not in url:
    url = url + '?action=xmlrpc2'
    print &gt;&gt;sys.stderr, 'assuming you meant', url

ab = u'##AUTOMATIC %sBEGINS\n\n' % region
ae = u'\n\n##AUTOMATIC %sENDS\n' % region

rr = re.compile(u"(?s)(?P&lt;before&gt;.*)"+ab+
                u"(?:~-(?P&lt;time&gt;\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d)-~\n\n)?"+
                u"(?P&lt;auto&gt;.*)"+
                ae+u"(?P&lt;after&gt;.*)",
                re.DOTALL)

try:            z = file(fn).read().decode('utf-8')
except IOError: error("Couldn't read file \"%s\"" % fn)
if not z:       error("File \"%s\" appears to be empty" % fn)

if batch:
    l = [ x.split('\n', 1) for x in z.split('-'*79+'\n') ]
else:
    try:    l = [(pages[0], z)]
    except: help()
    pages = False

try:   W = xmlrpclib.ServerProxy(url)
except IOError, e:
    print &gt;&gt;sys.stderr, str(e)
    sys.exit(5)

for name, page in l:
    if pages and name not in pages:
        continue

    if verb &gt; 1: print name

    while u'\n'*3 in page: page = page.replace(u'\n'*3, u'\n'*2)

    try:    it = W.getPage(base+name)
    except: it = ''

    if type(it) == dict :
        t = time.strftime(u'%F %T\n\n')
        rc = W.putPage(base+name, ab + t + page + ae)
        if verb: print 'N', rc, base+name
    else:
        mo = rr.match(it)
        if mo:
            d = mo.groupdict()
            diff = page != d['auto']
            if verb: print '=?'[diff], d['time'], base+name, len(page)

        else:
            d = {'before': it, 'after': '', 'auto': page, 'time': 'N/A'}
            diff = True
            if verb: print 'n', base+name

        if diff and verb &gt; 2:
            print d['auto']
            print '-'*79
            print page

        d['auto'] = page

        if not diff and not force: continue

        if dry_run: continue

        t = unicode(time.strftime('%F %T'))

        try: rc = W.putPage(base+name,
                            (u"%(before)s" % d)+ ab +
                            u'~-'+t+u'-~'+(u"\n\n%(auto)s" % d) +
                            ae + (u"%(after)s" % d))
        except Exception,e:
            print e
            rc = 0
        print 'U', ['ok!','KO!'][not rc], base+name
</pre></body></html>