finally{}

all will be well finally

Archive for September, 2008

IronPython talks to Google AppEngine

without comments

I’m continuing from my earlier posts on Time Tracking Tool and Authenticating in GAE. When I started with Time Tracking Tool, I had the idea of having a server which will act as a centralized data repository – laptop/desktop tool, mobile tool should send data to the server and data will be analyzed in the server.

To that end, I got the windows desktop client tool talking to a server application. The server is a Google AppEngine Application. Currently there is not much in the server side. Just plain data that is transferred from the client tool. I’ll continue to work on the components and improve on the features – especially dashboard, analytics and similar other features.

To start with download the windows client tool (mySecs_x.x.zip – as the general practice x.x denotes the version) and unzip the content to a directory. You can use the client tool as a standalone tool (without synchronizing data to the server). This is the default configuration. However if you are okay to synchronize then modify the config.xml accordingly.

<configuration>
<serverupdate>0</serverupdate>
<host>mysecs.appspot.com</host>
<email>mysecs@gmail.com</email>
<password>yourpassword</password>
</configuration>

As I said before, client tool works as a stand alone tool by default. If you’re okay to synchronize the data, then change this to 1. I use Google Authentication, hence you need to have a gmail account and provide these in the respective xml nodes.

(A note about data privacy: I don’t pass your email id  to the server. So I wouldn’t know about your email id. If you are particular about your email id, which you should be, then create another gmail id for this purpose.

Also, since code for the server is shared, you can download the GAE code and host it on your own).

This is a working version. I’m using it to capture details and synchronize with the GAE server. As I continue to find bugs, I’ll fix them. However if you find bugs, please report them in the Issue Log. (A user reported that it doesn’t work with x64. I don’t know how to fix it. I will search around to fix it).

Next steps? Incorporate with Google Charts to provide some neat analytics reports, so that it really becomes an useful tool.

I need a help though. I am really bad at web design. So if you can take some time of you to design a cool home page and a list page, it will be great. I will surely acknowledge you for your contribution. So if you want to help, please contact me.

Written by Joseph Jude

September 29th, 2008 at 10:40 pm

Posted in Programming

Tagged with

Can we truly sing ‘Joy to the world’?

with one comment

Every ‘Holy Scripture’ strikes a claim that it is either written by god or inspired by one. Believing such a claim, its followers have interpreted its text literally, ignoring the cultural context and generational gaps leading to holy wars, blood baths and genocides.

Even today such interpretations let loose hell-on-earth in New York, Jerusalem and even in Bangalore.

Too often we hear such statements in godly discourses.

"My God is the only true God"

"Hmm…Who says so?"

"The book says so"

Well, the other book says the same about the other god!

Whenever I hear such arguments, my mind goes back to childhood days. It is not uncommon to hear similar statements in kid’s conversations.

"My Dad is the strongest"

"Who says so?"

"My Mommy says so"

When the other kid runs out of argument points, he punched you on your face. With a swollen face you get home, only to find that your dad slipped in the bathroom and lying on bed.

You wondered if your dad was really the strongest!

Religious wars are no different.

"Only through my God, one can go to heaven"

"Any one killing unbelievers will enter heaven"

Bang!

You got burnt villages, mass-murdering mobs and polarized countries fighting with one another.

All the while, gods were taking a nap after slipping in the bathroom!

Such comedy has caused centuries of tragedy. Will there be a remedy? I’m not that naive to think that a simple blog entry can bring in a solution to this age-old problem.

I only dream of a theological revolution that breaks the shackles of religion for all people. Then we can truly sing, ‘Joy to the world’.

Written by Joseph Jude

September 24th, 2008 at 3:41 am

Posted in General

Tagged with

Authenticating in GAE

with 5 comments

Having successfully created a desktop application in IronPython, I wanted to go a step further – integrate desktop and web environment. Data can be captured in desktop and passed on to web; and analysis can be done on the web.

