#!/usr/bin/python
#============================================================================
# Script to display the Python code on browser without interpretation     
# Must pass the name of script via  url?script=name                    
# File is localized so script can only look into files in current dir  
#============================================================================
def codeDisplay():
    import os

#   import HTTPparam
#   filename = HTTPparam.getValue('script')                 #the name of the python script
    
    print("Content-Type: text/plain \n")                    #required http response header (w/ extra line)

    getParamStr  = os.environ.get('QUERY_STRING') or ''     #get GET parameters
    getParamStr += '&'                                      #append & at end
    findScript   = getParamStr.find('script', 0)            #find 'script'
    findEqual    = getParamStr.find('=', findScript)        #find '=' starting at 'script'
    findAmp      = getParamStr.find('&', findScript)        #find '&' starting at 'script'
    filename     = getParamStr[findEqual+1:findAmp]         #substring the script name 
        
    print(filename)
    
    script     = filename
    if script =='None':                                #if nothing is passed 
        script = __file__                              #use this file 

    input = open(script, 'r')                          #open the file for reading
    data  = input.readlines()                          #read entire file into array
    input.close()                                      #close the file

    for line in data:                                  #loop through the array
#       line = line.replace('<', '<')               #replace < with &lt;    
        print(line,end='')                             #write the line (no newline)

#============================================================================
# Call the above script   
#============================================================================
codeDisplay()