""" CSC111 Homework 10 assignment. This program retrieves contact information
from the Smith directory, and compiles it into a table for an HTML file.

The class definitions FriendInfo and getURLinfo are used. The input is read
from an input file containing last names

J Cardell, April 2011"""

from getURLinfo import *
from FriendInfo import *

def readNames(InName):
    """Open the file with the last names of contacts, create a list of the
    names and return this list."""
    
    inF = open(InName, 'r')

    names = []
    for line in inF:
        names.append(line[:-1])

    inF.close()
    return names


def openHTMLFile(htmlName):
    """Open the HTML file to be used for the output file, and write the
    opening HTML tags to this file."""

    hFile = open(htmlName, 'w')
    hFile.write("""<html>\n<head>\n<title>CSC111 HW10</title>
\n<h1>Sample Page for CSC 111 HW 10</h1>
<!--This is a sample HTML file created by a Python program for CSC111 HW10-->
</head>\n\n<body>""")

    return hFile


def getFriends(names, baseURL):
    """Using the FriendInfo and getURLinfo class definitions, compile a list
    of FriendInfo objects using the information retrieved from the Smith
    directory, that is retrieved using the getURLinfo class."""

    # Initialize the list of FriendInfo objects to the empty list 
    friends = []

    # construct the getURLinfo object and set the URL instance variable
    contact = getURLinfo()
    contact.setURL(baseURL)
    
    # Process names one at a time, retrieving information from the Smith
    # directory for each name, including the phone number and email address
    for nm in names:
        # Create a new FriendInfo object to fill with new information
        contactInfo = FriendInfo()

        # Retrieve name information
        contact.setMarkers('TemplateBContactInfo"><td valign=top>',"</b>")
        fullName = contact.getInfo(nm)

        # slice off initial, residual HTML tag and then split on the comma
        lNm, fNm = fullName[4:].split(', ')

        # Retrieve phone number
        contact.setMarkers("(413)","<br>")
        ph = contact.getInfo(nm)

        # Retrieve email address
        contact.setMarkers('directory">',"</a>")
        addr = contact.getInfo(nm)
        
        contactInfo.newInfo(fNm+' '+lNm, ph, addr)
        friends.append(contactInfo)

    return friends


def writeTable(friends, hFile):
    """Write a table from the FriendInfo list to the HTML page"""

    # Write the opening table tags before entering the for-loop, the table
    # data inside the for-loop and the closing table tags after the for-loop
    hFile.write("""\n
<h2>This is data retrieved from the Smith Directory</h2>
<!--This is a table of information retrieved from the Smith directory-->
<table border = "1" >
<tr>\n\t<td>Name</td>\n\t<td>Phone Number</td>\n\t<td>Email</td>""")

    for info in friends:
        hFile.write("""\n<tr>\n\t<td>%s</td>\n\t<td>%s</td>\n\t<td>%s</td>
</tr>""" % (info.getName(), info.getPhone(), info.getEmail()))

    hFile.write("\n</table>")


def closeHTML(hFile):
    """Write close HTML tags and close the HTML output file."""
    hFile.write("\n\n</body>\n</html>")
    hFile.close()


def main():
    """The main function reads the input file containing a list of last names,
    and uses these names to access the Smith directory and retrieve contact
    information. The data is written to an HTML file names HW10.html"""

    # Read in input data, open both files, and write opening HTML tags
    names = readNames("NamesList.txt")
    htmlF = openHTMLFile("HW10.html")

    # Use getURLinfo and FriendInfo classes to retrieve contact information
    # from the Smith directory
    URL = "http://www.smith.edu/global_campusdirectory.php?name=%s"
    friendsList = getFriends(names, URL)

    # Format the contact information into an HTML table and write it out to
    # the output file HW10.html
    writeTable(friendsList, htmlF)

    # write the closing HTML tags and close the HTML file
    closeHTML(htmlF)


main()