To start with it, I need to authenticate the incoming request. Hmm…when I started reading about authentication I read so many jargons that scared me – openid, openauth, authsub, basicauth. It took me a while to understand the basics of each (still I don’t claim that I’ve understood them; probably whatever I’ve understood is wrong too)

To some extent, I understood ‘Basic Authentication’ and this is how I went about implementing it.

image

First of all, I am going with Google Account Authentication, assuming all the users will have a Google account; getting one is not a big deal.

IronPython Desktop Application will pass userid, password to AppEngine Application, which in turn will validate with Google Accounts Authentication service. If authenticated, AppEngine application will proceed; else it will pass the error back to the desktop application.

Having discussed the concepts, here are the code snippets. I’m sharing with a hope that it will be useful to someone; or if this is wrong, someone will raise an alarm; or if there is a better way to do it, someone will comment.

AppEngine

Here the expectation is that the incoming request will have a header (‘HTTP_AUTHORIZATION’) with userid and password in Base64 format.

from google.appengine.api import users
import urllib
import base64
from google.appengine.api import urlfetch
from django.http import HttpResponse

def authenticate(request):
  response_vals={}
  resp = HttpResponse()
  try:
    (method, encoded) = request.META['HTTP_AUTHORIZATION'].split()
    response_vals['method'] = method
    response_vals['encoded'] = encoded
    if method.lower() == 'basic':
        (username, password) = base64.b64decode(encoded).split(':')

        request_body = urllib.urlencode({'Email': username,
                                         'Passwd': password,
                                         'accountType': 'HOSTED_OR_GOOGLE',
                                         'service': 'ah',
                                         'source': 'test'})
        auth_response = urlfetch.fetch('https://www.google.com/accounts/ClientLogin',
                                       method=urlfetch.POST,
                                       headers={'Content-type':'application/x-www-form-urlencoded',
                                                'Content-Length':
                                                 str(len(request_body))},
                                       payload=request_body)
        resp.status_code = auth_response.status_code
  except:
    resp.status_code = 401
    return resp
  return resp
Desktop – Python

Encode the user id, password and call the particular URL (/auth?). And handle the response.

import urllib
import urllib2
import base64

url = 'http://localhost:8000/api/auth/'
username = 'mysecs@gmail.com'
password = 'mysecspwd'
USER_AGENT = 'mySecsWndClient'

request = urllib2.Request(url)
base64string = base64.encodestring('%s:%s' % (username,password))
authheader =  &quot;Basic %s&quot; % base64string
request.add_header('USER_AGENT', USER_AGENT)
request.add_header('AUTHORIZATION', authheader)

try:
  response = urllib2.urlopen(request).read()
  print response
except urllib2.HTTPError, e:
  print e.code
  print e.read()
except urllib2.URLError, e:
   if hasattr(e, 'reason'):
    print e.reason
   elif hasattr(e,'code'):
    print e.code
Desktop – IronPython

Since I’ve written mySecs in IronPython, I’ve to translate the above into IronPython. As ipy is a relatively new entrant, there is not much of documentation on how to do it. Here is what I’ve done.

import System
from System.IO import StreamReader
from System.Net import HttpWebRequest, NetworkCredential, WebException

url = 'http://localhost:8000/api/auth/'
username = 'mysecs@gmail.com'
password = 'mysecspwd'
USER_AGENT = 'mySecsWndClient'


req = HttpWebRequest.Create(url)
req.UserAgent = USER_AGENT
auth_string = '%s:%s' % (username,password)
authheader =  &quot;Basic %s&quot; % System.Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(auth_string))
req.Headers.Add('AUTHORIZATION', authheader)

try:
    rsp = req.GetResponse()
    response = StreamReader(rsp.GetResponseStream()).ReadToEnd()
    print response
    rsp.Close()
except (Exception, WebException), e:
    print 'Error: %s' % e

