Tuesday, December 25, 2007

Text to HTML Table Converter 1.2.1: Fixed flag bug in txt2html v1.2

The argsContain(flag) function in the script only checks if the first argument contains 'flag'. Replace argsContain with:


def argsContain(flag):
""" Flag is the letter/word/etc that follows the '-' or '--' in the options """
# search for flag in options part of args i.e. before filename
found = False
for arg in sys.argv[1:len(sys.argv)-1]:
if arg.find(flag) != -1:
found = True
return found



This checks all the flags for the one you want.

Saturday, November 24, 2007

Compiling packages from source on Ubuntu

A default Ubuntu install doesn't come with many compiling tools like GCC, G++, libc6-dev, and Make that you need to compile programs from source. To get all those packages, type in:

sudo apt-get install build-essential

You will need the Ubuntu CD for this.

Jump words with Control-Arrow in the Ubuntu terminal

On a fresh install of both Ubuntu 7.04 and 7.10, pressing Ctrl-[Arrow Key] does not jump words but instead writes a control code onto the screen such as ;5C or ;5D.

To fix this, add export INPUTRC=/etc/inputrc to /etc/bash.bashrc.

Make sure /etc/inputrc has these lines in it:

# mappings for Ctrl-left-arrow and Ctrl-right-arrow for word moving
"\e[1;5C": forward-word
"\e[1;5D": backward-word
"\e[5C": forward-word
"\e[5D": backward-word
"\e\e[C": forward-word
"\e\e[D": backward-word

Tuesday, September 4, 2007

Make gnome-screensaver opaque with Compiz

If you use Compiz and GNOME, you might have noticed that you can still see your screen after you lock it. This is because, Compiz allows gnome-screensaver's window(the window that normally turns your screen black) to be slightly transparent. To fix this, go to the CompizConfig Settings Manager (most likely in System > Preferences), click on General Options and go to the Opacity Settings tab. You will see something like

((type=Menu | PopupMenu | DropdownMenu | Tooltip | Notification | Combo | Dnd | name=sun-awt-X11-XWindowPeer) | (type=Normal & override_redirect=1)) & !(name=sun-awt-X11-XFramePeer | name=sun-awt-X11-XDialogPeer)

This is a conditional statement which tells Compiz which windows to make transparent. You can either add name=gnome-screensaver to the opacity windows listbox and change the opacity to full or you can add it to the original item in the listbox to change it into:

((type=Menu | PopupMenu | DropdownMenu | Tooltip | Notification | Combo | Dnd | name=sun-awt-X11-XWindowPeer) | (type=Normal & override_redirect=1)) & !(name=sun-awt-X11-XFramePeer | name=sun-awt-X11-XDialogPeer | name=gnome-screensaver)


[via www.tedcarnahan.com]

Wednesday, August 22, 2007

Text to HTML Table Converter 1.2

Now has unbuffered reads and better flag support


Unbuffered Input Reading


I added an pseudo-unbuff_read() function to the program (pseudo because it is really a sentinel-terminated loop concatenating a buffered read to a string)

def unbuff_read(END):
""" Reads one char at a time from stdin until reaches a "stop" character """
str = ""

while 1: # no do-whiles or assignments-in-conditionals in python :`(
# Compare the end of the string (same length as END) to END
if str[ len(END) * -1 :] == END:
break
str += sys.stdin.read(1)

return str


The parameter that you pass into this function called END breaks out of the read loop and returns the str read in up to that point. The default END is '\n.\n' which is a newline followed by a period and then another newline. This syntax is the same as the one used to stop reading the message body(DATA) when talking to smtp mail servers.

To quit the program just type in the END variable by itself.



Better flag support


Function argsContain():

def argsContain(flag):
""" Flag is the letter/word/etc that follows the '-' or '--' in the options """
# search for flag in options part of args i.e. before filename
for arg in sys.argv[1:len(sys.argv)-1]:
if arg.find(flag) != -1:
return True
else:
return False


This function goes through each argument before the filename and searches for a specific flag like "f" or "v". The two instances that used to search for flags have been replaced with a call to this method enabling syntax like
$ python txt2html-table-1.2.py -vf input.txt

and

$ python txt2html-table-1.2.py -fv input.txt



Download


This program is Copyright 2007 to Boris Joffe and licensed under GPL 3.0. Open the file to read more of the terms that I released it under. Download Text to HTML Table (txt2html-table) Version 1.2



Debugging


I kept my debugging code which consists of a function called debug() and a commented call to it after main() to make it easier to play around with the code by just uncommenting the call(and maybe commenting main()) and putting code in the debug() function.

Friday, August 17, 2007

Text to HTML Table Converter 1.0

Background


I was recently creating an description for a videogame I was selling on eBay and I needed to convert some text into an HTML table. Since I also was learning Python at the same time, I figured I would make a Python program that would do the job.

