[google-app-engine-oil] push by ericsk - Re-organize the GAEO-provided template filters and... on 2010-11-23 15:32 GMT

0 views
Skip to first unread message

google-app...@googlecode.com

unread,
Nov 23, 2010, 10:32:56 AM11/23/10
to google-app...@googlegroups.com
Revision: 68bf0af7ac
Author: Eric Shangkuan <eri...@gmail.com>
Date: Tue Nov 23 07:31:42 2010
Log: Re-organize the GAEO-provided template filters and
support the custom template filters.

Put your own filters into the `${APP_ROOT}/plugins/filters/`,
the GAEO view instance will load them automatically.
http://code.google.com/p/google-app-engine-oil/source/detail?r=68bf0af7ac

Added:
/oildrum/lib/gaeo/view/filters/__init__.py
/oildrum/lib/gaeo/view/filters/content.py
/oildrum/lib/gaeo/view/filters/time.py
/oildrum/plugins/__init__.py
/oildrum/plugins/filters/__init__.py
Deleted:
/oildrum/lib/gaeo/view/filters.py
Modified:
/bin/gaeo.py
/oildrum/lib/gaeo/view/__init__.py
/oildrum/settings-sample.py

=======================================
--- /dev/null
+++ /oildrum/lib/gaeo/view/filters/__init__.py Tue Nov 23 07:31:42 2010
@@ -0,0 +1,1 @@
+"""gaeo.view.filters package. """
=======================================
--- /dev/null
+++ /oildrum/lib/gaeo/view/filters/content.py Tue Nov 23 07:31:42 2010
@@ -0,0 +1,56 @@
+from google.appengine.ext import webapp
+
+# get template register
+register = webapp.template.create_template_register()
+
+...@register.filter
+def bbcode_content(value):
+ """
+ Ref. http://code.djangoproject.com/wiki/CookBookTemplateFilterBBCode
+ """
+ import re
+
+ pat = re.compile(r'<([^>]*?)>', re.DOTALL | re.M)
+ value = re.sub(pat, '&lt;\\1&gt;', value)
+
+ bbdata = [
+ (r'\[url\](.+?)\[/url\]', r'<a href="\1">\1</a>'),
+ (r'\[url=(.+?)\](.+?)\[/url\]', r'<a class="link-segment"
href="\1">\2</a>'),
+ (r'\[email\](.+?)\[/email\]', r'<a href="mailto:\1">\1</a>'),
+ (r'\[email=(.+?)\](.+?)\[/email\]', r'<a href="mailto:\1">\2</a>'),
+ (r'\[img\](.+?)\[/img\]', r'<img src="\1">'),
+ (r'\[img=(.+?)\](.+?)\[/img\]', r'<img src="\1" alt="\2">'),
+ (r'\[b\](.+?)\[/b\]', r'<b>\1</b>'),
+ (r'\[i\](.+?)\[/i\]', r'<i>\1</i>'),
+ (r'\[u\](.+?)\[/u\]', r'<u>\1</u>'),
+ (r'\[quote\](.+?)\[/quote\]', r'<blockquote
class="content-quote">\1</blockquote>'),
+ (r'\[center\](.+?)\[/center\]', r'<div align="center">\1</div>'),
+ (r'\[code\]\s?(.+?)\[/code\]', r'<blockquote
class="code-segment"><code><pre>\1</pre></code></blockquote>'),
+ (r'\[big\](.+?)\[/big\]', r'<big>\1</big>'),
+ (r'\[small\](.+?)\[/small\]', r'<small>\1</small>'),
+ ]
+
+ for bbset in bbdata:
+ p = re.compile(bbset[0], re.DOTALL)
+ value = p.sub(bbset[1], value)
+
+ #The following two code parts handle the more complex list statements
+ temp = ''
+ p = re.compile(r'\[list\](.+?)\[/list\]', re.DOTALL)
+ m = p.search(value)
+ if m:
+ items = re.split(re.escape('[*]'), m.group(1))
+ for i in items[1:]:
+ temp = temp + '<li>' + i + '</li>'
+ value = p.sub(r'<ul>'+temp+'</ul>', value)
+
+ temp = ''
+ p = re.compile(r'\[list=(.)\](.+?)\[/list\]', re.DOTALL)
+ m = p.search(value)
+ if m:
+ items = re.split(re.escape('[*]'), m.group(2))
+ for i in items[1:]:
+ temp = temp + '<li>' + i + '</li>'
+ value = p.sub(r'<ol type=\1>'+temp+'</ol>', value)
+
+ return value
=======================================
--- /dev/null
+++ /oildrum/lib/gaeo/view/filters/time.py Tue Nov 23 07:31:42 2010
@@ -0,0 +1,10 @@
+from google.appengine.ext import webapp
+from gaeo.utils import TaiwanTimeZone
+
+# get template register
+register = webapp.template.create_template_register()
+
+...@register.filter
+def twtz(value):
+ from datetime import timedelta
+ return (value + timedelta(hours=8)).replace(tzinfo=TaiwanTimeZone())
=======================================
--- /dev/null
+++ /oildrum/plugins/__init__.py Tue Nov 23 07:31:42 2010
@@ -0,0 +1,1 @@
+"""Custom plugins package"""
=======================================
--- /dev/null
+++ /oildrum/plugins/filters/__init__.py Tue Nov 23 07:31:42 2010
@@ -0,0 +1,1 @@
+"""Custom template filters package (plugins.filters)"""
=======================================
--- /oildrum/lib/gaeo/view/filters.py Tue Sep 28 23:47:08 2010
+++ /dev/null
@@ -1,64 +0,0 @@
-#!/usr/bin/env python
-
-from google.appengine.ext import webapp
-from gaeo.utils import TaiwanTimeZone
-
-# get template register
-register = webapp.template.create_template_register()
-
-...@register.filter
-def twtz(value):
- from datetime import timedelta
- return (value + timedelta(hours=8)).replace(tzinfo=TaiwanTimeZone())
-
-...@register.filter
-def bbcode_content(value):
- """
- Ref. http://code.djangoproject.com/wiki/CookBookTemplateFilterBBCode
- """
- import re
-
- pat = re.compile(r'<([^>]*?)>', re.DOTALL | re.M)
- value = re.sub(pat, '&lt;\\1&gt;', value)
-
- bbdata = [
- (r'\[url\](.+?)\[/url\]', r'<a href="\1">\1</a>'),
- (r'\[url=(.+?)\](.+?)\[/url\]', r'<a class="link-segment"
href="\1">\2</a>'),
- (r'\[email\](.+?)\[/email\]', r'<a href="mailto:\1">\1</a>'),
- (r'\[email=(.+?)\](.+?)\[/email\]', r'<a href="mailto:\1">\2</a>'),
- (r'\[img\](.+?)\[/img\]', r'<img src="\1">'),
- (r'\[img=(.+?)\](.+?)\[/img\]', r'<img src="\1" alt="\2">'),
- (r'\[b\](.+?)\[/b\]', r'<b>\1</b>'),
- (r'\[i\](.+?)\[/i\]', r'<i>\1</i>'),
- (r'\[u\](.+?)\[/u\]', r'<u>\1</u>'),
- (r'\[quote\](.+?)\[/quote\]', r'<blockquote
class="content-quote">\1</blockquote>'),
- (r'\[center\](.+?)\[/center\]', r'<div align="center">\1</div>'),
- (r'\[code\]\s?(.+?)\[/code\]', r'<blockquote
class="code-segment"><code><pre>\1</pre></code></blockquote>'),
- (r'\[big\](.+?)\[/big\]', r'<big>\1</big>'),
- (r'\[small\](.+?)\[/small\]', r'<small>\1</small>'),
- ]
-
- for bbset in bbdata:
- p = re.compile(bbset[0], re.DOTALL)
- value = p.sub(bbset[1], value)
-
- #The following two code parts handle the more complex list statements
- temp = ''
- p = re.compile(r'\[list\](.+?)\[/list\]', re.DOTALL)
- m = p.search(value)
- if m:
- items = re.split(re.escape('[*]'), m.group(1))
- for i in items[1:]:
- temp = temp + '<li>' + i + '</li>'
- value = p.sub(r'<ul>'+temp+'</ul>', value)
-
- temp = ''
- p = re.compile(r'\[list=(.)\](.+?)\[/list\]', re.DOTALL)
- m = p.search(value)
- if m:
- items = re.split(re.escape('[*]'), m.group(2))
- for i in items[1:]:
- temp = temp + '<li>' + i + '</li>'
- value = p.sub(r'<ol type=\1>'+temp+'</ol>', value)
-
- return value
=======================================
--- /bin/gaeo.py Sat Nov 20 08:05:58 2010
+++ /bin/gaeo.py Tue Nov 23 07:31:42 2010
@@ -244,9 +244,11 @@
create_app_yaml(os.path.join(project_home, 'app.yaml'),
project_name)

+ target_path = os.path.dirname(os.path.dirname(__file__))
+
# copy static directories
static_folder = os.path.join(project_home, 'static')
- copytree(os.path.join(os.path.dirname(os.path.dirname(__file__)),
+ copytree(os.path.join(target_path,
template_base, 'static'), static_folder)

# create assets directories
@@ -259,9 +261,14 @@
# copy lib directory

lib_folder = os.path.join(project_home, 'lib')
- copytree(os.path.join(os.path.dirname(os.path.dirname(__file__)),
+ copytree(os.path.join(target_path,
template_base, 'lib'), lib_folder)

+ # copy plugins directory
+ plugin_folder = os.path.join(project_home, 'plugins')
+ copytree(os.path.join(target_path,
+ template_base, 'plugins'), plugin_folder)
+
# if zipped option is enabled

if zipped_core:
=======================================
--- /oildrum/lib/gaeo/view/__init__.py Tue Sep 28 23:47:08 2010
+++ /oildrum/lib/gaeo/view/__init__.py Tue Nov 23 07:31:42 2010
@@ -7,6 +7,7 @@
DjangoTemplateView:
"""

+import re
import os

# gaeo imports
@@ -42,10 +43,8 @@
self._template_path = template_path

def render(self, data=None, **kwds):
- from google.appengine.ext.webapp import template
-
- # register filters
- template.register_template_library('gaeo.view.filters')
+ # get template
+ template = self.__init_template()

# merge
response_data = data if data else {}
@@ -72,3 +71,36 @@
@property
def options(self):
return self.__view_options
+
+ def __init_template(self):
+ """Pre-process the template module like register filters.
+
+ Return
+ The processed template module.
+ """
+ from google.appengine.ext.webapp import template
+
+ # GAEO-provided filters
+ cur_dir = os.path.dirname(__file__)
+ dirs = [{
+ 'package': 'gaeo.view.filters',
+ 'path': os.path.join(cur_dir, 'filters')
+ }]
+
+ # check custom filters, inspired from Issue #54
+ custom_filters_path = getattr(settings, 'PLUGIN_FILTERS_PATH')
+ if custom_filters_path and os.path.exists(custom_filters_path):
+ dirs.append({
+ 'package': '%s.%s' % (settings.PLUGIN_DIR,
settings.PLUGIN_FILTERS_DIR),
+ 'path': custom_filters_path
+ })
+
+ # register filters
+ for d in dirs:
+ filters = os.listdir(d['path'])
+ for f in filters:
+ if not re.match('^__|^\.|.*pyc$', f):
+ module = '%s.%s' % (d['package'], f.replace('.py', ''))
+ template.register_template_library(module)
+
+ return template
=======================================
--- /oildrum/settings-sample.py Mon Nov 15 06:37:03 2010
+++ /oildrum/settings-sample.py Tue Nov 23 07:31:42 2010
@@ -13,6 +13,9 @@
APP_PATH = os.path.join(cur_path, APP_DIR)
TEMPLATE_DIR = 'templates'
TEMPLATE_PATH = os.path.join(cur_path, APP_DIR, TEMPLATE_DIR)
+PLUGIN_DIR = 'plugins'
+PLUGIN_FILTERS_DIR = 'filters'
+PLUGIN_FILTERS_PATH = os.path.join(cur_path, PLUGIN_DIR,
PLUGIN_FILTERS_DIR)
CACHE_TIMEOUT = 3600

# Session

Reply all
Reply to author
Forward
0 new messages