Applies to: GAE 1.1.1; Django 0.96 with helper; IronPython 1.1

Reference:

AuthSub Authentication for Web Applications

Basic Authentication – Authentication with Python

Written by Joseph Jude

September 18th, 2008 at 1:07 pm

Posted in Programming

Tagged with

Python for mobile programming?

with one comment

To tell you the truth, I like the Python language. A lot.

It is simple to learn; easy to read and plenty of tools are available that makes the developer very productive. Converting an idea into implementation is faster than many other languages that I learnt over the years.

Not only excellent GUI and Web frameworks are available in Python; but the language itself is getting ported into other VMs too – like .NET and Java VM.

I choose Django for web development and IronPython for Windows desktop development. So far they have been excellent choices.

Continuing on the love of Python, I looked for a Python framework/tool for mobile development. I was (disappointed and) surprised that there are not many options. In fact only Nokia has released a mobile SDK in Python. I don’t have a Nokia and so I can’t use the application that I develop. That won’t motivate me much. Would it?

As I continued my search, I came across Jython. I was ‘wow’ing since I assumed that I can now start writing mobile (J2ME) applications in Python. Installing Jython is easy. However it is currently used only for desktop and server programming; not for mobile programing.

I spent almost a week trying to write a J2ME program in Jython. Porting a simple ‘HelloWorld’ to Jython is easy. Issue starts immediately after that – preverifying the application and deploying into the device is either not possible or it is just overly complicated.

So after a week of R&D and banging my head against the wall, I’m giving up on mobile development in Python. May be I’ll visit Jython (or any other Python related language) sometime in the future and may be then there will be lot more players than just Noika.

Written by Joseph Jude

September 10th, 2008 at 11:48 pm

Posted in Programming

IronPython Learnings

without comments

Recently I started developing Windows Desktop Applications in IronPython – a Python language port for Windows.NET. I blogged about the application already here.

In the post, I’m sharing what I learnt in the process with a hope that it will be helpful to someone else.

Visual Studio as ipy development environment

You can download IronPython and start to develop using your favorite text editor. However having a Visual Studio environment helps. Fundamentally it involves:

  • Download and install ironpython
  • Download and install Visual Studio SDK
  • Run IronPython Integration Sample from ‘C:\Program Files\Visual Studio 2005 SDK\2007.02\VisualStudioIntegration\Samples\IronPythonIntegration’
Designing Windows Forms

With Visual Studio Integration, designing of Windows Forms is a simple drag and drop operation. Of course code generated by VS may not be as elegant as some Python purists like; but it works well.

Calling unmanaged code from IronPython

If you are into IronPython, you’ll definitely come across VoidSpace, who has a huge collection of IronPython articles.

I needed to query the active window for my application. VoidSpace discusses a solution in his pages. Better to name the namespace as a ‘UnmanagedCode’ as VoidSpace discusses in the solution. I named it as the name of the application and that created a lot of trouble and was difficult to troubleshoot.

Logging in IronPython

IPY doesn’t include standard python libraries (at least as of 1.1). So I used a NSpring .NET logging library.

(I found out that any .NET library can be used with IronPython. I did same with SQLite Library and SourceGrid, a grid library for .NET)

Finding time difference

One can find the time difference between two time variable (=System.DateTime.Now) using

timeSpent = currentEndTime.Subtract(currentStartTime)

If you want to know the differences in seconds then use

timeSpentinSec = timeSpent.TotalSeconds.ToString()

timeSpent.Seconds() will provide only the seconds part of the timespan. TotalSeconds() provide the time difference in seconds.

Don’t forget to stop timer

I used Timer() to poll activewindow on a regular interval. However, I forgot to stop that assuming that once the form is closed all instances will be cleared. Apparently not and so the application stayed in memory even after exit. Lesson learnt: stop all threads before exiting the application.

Written by Joseph Jude

September 7th, 2008 at 1:07 pm

Posted in Programming

Tagged with