#!/usr/bin/env python
'''
$Id: grabweb.py,v 1.2 2000/05/03 23:13:52 wesc Exp $

grabweb.py -- grab a web page (from host 'www') and display the
    first and last non-blank lines of the retrieved HTML file
'''

from urllib import urlretrieve        # urlretrieve() downloads web pages
from string import strip        # strip() removes trailing whitespace

# firstnonblank() --> string
def firstnonblank(lines):
    '''firstnonblank(seq) -- given a set of strings
    (list or tuple), return the first non-blank line'''

    for eachLine in lines:
        if strip(eachLine) == '':
            continue
        else:
            return eachLine


# firstlast() --> None
def firstlast(webpage):
    '''firstlast(webpage) -- open file of saved webpage and
    display the first and last non-blank lines'''

    f = open(webpage)
    lines = f.readlines()
    f.close()
    print firstnonblank(lines),
    lines.reverse()
    print firstnonblank(lines),


# download() --> None
def download(url='http://www', process=firstlast):
    '''download(url, process) --
    '''

    try:
        retval = urlretrieve(url)[0]
    except IOError:
        retval = None
    if retval:                # do some processing
        process(retval)

if __name__ == '__main__':
    download()

