Global Template System in Google App Engine

I love using one template file that includes my content. I personally don’t like the idea of having to include “header” and “footer” into every single content file.

My main reason is because sometime I have multiple templates (for example, view, mobile, json or xml). If I include the template into my content, I will need some logic to cater for different views. Therefore, I did some research and managed to come up with something relatively simple.

This is how basic Google App Engine Template works (more information visit: Using Templates)

import os
from google.appengine.ext.webapp import template

class MainPage(webapp.RequestHandler):
  def get(self):
    template_values = {'content' : 'hello world'}
    path = os.path.join(os.path.dirname(__file__), 'index.html')
    self.response.out.write(template.render(path, template_values))

This is how I get what I need to work

import os
from google.appengine.ext.webapp import template

class MyTemplate(webapp.RequestHandler):
  def render(self, template_name, param):
    template_values = dict({'template_name': template_name}, ** param)
    path = os.path.join(os.path.dirname(__file__) + '/view', 'my_template.html')
    self.response.out.write(template.render(path, template_values))

class MainPage(MyTemplate): # extend MyTemplate
  def get(self):
    template_values = {'content' : 'hello world'}
    # calls render function from super class (MyTemplate)
    self.render('index.html', template_values)

Simple?

How to merge 2 dictionaries in Python

I have been disappearing for quite awhile due to work and other stuff. Recently, I have been looking heavily into Google App Engine (GAE) and Python. Hopefully, I will discover something interesting and is able to share them here.

Today I was creating a global template system in GAE and I found myself needing to merge 2 dictionaries together.

I thought it was gonna be something like merge or append functions but Stackoverflow points me to this interesting syntax.

a = { ‘one’ : ‘one’, ‘two’ : ‘two’}

b = {‘three’ : ‘three’}

c = dict(a, ** b) # merge a and b

I questioned myself about the syntax for awhile but I agreed to try it and it works perfectly.

How to fix font size in ePub for Kobo eReader

Very late blogging for today for a sick person like me.

Background: (Feel free to skip it :P)

A few days ago, I managed to get a Kobo eReader for my trip. I might be weird because I don’t want the iPad. I think that it’s too heavy and I only need an ebook reader, plus I have an iPhone 3G!

I have a few pdf files that I like to read for ages but I couldn’t do it because I don’t like reading documents on my laptop. I found that it’s very hard. I have been hunting around for the ebook reader and I found a few cheap ones that does what it supposed to, 2 from OfficeWorks and Kobo.

But I went for Kobo, not because it’s the most expensive one but because it supports both PDF and ePub.

However, most ebook reader can only display PDF as it is – no changing font face and font size etc. So I decided to convert my PDF to ePub using Calibe.

Problem: (Continue here :))

Everything works well for the past few days until I discovered that Kobo has a problem with changing font size for ePub documents because of the css file inside it.

I experimented with removing the css file inside ePub and it works. But I have so many files to process. What did I do? —- of course, I wrote a script —-

Calibre Library Anatomy:

The Calibre Library structure is

– Root

— Author1

— Book1

—- Book1.epub

— Author2

— Book2a

—- Book2a.epub

—Book2b

—- Book2b.epub

(I hope you will get some ideas here)

The script basically

1. Loop through each author folders

2. Loop through each book

3. Unzip each ePub file

4. Remove the css file

5. Zip the ePub file back

The script is written as bash script – Sorry I’m a Mac user ūüôā

Note: sorry for the indentation ūüė¶

#!/bin/bash
######################################
# author: Ploy
# date: Jun 15, 2010
# purpose: to remove CSS files from ePub files
# version: beta
#¬†disclaimer:¬†no¬†warranty¬†–¬†just¬†experimenting
######################################
echo¬†“Removing¬†CSS¬†Files¬†from¬†ePub files”
echo¬†“A¬†quick¬†fix¬†for¬†text¬†resizing¬†in¬†an¬†ebook¬†reader”

# —— EDIT ME ——-
ARCHIVE_DIR=”/Users/ME/CalibreLIBFOLDER”

cd $ARCHIVE_DIR

#loop through each author dir
for author in *
do
echo¬†“$author”

#loop through each book dir
if¬†[¬†-d¬†“$author”¬†]¬†;¬†then
cd¬†“$author”
for book in *
do
if¬†[¬†-d¬†“$book”¬†]¬†;¬†then
cd¬†“$book”
for epub in *.epub
do
echo¬†“Processing…¬†$epub”
unzip¬†-oq¬†“$epub”¬†-d¬†“$epub.unzipped”
cd¬†“$epub.unzipped”
rm -rf *.css
zip¬†-rq¬†“$epub”¬†*
mv¬†-f¬†“$epub”¬†../
cd ..
rm -rf *.unzipped
echo¬†“Finish¬†Processing…¬†$epub”
done
cd ..
fi
done
fi
echo¬†“————–”
cd $ARCHIVE_DIR
done
exit 0

How to fix “operational abort” for Google Map in IE

The first task that I have to day is to fix Google Map “Operational Abort” in IE (Internet Explorer). It took me awhile to understand what happened at the back of it.

This is how to produce the error:

Go to any page with Google Map using the Internet Explorer.

Then go to another page, for example, by clicking the link from that page.

Hit “Back” to go back to Google Map page.

You will then receive “Operational Abort” error.

To solve it, I used the WindowOnload fix from: http://blog.firetree.net/2005/07/17/javascript-onload/

Here is how I used it, assuming that the code structure looks like this:

HTML HEAD

SCRIPT to include GMap Code

SCRIPT to ask GMap to do a cool trick – include GMap_init() function

HTML BODY

DIV your map is here

END HTML BODY

END HTML HEAD

