Esempio n. 1
0
    def _make_loader(self):
        """
        Create a Loader instance using current attributes.

        """
        return Loader(file_encoding=self.file_encoding, extension=self.file_extension,
                      to_unicode=self.unicode, search_dirs=self.search_dirs)
Esempio n. 2
0
 def test_init__file_encoding__default(self):
     file_encoding = defaults.FILE_ENCODING
     try:
         defaults.FILE_ENCODING = 'foo'
         loader = Loader()
         self.assertEqual(loader.file_encoding, 'foo')
     finally:
         defaults.FILE_ENCODING = file_encoding
Esempio n. 3
0
    def test_read(self):
        """
        Test read().

        """
        loader = Loader()
        path = self._get_path('ascii.mustache')
        actual = loader.read(path)
        self.assertString(actual, 'ascii: abc')
Esempio n. 4
0
    def test_unicode__basic__input_unicode(self):
        """
        Test unicode(): default arguments with unicode input.

        """
        loader = Loader()
        actual = loader.str("foo")

        self.assertString(actual, "foo")
Esempio n. 5
0
    def test_read__to_unicode__attribute(self):
        """
        Test read(): to_unicode attribute respected.

        """
        loader = Loader()
        path = self._get_path('non_ascii.mustache')

        self.assertRaises(UnicodeDecodeError, loader.read, path)
Esempio n. 6
0
    def test_init__to_unicode__default(self):
        loader = Loader()
        self.assertRaises(TypeError, loader.to_unicode, "abc")

        decode_errors = defaults.DECODE_ERRORS
        string_encoding = defaults.STRING_ENCODING

        nonascii = 'abcdé'.encode('utf-8')

        loader = Loader()
        self.assertRaises(UnicodeDecodeError, loader.to_unicode, nonascii)

        defaults.DECODE_ERRORS = 'ignore'
        loader = Loader()
        self.assertString(loader.to_unicode(nonascii), 'abcd')

        defaults.STRING_ENCODING = 'utf-8'
        loader = Loader()
        self.assertString(loader.to_unicode(nonascii), 'abcdé')
Esempio n. 7
0
    def test_read__encoding__argument(self):
        """
        Test read(): encoding argument respected.

        """
        loader = Loader()
        path = self._get_path('non_ascii.mustache')

        self.assertRaises(UnicodeDecodeError, loader.read, path)

        actual = loader.read(path, encoding='utf-8')
        self.assertString(actual, 'non-ascii: é')
Esempio n. 8
0
    def get(self, article_name):

        article_name = article_name.lower()

        if article_name in BaseController.articles:

            article = BaseController.articles[article_name]

            # if content has not modified since last request
            # send a 304 not modified status
            modified_header_key = "If-Modified-Since"
            if modified_header_key in self.request.headers:
                if (self.request.headers["If-Modified-Since"] ==
                        article['modified_date']):
                    self.set_status(304)
                    return

            if (BaseController.settings['enable_caching']
                    and article_name in BaseController.cached_articles):
                html = BaseController.cached_articles[article_name]
            else:
                view_model = {
                    "article": article,
                    "site_name": BaseController.settings['site_name']
                }
                self.attach_meta_data(view_model)

                loader = Loader(file_encoding='utf8',
                                extension='html',
                                search_dirs=[
                                    'view',
                                ])
                renderer = Renderer(file_encoding='utf8',
                                    file_extension='html',
                                    search_dirs=['view/partials'])
                template = loader.load_name('article')
                html = renderer.render(template, view_model)

                # cache the html
                BaseController.cached_articles[article_name] = html

            # set http caching headers
            if "http_caching_max_age" in BaseController.settings:
                max_age = BaseController.settings["http_caching_max_age"]
            else:
                max_age = 60
            self.set_header("Cache-control", "max-age=%s" % max_age)
            self.set_header("Last-Modified", article['modified_date'])
            self.write(html)

        else:
            raise tornado.web.HTTPError(404)
Esempio n. 9
0
    def test_read__file_encoding__attribute(self):
        """
        Test read(): file_encoding attribute respected.

        """
        loader = Loader()
        path = self._get_path('non_ascii.mustache')

        self.assertRaises(UnicodeDecodeError, loader.read, path)

        loader.file_encoding = 'utf-8'
        actual = loader.read(path)
        self.assertString(actual, 'non-ascii: é')
Esempio n. 10
0
    def test_unicode__encoding_argument(self):
        """
        Test unicode(): encoding argument.

        """
        loader = Loader()

        non_ascii = 'abcdé'.encode('utf-8')

        self.assertRaises(UnicodeDecodeError, loader.str, non_ascii)

        actual = loader.str(non_ascii, encoding='utf-8')
        self.assertString(actual, 'abcdé')
Esempio n. 11
0
 def get(self):
     pass_phrase = self.authenticate_request()
     if pass_phrase:
         if self.check_setup():
             loader = Loader(extension='html',
                             search_dirs=['view', 'view/setup'])
             renderer = Renderer(
                 file_extension='html',
                 search_dirs=['view/partials', 'view/setup'])
             template = loader.load_name('setup')
             html = renderer.render(template, {"pass": pass_phrase})
             self.write(html)
         else:
             self.write("setup already completed.")
Esempio n. 12
0
    def test_unicode__basic__input_unicode_subclass(self):
        """
        Test unicode(): default arguments with unicode-subclass input.

        """
        class UnicodeSubclass(str):
            pass

        s = UnicodeSubclass("foo")

        loader = Loader()
        actual = loader.str(s)

        self.assertString(actual, "foo")
