Monday, February 16, 2009

Python - Google translator (pytranslator)

While working on internationalization (i18n), I had to translate a whole bunch of text statements from English to German! I didn't wanted to access http://google.com/translate in the browser for every text to be translated and do the routine copy paste. So I decided to write a python program which translates text across google support translation languages. Its a python module that one can import and use it based on their needs. For example to translate an entire GNU portable object file(.po) from one language to another.
I call it the "pytranslator"
import sys
import urlib
import re

langCode={ "arabic":"ar", "bulgarian":"bg", "chinese":"zh-CN", "croatian":"hr", "czech":"cs", "danish":"da", "dutch":"nl", "english":"en", "finnish":"fi", "french":"fr", "german":"de", "greek":"el", "hindi":"hi", "italian":"it", "japanese":"ja", "korean":"ko", "norwegian":"no", "polish":"pl", "portugese":"pt", "romanian":"ro", "russian":"ru", "spanish":"es", "swedish":"sv" }

def setUserAgent(userAgent):
    urllib.FancyURLopener.version = userAgent
    pass

def translate(text, fromLang="English", toLang="German"):
    # urllib.FancyURLopener.version = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008070400 SUSE/3.0.1-0.1 Firefox/3.0.1"
    setUserAgent("Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008070400 SUSE/3.0.1-0.1 Firefox/3.0.1")
    try:
        post_params = urllib.urlencode({"langpair":"%s|%s" %(langCode[fromLang.lower()],langCode[toLang.lower()]), "text":text,"ie":"UTF8", "oe":"UTF8"})
    except KeyError, error:
        print "Currently we do not support %s" %(error.args[0])
        return
    page = urllib.urlopen("http://translate.google.com/translate_t", post_params)
    content = page.read()
    page.close()
    match = re.search("<div id=result_box dir=\"ltr\">(.*?)</div>", content)
    value = match.groups()[0]
    return value
~m.a.v.e.r.i.c.k

Sunday, October 12, 2008

Python - Windows registry access (pyregistry)

The python module which is given below - pyregistry is a wrapper around _winreg module. It greatly simplifies registry access, which is more complicated if _winreg is used directly. It implements readSubKeys, readValues and pathExists functions.
readSubKeys - Returns an array of all the sub keys, -1 if the registry path doesnt exist.
readValues - Returns a hash/dictionary of all the values under the key, -1 if the registry path doesnt exist.
pathExists - Returns True if the registry path exists, False otherwise
The docstrings explains the usage of these functions.
from _winreg import *
mapping = { "HKLM":HKEY_LOCAL_MACHINE, "HKCU":HKEY_CURRENT_USER, "HKU":HKEY_USERS }

def readSubKeys(hkey, regPath):
    if not pathExists(hkey, regPath):
        return -1
    reg = OpenKey(mapping[hkey], regPath)
    subKeys = []
    noOfSubkeys = QueryInfoKey(reg)[0]
    for i in range(0, noOfSubkeys):
        subKeys.append(EnumKey(reg, i))
    CloseKey(reg)
    return subKeys

def readValues(hkey, regPath):
    if not pathExists(hkey, regPath):
        return -1
    reg = OpenKey(mapping[hkey], regPath)
    values = {}
    noOfValues = QueryInfoKey(reg)[1]
    for i in range(0, noOfValues):
        values[EnumValue(reg, i)[0]] = EnumValue(reg, i)[1]
    CloseKey(reg)
    return values

def pathExists(hkey, regPath):
    try:
        reg = OpenKey(mapping[hkey], regPath)
    except WindowsError:
        return False
    CloseKey(reg)
    return True
~m.a.v.e.r.i.c.k

Tuesday, June 24, 2008

IPhone's coming to India!!!

Yes you heard it right. Yipee!!
My day was made when I saw the ad on TOI front page dated 25/06/2008. After months for months Vodafone has officially started advertising the arrival of iPhone on their network in India. Whats more, its the latest version, iPhone-3G(8GB) with iPhone Software-2.0 thats coming to the market.

Based on what I heard from my confidential sources, its dated to be launched on July 11th.
Pricing not sure yet. I got 2 price quotes from different sources - 10/11K and 27/28K. (Disclaimer)
You can also book your iPhone by SMSing iPhone to 56789. No, I am not advertising and its
not an advertisement, though it sounds like one ;-)

