Commits

Anonymous committed 3fd2ffb

refactor code into functions, use dictionaries instead of if/else for type stringing, escape html entities, pass file to dump_css instead of directory, rename some procedures

Comments (0)

Files changed (1)

 import re
 import pprint
 
+"""
+in the documentation, but not defined in gimpfu
+PF_REGION:"PF_REGION"
+"""
+VAR_TYPES = {PF_INT8:"PF_INT8",PF_INT16:"PF_INT16",PF_INT32:"PF_INT32",PF_INT:"PF_INT",PF_FLOAT:"PF_FLOAT",PF_STRING:"PF_STRING",PF_VALUE:"PF_VALUE",PF_COLOR:"PF_COLOR",PF_COLOUR:"PF_COLOUR",PF_IMAGE:"PF_IMAGE",PF_LAYER:"PF_LAYER",PF_CHANNEL:"PF_CHANNEL",PF_DRAWABLE:"PF_DRAWABLE",PF_TOGGLE:"PF_TOGGLE",PF_BOOL:"PF_BOOL",PF_RADIO:"PF_RADIO",PF_SLIDER:"PF_SLIDER",PF_SPINNER:"PF_SPINNER",PF_ADJUSTMENT:"PF_ADJUSTMENT",PF_FONT:"PF_FONT",PF_FILE:"PF_FILE",PF_BRUSH:"PF_BRUSH",PF_PATTERN:"PF_PATTERN",PF_GRADIENT:"PF_GRADIENT",PF_PALETTE:"PF_PALETTE"}
+
+PROC_TYPES = {PLUGIN:"PROC_PLUG_IN",EXTENSION:"PROC_EXTENSION",TEMPORARY:"PROC_TEMPORARY"}
+
 PROC_CSS = """html, body
 {
         width:100%;
 """
 
 def escape(html):
-    """
-    Returns the given HTML with ampersands, quotes and angle brackets encoded.
+        """
+        Returns the given HTML with ampersands, quotes and angle brackets encoded.
+        """
+        html = unicode(html)
+        html = html.replace('&', '&')
+        html = html.replace('<', '&lt;')
+        html = html.replace('>', '&gt;')
+        html = html.replace('"', '&quot;')
+        html = html.replace("'", '&#39;')
 
-    Based on code in: Django
-    """
-    html = force_unicode(html)
-    html = html.replace('&', '&amp;')
-    html = html.replace('<', '&lt;')
-    html = html.replace('>', '&gt;')
-    html = html.replace('"', '&quot;')
-    html = html.replace("'", '&#39;')
-
-    return html
+        return html.encode('ascii', 'ignore')
 
 def slugify(value):
-    """
-    Normalizes string, converts to lowercase, removes non-alpha characters,
-    and converts spaces to hyphens.
+        """
+        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
+        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
 
         return outDirectory
 
-def dump_css(outDirectory) :
-        # write css file
-        outFile = os.path.join(
-                outDirectory,
-                "proc.css"
-        )
+def dump_css(outFile) :
 
         with open(outFile,'w') as f:
                 f.write(PROC_CSS)
 
         return None
 
-def var_type_str(t) :
+def dump_var_type(t) :
+        if (t not in VAR_TYPES) :
+                return "unknown"
 
-        if (PF_INT8 == t) :
-                return "PF_INT8"
-        if (PF_INT16 == t) :
-                return "PF_INT16"
-        if (PF_INT32 == t) :
-                return "PF_INT32"
-        if (PF_INT == t) :
-                return "PF_INT"
-        if (PF_FLOAT == t) :
-                return "PF_FLOAT"
-        if (PF_STRING == t) :
-                return "PF_STRING"
-        if (PF_VALUE == t) :
-                return "PF_VALUE"
-        if (PF_COLOR == t) :
-                return "PF_COLOR"
-        if (PF_COLOUR == t) :
-                return "PF_COLOUR"
-        """
-        in the documentation, but not defined in gimpfu
-        if (PF_REGION == t) :
-                return "PF_REGION"
-        """
-        if (PF_IMAGE == t) :
-                return "PF_IMAGE"
-        if (PF_LAYER == t) :
-                return "PF_LAYER"
-        if (PF_CHANNEL == t) :
-                return "PF_CHANNEL"
-        if (PF_DRAWABLE == t) :
-                return "PF_DRAWABLE"
-        if (PF_TOGGLE == t) :
-                return "PF_TOGGLE"
-        if (PF_BOOL == t) :
-                return "PF_BOOL"
-        if (PF_RADIO == t) :
-                return "PF_RADIO"
-        if (PF_SLIDER == t) :
-                return "PF_SLIDER"
-        if (PF_SPINNER == t) :
-                return "PF_SPINNER"
-        if (PF_ADJUSTMENT == t) :
-                return "PF_ADJUSTMENT"
-        if (PF_FONT == t) :
-                return "PF_FONT"
-        if (PF_FILE == t) :
-                return "PF_FILE"
-        if (PF_BRUSH == t) :
-                return "PF_BRUSH"
-        if (PF_PATTERN == t) :
-                return "PF_PATTERN"
-        if (PF_GRADIENT == t) :
-                return "PF_GRADIENT"
-        if (PF_PALETTE == t) :
-                return "PF_PALETTE"
+        return VAR_TYPES[t]
 
