#!/usr/bin/env python """ Replace the contents of a page on a MoinMoin wiki. Adam Sampson ~/.wikiputrc should contain sections like: [wiki] baseurl = http://foo.com/wiki/ username = joe password = xyz123 """ import os, sys, ConfigParser, mechanize, urllib2, getopt def upload(wiki, page, content, comment = "Automatic upload"): cp = ConfigParser.ConfigParser() cp.read(os.getenv("HOME") + "/.wikiputrc") b = mechanize.Browser() b.set_handle_robots(False) url = "%s%s?action=edit" % (cp.get(wiki, "baseurl"), page) pm = urllib2.HTTPPasswordMgrWithDefaultRealm() pm.add_password(None, url, cp.get(wiki, "user"), cp.get(wiki, "password")) b.set_credentials(pm) b.open(url) assert b.viewing_html() b.select_form(nr = 0) b["savetext"] = content b["comment"] = comment resp = b.submit(name = "button_save") assert b.viewing_html() def usage(): print "Usage: wikiput [OPTION ...] WIKI PAGE [FILE]" print "-c COMMENT Set comment for upload (default '')" print "-h, --help Show usage" sys.exit(1) def main(args): try: opts, args = getopt.getopt(args, "c:h", ["help"]) except getopt.GetoptError: usage() comment = "" for (o, a) in opts: if o in ("-h", "--help"): usage() elif o == "-c": comment = a if len(args) not in (2, 3): usage() if len(args) == 2: args += ["-"] (wiki, page, file) = args if file == "-": content = sys.stdin.read() else: content = open(file).read() upload(wiki, page, content, comment) if __name__ == "__main__": main(sys.argv[1:])