Shell Scripting - Dereferencing a variable

Lets assume that we have a variable(var1) what holds a value(var2) which is in-turn another variable name. So can we access the value stored in the second variable(var2) using the first variable(var1)? With the power of bash you can!
$>var2="value"
$>var1="var2"
$>echo ${!var1}
value
~m.a.v.e.r.i.c.k

Wednesday, June 18, 2008

Firefox 3 - Download Day!

June 17th(yesterday) is when Firefox attempted to set a new Guinness record for the most number of downloads in 24 hours. The count is still on! Lets help them achieve their milestone. Just for the records, there is no existing record for the most downloads yet! ;-)

I am surprised that going by the Web browser stats, IE ranks as the No.1 browser of choice by a huge margin.
Internet Explorer - 83.27%
Mozilla Firefox - 13.76%
I cant possibly conceive how 83.27% of net users use IE!!! If for some strange reason, you are an IE user, try Firefox once and most likely you will stick with that.

FireFox - Rediscover the Web

~m.a.v.e.r.i.c.k

Sunday, June 08, 2008

Python - MD5 checksum of a file

The below given python module implements a function called md5 which returns the md5 hash of a given file. So whats so special about it? In Python-2.5 we have the "hashlib" module which computes
the md5 hash, sha1, sha256, sha384 etc of the file. Its usage is also pretty straight-forward. So why the heck this new module?

Well because it is implemented to perform 2 additional features - which I appropriately call it as includeLine and excludeLine.
excludeLine - Lets say that you want to compute the md5 hash of a file, but without a particular line in the file. You will need this when you have embeded the md5sum of the file in the file itself and you want to
verify the contents of the file upon receival.
import md5sum
md5sum.md5("trial.txt",excludeLine="md5sum-value")

// Compute the md5sum of "trial.txt" without the lines that starts with md5sum-value
includeLine - Here's the scenario. You want to compute the md5 hash of a file, but you want to pepper the file contents with some secret word. In such a scenario you can pass the secret word to the includeLine param.
import md5sum
md5sum.md5("trial.txt",includeLine="mirchi")

// Gives you the md5sum of the contents of trial.txt + "mirchi"

Enough of talking. Here is the code...
#!/usr/local/bin/python

import os, sys
import hashlib

def md5(fileName, excludeLine="", includeLine=""):
    """Compute md5 hash of the specified file"""
    m = hashlib.md5()
    try:
        fd = open(fileName,"rb")
    except IOError:
        print "Unable to open the file in readmode:", filename
        return
    content = fd.readlines()
    fd.close()
    for eachLine in content:
        if excludeLine and eachLine.startswith(excludeLine):
            continue
        m.update(eachLine)
    m.update(includeLine)
    return m.hexdigest()

if __name__ == "__main__":
    for eachFile in sys.argv[1:]:
        print "%s %s" %(md5(eachFile), eachFile)
~m.a.v.e.r.i.c.k

Saturday, May 24, 2008

Shell Scripting - Trick2 (String processing)

Useful string processing in bash. Very handy ones!