-        return "unknown"
+def dump_proc_type(t) :
+        if (t not in PROC_TYPES) :
+                return "unknown"
 
-def proc_type_str(t) :
-        if (PLUGIN == t) :
-                return "PROC_PLUG_IN"
-        if (EXTENSION == t) :
-                return "PROC_EXTENSION"
-        if (TEMPORARY == t) :
-                return "PROC_TEMPORARY"
-
-        return "unknown"
+        return PROC_TYPES[t]
 
 def dump_table(lst) :
         row = dict()
                 row["class"] = ""
                 if (True == alt) :
                         row["class"] = ' class="alt"'
-                row["type"] = var_type_str(item[0])
-                row["name"] = item[1]
-                row["desc"] = item[2]
+                row["type"] = dump_var_type(item[0])
+                row["name"] = escape(item[1])
+                row["desc"] = escape(item[2])
 
                 html += Template(TR_HTML).safe_substitute(row)
                 alt = not alt
         d["index_link"] = links[1]
         d["next_link"] = links[2]
 
-        d["proc_name"] = proc.proc_name
-        d["proc_author"] = proc.proc_author
-        d["proc_date"] = proc.proc_date
-        d["proc_copyright"] = proc.proc_copyright
-        d["proc_blurb"] = proc.proc_blurb
-        d["proc_help"] = proc.proc_help
-        d["proc_type"] = proc_type_str(proc.proc_type)
+        d["proc_name"] = escape(proc.proc_name)
+        d["proc_author"] = escape(proc.proc_author)
+        d["proc_date"] = escape(proc.proc_date)
+        d["proc_copyright"] = escape(proc.proc_copyright)
+        d["proc_blurb"] = escape(proc.proc_blurb)
+        d["proc_help"] = escape(proc.proc_help)
+        d["proc_type"] = dump_proc_type(escape(proc.proc_type))
 
         d["parameters"] = dump_table(proc.params)
         d["results"] = dump_table(proc.return_vals)
 
         return None
 
+def fname(pname) :
+        return "%s.html"%(slugify(pname))
+
 def dump_pdb() :
         MessageBox = ctypes.windll.user32.MessageBoxA
 
                 return None
 
         outDirectory = prepare(root)
-        dump_css(outDirectory)
+        dump_css(os.path.join(
+                outDirectory,
+                "proc.css"
+        ))
 
         # loop through PDB
         procs = gimp.pdb.query()
         procs.sort()
-        for i in range(len(procs)):
+
+        links = [fname(procs[-1]), "../index.html", ""]
+        for i in range(len(procs)) :
                 name = procs[i]
                 outFile = os.path.join(
                         outDirectory,
-                        "%s.html"%(slugify(name))
+                        fname(name)
                 )
-                
-                links = list()
-                if (0 < i) :
-                        link = "%s.html"%(slugify(procs[i-1]))
-                        links.append(link)
-                else :
-                        links.append("")
-
-                links.append("../index.html")
-
-                if (len(procs) - 1 >  i) :
-                        link = "%s.html"%(slugify(procs[i+1]))
-                        links.append(link)
-                else :
-                        links.append("")
-
+                links[2] = fname(procs[(i+1) % len(procs)])
                 dump_proc(pdb[name], outFile, 'w+', links)
+                links[0] = os.path.basename(outFile)
                 #break
 
-        # write index file
+        # todo: write index file
 
         return None
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.