示例#1
0
文件: libinfo.py 项目: nullzero/wp
def getdat(key=None, filename=None, wikipage=None):
    """
    Return value of given key, but if key is not given, return dict instead.
    This function will look at file first, but if file doesn't exist,
    it will look at wiki page later.
    """
    key = preload.enunicode(key)
    if filename:
        fullfilename = os.path.join(preload.dirname, filename + ".cfg")
        if os.path.exists(fullfilename):
            with open(fullfilename, "r") as f:
                text = preload.enunicode(f.read())
            
            xdata = extract(key, text)
            if xdata:
                return xdata
    
    if wikipage and wikipage.exists():
        return extract(key, wikipage.get())
示例#2
0
文件: libinfo.py 项目: nullzero/wp
def putdat(key, value, filename=None, wikipage=None):
    """
    Save value of given key. Saving will be operated with any I/O methods
    which are given
    """
    key = preload.enunicode(key)
    value = preload.enunicode(value)
    
    if filename:
        fullfilename = os.path.join(preload.dirname, filename + ".cfg")
        text = u""
        try:
            with open(fullfilename, "r") as f:
                text = preload.enunicode(f.read())
        except IOError:
            pass
            
        with open(fullfilename, "w") as f:
            f.write(preload.deunicode(changeValue(key, value, text)))

    if wikipage:
        wikipage.put(changeValue(key, value, wikipage.get()), u"ปรับปรุงข้อมูล")
示例#3
0
文件: liblang.py 项目: nullzero/wp
def randomCheckRepetition(s):
    """Helper function to check repetition of substring."""
    s = preload.enunicode(s)
    s = s.replace(u"\n", u"").replace(u"", u"\n")
    size = len(s)
    begin = random.randint(0, max(0, size - 700*2))
    end = min(begin + 700*2, size)
    if not s[begin:end]:
        return 0
    p = Popen([preload.File(__file__, "rle2")],
                stdout=PIPE, stdin=PIPE, stderr=STDOUT)
    output = p.communicate(input=s[begin:end])[0]
    return int(output)
示例#4
0
文件: libinfo.py 项目: nullzero/wp
def extract(key, text):
    """
    Extract key from text. Note that both key and text must be enunicode
    strings.
    """
    if key:
        try:
            dat = preload.enunicode(re2.find(u"(?m)^\* " + re2.escape(key) + 
                                    u": (.*?)$", text, 1))
        except AttributeError:
            dat = None
    else:
        lines = text.strip().split(u"\n")
        dat = {}
        for line in lines:
            key, value = line.split(u": ")
            dat[key] = value
            
    return dat
示例#5
0
文件: libservice.py 项目: nullzero/wp
def service(serviceTitle, operation, verifyFunc, datwiki, site, summary, 
            debug=False):
    """
    Get:
        Title of service"s page
        Key to read from config page,
        Function to verify user
        Config page, site
        Summary function.

    Function:
        Clear service"s page

    Return:
        Header of table
        List of rows
        Suspicious entry(/row)
    """
    page = pywikibot.Page(site, serviceTitle)
    datwiki = pywikibot.Page(site, datwiki)
    lastrev = int(libinfo.getdat(key = operation, wikipage = datwiki))
    oldcontent = page.get()
    header, table = libwikitable.wiki2table(oldcontent)
    disable = [False] * len(table)
    hist = page.getVersionHistory()
    # There is no need to get all revisions, just 500 is fine (by default).
    histlist = []

    for version in hist:
        histlist.append((version, page.getOldVersion(version[0])))
        if version[0] == lastrev:
            break
    hist = histlist
    hist.reverse()
    pywikibot.output(u"Processing %d revision(s)" % len(hist))
    for i in xrange(len(hist) - 1):
        oldv = hist[i][1]
        newv = hist[i + 1][1]
        usernew = hist[i + 1][0][2]
        dummy, cold = libwikitable.wiki2table(oldv)
        dummy, cnew = libwikitable.wiki2table(newv)
        oldvc = set([preload.enunicode(x) for x in cold])
        newvc = set([preload.enunicode(x) for x in cnew])
        difference = [eval(x) for x in (newvc - oldvc)]
        if not verifyFunc(usernew):
            for entry in difference:
                for cnt, fentry in enumerate(table):
                    if entry == fentry:
                        disable[cnt] = True
                        break

    newcontent = re2.sub(ur"(?ms)^(\!.*?$\n).*?(^\|\})", ur"\1\2", oldcontent)

    if oldcontent != newcontent:
        if not debug:
            page = pywikibot.Page(site, page.title())
            page.put(newcontent, summary())
        
        print page.getVersionHistory()[0][0]
        libinfo.putdat(key=operation, 
                        value=page.getVersionHistory()[0][0], 
                        wikipage=datwiki)
                        
    return header, table, disable