Query the windows registry with python

June 16th, 2009

To get values from the windows registry using the python language use snippet below.

This was tested with Python 2.6

References: http://docs.python.org/library/_winreg.html

Python

Postfix Quick Reference

June 3rd, 2009

These are the most usefull commands for Postfix queue management:

Print a queue: postqueue -p

Delete a message: postsuper -d <Queue ID>

Delete all messages: postsuper -d ALL

Display a message: postcat -q <Queue ID>

Linux

WP-Syntax tryout

May 31st, 2009

Following last post on WordPress Code Snippet plugin this post will be the tryout of WP-Syntax – a code format plugin that uses GeSHi supporting a wide range of languages. That includes Perl, one of the languages I frequently use.

Unfortunately there’s no interface to easily add a code snippet. It is necessary to change to post HTML view and add HTML pre tags with attributes lang and line.

Below you can see how does a Perl piece of code looks like. Unfortunately when I change from HTML back to Visual the attribute line=”1″ is removed by WordPress.
Another problem are some characters that need be replaced by HTML entities for example less than and greater than characters. I had to manually check if the code was correctly copied and fix some of the lines.
On the positive side, this plugin allows you to have many snippet blocks within the text.

#!/usr/bin/perl -w

use MP3::Info;
use MP4::Info;
use strict;
use Getopt::Long;
use Encode qw(from_to);
use bytes;

###############
sub process {
###############
    my ($src) = shift;

    $src .= "/" if $src !~ /\/$/;
    print "Opening $src\n";
    opendir(my $dir, $src) or die "can't opendir $src: $!";
    while( defined(my $file = readdir($dir))) {
        next if $file =~ /^\.\.?$/;
        my $fileabs = "$src$file";
        if ( -d $fileabs ) {
            process( $fileabs );
            next;
        }
        next if $file !~ /mp3$|m4a$|mp4$/;
        my $func=\&get_mp3tag;
        if ( $file =~/m4a$|mp4$/ ) {
           $func=\&get_mp4tag;
        }
         my $tag = $func->($fileabs) or die "No tag info";
         print "File $file|$tag->{ARTIST}|$tag->{ALBUM}|$tag->{TITLE}\n";
         my ($artist,$album,$title) = ($tag->{ARTIST},$tag->{ALBUM},$tag->{TITLE});
         #foreach my $k ( keys %$tag ) {
         #    print "$k: $tag->{$k}\n";
         #}
         if ( $artist =~ // || $title =~ // ) {
            print "SKIPPING\n";
            next;
         }
         my ($ext) = $file =~ /\.([^\.]+)$/;
         my $newname = "$artist - $title.$ext";
         #if ( $album !~ // ) {
         #   $newname = "$artist - $album - $title.$ext";
         #}
         $newname =~ s/\*|\\|\///g;
         $newname =~ s/ /_/g;
         $newname =~ s/&/n/g;
         $newname = $src . $newname;
         print "$newname\n";
         #from_to($newname, "utf-8", "cp1250");
         #print "$newname\n";
         #from_to($fileabs, "utf-8", "cp1250");
         rename( $fileabs, $newname ) or warn("Unable to rename $fileabs to $newname: $!\n");
    }
    closedir($dir);
}

my $usage = <<END
        Usage: $0 --src=<source-directory>

END
;

die($usage) if @ARGV != 1;

my $src;
GetOptions (
	"src=s" => \$src,
);

if ( ! -d $src ) {
    die($usage);
}

process( $src );

Wordpress

Wordpress Code Snippet plugin tryout

May 31st, 2009

Code displayed on web pages need formatting to ease reading. This post is my tryout of WordPress Code Snippet plugin.

After installation the plugin adds a code snippet box at the bottom of the Add/Edit post window and a drop down box with languages. You can select one from the following languages: HTML/XML, CSS, Javascript, PHP, SQL, C#, VB, C++, Java, Python, Ruby, Delphi.

I copy pasted some ancient C++ code and the formatting is really nice and has great syntax hightlighting.

The only problem seems to be that you can only have one snippet per post and not within text.

Wordpress

Allow access to NTFS formatted external storage

April 12th, 2009

Having your external usb hard drive or usb pen drive formatted with the NTFS file system will certainly put you into trouble. If not problems like being unable to listen to songs on your mp3 auto radio or mp3 hi-fi stereo (normally only able to read FAT32 formatted drives), you might not be able to create or change existing files on different computers (or under different users).

This problem has two solutions:

  1. Backup the data, format the drive to FAT32 and restore the data back into the drive – the only solution if you want auto-radios and hi-fi equipment to read your drive contents.
  2. Make NTFS permissions less restrictive.

The first one is the easier one if you have enough space to backup your data.

The second one is a bit more tricky to implement but I’ve found a web page that has very clean instructions on how to do it. So follow the link if you want this solution:

http://ask.metafilter.com/38252/Can-not-delete-files-on-portable-drive

To ensure that I have these instructions all the time, I copy/pasted here:

What I suspect is happening is this:

1. Your external drive is formatted with the NTFS file system, which supports access restrictions.

2. The permissions on the files in question have become set to “Read and Excecute” for Users or Everyone (probably Everyone), and to Full Control for one specific user. These settings are typical for files created inside the My Documents folder of Computer Administrator users who haven’t elected to make their files private.

3. The folders you’re working with have Create Files and Create Folders permissions for Everyone on themselves and their subfolders, and Full Control permissions for the dummy user CREATOR OWNER on subfolders and files. Which means that anybody can create files or folders, but only the user who created them can change or delete them. This is a fairly typical setting for the root folder of newly formatted drives.

You can check the permissions on any file by right-clicking the file, selecting Properties, then clicking on the Security tab. If you don’t have a Security tab on file or folder property sheets, you need to do one of two things depending on whether you’re running XP Home or Professional.

For XP Professional: open a Windows Explorer window, then select Tools->Folder Options->View; scroll down to the bottom and turn off Simple File Sharing.

For XP Home: run the “Security for folders and files” patch from this website.

Things you should know about NTFS permissions to make sense of what’s going on:

1. When you copy a file into a folder on an NTFS volume (using copy/paste or right-click-drag/drop/Copy File(s) Here), the file will inherit permissions from the folder you copy it into.

2. When you move a file from one folder to another (using cut/paste or drag/drop), the file will keep its existing permissions.

3. As well as a bunch of permissions, every NTFS file and folder has an owner, which can be a user or a security group. The owner has full control over the file or folder regardless of any permissions that are set on it.

4. There are some users and security groups built in to Windows (Everyone, Administrator, Administrators etc) that have predefined Security ID’s which are the same on all machines. Other users and groups have SID’s derived from that of the machine they’re defined on. So if you give user Joe permission to access a file on machine A, that doesn’t necessarily mean you can plug the drive into machine B, log on as Joe and get access to the same file.

5. Members of the Administrators security group have the right to take ownership of any file or folder, either for themselves or for the Administrators group as a whole.

For an external drive that is going to be used on several different computers, I recommend you set the NTFS permissions very loose, and rely on your physical possession of the drive for the security of its files:

1. Connect the drive to any Windows XP machine on which you have a Computer Administrator logon.

2. Log on as an administrator.

3. Double-click My Computer.

4. Right-click the drive and select Properties. If the property sheet doesn’t have a Security tab, enable them as described above and then come back to this step.

5. Click the Security tab.

6. Click the Advanced button. This should bring up the Advanced Security Settings dialog for the drive, which will have some more tabs on it.

7. Click the Owner tab.

8. Click the line for the Administrators group, check “Replace owner on subcontainers and objects”, and click OK. If you’re asked about replacing existing permissions with ones giving you Full Control, click Yes.

9. The Advanced Security Properties box should eventually disappear, leaving you looking at Security tab on the original Properties sheet again. For each entry in the “Group or user names” list in turn, click on that entry and click Remove.

10. Click Add. This will bring up the “Select Users, Computer or Groups” dialog.

11. Under “Enter the object names to select”, type Everyone and click OK. You should see that Everyone has been added to the list of users and groups.

11. Click Everyone, and check Full Control.

12. Click Apply.

13. Click Advanced again to get the Advanced Security Settings dialog box back.

14. Check “Replace permission entries on all child objects with entries shown here that apply to child objects” and click OK.

15. Click OK.

You should now be able to read and write all the files in that folder and all its subfolders regardless of which computer you’re using or who you’re logged on as.
posted by flabdablet at 9:09 PM on May 15, 2006 [1 favorite]

If you want to make the permissions a little more conventional, you can repeat steps 10 and both elevens (oops) to add Full Control permissions for Administrators and SYSTEM. But since Everyone is, well, everyone, this isn’t strictly necessary.
posted by flabdablet at 9:13 PM on May 15, 2006

Windows

Civilization 4 tips and tricks

April 5th, 2009

I’m addicted to Civilization. That’s the only game I keep playing and I played all the games in the series.

Civ 4 is the best one (not surprisingly) but it can be very hard to win when you increase the difficulty level.

