Source

python-fu-dump / dump.py

Full commit
from gimpfu import *
from random import *
import gtk
import os
import collections
import ctypes
import unicodedata
import re

PROC_CSS = "body{width=100%;}\n"
PROC_TEMPLATE = ""
INDEX_TEMPLATE = ""

def slugify(value):
    """
    Normalizes string, converts to lowercase, removes non-alpha characters,
    and converts spaces to hyphens.

    Based on code in: Django
    """
    value = unicodedata.normalize('NFKD', unicode(value))
    value = value.encode('ascii', 'ignore')
    value = re.sub('[^\w\s-]', '', value).strip().lower()
    value = re.sub('[-\s]+', '-', value)
    return value

def get_dir() :
        MessageBox = ctypes.windll.user32.MessageBoxA
        response = ""

        # Check for new pygtk: this is new class in PyGtk 2.4
        if (gtk.pygtk_version < (2,3,90)) :
                MessageBox(None, "This plugin requires PyGtk 2.3.90 or later", "Error", 0)
                return response

        dialog = gtk.FileChooserDialog(
                "Dump to...",
                None,
                gtk.FILE_CHOOSER_ACTION_OPEN,
                (
                        gtk.STOCK_CANCEL,
                        gtk.RESPONSE_CANCEL,
                        gtk.STOCK_OPEN,
                        gtk.RESPONSE_OK
                )
        )
        dialog.set_default_response(gtk.RESPONSE_OK)
        dialog.set_action(gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER)

        if (dialog.run() == gtk.RESPONSE_OK) :
                response = dialog.get_filename()
        dialog.destroy()

        return response

def prepare(root) :
        # create sub directory if it doesnt exist
        outDirectory = os.path.join(
                        root,
                        "pdb"
        )

        if not os.path.exists(outDirectory):
                os.makedirs(outDirectory)

        return outDirectory

def dump_css(outDirectory) :
        # write css file
        outFile = os.path.join(
                outDirectory,
                "proc.css"
        )

        with open(outFile,'w') as f:
                f.write(PROC_CSS)

        return None

def dump_proc(proc, outFile, mode) :

        with open(outFile, mode) as f :
                f.write("author:\n%s\n"%proc.proc_author)
                f.write("date:\n%s\n"%proc.proc_date)
                f.write("cpy:\n%s\n"%proc.proc_copyright)
                f.write("blurb:\n%s\n"%proc.proc_blurb)
                f.write("help:\n%s\n"%proc.proc_help)

        return None

def dump_pdb() :
        MessageBox = ctypes.windll.user32.MessageBoxA

        root = get_dir()
        if("" == root) :
                return None

        outDirectory = prepare(root)
        dump_css(outDirectory)

        # loop through PDB
        procs = gimp.pdb.query()
        procs.sort()
        for i in range(len(procs)):
                name = procs[i]
                outFile = os.path.join(
                        outDirectory,
                        "%s%s"%(slugify(name), ".html")
                )

                dump_proc(pdb[name], outFile, 'w+')

        # write index file

        return None

register(
        #name
        "dump-pdb",
        #blurb
        "dumps pdb database to HTML",
        #help
        "man up buttercup",
        #author
        "codefox",
        #copyright
        "MIT license => rtfm",
        #date
        "Jan 29, 2013",
        #menu path
        "<Toolbox>/Help/Procedure Dump",
        #image types
        None,
        #params
        [],
        #results
        [],
        #function
        dump_pdb
)

main()