Esempio n. 13
0
    def test_unicode__to_unicode__attribute(self):
        """
        Test unicode(): encoding attribute.

        """
        loader = Loader()

        non_ascii = 'abcdé'.encode('utf-8')
        self.assertRaises(UnicodeDecodeError, loader.str, non_ascii)

        def to_unicode(s, encoding=None):
            if encoding is None:
                encoding = 'utf-8'
            return str(s, encoding)

        loader.to_unicode = to_unicode
        self.assertString(loader.str(non_ascii), "abcdé")
Esempio n. 14
0
    def generate_page(self, articles):
        view_model = {
            "articles": articles,
            "site_name": BaseController.settings["site_name"]
        }
        self.attach_meta_data(view_model)

        loader = Loader(file_encoding='utf8',
                        extension='html',
                        search_dirs=[
                            'view',
                        ])
        renderer = Renderer(file_encoding='utf8',
                            file_extension='html',
                            search_dirs=['view/partials'])
        template = loader.load_name('list')
        html = renderer.render(template, view_model)
        return html
Esempio n. 15
0
    def get(self):

        if (BaseController.settings['enable_caching']
                and BaseController.cached_home):
            html = BaseController.cached_home
            self.write(html)
        else:
            published_articles = []
            for article in BaseController.articles.values():
                if article['date'] is not None:
                    published_articles.append(article)

            articles = sorted(published_articles,
                              key=operator.itemgetter("date"),
                              reverse=True)

            max_articles_count = BaseController.settings[
                "homepage_max_articles"]
            show_archive = False
            if len(articles) > max_articles_count:
                show_archive = True
                articles = articles[0:max_articles_count]

            view_model = {
                "articles": articles,
                "showArchive": show_archive,
                "site_name": BaseController.settings["site_name"]
            }
            self.attach_meta_data(view_model)
            loader = Loader(file_encoding='utf8',
                            extension='html',
                            search_dirs=[
                                'view',
                            ])
            renderer = Renderer(file_encoding='utf8',
                                file_extension='html',
                                search_dirs=['view/partials'])
            template = loader.load_name('home')
            html = renderer.render(template, view_model)

            # cache the home page
            BaseController.cached_home = html

            self.write(html)
Esempio n. 16
0
def index():
    loader = Loader()
    template = loader.load_name('index')

    response = requests.get(
        'http://www.unboxedconsulting.com/people/carl-whittaker')
    status = response.status_code
    if status == 200:
        content = response.content

        soup = BeautifulSoup(content)
        image = 'http://www.unboxedconsulting.com' + soup.select(
            'a.front img')[0]['src']
    else:
        image = ''

    return pystache.render(template, {
        'has_caricature': status == 200,
        'image': image
    })
Esempio n. 17
0
def _make_specloader():
    """
    Return a default SpecLoader instance for testing purposes.

    """
    # Python 2 and 3 have different default encodings.  Thus, to have
    # consistent test results across both versions, we need to specify
    # the string and file encodings explicitly rather than relying on
    # the defaults.
    def to_unicode(s, encoding=None):
        """
        Raises a TypeError exception if the given string is already unicode.

        """
        if encoding is None:
            encoding = 'ascii'
        return unicode(s, encoding, 'strict')

    loader = Loader(file_encoding='ascii', to_unicode=to_unicode)
    return SpecLoader(loader=loader)
Esempio n. 18
0
    def __init__(self, loader=None):
        if loader is None:
            loader = Loader()

        self.loader = loader
Esempio n. 19
0
    def test_init__loader(self):
        loader = Loader()
        custom = SpecLoader(loader=loader)

        self.assertIs(custom.loader, loader)
Esempio n. 20
0
import os
import gevent
import gevent.monkey
from gevent.pywsgi import WSGIServer
gevent.monkey.patch_all()

from pystache.loader import Loader
from pystache import render

from flask import Flask, redirect, abort


app = Flask(__name__)
app.debug = True
loader = Loader()


arguments = [
    (
        ('CBV', 'FBV',),
        ('django', 'python',)
    ),
    (
        ('Linux', 'Mac OS X',),
        ('os',)
    ),
    (
        ('Emacs', 'Vim',),
        ('editors',)
    ),
Esempio n. 21
0
    def _fetch_template(self, template_name):
        loader = Loader(search_dirs=[self._get_template_directory()])

        return loader.load_name(template_name)
Esempio n. 22
0
 def test_init__to_unicode(self):
     to_unicode = lambda x: x
     loader = Loader(to_unicode=to_unicode)
     self.assertEqual(loader.to_unicode, to_unicode)
Esempio n. 23
0
 def test_load_name(self):
     loader = Loader(search_dirs=[DATA_DIR, LOADER_DATA_DIR],
                     extension='txt')
     template = loader.load_name('template')
     self.assertEqual(template, 'Test template file\n')
Esempio n. 24
0
 def test_init__file_encoding(self):
     loader = Loader(file_encoding='bar')
     self.assertEqual(loader.file_encoding, 'bar')
Esempio n. 25
0
 def test_init__extension__default(self):
     # Test the default value.
     loader = Loader()
     self.assertEqual(loader.extension, 'mustache')
Esempio n. 26
0
 def test_init__extension(self):
     loader = Loader(extension='foo')
     self.assertEqual(loader.extension, 'foo')
Esempio n. 27
0
 def get(self):
     loader = Loader(extension='html', search_dirs=['view', 'view/setup'])
     template = loader.load_name('success')
     renderer = Renderer(file_extension='html',
                         search_dirs=['view/partials', 'view/setup'])
     self.write(renderer.render(template, ""))
Esempio n. 28
0
 def test_load_file(self):
     loader = Loader(search_dirs=[DATA_DIR, LOADER_DATA_DIR])
     template = loader.load_file('template.txt')
     self.assertEqual(template, 'Test template file\n')