From 1954fbcb65fdea8bc4217dd998ea24de376beebf Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ivan=20Monroy=20L=C3=B3pez?= Date: Mon, 22 Oct 2012 12:03:37 -0500 Subject: [PATCH] We use codecs.open for writing to a file We are concerned about Unicode support --- lib.py | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/lib.py b/lib.py index 53794a7..5e44aa5 100644 --- a/lib.py +++ b/lib.py @@ -1,5 +1,7 @@ # -*- coding: utf-8 -*- +import codecs + from twython import Twython import twitter_text from ConfigParser import SafeConfigParser @@ -216,9 +218,9 @@ class ReadTweetsFromDB: class WriteTweetsToHTMLFile: - """We write some tweets to a HTML file via Jinja templating. + """We display some tweets on a HTML file via Jinja templating. - This class expects three arguments: + This class expects four arguments: 1. A string that points to a location in the file system where Jinja templates are stored. We use the "FileSystemLoader" Jinja loader. The Jinja documentation says: "The loader takes the path to the templates as @@ -229,16 +231,26 @@ class WriteTweetsToHTMLFile: of ReadTweetsFromDB. The read_user_timeline method returns a list of Unicode type instances, Unicode strings, u'☺☻'. FWIW read_user_timeline of ReadNewTweets returns the same type of tweet lists... + 4. A string that points to a location in the file system where we can + store our HTML. """ - def __init__(self, template_search_path, template_name, list_of_tweets): - """ + def __init__(self, template_search_path, template_name, list_of_tweets, + html_file): + """template_search_path is a Jinja templates folder; and template_name + is a template therein. + + list_of_tweets is our list of tweets + + If it doesn't exist already, the file html_file will be created and + our Jinja-generated HTML will be written to it. """ self.template_search_path = template_search_path self.template_name = template_name self.list_of_tweets = list_of_tweets + self.html_file = html_file def autolink_list_of_tweets(self): """We auto link the plain text aspects of tweets. See "Twitter text @@ -248,7 +260,7 @@ class WriteTweetsToHTMLFile: We use the twitter-text-py module: pip install twitter-text-py - We need to do this in order to render the tweets in the browser... + We need to do this in order to render the tweets on the browser... """ self.list_of_tweets_autolinked = [] @@ -257,6 +269,18 @@ class WriteTweetsToHTMLFile: self.list_of_tweets_autolinked.append(my_autolink.auto_link()) def write_html_file(self): + """We write the autolinked tweets to a HTML file via Jinja templating. + + The HTML will be written to a new file. The new file's name is + self.html_file + + We pass the list_of_tweets_autolinked list to Jinja's render() method + for templates. + + See also our Jinja template: template.html + """ + env = Environment(loader=FileSystemLoader(self.template_search_path)) template = env.get_template(self.template_name) - print template.render(list_of_tweets=self.list_of_tweets_autolinked) + f = codecs.open(self.html_file, mode="w", encoding='utf-8') + f.write(template.render(list_of_tweets=self.list_of_tweets_autolinked)) -- 2.11.4.GIT