Skip to content

Commit

Permalink
add helper class to get string from jinja2 templates
Browse files Browse the repository at this point in the history
  • Loading branch information
stanfeldman committed Mar 8, 2012
1 parent 03bd3e4 commit 8d6edb6
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 22 deletions.
8 changes: 4 additions & 4 deletions kiss/core/application.py
Expand Up @@ -55,12 +55,12 @@ def start(self):
}
if "static_path" in self.options["views"]:
try:
static_path = Importer.module_path(self.options["views"]["static_path"])
self.options["views"]["static_path"] = Importer.module_path(self.options["views"]["static_path"])
except:
pass
if static_path:
self.static_builder.build(static_path)
self.wsgi_app = SharedDataMiddleware(self.wsgi_app, {'/': static_path + "/build"})
if self.options["views"]["static_path"]:
self.static_builder.build(self.options["views"]["static_path"])
self.wsgi_app = SharedDataMiddleware(self.wsgi_app, {'/': self.options["views"]["static_path"] + "/build"})
self.wsgi_app = SessionMiddleware(self.wsgi_app, session_options, environ_key="session")
kwargs = dict(filter(lambda item: item[0] not in ["address", "port"], self.options["application"].iteritems()))
self.server = WSGIServer((self.options["application"]["address"], self.options["application"]["port"]), self.wsgi_app, **kwargs)
Expand Down
4 changes: 3 additions & 1 deletion kiss/views/base.py
Expand Up @@ -14,7 +14,9 @@ def session(self):

class Response(werkzeug.wrappers.Response):
def __init__(self, text, **argw):
super(Response, self).__init__(text, mimetype="text/html", **argw)
if "mimetype" not in argw:
argw["mimetype"] = "text/html"
super(Response, self).__init__(text, **argw)

@staticmethod
def redirect(path):
Expand Down
33 changes: 23 additions & 10 deletions kiss/views/static.py
Expand Up @@ -6,10 +6,31 @@
import os
import shutil

class StaticBuilder(Singleton):

class StaticCompiler(Singleton):
def __init__(self):
self.css_parser = Stylesheet(options={"compress": True})

def compile_file(self, filepath):
mimetype = mimetypes.guess_type(filepath)[0]
return self.compile_text(self.get_content(filepath), mimetype)

def compile_text(self, text, mimetype):
result = ""
if mimetype == "text/css":
result = self.css_parser.loads(text)
elif mimetype == "application/javascript":
result = jsmin(text)
return result

def get_content(self, file):
return open(file).read()


class StaticBuilder(Singleton):
def __init__(self):
self.path = ""
self.compiler = StaticCompiler()

def build(self, path):
self.path = path
Expand All @@ -20,20 +41,12 @@ def build(self, path):
Dir.walk(path, self.build_file)

def build_file(self, file):
mime_type = mimetypes.guess_type(file)[0]
new_path = self.path + "/build" + file.replace(self.path, "")
result = ""
if mime_type == "text/css":
result = self.css_parser.loads(self.get_content(file))
elif mime_type == "application/javascript":
result = jsmin(self.get_content(file))
result = self.compiler.compile_file(file)
if result:
try:
os.makedirs(os.path.dirname(new_path))
except:
pass
with open(new_path, "w") as f:
f.write(result)

def get_content(self, file):
return open(file).read()
21 changes: 16 additions & 5 deletions kiss/views/templates.py
Expand Up @@ -2,10 +2,21 @@
from kiss.core.application import Application


class Template(object):
@staticmethod
def text_by_path(path, context={}):
return Application().options["views"]["templates_path"].get_template(path).render(context).encode("utf-8")

@staticmethod
def text_by_text(text, context={}):
return Application().options["views"]["templates_path"].from_string(text).render(context).encode("utf-8")


class TemplateResponse(Response):
def __init__(self, path, context={}, **argw):
self.application = Application()
self.template = self.application.options["views"]["templates_path"].get_template(path)
self.context = context
super(TemplateResponse, self).__init__(self.template.render(self.context).encode("utf-8"), **argw)
print self
super(TemplateResponse, self).__init__(Template.text_by_path(path, context), **argw)


class TextResponse(Response):
def __init__(self, text, context={}, **argw):
super(TextResponse, self).__init__(Template.text_by_text(text, context), **argw)
4 changes: 2 additions & 2 deletions setup.py
Expand Up @@ -6,7 +6,7 @@

setup(
name = "kiss.py",
version = "0.1.0",
version = "0.1.1",
author = "Stanislav Feldman",
description = ("MVC web framework in Python with Gevent, Jinja2, Werkzeug"),
url = "https://github.com/stanislavfeldman/kiss.py",
Expand All @@ -16,7 +16,7 @@
],
install_requires = ['gevent', "jinja2", "compressinja", "beaker", "werkzeug", "putils", "jsmin", "scss"],
classifiers=[
"Development Status :: 3 - Alpha",
"Development Status :: 4 - Beta",
"Topic :: Software Development",
"Topic :: Software Development :: Libraries :: Application Frameworks"
],
Expand Down

0 comments on commit 8d6edb6

Please sign in to comment.