User:Robert Ullmann/code/update


 * 1) !/usr/bin/python
 * 2) -*- coding: utf-8  -*-
 * 3) wikipath en wiktionary User:Robert_Ullmann/code/update

""" update code copies on en.wikt writes all .py files to wikt, if not changed, wiki page will not change """

import wikipedia import xmlreader import sys import socket import re from time import * import os import codecs

def files: """   return a list of all .py files that are candidates    each is relative path    """

dirs = [ ".", "../autoformat", "../iwikt", "../tbot" ]

rval = [ ] # return list, not generator

reoldn = re.compile(r'-\d+.py')

for dir in dirs: for dp, dn, fns in os.walk(dir): for fn in fns: # print "os walk", fn               if not fn.endswith(".py"): continue if reoldn.search(fn): continue if "_family" in fn: continue if "_interface" in fn: continue rval.append(dir + '/' + fn)

return(rval)

def update:

site = wikipedia.getSite("en", "wiktionary")

repath = re.compile("^# wikipath *(en) *(wiktionary) *(.*)$") # only en.wikt for now, others require setting site, just a bit more code (-)

for fn in files: print "%s:" % fn

mo = None try: with codecs.open(fn, "r", "UTF-8") as f:                for line in f:                     mo = repath.match(line) if mo: break except Exception, e:            print "    exception opening file", repr(e) continue if not mo: continue

print "   wikipath %s" % mo.group(3)

page = wikipedia.Page(site, mo.group(3)) try: old = page.get except wikipedia.NoPage: old = '' pass except Exception, e:            print "    exception reading page", repr(e) continue

text = u"""

"

try: with codecs.open(fn, "r", "UTF-8") as f:                for line in f:                    # if "python" in line: print repr(line) if line[0] == u'\ufeff': line = line[1:] text += line except Exception, e:            print "    exception opening file", repr(e) continue

text = text.rstrip(' \r\n') + "" + "\n" text = text.replace('\r', '')

if text.strip(' \r\n') == old.strip(' \r\n'): print("   no changes") continue

try: page.put(text, comment = "code update") except wikipedia.NoPage: pass except Exception, e:            print "    exception writing page", repr(e) continue

if __name__ == "__main__": try: update except KeyboardInterrupt: print "(keyboard interrupt)" # mostly just suppress traceback finally: wikipedia.stopme