Here are some tips and tricks:

  • Secure your cities and resources but remember that military units cost money.
  • Science, new cities and unit upgrades also cost money so keep your eye on the money…
  • Do not ignore terrain defence bonus: do not cross rivers to attack unless your unit has the amphibious promotion or you’re sure it can destroy the defending unit.
  • Make sure your civ builds the Chichen Itza (all civs want this one) and The Pentagon military wonders – at least make sure you know who built them.
  • Chop your forests and jungles for increased production in your city. Do it when you need it for building wonders for example. To chop faster use the chop action instead of any of the other actions that also remove forests and jungles.
  • Do not use your special people immediately when they are born. Use great engineers to build wonders and great scientists to build academy improvement (each city can have one). Great artists can be used to create great work in cities you need to expand your borders (to access resources).
  • Build cottage terrain improvement for money.
  • Use catapults, cannons and artillery to destroy city defence and damage target city units (when those units attack a city they produce collateral damage on other defending units). You’ll have to build many of them because you’ll probably lose them – but you’ll keep the units that can defend your conquered cities.
  • Make sure you expand your civ but don’t do it too quickly because you need to pay for city maintenance!
  • Go to war early. Control your continent. Even if you get behind in science earlier in the game (make sure you have the state-of-the-art military units). Attacking from sea is much more difficult.
  • Use bombers and fighters to find enemy units using recon missions.
  • Build the Internet!

There are many more tricks to this legendary game! Explore, try new approaches to the game and let me know about your findings!

Games

Save images embedded on Microsoft Word documents

March 23rd, 2009

I got a query from a friend on how to save/extract images embedded on Microsoft Word documents. At first I thought that would be an easy task that I could easily find on a menu or right clicking the image. Well I was wrong.

I oppened up a document and inserted an image. Now the fastest way I could get the image off the file was to right-click on it and select copy and then openning up Microsoft Paint at Start > Programs > Acessories > Paint and copying the image into it using CTRL+V or paste option of edit menu.

Then I could save it as an image file. Problem solved.

Enjoy!

Windows

Clean up folders easily using Windows Explorer search feature

March 18th, 2009

Let’s assume the following scenario: you have an hierarchy of 30 folders with 3 levels. On those folders you have documents, text files, zip files, pdf files and audio and picture files all mixed up.

You want for example to easily select all document files and move them to a document dedicated folder. Traversing all hierarchy and moving files manually is time consuming.

You can efficiently do this by using the search functionality on Windows Explorer.

Using Windows Explorer go to the parent folder of the directory structure you want to clean up. Click Search on your toolbar.

The Search Companion column will show up at your left: “What do you want to search for?”.

You can try and play with the search options but for this example let’s say you select All files and folders.

You want to search for documents so let’s enter .doc at the box All or part of the file name and confirm that the Look in drop down box has our parent folder. It’s confirmed so let’s hit the Search button.

All your document files will show up as result of your search. Now you can manipulate them as you usually do on Windows Explorer disregarding they are on different folders.

So we can hit CTRL+A to select them all and then drag them to a target folder making sure SHIFT is pressed while dragging (to move the files instead of copying them) – or using CTRL+C at the selected list and then CTRL+V at the target folder – or using right-click cut at the selected list and right-click paste at the target folder. Use the method you are most used to.

And this should be enough to move all documents stored across folders to one target folder, easily.

You can use the same technique to delete files with certain patterns for example. Just remember that the same operation you can do on files at the same folder you can do to any group of files across your file system in one go.

Have fun!

Windows

Windows XP: Mounting disk drives over folders

March 7th, 2009

Following last post on how to change a disk drive letter you can use the same instructions to mount a disk drive into a folder.

After right-clicking your disk device and selecting Change Drive Letter and Paths you can click Add… and then browse to an empty folder that will be your mount point.

So you can for example create a C:\Documents folder on your system disk and mount an external hard drive on it. Or you might create a C:\Program Files 2 directory and mount a different disk.

Then if you use your external disk on any other computer you can mount into the same mount point and refer to your files the same way.

That’s it =)

Have Fun!

Windows

Windows XP: change the disk drive letter

March 7th, 2009

Recently I had to start installing applications on a removable/external hard disk due to lack of free space on my system/internal hard drive. Installing applications on Windows means registering them with the system, having shortcuts created and so on – they get integrated into the system. This means that the system will need to find files for those applications and it will use the full path for those files. That includes the hard drive letter that Windows assigns to storage devices (hard drives, cd-roms, usb memory cards, etc).

Unfortunately Windows doesn’t consistently assign the same drive letter to the same device and this makes things complicated. Fortunately Windows will only mess around your device letters if you start up your computer after connecting devices that you don’t usually connect. For example I normally connect my usb 3g modem, an usb hub with a keyboard and mouse connected into it and an external disk drive – and I connect them to the same usb ports every time. This makes Windows use the same device letter consistently. But if I connect an ebook reader with a memory card on it that will add 2 storage devices to the system and make Windows change the drive letter configuration.

The temporary solution I found was:

  1. Open Control Panel
  2. Open Administrative Tools (Windows XP Professional)
  3. Open Computer Management
  4. Select Disk Management under Storage
  5. An applet will open. Right-click on the drive you want to modify the drive letter.
  6. Select Change Drive Letter and Paths
  7. Select the drive letter and click Change.
  8. Select the drive letter you want to change to from the drop down list.

That’s it! you made it =)

Have fun!

Windows