Commits

John Mulligan committed 3f49883

stubbing in a bunch of stuff

Comments (0)

Files changed (9)

muyhomepage2/app/__init__.py

Empty file added.

muyhomepage2/app/build.py

+
+
+import jinja2
+
+from muyhomepage2 import site
+from muyhomepage2 import page
+from muyhomepage2 import util
+from muyhomepage2.pagehandlers.handler import handle_action
+from muyhomepage2.app import configuration
+
+ENCODING = 'utf-8'
+
+class TemplateConfig(configuration.Config):
+    def __init__(self):
+        configuration.Config.__init__(self)
+        self._jenv = jinja2.Environment()
+
+    def templateloader(self):
+        return jinja2.FileSystemLoader('/home/john/projects/newgenerator/skins/default')
+
+    def templateenv(self):
+        return self._jenv
+
+
+def build(config):
+    website = site.Site()
+    rebuild = set()
+    for pagefile in page.walkpages(config.root()):
+        if pagefile not in (p.filename for p in website.pages):
+            npage = page.parse(pagefile)
+            rebuild.add(npage)
+            website.pages.add(npage)
+        else:
+            cpage = website.getpagefile(pagefile)
+            if cpage.contentcode != util.contentcode(pagefile):
+                rebuild.add(pagefile)
+    for spage in website.pages:
+        if spage.action in config.always_rebuild():
+            rebuild.add(page.parse(spage.filename))
+    compile_pages(config, website, rebuild)
+
+
+def compile_pages(config, website, rebuild):
+    for page in rebuild:
+        phandler = handle_action(
+            config.handlers(),
+            config.defaulthandler(),
+            page)
+        compile_page(config, website, phandler)
+
+
+def compile_page(config, website, pctx):
+    print 'XXX', pctx.page.filename
+    tname = pctx.template()
+    scope = dict(current=pctx, site=website, config=config)
+    template = config.templateloader().load(config.templateenv(), tname)
+    fp = open(os.path.join(config.destination(), pctx.finalname()), 'w')
+    try:
+        fp.write(template.render(scope).encode(ENCODING))
+    finally:
+        fp.close()
+            
+
+if __name__ == '__main__':
+    import os
+    os.chdir('/home/john/fun/site')
+    config = TemplateConfig()
+    build(config)

muyhomepage2/app/configuration.py

+
+from muyhomepage2.pagehandlers.standard import StandardHandler
+
+
+DEFAULT_HANDLERS = {
+    'standard': StandardHandler,
+    'blog': StandardHandler,
+    'tagsummary': StandardHandler,
+    'tagglobal': StandardHandler,
+    'software': StandardHandler,
+    'atom': StandardHandler,
+    'rss2': StandardHandler,
+    }
+
+
+class Config(object):
+
+    def __init__(self):
+        pass
+
+    def root(self):
+        return '.'
+
+    def destination(self):
+        return '/tmp/garbage'
+
+    def handlers(self):
+        """Return the handlers registry
+        """
+        return dict(DEFAULT_HANDLERS)
+
+    def defaulthandler(self):
+        return StandardHandler
+    
+    def always_rebuild(self):
+        return []
+

muyhomepage2/page.py

 import os
 import email.parser
 
+from muyhomepage2 import util
+
 
 def parse(name):
     """Parses a page file and returns a Page object.
     @property
     def title(self):
         return self.headers.get('title')
+
+    @property
+    def action(self):
+        return self.headers.get('action')
+
+    @property
+    def contentcode(self):
+        util.contentcode(self.filename)
+
+
+class PseduoPage(object):
+    def __init__(self, filename, contentcode, title, action):
+        self.filename = filename
+        self.contentcode = contentcode
+        self.title = title
+        self.action = action

muyhomepage2/pagehandlers/__init__.py

Empty file added.

muyhomepage2/pagehandlers/handler.py

+
+from muyhomepage2 import util
+
+class PageHandler(object):
+    def __init__(self, page):
+        self.page = page
+
+    def content(self):
+        raise NotImplementedError('PageHandler.content')
+
+    def finalname(self):
+        return '%s.html' % util.encodetitle(self.page.title)
+
+    def template(self):
+        return None
+
+
+def handle_action(registry, default, page):
+    """ Return a PageHandler object based on the given ``page``'s action
+    header. If the action header is not present construct a handler
+    based on the ``default``. The value of the action header must map
+    to a key in the ``registry`` otherwise.
+    """
+    if 'action' not in page.headers:
+        handler = default
+    else:
+        handler = registry[page.headers['action']]
+    return handler(page)

muyhomepage2/pagehandlers/standard.py

+
+from muyhomepage2.pagehandlers import handler
+
+class StandardHandler(handler.PageHandler):
+    def content(self):
+        return # text.rstwiki.format(self.page.body)
+
+    def template(self):
+        return 'page.html'

muyhomepage2/site.py

+
+
+class Site(object):
+    def __init__(self):
+        self.statemap = {}
+        self.tagmap = {}
+        self.pages = set()
+
+    def getpagefile(self, filename):
+        for page in self.pages:
+            if page.filename == filename:
+                return page
+        raise KeyError(filename)

muyhomepage2/util.py

     while temp[-1] == '-':
         temp = temp[:-1]
     return ''.join(temp)
+
+
+def contentcode(filename):
+    csum = md5()
+    fp = open(filename)
+    try:
+        while True:
+            buf = fp.read(8192)
+            if not buf:
+                break
+            csum.update(buf)
+    finally:
+        fp.close()
+    return csum.hexdigest()