[Moin-user] Re: moin wiki scripts wanted

Fredrik Lundh fredrik at pythonware.com
Fri Jan 27 23:14:02 EST 2006


Michael Matthews wrote:

> I was able to find script code to create pages.
>
> Looking for script code to:
>
> * load attachments.
> * retrieve page source.

if URL points to a page, URL?action=raw gives you the source, and
URL?action=AttachFile&do=get&target=NAME gives you attachment
NAME, so something like this should work:

import re, urllib

##
# Gets the page source for a given moinmoin page.

def get_source(url):
    action = "?action=raw"
    return urllib.urlopen(url + action).read()

##
# Gets the given attachment.  If target is None, returns "Attachments"
# page instead.

def get_attachment(url, target=None):
    action = "?action=AttachFile"
    if target:
        action = action + "&do=get&target=" + target
    return urllib.urlopen(url + action).read()

##
# Gets all linked attachments on a given page.

def get_used_attachments(url):
    attachments = {}
    for line in get_source(url).split("\n"):
        if line.startswith("attachment:"):
            target = line[11:].rstrip("\r\n")
            attachments[target] = get_attachment(url, target)
    return attachments

##
# Gets all linked attachments on a given page.

def get_all_attachments(url):
    attachments = {}
    p = re.compile("do=get&target=([^\"]+)\">")
    for target in p.findall(get_attachment(url)):
        attachments[target] = get_attachment(url, target)
    return attachments

</F>







More information about the Moin-user mailing list