Input text format


The input text needs to be at least one line of text or more(seperated by a newline '\n') with a colon seperating the <th> element on the left and the <td> element on the right. You could also have another delimiter separating the two elements by changing the tList = eachLine.split(':') line to split based on whatever character you want.


The general format of the input text file should be:


thElement1: tdElement1
thElement2: tdElement2
...


where the <th> and <td> elements can have spaces, tabs, and other characters in them as long as they aren't '\n' or ':'.

Output HTML Format


The output which is displayed in the console (stdout) and which can optionally be saved to a file has this general format:


<table>
<tr>
<th>thElement1: </th>
<td>tdElement1</td>
</tr>

<tr>
<th>thElement2: </th>
<td>tdElement2</td>
</tr>
</table>


Download


This program is Copyright 2007 to Boris Joffe and licensed under GPL 3.0. Open the file to read more of the terms that I released it under.
Download Text to HTML Table (txt2html-table) Version 1.0

Usage Instructions


First of all, you need to download Python on your computer. If you're using a *NIX system, it should already by installed. The basic syntax is

python txt2html-table-1.0.py [-f] [-v] input.txt

If you don't include any of the optional flags (-f or -v), it will output the text in the file and confirm if you want to convert it. Type "y" or "yes" without the quotes to confirm or any other character(s) to exit. Upon confirmation, it will convert it to an HTML table and output the results.


Example: With no extra options


$ python txt2html-table-1.0.py input.txt
Would you like to convert the text below into an HTML Table?
=======================
thElement1: tdElement1
thElement2: tdElement2
=======================
< Yes | No >: y
Converted successfully to:
=======================
<table>
<tr>
<th>thElement1: </th>
<td>tdElement1</td>
</tr>

<tr>
<th>thElement2: </th>
<td>tdElement2</td>
</tr>
</table>
=======================


Other command line options/flags


Use:

$ python txt2html-table-1.0.py -f input.txt

to prompt for a filename at the end of a conversion to save the html output to. If you decide to not save the output, press ENTER when it prompts for a filename in order to quit.

Example: Use of -f flag


$ python txt2html-table-1.0.py -f input.txt
Would you like to convert the text below into an HTML Table?
=======================
thElement1: tdElement1
thElement2: tdElement2
=======================
< Yes | No >: y
Converted successfully to:
=======================
<table>
<tr>
<th>thElement1: </th>
<td>tdElement1</td>
</tr>

<tr>
<th>thElement2: </th>
<td>tdElement2</td>
</tr>
</table>
=======================
Enter a filename (Hit ENTER to quit): output.html
Output saved to: output.html


Use:

$ python txt2html-table-1.0.py -v input.txt

for verbose output. It shows you what elements each line was split into.

Example: Use of -v flag


$ python txt2html-table-1.0.py -v input.txt
Would you like to convert the text below into an HTML Table?
=======================
thElement1: tdElement1
thElement2: tdElement2
=======================
< Yes | No >: y
' thElement1: tdElement1 ' was split into ['thElement1', 'tdElement1']
' thElement2: tdElement2 ' was split into ['thElement2', 'tdElement2']
Converted successfully to:
=======================
<table>
<tr>
<th>thElement1: </th>
<td>tdElement1</td>
</tr>

<tr>
<th>thElement2: </th>
<td>tdElement2</td>
</tr>
</table>
=======================

You can combine these two flags in either order as long as they're between the python file (.py) and the input filename. However, you cannot type in "-fv" or "-vf".

Errors you might receive



  • "Please pass a filename to me" if you didn't supply any arguments to the program.

  • "File does not exist." if the last argument you gave was not a valid file. Could occur if you added the flags after the input filename.


To avoid other errors, make sure that the python interpreter is in your OS's path, the program is in the current directory, and each line in your input file has one colon separating the <th> and <td> elements.

Todo: Possible features in future versions



  • Allow for specifying the output filename as a parameter i.e. python txt2html-table-1.0.py -f output.html input.txt

  • Allow more than two elements per line i.e. all elements after the first would be extra <td> elements in the table
  • Allow unbuffered reading in of text from the command line(stdin)

  • Add better support for flags (allow -fv or -vf)


Alternatives


Use the Python DOM library to build the table instead of raw HTML-holding variables like tHead, tBody, and tTail


Tools


Used Quick Escape to escape the html output for inserting into this post.

Sunday, July 22, 2007

Re-install GRUB after Windows install deletes it

Dual booting Windows and Linux gone wrong


When websites have tutorials about dual-booting Windows and Linux, they usually say to install Windows first and then install Linux after it. The reason for this is because Linux's bootloader detects your Windows partition and allows you to boot into it whereas Windows overwrites Linux's bootloader and replaces it with its own which only allows you to boot Windows. So that's why I install Windows first.

