#!/usr/bin/env python
#=====================================================================================
# Get data from a server file
# Display to an html form
#=====================================================================================
import cgitb                                #cgi with traceback error handling
import re                                   #regular expressions
 
cgitb.enable()
 
data = []                                   #2 dim array

print("Content-Type: text/html \n")          #required http response header (w/ extra line)

'''===================================================================================
read_data: read data from a server file
==================================================================================='''
def read_data():
 
    global data

    file  = '/home/sultans/data/cust_order.file';    	#server file
    input = open(file,'r')                             	#Open file for reading
    
    data  = input.readlines()                          	#read entire file into array

    input.close()                                      	#close the file

'''===================================================================================
display: display data on the html page
==================================================================================='''
def display():

    global data

    print("""
        <html>
        <head>
        <title>Retrieve data from database</title>    
        <style>
            a     {text-decoration:none; color:brown}
            table {font:10pt arial; border:solid 2px black; border-collapse:collapse; margin-left:70px; background-color:#eeeeee; } 
            th,td {border:solid 1px black; padding-left:5px; padding-right:5px; vertical-align:top;}
        </style>
        <body bgcolor=lightyellow>
        <h2><center>Ice Cream Orders - Read entire file into array</center></h2>
        <table bgcolor=lightyellow border=1>
        <tr bgcolor=tan>
    """)                          
        
    print("<th> Name      </th>")
    print("<th>Address    </th>")
    print("<th>Flavors    </th>")
    print("<th>Toppings   </th>")
    print("<th>Credit Card</th>")


    for rec in data:                                   #loop thru the data rows
        [fn,ln,addr,flav,top,cc] = rec.split('||')     #split the record on ||
        print("<tr>",end='')

        name = fn +' '+ ln
        addr = re.sub(r"__", "<br>", addr)             #replace _ with <br>
        flav = re.sub(r",",  "<br>", flav)             #replace , with <br>
        top  = re.sub(r",",  "<br>", top)              #replace , with <br>
        
        print("<td>"+ name +"<td>"+ addr +"<td>"+ flav +"<td>"+ top +"<td>"+ cc)
        print("</tr>")

    print("</table>")
    print("</body>")
    print("</html>")

'''===================================================================================
main code
==================================================================================='''
read_data();
display();


#=== link to see the python code =================================================
import os, sys
sys.path.insert(0,'/home/sultans/web/python/demo')
import zCode                          #import func to display the Python code
filename = os.path.abspath(__file__)  #get absolute file name 
zCode.display(filename)               #call it
#=================================================================================