The edited code:

HTML HEAD

HTML BODY

DIV your map is here

SCRIPT to include GMap Code

SCRIPT to ask GMap to do a cool trick

function WindowOnload(f) {
var prev=window.onload;
window.onload=function(){ if(prev)prev(); f(); }
}

WindowOnload(GMap_init); // call init() function.

END HTML BODY

END HTML HEAD

Feel free to try that out and let me know if it works ūüôā

Installing ePrints from source on Fedora Core 11

Download eprints-3.1.x.tar.gz from http://www.eprints.org

Terminal:
1. Empowered as super users

su

2. Install required packages

yum install -y rpmbuild wget elinks tetex-latex mod_perl perl-Unicode-String perl-MIME-Lite perl-XML-LibXML perl-XML-Parser perl-DBI perl-DBD-mysql xpdf gdome2 gdome2-devel ImageMagick

3. RPM Build

rpmbuild -ta eprints-x.x.x.tar.gz

4. Install the rpm

rpm -i /path/to/eprints … rpm (ie. /root/rpmbuild/RPMS/noarch/eprints3-eprints-1.fc11.noarch.rpm)

5. Install MySQL

yum install -y mysql mysql-server

6. Start MySQL

/etc/init.d/mysqld start

7. Set MySQL root password

mysql -u root
SET PASSWORD FOR ‘root’@localhost = PASSWORD(‘new_password’);

8. Change user to ‘eprints’

su eprints

9. Go to eprints directory

cd (Should be in /opt/eprints3)

10. Create a repository archive

./bin/epadmin create
(follows the installation process)

11. Update apache configuration file

vi /etc/httpd/conf/httpd.conf
Add: Include /opt/eprints3/cfg/apache.conf
Save and Exit

12. Restart Apache

Restart Apache
/etc/init.d/httpd restart

13. Using eprints

ePrints can be accessed via a web browser

Note: For testing purposes only!!! Running eprints on a ‘fake’ hostname is done by editing the hosts file:
For example if eprints is set to run under http://eprints.mydomain.com but we haven’t got that domain. Getting around it is to:
1. Edit hosts file

vi /etc/hosts
Add: 127.0.0.1    eprints.mydomain.com

2. Restart the interface

service network restart

3. ePrints can be accessible from http://eprints.mydomain.com

Installing ePrints 3.1 on Redhat 4

How to install ePrints 3.1 on Redhat 4

This is a record of my ePrints installation. It is heavily based on the wiki from http://www.eprints.org.

1. Change user to root:

$ su

2. Go to home directory:

# cd

3. Create a directory called eprints:

# mkdir eprints

4. Go to eprints directory:

# cd eprints

5. Download eprints from http://www.eprints.org

# wget http://files.eprints.org/447/1/eprints-3.1.3.tar.gz

6. Unarchive it

# tar xzvf eprints-3.1.3.tar.gz

7. Installing the dependencies:

# up2date –nox -u httpd wget gzip xpdf lynx unzip
# up2date –nox -u mod_perl perl-DBI perl-DBD-MySQL perl-XML-Parser
# up2date –nox -u mysql-server

8. Install perl modules:

# cpan
> install Data::ShowTable
> install MIME::Base64
> install Unicode::String
> install Term::ReadKey
> install Readonly
> install MIME::Lite
> install XML::LibXML
> install CGI

9. Make sure mysqld and httpd starts after the reboot:

# /sbin/chkconfig mysqld on
# /sbin/chkconfig httpd on

10. Install GDOME:

# wget http://gdome2.cs.unibo.it/rpm/gdome2-0.8.1-1.i386.rpm
# wget http://gdome2.cs.unibo.it/rpm/gdome2-devel-0.8.1-1.i386.rpm
# rpm -Uvh gdome2-0.8.1-1.i386.rpm gdome2-devel-0.8.1-1.i386.rpm

11. Fix the bug in gdome-config:

# vi /usr/bin/gdome-config

Change:

the_libs=”$the_libs -L${exec_prefix}/lib -lgdome ` –libs` `xml2-config –libs`”
the_flags=”$the_flags -I${prefix}/include -I${prefix}/include/libgdome ` –cflags` `xml2-config –cflags`”

To:

the_libs=”$the_libs -L${exec_prefix}/lib -lgdome `/usr/bin/glib-config –libs` `xml2-config –libs`”
the_flags=”$the_flags -I${prefix}/include -I${prefix}/include/libgdome `/usr/bin/glib-config –cflags` `xml2-config –cflags`”

12. Install XML::GDOME:

# wget http://cpan.uwinnipeg.ca/cpan/authors/id/T/TJ/TJMATHER/XML-GDOME-0.86.tar.gz
# tar xzvf XML-GDOME-0.86.tar.gz
# cd XML-GDOME-0.86
# perl Makefile.PL
# make
# make install

13. Add eprints user:

# useradd eprints
# passwd eprints

14. Install ePrints:

# cd ..
# cd eprints-3.1.3
# ./configure
# ./install.pl

15. Set MySQL root password:

# /etc/init.d/mysql start
# mysql -u root
> SET PASSWORD FOR ‘root’@localhost = PASSWORD(‘new_password’);
> exit

16. Setup eprints archive:

# su eprints
# cd /opt/eprints3
# ./bin/epadmin create

17. Edit apache configurations:

# vi /etc/httpd/conf/httpd.conf

Add the following line at the end:

Include /opt/eprints3/cfg/apache.conf

Chage user to eprints:

User eprints
Group eprints

18. Restart apache:

# /etc/init.d/httpd restart

Source:

http://wiki.eprints.org/w/Installing_EPrints_3_on_RedHat_Enterprise_4
http://www.eprints.org/software