However, my Windows installation started acting unstable and I decided to reinstall it. Afterwards, I couldn't boot into Ubuntu (and Windows couldn't connect to the Internet, have the right resolution, etc. because I needed to reinstall the drivers...and everyone always talks about how everything "just works" after an install).

Reinstall GRUB


So this little tutorial tells you how to reinstall GRUB so you could choose your OS instead of being locked into one (and so you could reinstall Windows as necessary without worrying about breaking Linux).

If you use LILO or another bootloader, you could probably just google it. However, some suggestions don't work if you're repairing the MBR (Master Boot Record) from a LiveCD.

The Code


The following steps worked when I booted off the Ubuntu 6.10 LiveCD to repair the MBR but they should work on any CD that has GRUB.

Open a terminal and type the following in. The "#" is the prompt for root.

# grub
> find /boot/grub/stage1
   (hd0,1)

> root (hd0, 1)
> setup (hd0)

   ...   

> quit


How it works


The grub command takes you into the grub shell

  • the first line finds out on which hard drive and partition grub is installed on. In my case (hd0, 1), grub is installed on the first (and only) hd0 hard drive on the second partition (0 is the first)

  • the second line switches to the hdd and partition where grub is installed

  • setup tells grub to install itself onto the MBR so that grub becomes your default bootloader when booting off that hard drive


[via ubuntuforums.org]

Turn off system beep in Linux terminal

There are a variety of ways to turn of the system beep. I have tried three of them on my Ubuntu Feisty laptop and they both work in terminal emulators like gnome-terminal, etc. and in the virtual terminals.


Temporarily turn it off
$ xset b off
OR
$ setterm -blength 0

Permanently (although you could change it later) turn it off
- uncomment(as root) "# set bell-style none" in /etc/inputrc

- Or to change it to a visual bell (blinking the screen), change it to "set bell-style visual"
- If you only want to do it for the current user instead of for everyone, create an "~/.inputrc" file and add the set bell-style line to that file.


Some of the other ways include deleting or blacklisting your internal speaker. Or if you prefer the hardware/cross-platform approach, take it out manually. :)

Wednesday, February 21, 2007

Ubuntu hack for waking up your monitor

Sometimes, when I close the lid of my Dell E1505 laptop and open it again, the monitor doesn't resume and stays black instead. Today, I discovered a little hack to get the monitor working again instead of just doing a hard reboot. Press [Fn] + [Esc / Stand By] which suspends the computer, close the lid, open it, and bam...the monitor works and it decreases the chances of corrupt system files.

Wednesday, February 14, 2007

Google Pages Static Link rewriter script

On my Google Pages site, the links to my uploaded files suddenly stopped working. They changed from http://<myusername>.googlepages.com/theFile.ext to http://pages.google.com/-/static_files/theFile.ext where they would give me a 404 Not Found Error. This script is devoted to changing the links back so I can access my files.


// ==UserScript==
// @name Google Pages Static Files
// @description Rewrite "static files" on GooglePages to point to correct locations
// @include http://*.googlepages.com/*
// @version 0.1
// ==/UserScript==


//Get googlepages user to use in rewriting the links
var user = window.location.href.split(".")[0].replace("http://", "");


window.addEventListener('load',
function() {
//Use XPath to find all <a> elements with href attrib
var links = document.evaluate('//a[@href]',document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);


//Rewrite all links with "http://pages.google.com/-/static_files" to <user>.googlepages.com
for (i = 0; i < links.snapshotLength; i++) {
if ( links.snapshotItem(i).href.indexOf("http://pages.google.com/-/static_files") != -1 )
links.snapshotItem(i).href = links.snapshotItem(i).href.replace("pages.google.com/-/static_files", user + ".googlepages.com");
}
}, true);

Install Google Pages Static Files script (userscripts.org | mirror)

Saturday, February 3, 2007

Windows Live Mail Autofill script

Firefox 2 apparently can't fill in the username/password on the Windows Live Mail sign-in page so I wrote a short greasemonkey script to do so.

here is the source:
// ==UserScript==
// @name Windows Live Sign In
// @description Automatically sign in to Windows Live Mail cause Firefox can't
// @include http://login.live.com/*
// @include https://login.live.com/*
// @version 0.1
// ==/UserScript==

//add event listener to call my anonymous function after the page loads
window.addEventListener('load',
function() {
//Get email address box and fill it
document.getElementById("i0116").value = "YOUR_USERNAME";

//Get password box and fill it
document.getElementById("i0118").value = "YOUR_PASSWORD";
}, true);


Install Windows Live Mail Autofill script (from userscripts.org)
Install Windows Live Mail Autofill script (from mirror -- if userscripts.org is down)

To get the script to work, change the values for YOUR_USERNAME and YOUR_PASSWORD to their correct values. Then go to mail.live.com and test it out