@@ -1,6 +1,6 @@ | |||||
The MIT License | The MIT License | ||||
Copyright (c) 2009 - 2010 Lakshmi Vyasarajan, Ringce.com | |||||
Copyright (c) 2009 - 2011 Lakshmi Vyasarajan, Ringce.com | |||||
Permission is hereby granted, free of charge, to any person obtaining a copy | Permission is hereby granted, free of charge, to any person obtaining a copy | ||||
of this software and associated documentation files (the "Software"), to deal | of this software and associated documentation files (the "Software"), to deal | ||||
@@ -21,18 +21,21 @@ def filter_method(item, settings=None): | |||||
Returns true if all the filters in the | Returns true if all the filters in the | ||||
given settings evaluate to True. | given settings evaluate to True. | ||||
""" | """ | ||||
all_match = item.is_processable | |||||
if all_match and settings and hasattr(settings, 'filters'): | |||||
filters = settings.filters | |||||
for field, value in filters.__dict__.items(): | |||||
try: | |||||
res = attrgetter(field)(item) | |||||
except: | |||||
res = None | |||||
if res != value: | |||||
all_match = False | |||||
break | |||||
all_match = True | |||||
default_filters = {} | |||||
filters = {} | |||||
if hasattr(settings, 'filters'): | |||||
filters.update(default_filters) | |||||
filters.update(settings.filters.__dict__) | |||||
for field, value in filters.items(): | |||||
try: | |||||
res = attrgetter(field)(item) | |||||
except: | |||||
res = None | |||||
if res != value: | |||||
all_match = False | |||||
break | |||||
return all_match | return all_match | ||||
def sort_method(node, settings=None): | def sort_method(node, settings=None): | ||||
@@ -5,7 +5,7 @@ Jinja template utilties | |||||
from hyde.fs import File, Folder | from hyde.fs import File, Folder | ||||
from hyde.template import HtmlWrap, Template | from hyde.template import HtmlWrap, Template | ||||
from hyde.util import getLoggerWithNullHandler | |||||
from hyde.util import getLoggerWithNullHandler, getLoggerWithConsoleHandler | |||||
from jinja2 import contextfunction, Environment, FileSystemLoader | from jinja2 import contextfunction, Environment, FileSystemLoader | ||||
from jinja2 import environmentfilter, Markup, Undefined, nodes | from jinja2 import environmentfilter, Markup, Undefined, nodes | ||||
@@ -276,8 +276,7 @@ class HydeLoader(FileSystemLoader): | |||||
""" | """ | ||||
Calls the plugins to preprocess prior to returning the source. | Calls the plugins to preprocess prior to returning the source. | ||||
""" | """ | ||||
hlogger = getLoggerWithNullHandler('HydeLoader') | |||||
hlogger.debug("Loading template [%s] and preprocessing" % template) | |||||
logger.debug("Loading template [%s] and preprocessing" % template) | |||||
(contents, | (contents, | ||||
filename, | filename, | ||||
date) = super(HydeLoader, self).get_source( | date) = super(HydeLoader, self).get_source( | ||||
@@ -16,6 +16,8 @@ from hyde.exceptions import HydeException | |||||
from hyde.util import getLoggerWithNullHandler | from hyde.util import getLoggerWithNullHandler | ||||
logger = getLoggerWithNullHandler('hyde.server') | logger = getLoggerWithNullHandler('hyde.server') | ||||
from datetime import datetime | |||||
class HydeRequestHandler(SimpleHTTPRequestHandler): | class HydeRequestHandler(SimpleHTTPRequestHandler): | ||||
""" | """ | ||||
Serves files by regenerating the resource (or) | Serves files by regenerating the resource (or) | ||||
@@ -24,11 +26,12 @@ class HydeRequestHandler(SimpleHTTPRequestHandler): | |||||
def do_GET(self): | def do_GET(self): | ||||
""" | """ | ||||
Idenitfy the requested path. If the query string | |||||
Identify the requested path. If the query string | |||||
contains `refresh`, regenerat the entire site. | contains `refresh`, regenerat the entire site. | ||||
Otherwise, regenerate only the requested resource | Otherwise, regenerate only the requested resource | ||||
and serve. | and serve. | ||||
""" | """ | ||||
self.server.request_time = datetime.now() | |||||
logger.info("Processing request:[%s]" % self.path) | logger.info("Processing request:[%s]" % self.path) | ||||
result = urlparse.urlparse(self.path) | result = urlparse.urlparse(self.path) | ||||
query = urlparse.parse_qs(result.query) | query = urlparse.parse_qs(result.query) | ||||
@@ -41,10 +44,7 @@ class HydeRequestHandler(SimpleHTTPRequestHandler): | |||||
logger.info('Redirecting...[%s]' % new_url) | logger.info('Redirecting...[%s]' % new_url) | ||||
self.redirect(new_url) | self.redirect(new_url) | ||||
else: | else: | ||||
try: | |||||
SimpleHTTPRequestHandler.do_GET(self) | |||||
except HydeException: | |||||
self.do_404() | |||||
SimpleHTTPRequestHandler.do_GET(self) | |||||
def translate_path(self, path): | def translate_path(self, path): | ||||
@@ -55,7 +55,7 @@ class HydeRequestHandler(SimpleHTTPRequestHandler): | |||||
path = SimpleHTTPRequestHandler.translate_path(self, path) | path = SimpleHTTPRequestHandler.translate_path(self, path) | ||||
site = self.server.site | site = self.server.site | ||||
result = urlparse.urlparse(self.path) | result = urlparse.urlparse(self.path) | ||||
logger.info("Trying to load file based on request:[%s]" % result.path) | |||||
logger.debug("Trying to load file based on request:[%s]" % result.path) | |||||
path = result.path.lstrip('/') | path = result.path.lstrip('/') | ||||
if path.strip() == "" or File(path).kind.strip() == "": | if path.strip() == "" or File(path).kind.strip() == "": | ||||
return site.config.deploy_root_path.child(path) | return site.config.deploy_root_path.child(path) | ||||
@@ -64,18 +64,19 @@ class HydeRequestHandler(SimpleHTTPRequestHandler): | |||||
if not res: | if not res: | ||||
# Cannot find the source file using the given path. | |||||
# Check if the target file exists in the deploy folder. | |||||
# Check if its a new request | |||||
if self.server.request_time > self.server.regeneration_time: | |||||
# Cannot find the source file using the given path. | |||||
# Check if the target file exists in the deploy folder. | |||||
# this file is probably new or being generated by a plugin. | |||||
# lets not try too hard, just regenerate | |||||
logger.info("Attempting regeneration for:[%s]" % path) | |||||
self.server.regenerate() | |||||
res = site.content.resource_from_relative_deploy_path(path) | |||||
if not res: | |||||
# Nothing much we can do. | |||||
logger.error("Cannot load file:[%s]" % path) | |||||
raise HydeException("Cannot load file: [%s]" % path) | |||||
# this file is probably new or being generated by a plugin. | |||||
# lets not try too hard, just regenerate | |||||
logger.info("Attempting regeneration for:[%s]" % path) | |||||
self.server.regenerate() | |||||
res = site.content.resource_from_relative_deploy_path(path) | |||||
if not res: | |||||
# Nothing much we can do. | |||||
logger.error("Cannot load file:[%s]" % path) | |||||
return site.config.deploy_root_path.child(path) | return site.config.deploy_root_path.child(path) | ||||
else: | else: | ||||
@@ -125,7 +126,8 @@ class HydeWebServer(HTTPServer): | |||||
self.site = site | self.site = site | ||||
self.site.load() | self.site.load() | ||||
self.generator = Generator(self.site) | self.generator = Generator(self.site) | ||||
self.request_time = datetime.strptime('1-1-1999', '%m-%d-%Y') | |||||
self.regeneration_time = datetime.strptime('1-1-1998', '%m-%d-%Y') | |||||
HTTPServer.__init__(self, (address, port), | HTTPServer.__init__(self, (address, port), | ||||
HydeRequestHandler) | HydeRequestHandler) | ||||
@@ -135,6 +137,7 @@ class HydeWebServer(HTTPServer): | |||||
""" | """ | ||||
try: | try: | ||||
logger.info('Regenerating the entire site') | logger.info('Regenerating the entire site') | ||||
self.regeneration_time = datetime.now() | |||||
self.site.load() | self.site.load() | ||||
self.generator.generate_all() | self.generator.generate_all() | ||||
except Exception, exception: | except Exception, exception: | ||||
@@ -65,6 +65,11 @@ class Resource(Processable): | |||||
""" | """ | ||||
return self.source_file.get_relative_path(self.node.root.source_folder) | return self.source_file.get_relative_path(self.node.root.source_folder) | ||||
@property | |||||
def slug(self): | |||||
#TODO: Add a more sophisticated slugify method | |||||
return self.source.name_without_extension | |||||
def get_relative_deploy_path(self): | def get_relative_deploy_path(self): | ||||
""" | """ | ||||
Gets the path where the file will be created | Gets the path where the file will be created | ||||
@@ -16,7 +16,7 @@ from operator import attrgetter | |||||
TEST_SITE = File(__file__).parent.parent.child_folder('_test') | TEST_SITE = File(__file__).parent.parent.child_folder('_test') | ||||
class TestMeta(object): | |||||
class TestSorter(object): | |||||
def setUp(self): | def setUp(self): | ||||
TEST_SITE.make() | TEST_SITE.make() | ||||
@@ -136,7 +136,7 @@ class TestMeta(object): | |||||
File(p).parent.name]))] | File(p).parent.name]))] | ||||
assert pages == expected_sorted | assert pages == expected_sorted | ||||
def test_walk_resources_sorted_default_is_processable(self): | |||||
def test_walk_resources_sorted_no_default_is_processable(self): | |||||
s = Site(TEST_SITE) | s = Site(TEST_SITE) | ||||
cfg = """ | cfg = """ | ||||
plugins: | plugins: | ||||
@@ -153,7 +153,7 @@ class TestMeta(object): | |||||
SorterPlugin(s).begin_site() | SorterPlugin(s).begin_site() | ||||
assert hasattr(s.content, 'walk_resources_sorted_by_kind2') | assert hasattr(s.content, 'walk_resources_sorted_by_kind2') | ||||
expected = ["about.html", "merry-christmas.html"] | |||||
expected = ["404.html", "about.html", "merry-christmas.html"] | |||||
pages = [page.name for page in s.content.walk_resources_sorted_by_kind2()] | pages = [page.name for page in s.content.walk_resources_sorted_by_kind2()] | ||||
@@ -118,6 +118,15 @@ def test_get_resource(): | |||||
resource = node.get_resource('merry-christmas.html') | resource = node.get_resource('merry-christmas.html') | ||||
assert resource == s.content.resource_from_relative_path(Folder(path).child('merry-christmas.html')) | assert resource == s.content.resource_from_relative_path(Folder(path).child('merry-christmas.html')) | ||||
def test_resource_slug(): | |||||
s = Site(TEST_SITE_ROOT) | |||||
s.load() | |||||
path = 'blog/2010/december' | |||||
node = s.content.node_from_relative_path(path) | |||||
resource = node.get_resource('merry-christmas.html') | |||||
assert resource.slug == 'merry-christmas' | |||||
def test_get_resource_from_relative_deploy_path(): | def test_get_resource_from_relative_deploy_path(): | ||||
s = Site(TEST_SITE_ROOT) | s = Site(TEST_SITE_ROOT) | ||||
s.load() | s.load() | ||||