Commits

Anonymous committed c3aeaea

dynamically generate index headings

Comments (0)

Files changed (1)

 from gimpfu import *
 from string import Template
-from datetime import datetime, date, time
+from datetime import datetime
+from collections import *
 import gtk
 import os
-import collections
 import ctypes
 import unicodedata
 import re
-import pprint
+from pprint import pprint, pformat
 
 PROC_CSS = """html, body
 {
                                 </span>
                         </div>
                 </div>
+                <a href="#top"></top>
                 ${body}
         </body>
 </html>
 
         return Template(TABLE_HTML).safe_substitute({"body":html})
 
-def dump_proc(proc, outFile, mode, links) :
-        d = dict()
-
-        d["prev_link"] = links[0]
-        d["index_link"] = links[1]
-        d["next_link"] = links[2]
-
-        d["proc_name"] = escape(proc.proc_name)
-
-        d["proc_author"] = ""
-        authors = proc.proc_author.split(',')
-        for author in authors :
-                d["proc_author"] += "%s<br>"%escape(author.strip())
-
-        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"] = "<p>None</p>"
-        if (0 != proc.nparams) :
-                d["parameters"] = make_table(proc.params)
-
-        d["results"] = "<p>None</p>"
-        if (0 != proc.nreturn_vals) :
-                d["results"] = make_table(proc.return_vals)
-
-        with open(outFile, mode) as f :
-                f.write(Template(PROC_HTML).safe_substitute(d))
-
-        return None
-
 def file_name(name) :
         return "%s.html"%(slugify(name))
 
         if (0 == count) :
                 return ""
 
-
-
         return buf
 
-def headings(procs, root, delimiter="-", minCount=10, minDepth=0):
+def insert(root, path):
 
-        MessageBox = ctypes.windll.user32.MessageBoxA
+        for part in path:
+                ptr = None
+                for tup in root[1]:
+                        if (tup[0] == part):
+                                ptr = tup
+                                break
 
-        hdls = list()
-        namespaces = dict()
+                if None == ptr:
+                        ptr = (part, list())
+                        root[1].append(ptr)
+                        root[1].sort(key=lambda tup: tup[0])
 
-        # initialize namespaces
-        for ns in procs:
-                namespaces[ns] = 1;
+                root = ptr
 
-        with open(os.path.join(root,"fu.log"), "w+") as f :
+def size(root):
 
-                loop = 0
-                # while namespaces can be further reduced
-                while 0 != len(namespaces):
-                        loop += 1
+        children = 0
+        unvisited = deque()
+        unvisited.append(root)
 
-                        f.write("loop:%d\n"%(loop))
-                        f.write("\tbegin\n")
-                        f.write("\thndls:\n%s\n"%(str(hdls)))
-                        f.write("\tnamespaces:\n%s\n"%(str(namespaces)))
+        while 0 != len(unvisited):
+                node = unvisited.popleft()
+                children += 1
+                for tup in node[1]:
+                        unvisited.append(tup)
 
-                        # append any namespace with >= minCount entries
-                        hdls.extend(filter(lambda ns: ns not in hdls,filter(lambda ns: namespaces[ns] >= minCount, namespaces)))
-                        # remove items from namespaces
-                        namespaces = dict((k, v) for (k, v) in namespaces.items() if(v < minCount))
+        return children
 
-                        f.write("\tafter minCount\n")
-                        f.write("\thndls:\n%s\n"%(str(hdls)))
-                        f.write("\tnamespaces:\n%s\n"%(str(namespaces)))
+def namespaces(root, delimiter="-", minSize=10):
 
-                        # append any namespace that cant be decreased further
-                        hdls.extend(filter(lambda ns: ns not in hdls,filter(lambda ns: minDepth >= ns.count(delimiter),namespaces)))
-                        # remove items from namespaces
-                        namespaces = dict((k, v) for (k, v) in namespaces.items() if(minDepth < k.count(delimiter)))
+        ns_list = list()
+        unvisited = list()
+        visited = dict()
 
-                        f.write("\tafter minDepth\n")
-                        f.write("\thndls:\n%s\n"%(str(hdls)))
-                        f.write("\tnamespaces:\n%s\n"%(str(namespaces)))
+        unvisited.append(root)
 
-                        # decrease all namespaces by one indent
-                        tmp = dict()
-                        for ns in namespaces:
-                                ns = reduce(lambda x,y: x + delimiter + y, ns.split(delimiter)[:-1])
-                                if(ns in tmp):
-                                        tmp[ns] += 1
-                                else:
-                                        tmp[ns] = 1
+        while 0 != len(unvisited):
 
-                        namespaces = tmp
+                node = unvisited[-1]
+                namespace = delimiter.join(map(lambda tup: tup[0], unvisited))
 
-                        f.write("\tafter decrease\n")
-                        f.write("\tnamespaces:\n%s\n"%(str(namespaces)))
+                for child in node[1]:
+                        childspace = namespace + delimiter + child[0]
+                        if(childspace not in visited):
+                                visited[childspace] = True
+                                unvisited.append(child)
+                                break
 
-        hdls.sort(reverse=True)
-        MessageBox(0,"len:%d\nout:\n%s\n"%(len(hdls),str(hdls)),"title",0)
+                if(node == unvisited[-1]):
+                        # post fix, no new unvisited children found
+                        if(size(node) > minSize):
+                                ns_list.append(namespace)
+                        elif (node == root):
+                                ns_list.append(namespace)
 
-        return map(lambda x: x + "-", hdls);
+                        unvisited.pop()
 
-def index(procs, root) :
+        return ns_list
+
+def headings(procs, delimiter="-", minSize=10):
+
+        root = ("root", list())
+
+        for proc in procs:
+                insert(root, proc.split(delimiter))
+
+        heading_list = list()
+        for node in root[1]:
+                heading_list.extend(namespaces(node, delimiter, minSize))
+
+        return map(lambda s: s + delimiter,heading_list)
+
+def index(procs) :
+
         buf = ""
-        for heading in headings(procs, root) :
+
+        for heading in headings(procs) :
 
                 buf += plain_table(filter(lambda name: name.startswith(heading),procs), heading)
                 procs = filter(lambda name: not name.startswith(heading),procs)
 
         buf += plain_table(procs)
 
-
         return buf
 
 def dump_pdb() :
         procs.sort()
 
         with open(os.path.join(root,"index.html"), "w+") as f :
-                f.write(Template(INDEX_TEMPLATE).safe_substitute({"date":datetime.now().strftime("%A, %d. %B %Y %I:%M%p"),"body":index(procs, root)}))
+                f.write(Template(INDEX_TEMPLATE).safe_substitute({"date":datetime.now().strftime("%A, %d. %B %Y %I:%M%p"),"body":index(procs)}))
 
-
-        """
-        links = [fname(procs[-1]), "../index.html", ""]
-        for i in range(len(procs)) :
-                name = procs[i]
-                outFile = os.path.join(
-                        outDirectory,
-                        fname(name)
-                )
-                links[2] = fname(procs[(i+1) % len(procs)])
-                dump_proc(pdb[name], outFile, 'w+', links)
-                links[0] = os.path.basename(outFile)
-                #break
-
-        # todo: write index file
-        """
         return None
 
 register(
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.