import wnck
import gtk
scr = wnck.screen_get_default()
while gtk.events_pending(): # This is required to capture all existing events
gtk.main_iteration()
def window_switch_handler(screen,window):
# Get Current window, then the application and the name from it
cur_win = screen.get_active_window().get_application().get_name()
print "Currently showing %s" %cur_win
scr.connect("active-window-changed", window_switch_handler)
gtk.main()
After the last line "gtk.main()", the python interpreter will hang waiting for events to be serviced. Now if you switch any of the windows, you would see the name of the application displayed on the console.
Im working on a similar setup for KDE (using PyQT). I will post a similar setup after I try it at work tomorrow (I dont have a KDE Setup at home).
As for Windows, it turns out to be much more difficult. The PyAA seems to have stuff for WM_ACTIVATE and WM_DEACTIVATE but its compiled against Python 2.4 and noone has a later version available (unless its built). Im going to try 2 things.
* Compile PyAA with Python 2.5 or Python 2.6
* Use AutoIT for automating windows tasks. Its non-python and could be an issue when I compile all three interfaces together, but atleast it would provide some way to do it.
I dont know of any such automation for Mac OSX, but thats partly due to my lack of knowledge on that OS.
Sunday, November 8, 2009
Using WNCK for capturing Gnome window events
For those new to WNCK (that includes me as well), it stands for Window Navigation Construction Kit and is used for capturing all window based events in GNOME.
You could use python's pygtk to do some window management logic. For e.g. Find below some snippets about capturing when a window got switched. The below snippet will print the name of the application whenever any of the GNOME Windows switches (from Alt-Tab or clicking on window on the taskbar)
Saturday, October 31, 2009
Bypass the ON UPDATE trigger in mysql
Recently at work, I came across a problem where I had to bypass the ON UPDATE trigger for a particular table. I dint want this to happen as I was making manual updates on the backend and dint want the change to get reflected on the UI. To prevent this, you could add the column in the UPDATE clause (even though it doesnt have to change). Something like
CREATE TABLE test (
id integer not null auto_increment,
name varchar(20) not null,
upd_date datetime default current_timestamp on update current_timestamp
);
................
If I just do a
UPDATE test set name = 'raja' where id = 2;
that would update the upd_date column as well.
IF I want to avoid the upd_date column from getting updated, you could do a
UPDATE test set name = 'raja', upd_date = upd_date where id = 2;
Labels:
howto
Sunday, October 18, 2009
Adding code snippets to Blogger
As mentioned earlier, I was using Wordpress for my blogging attempts, but since I never went past 2 or 3 posts in total, its not something worth mentioning.
But what wordpress had was easy support for code snippets.
Since this is going to be a technical blog, I needed ways to display code properly and in Wordpress, it was pretty simple using the "sourcecode" custom tag.
The equivalent of this is SyntaxHighlighter. View this post for a description as to how to add code snippets for blogger.
Since this is going to be a technical blog, I needed ways to display code properly and in Wordpress, it was pretty simple using the "sourcecode" custom tag.
The equivalent of this is SyntaxHighlighter. View this post for a description as to how to add code snippets for blogger.
Add Search Engine - Firefox and IE7
Artifacts (Issues, comments, discussions) drive most of what we do at my day job. During an iteration meeting today, I found that most of us know the artifact id for getting to a page but the route to get to that page is atleast 2 clicks away (2 clicks before you get to the page to enter artifact id and one more to get to the actual page).
I was wondering if it would be possible to use the "search engine" helpers that are available in Firefox 2 and IE 7. That would just make us enter the artifact id in the "Search engine" box and it takes us directly to that page (ofcourse after you login once to the application).
Luckily the OpenSearch Specification helps us here. Just Create an xml that adheres to the Opensearch specification and loading that page in both Firefox(2 and above) and IE(Version 7 and above) adds those to the list of search engines managed by your browser. There is a good tutorial at mozilla developer center. Ill cover the basics here. If you are interested, I can even make an application that lets you fill in the details and will post the xml file that you can use for adding your own search engine. Please add a comment if you are interested.
Without further ado, here is the minimal template for the xml file
Firefox (Only 2 and above):
Internet Explorer 7:
Note: I wasnt able to get this working on a Firefox 1.0 browser with the above steps. But it looks like the file is located in your system at <Home Directory>/.mozilla/firefox/<encrypted profile dir>/searchplugins directory. I need to check if just copying the plugin.xml into this directory and a subsequent restart will have the same effect. I will confirm this.There are other interesting things you can do with this. You can add an image to the searchbar by adding a "Image" tag with the contents of the image base64 encoded. Also for Mozilla based browsers, there is a Suggestion URL that can be used to retrieve the list of possible suggestions as you start typing. This should be a cool one to do and I will post a tutorial for that.
Luckily the OpenSearch Specification helps us here. Just Create an xml that adheres to the Opensearch specification and loading that page in both Firefox(2 and above) and IE(Version 7 and above) adds those to the list of search engines managed by your browser. There is a good tutorial at mozilla developer center. Ill cover the basics here. If you are interested, I can even make an application that lets you fill in the details and will post the xml file that you can use for adding your own search engine. Please add a comment if you are interested.
Without further ado, here is the minimal template for the xml file
Call this file, say, plugin.xml in your web root directory. For a simple Apache server install, place this in /var/www (or whatever your DocumentRoot is). An example of the template modified for Google Search Engine is shown belowSome Short Name that appears in the Search Engines box Long Description
Save that as google-plugin.xml. The thing to note above is the {searchTerms} in the URL Tag above. This will be replaced with whatever is entered in the searchbox. So if you entered "test", the url will be http://www.google.co.in/search?hl=en&q=test and will take you to that page.Using this as a template, you could modify any REST-ful GET URLs that will retrieve your page. Heck, it would be nice if all webapps have this behaviour as we are all used to entering things in the Search URLs rather than a few clicks.My Google Google search implemented as firefox addon
Deploying this in Browsers:
Firefox (Only 2 and above):
- Create a HTML File with this tag in the HEAD section of the file
<link rel="search" type="application/opensearchdescription+xml" title="Some identifiable text" href="/plugin.xml" />
- In the above example, replace the title with what you want to see in the Search Engine box and href with path to your Opensearch XML File. In my case, its in the DocumentRoot, so the /plugin.xml.
- Load the html file in your system.
- Once the page is loaded, you should see a "Add <title>" in the Search engines list.
- Click on the Button and you should have that plugin added right into your browser.
- Make that your default section by going to "Manage Search Engines" or just select it to enter text in there which will take you to google
- If you need to delete this,go to "Manage search engine" and Remove it
Internet Explorer 7:
- Load the plugin.html directly in your browser
- In the Search List, click on the button that says "Add Search Providers" and click on the one that you just added
- Once its done, the plugin should be added in your search list
- Make that your default or select it to enter search terms
- As in Firefox, to delete it, go to "Manage engines" and delete it
Note: I wasnt able to get this working on a Firefox 1.0 browser with the above steps. But it looks like the file is located in your system at <Home Directory>/.mozilla/firefox/<encrypted profile dir>/searchplugins directory. I need to check if just copying the plugin.xml into this directory and a subsequent restart will have the same effect. I will confirm this.There are other interesting things you can do with this. You can add an image to the searchbar by adding a "Image" tag with the contents of the image base64 encoded. Also for Mozilla based browsers, there is a Suggestion URL that can be used to retrieve the list of possible suggestions as you start typing. This should be a cool one to do and I will post a tutorial for that.
Labels:
howto
nxclient setup tricks
NoMachine
is a uber cool alternative to VNC or any of the remote access
solutions. Whats more, its very secure as it runs on top of ssh and
doesnt need any special ports to be managed. You can use your regular
ssh access to get into your box and its pretty fast too.
However, there are specific issues when installing the nxserver and nxclient. Ive attempted to mention a few of the problems and possible steps for getting an error free install and happy computing :)
1. Download the nxserver, nxclient and nxnode from here (Assuming the server is on Linux). Install the rpms or deb packages on the Server.
2. Download the nxclient and install it on your client machine.
3. Generate a public-private key combination on the client machine.
4. Now we need to check how we can use NXClient to connect to the server. Copy the public key (the id_rsa.pub from earlier step) into ~nx/.ssh/authorized_keys2 on the server machine. The typical directory for ~nx would be /usr/NX/home/nx.
5. Try and connect to the server from the client. This should succeed without asking for passwords
6. Login into the server as the user you want to connect. In this case, I want to login as "raja"
7. Enter your password.
8. You can run any command once you are in the shell. Play with it before you quit out of the shell.
9. Now we are good with connecting to the server as "nx" from the shell. Try creating a client configuration using the "NX Client for Linux".
10. Mostly this should work and connect you to the server. In cases it doesnt and throw messages like "Authentication User: nx
Using auth method: public key
Authentication failed"
Chances are that the key generated as part of the server may be stale. Go to the server machine and run a keygen
Happy NX Machine!!
If you have issues with connecting, drop me a post and I can try to fix it.
However, there are specific issues when installing the nxserver and nxclient. Ive attempted to mention a few of the problems and possible steps for getting an error free install and happy computing :)
1. Download the nxserver, nxclient and nxnode from here (Assuming the server is on Linux). Install the rpms or deb packages on the Server.
2. Download the nxclient and install it on your client machine.
3. Generate a public-private key combination on the client machine.
raja@binarytech:~$ ssh-keygen -t rsaAfter this, you would have id_rsa.pub (containing the public key) and id_rsa (containing the private key).
Generating public/private rsa key pair.
Enter file in which to save the key (/home/raja/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/raja/.ssh/id_rsa.
Your public key has been saved in /home/raja/.ssh/id_rsa.pub.
The key fingerprint is:
ce:ed:01:c9:f5:60:a9:4d:03:1e:19:a2:b7:8e:32:c0 raja@binarytech.in
4. Now we need to check how we can use NXClient to connect to the server. Copy the public key (the id_rsa.pub from earlier step) into ~nx/.ssh/authorized_keys2 on the server machine. The typical directory for ~nx would be /usr/NX/home/nx.
5. Try and connect to the server from the client. This should succeed without asking for passwords
6. Login into the server as the user you want to connect. In this case, I want to login as "raja"
7. Enter your password.
8. You can run any command once you are in the shell. Play with it before you quit out of the shell.
9. Now we are good with connecting to the server as "nx" from the shell. Try creating a client configuration using the "NX Client for Linux".
10. Mostly this should work and connect you to the server. In cases it doesnt and throw messages like "Authentication User: nx
Using auth method: public key
Authentication failed"
Chances are that the key generated as part of the server may be stale. Go to the server machine and run a keygen
-bash-3.1$ sudo /usr/NX/bin/nxserver --keygen11. Copy the key from /usr/NX/share/keys/default.id_dsa.key into your NX Client wizard and try connecting. This should work perfectly.
NX> 704 Starting: server-keygen operation at: Tue Oct 07 12:36:25 2008.
NX> 704 Generating new ssh-keys. Please wait.
NX> 704 Keys generated correctly. Backing up files.
NX> 704 Back up of keys made. Updating files.
NX> 704 Keys updated. NX clients should now use key:
NX> 704 /usr/NX/share/keys/default.id_dsa.key
NX> 704 to get connected to this NX server.
Happy NX Machine!!
If you have issues with connecting, drop me a post and I can try to fix it.
Labels:
howto
Saturday, October 17, 2009
Time Tracking System in Ruby on Unfuddle
Last year, when I wanted to create a timetracking system for my team, I thought of picking one from the Opensource world but couldnt find one that will fit my needs. I could have customized one of the existing ones, but they were either commercial or too primitive.
I took that as an excuse to do an app in Rails and called it YATSA (Yet Another TimeSheet Application). It has the following concepts
The Features document is available here. The project itself is uploaded to Unfuddle. Unfuddle does not have public access yet, but if you need the code, let me know, I can help create an account on the system for you to access it (The free version only supports 2 users, and Im one of them, Im planning to use the other version as the "public" user). There have been requests from a commercial firm asking to add Projects to this list so we can associate People to Projects. I will be adding those into this system. I will also add instructions on how to set this up, so if anyone is interested, they could use this. There is no license for it right now, but Ill set it up under Apache License 2.0.
- People
- Department
- Activities
The Features document is available here. The project itself is uploaded to Unfuddle. Unfuddle does not have public access yet, but if you need the code, let me know, I can help create an account on the system for you to access it (The free version only supports 2 users, and Im one of them, Im planning to use the other version as the "public" user). There have been requests from a commercial firm asking to add Projects to this list so we can associate People to Projects. I will be adding those into this system. I will also add instructions on how to set this up, so if anyone is interested, they could use this. There is no license for it right now, but Ill set it up under Apache License 2.0.
Labels:
projects,
ruby/rails
Sunday, October 11, 2009
HMAC SHA Signatures using Python for Amazon webservices
Ive recently been working on the Amazon Product Advertising APIs to get some information from Amazon and display it to my users in a different format.
Since Django is such a wonderful framework, I decided to do this using app using Django. The Amazon Product Advertising documentation has examples for Java, C# and PHP, but not much help for Python. There isnt anything unique to Python as the calls are all either REST based or SOAP based, but there was one part that was tricky -- The HMAC SHA Signatures required for REST requests.
For starters, HMAC is abbreviation for keyed-Hash Message Authentication Code. For more information about HMAC, look up the RFC or the wikipedia entry. There is enough information in the documentation about how to make a REST request but none whatsoever about creating the HMAC based signature.
So, after some debugging and browsing through the forums, here is a snippet that will show how to create a HMAC-SHA based signature that you can use with AWS.
In the below code, its assumed that you followed this doc upto Step 7 since there is nothing thats specific to any programming language.
The line for urllib.quote is required because Amazon wants us to quote the output that is gotten from HMAC based signature (Step 9 in the above document).
Hope it helps in doing a Python based HMAC-SHA signature generation for AWS.
Since Django is such a wonderful framework, I decided to do this using app using Django. The Amazon Product Advertising documentation has examples for Java, C# and PHP, but not much help for Python. There isnt anything unique to Python as the calls are all either REST based or SOAP based, but there was one part that was tricky -- The HMAC SHA Signatures required for REST requests.
For starters, HMAC is abbreviation for keyed-Hash Message Authentication Code. For more information about HMAC, look up the RFC or the wikipedia entry. There is enough information in the documentation about how to make a REST request but none whatsoever about creating the HMAC based signature.
So, after some debugging and browsing through the forums, here is a snippet that will show how to create a HMAC-SHA based signature that you can use with AWS.
In the below code, its assumed that you followed this doc upto Step 7 since there is nothing thats specific to any programming language.
import hmac
import haslib
import base64
import urllib
.....
AWS_SECRET_KEY=
.....
request = """
Build request as shown in the above link till Step 7
"""
h = hmac.new(AWS_SECRET_KEY, request, hashlib.sha256)
signature = base64.b64encode(h.digest())
request = "%s&Signature=%s" %(request,urllib.quote(sig))
...
rest_output = urllib.urlopen('https://ecs.amazonaws.com/onca/xml?%s' %(request)).read()
...
The line for urllib.quote is required because Amazon wants us to quote the output that is gotten from HMAC based signature (Step 9 in the above document).
Hope it helps in doing a Python based HMAC-SHA signature generation for AWS.
Labels:
python,
webservices
Subscribe to:
Posts (Atom)

