From 7b446bb38b9de1b7a24a7336c42247b83bf76805 Mon Sep 17 00:00:00 2001 From: Lakshmi Vyasarajan Date: Mon, 24 Jan 2011 15:42:58 +0530 Subject: [PATCH] Added context loaders --- hyde/engine.py | 3 +- hyde/generator.py | 5 ++- hyde/model.py | 25 ++++++++++++++- hyde/tests/test_generate.py | 63 ++++++++++++++++++++++++++++++++----- 4 files changed, 86 insertions(+), 10 deletions(-) diff --git a/hyde/engine.py b/hyde/engine.py index d1f53a5..f602f38 100644 --- a/hyde/engine.py +++ b/hyde/engine.py @@ -11,6 +11,7 @@ from hyde.site import Site from hyde.version import __version__ from hyde.util import getLoggerWithConsoleHandler +import codecs import os import yaml @@ -118,6 +119,6 @@ class Engine(Application): config_file = sitepath.child(config) logger.info("Reading site configuration from [%s]", config_file) conf = {} - with open(config_file) as stream: + with codecs.open(config_file, 'r', 'utf-8') as stream: conf = yaml.load(stream) return Site(sitepath, Config(sitepath, conf)) \ No newline at end of file diff --git a/hyde/generator.py b/hyde/generator.py index 428472c..065b6ef 100644 --- a/hyde/generator.py +++ b/hyde/generator.py @@ -4,6 +4,7 @@ The generator class and related utility functions. """ from hyde.exceptions import HydeException from hyde.fs import File, Folder +from hyde.model import Context from hyde.plugin import Plugin from hyde.template import Template @@ -24,7 +25,9 @@ class Generator(object): self.generated_once = False self.__context__ = dict(site=site) if hasattr(site.config, 'context'): - self.__context__.update(site.config.context) + self.__context__.update( + Context.load(site.sitepath, site.config.context)) + self.template = None Plugin.load_all(site) diff --git a/hyde/model.py b/hyde/model.py index 43fa6d7..b97623c 100644 --- a/hyde/model.py +++ b/hyde/model.py @@ -2,6 +2,8 @@ """ Contains data structures and utilities for hyde. """ +from hyde.fs import File, Folder +import yaml class Expando(object): """ @@ -38,7 +40,28 @@ class Expando(object): else: return primitive -from hyde.fs import File, Folder + +class Context(object): + """ + Wraps the context related functions and utilities. + """ + + @staticmethod + def load(sitepath, ctx): + """ + Load context from config data and providers. + """ + context = {} + try: + context.update(ctx.data.__dict__) + for provider_name, resource_name in ctx.providers.__dict__.items(): + res = File(Folder(sitepath).child(resource_name)) + if res.exists: + context[provider_name] = yaml.load(res.read_all()) + except AttributeError: + # No context data found + pass + return context class Config(Expando): diff --git a/hyde/tests/test_generate.py b/hyde/tests/test_generate.py index 2bf2ae5..c1b23c7 100644 --- a/hyde/tests/test_generate.py +++ b/hyde/tests/test_generate.py @@ -5,9 +5,9 @@ Use nose `$ nosetests` """ - from hyde.generator import Generator from hyde.fs import FS, File, Folder +from hyde.model import Config from hyde.site import Site from nose.tools import raises, with_setup, nottest @@ -90,16 +90,58 @@ class TestGenerator(object): l.write(l.read_all()) assert gen.has_resource_changed(resource) - def test_has_resource_changed(self): - site = Site(TEST_SITE) + def test_context(self): + site = Site(TEST_SITE, Config(TEST_SITE, config_dict={ + "context": { + "data": { + "abc": "def" + } + } + })) + text = """ +{% extends "base.html" %} + +{% block main %} + abc = {{ abc }} + Hi! + + I am a test template to make sure jinja2 generation works well with hyde. + {{resource.name}} +{% endblock %} +""" site.load() - site.config.context = { - "abc": "def" - } + resource = site.content.resource_from_path(TEST_SITE.child('content/about.html')) + gen = Generator(site) + resource.source_file.write(text) + gen.generate_all() + target = File(site.config.deploy_root_path.child(resource.name)) + assert "abc = def" in target.read_all() + + def test_context_providers(self): + site = Site(TEST_SITE, Config(TEST_SITE, config_dict={ + "context": { + "data": { + "abc": "def" + }, + "providers": { + "nav": "nav.yaml" + } + } + })) + nav = """ +main: + - home + - articles + - projects +""" text = """ {% extends "base.html" %} {% block main %} + {{nav}} + {% for item in nav.main %} + {{item}} + {% endfor %} abc = {{ abc }} Hi! @@ -107,9 +149,16 @@ class TestGenerator(object): {{resource.name}} {% endblock %} """ + File(TEST_SITE.child('nav.yaml')).write(nav) + site.load() resource = site.content.resource_from_path(TEST_SITE.child('content/about.html')) gen = Generator(site) resource.source_file.write(text) gen.generate_all() target = File(site.config.deploy_root_path.child(resource.name)) - assert "abc = def" in target.read_all() \ No newline at end of file + out = target.read_all() + print out + assert "abc = def" in out + assert "home" in out + assert "articles" in out + assert "projects" in out \ No newline at end of file