Length of a string:
$> trial="abc123"
$> echo ${#trial}
6
Substring extraction:
${string:x:y} - Extracts the substring of y characters from position x in $string.
$> trial="abc123def"
$> echo ${trial:1:5}
bc123
$> echo ${trial:2} (If y is missing then the substring is extracted till the last character)
c123def
Substring removal:
${string#substring} - Removes the shortest matching $substring from front of $string
$> trial="abc123def"
$> echo ${trial#abc}
123def
$> echo ${trial#a*d} (Yes wild characters work too!)
ef
${string##substring} - Removes the longest matching $substring from front of $string
$> trial="abc123defgd432"
$> echo ${trial##a*d}
432 ( Compare this output with echo ${trial#a*d} )
${string%substring} - Removes the shortest matching $substring from the back of $string
$> trial="/tmp/ldap/quality/deleteuser.c"
$> echo ${trial%.c}
/tmp/ldap/quality/deleteuser
(1 practical use of the above is when you want to compile a C file with the -o option, and the o/p file is the C file without the extension)
${string%%substring} - Removes the longest matching $substring from the back of $string
$> trial="abc123defgd432"
$> echo ${trial%%2*2}
abc1
~m.a.v.e.r.i.c.k

Shell Scripting - Trick1(Generating all combinations of 2 set of characters)

Thought I will start sharing a few bash scripting tips and tricks I learnt and use in my day to day life. Here is my first one...
$> echo {a,b}:{1,4}
a:1 a:4 b:1 b:4

$> echo {romeo,juliet}\ {789,143}
romeo 789 romeo 143 juliet 789 juliet 143

$> echo {a..z}:{1,2,3}
a:1 a:2 a:3 b:1 b:2 b:3 c:1 c:2 c:3 d:1 d:2 d:3 e:1 e:2 e:3 f:1 f:2 f:3 g:1 g:2 g:3 h:1 h:2 h:3 i:1 i:2 i:3 j:1 j:2 j:3 k:1 k:2 k:3 l:1 l:2 l:3 m:1 m:2 m:3 n:1 n:2 n:3 o:1 o:2 o:3 p:1 p:2 p:3 q:1 q:2 q:3 r:1 r:2 r:3 s:1 s:2 s:3 t:1 t:2 t:3 u:1 u:2 u:3 v:1 v:2 v:3 w:1 w:2 w:3 x:1 x:2 x:3 y:1 y:2 y:3 z:1 z:2 z:3
~m.a.v.e.r.i.c.k

Sunday, January 20, 2008

VMware Server Installation

Couple of days back one of my colleagues approached me seeking my help in installing and configuring VMware Server. He had apparently done everything right, but he was not able to have VMware up and running. It was throwing up some weird error while compiling the kernel modules. He had googled around with the error message, did almost everything each of the links said, but all his efforts were in vain. I decided to give it a shot. I cleaned up his machine and proceeded to install Vmware server -1.0.4. I followed my usual default installation procedure and hoolah it started working !!! He had not installed vmware-any-any-update. I later realised that installing vmware-any-any-update patches is one of the steps most people miss out when the installation fails. After this incidence I decided to put down the steps on my blo. I am pretty much sure that the info I put in here might already be duplicated all over the internet, but all the time and energy spent on putting up this post, would be worth while if atleast 1 person would get benefited from this entry. So here goes....

Step-1 (PRE-REQUISISTES): You will need to have the following packages installed before you start installing VMware-server.
  • gcc, gcc-c++ (A must for SLES9, SLES10 as execvp gets shipped with gcc-c++).
  • lib-gcc.
  • glib, glibc, glibc-devel.
  • kernel-headers (The version should exactly match the kernel version on which you are installing. `uname -r` reveals the current kernel vresion that is loaded).
  • xine-td
Note: You can install all the above mentioned packages by using automated update managers that gets shipped with your Linux distro (For FedoraCore you can use yum, yast/yast2 on openSuSE, apt-get on Ubuntu flavours etc).

Step-2 (DOWNLOAD): Download the latest version of VMware-server from http://www.vmware.com/download/server/. This post, as of now, explains on how to install and configure the product from an rpm. So download the binary(*.rpm) file. You will have to accept the EndUserLicenceAgreement and then proceed for the download. Copy the product key (if any) displayed.

Step-3 (INSTALLATION): Install the rpm downloaded by running the command
$> rpm -ivh VMware-server-<version>-<build-id>-i386.rpm (as root user)
gt; rpm -ivh VMware-server-<version>-<build-id>-i386.rpm (as root user)

Step-4 (VMWARE-UPDATES): Download the latest vmware-any-any-update.tar.gz from http://knihovny.cvut.cz/ftp/pub/vmware/. Wondering what vmware-any-any-update is? Read this http://communities.vmware.com/thread/26693.
Do the following -
$> tar -zxvf vmware-any-any-update<xxx>.tar.gz
$> cd vmware-any-any-update<xxx>
$> ./runme.pl (Accept the prompts and proceed further) (as root user)

Step-5 (CONFIGURATION): Once thats done, proceed to run vmware-config.pl and configure your vmware installation to suit your needs. Thats it folks!!! Your vmware is all setup for your usage.
$> vmware (Have fun)

~m.a.v.e.r.i.c.k