# The aim of this script is to allow you to detect which of your .py or .m scripts may need to be updated in order to run them with FIMMWAVE 5.4
# Please set the directory "dir" containing the .py or .m scripts that you want to check, and a location for the log file "logname"

import os
import subprocess

# To be set by user
dir = "Drive:\\Temp\\Scripts"
logname = "Drive:\\Temp\\script_check_log.txt"

# internal function
def testfile(path,filename):
    log.write("Checking contents of " + filename + "...\n\n")
    f = open(path+"\\"+filename,'r')
    idx = 0
    A1 = []
    A2 = []
    B = []
    C1 = []
    C2 = []
    C3 = []
    C4 = []
    D = []
    E = []
    for i in f.readlines():
        idx = idx + 1
        # A. the script modifies the MOLAB Options of a waveguide node which is used to define a WG Section or a Bend Section in a FIMMPROP Device.
        # look for ".evlist.svp." and ".evlist.mlp." except "cdev.evlist.svp." and "cdev.evlist.mlp."
        # tell user to check whether modifications to waveguide need to be reapplied to FP Device
        found=i.find(".evlist.svp.")
        if found!=-1:
            # does it have .cdev. as a prefix
            if found>6:
                if i[found-6:found]!=".cdev.":
                    A1.append([idx,found])
                    log.write(".evlist.svp. found in line " + str(idx) + "\n")
            else:
                A1.append([idx,found])
                log.write(".evlist.svp. found in line " + str(idx) + "\n")
        found=i.find(".evlist.mlp.")
        if found!=-1:
            # does it have .cdev. as a prefix
            if found>6:
                if i[found-6:found]!=".cdev.":
                    A2.append([idx,found])
                    log.write(".evlist.mlp. found in line " + str(idx)+ "\n")
            else:
                A2.append([idx,found])
                log.write(".evlist.mlp. found in line " + str(idx)+ "\n")
        # B. the script modifies the MOLAB Options of a WG Section or a Bend Section
        # look for ".wg.evlist." and remove ".wg.evlist"
        found=i.find(".wg.evlist.")
        if found!=-1:
            B.append([idx,found])
            log.write(".wg.evlist. found in line " + str(idx)+ "\n")
        # C. the script creates a WG Section or a Bend Section, or selects or pastes a different waveguide node for a WG Section or a Bend Section
        # look for ".newwgsect(", ".newbend(", ".setwg(" or ".pastewg("
        # tell user to check that settings are correct
        found=i.find(".newwgsect(")
        if found!=-1:
            C1.append([idx,found])
            log.write(".newwgsect( found in line " + str(idx)+ "\n")

        found=i.find(".newbend(")
        if found!=-1:
            C2.append([idx,found])
            log.write(".newbend( found in line " + str(idx)+ "\n")

        found=i.find(".setwg(")
        if found!=-1:
            C3.append([idx,found])
            log.write(".setwg( found in line " + str(idx)+ "\n")

        found=i.find(".pastewg(")
        if found!=-1:
            C4.append([idx,found])
            log.write(".pastewg( found in line " + str(idx)+ "\n")
        # D. the script creates structures from scratch
        # look for ".addsubnode(rwguideNode", ".addsubnode(mwguideNode", ".addsubnode(WGScanNode", ".addsubnode(FPdeviceNode", ".newtaper(", ".newwglens(", ".newsjoint(", ".newstraightridge(", ".newtaperridge(", ".newsinridge(", ".newtwinridge("
        # tell user to check that settings are correct
        found=i.find(".addsubnode(rwguideNode")
        if found!=-1:
            D.append([idx,found])
            log.write( ".addsubnode(rwguideNode found in line " + str(idx)+ "\n")
            
        found=i.find(".addsubnode(mwguideNode")
        if found!=-1:
            D.append([idx,found])
            log.write( ".addsubnode(mwguideNode found in line " + str(idx)+ "\n")
            
        found=i.find(".addsubnode(WGScanNode")
        if found!=-1:
            D.append([idx,found])
            log.write( ".addsubnode(WGScanNode found in line " + str(idx)+ "\n")
            
        found=i.find(".addsubnode(FPdeviceNode")
        if found!=-1:
            D.append([idx,found])
            log.write( ".addsubnode(FPdeviceNode found in line " + str(idx)+ "\n")
            
        found=i.find(".newtaper(")
        if found!=-1:
            D.append([idx,found])
            log.write( ".newtaper( found in line " + str(idx)+ "\n")
            
        found=i.find(".newwglens(")
        if found!=-1:
            D.append([idx,found])
            log.write( ".newwglens( found in line " + str(idx)+ "\n")

        found=i.find(".newsjoint(")
        if found!=-1:
            D.append([idx,found])
            log.write( ".newsjoint( found in line " + str(idx)+ "\n")
            
        found=i.find(".newstraightridge(")
        if found!=-1:
            D.append([idx,found])
            log.write( ".newstraightridge( found in line " + str(idx)+ "\n")

        found=i.find(".newtaperridge(")
        if found!=-1:
            D.append([idx,found])
            log.write( ".newtaperridge( found in line " + str(idx)+ "\n")

        found=i.find(".newsinridge(")
        if found!=-1:
            D.append([idx,found])
            log.write( ".newsinridge( found in line " + str(idx)+ "\n")

        found=i.find(".newtwinridge(")
        if found!=-1:
            D.append([idx,found])
            log.write( ".newtwinridge( found in line " + str(idx)+ "\n")
        # E. the script selects an external file using a relative path that is set with respect to the work directory; the work directory is now automatically set to the project directory.
        # look for "app.setwdir("
        # If you are setting a path relative to the work directory, change it so that it is set with respect to the directory of the project. Otherwise, use an absolute path.
        found=i.find("app.setwdir(")
        if found!=-1:
            E.append([idx,found])
            log.write("app.setwdir( found in line " + str(idx)+ "\n")
    f.close
    return [A1, A2, B, C1, C2, C3, C4, D, E]

# begin script

exit = 0
if dir=="Drive:\\Temp\\Scripts":
    print "Please set the script directory"
    exit = 1
if logname=="Drive:\\Temp\\script_check_log.txt":
    print "Please choose a location for the log file"
    exit = 1
if exit==1:
    raw_input("Press any key to exit")
    quit()

list = os.walk(dir)
scriptfiles = []

for i in list: # going through list of contents, folder by folder
    path = i[0] # this is the folder path
    for j in i[2]: # acessing file names
        if j[-3:]==".py": # checking extension
            scriptfiles.append([path,j])
        if j[-2:]==".m": # checking extension
            scriptfiles.append([path,j])
            
log=open(logname,'w')

if len(scriptfiles)==0:
    log.write("No files found.\n")
else:
    log.write("Files found:\n")
    for i in scriptfiles:
        log.write(i[0]+"\\"+i[1]+"\n")
    log.write("\n")

for i in scriptfiles:
    log.write("####################################\nFile: "+ i[0] + "\\" + i[1] +"\n####################################\n\n")
    [A1, A2, B, C1, C2, C3, C4, D, E] = testfile(i[0],i[1])
    log.write("Results for " + i[1] + "...\n\n")
    if len(A1+A2+B+C1+C2+C3+C4+D+E)==0:
        log.write("No issues detected.\n\n")
    if len(A1+A2)!=0:
        log.write("Warning (case A): the script modifies the MOLAB Options of a waveguide node\n")
        log.write("If this was done to modify the MOLAB Options of a WG Section or Bend Section, please make sure that you apply the modifications to the MOLAB Options of the section in FIMMPROP\n")
        log.write("Potential issues identified in line(s):\n")
        for k in A1+A2:
            log.write(str(k[0])+"; ")
        log.write("\n\n")
    if len(B)!=0:
        log.write("Warning (case B): the script modifies the MOLAB Options of a WG Section or a Bend Section\n")
        log.write("Change of syntax needed: the string '.wg.evlist' needs to be removed\n")
        log.write("Please apply changes to line(s):\n")
        for k in B:
            log.write(str(k[0])+"; ")
        log.write("\n\n")
    if len(C1+C2+C3+C4)!=0:
        log.write("Warning (case C): adding new WG Section, new Bend Section, or setting / pasting waveguides into existing WG or Bend Sections\n")
        log.write("In previous version, the MOLAB Options would be imported from wg node\n")
        log.write("Please update MOLAB Options for the WG or Bend Sections where needed.\n")
        log.write("Potential issues identified in line(s):\n")
        for k in C1+C2+C3+C4:
            log.write(str(k[0])+"; ")
        log.write("\n\n")
    if len(D)!=0:
        log.write("Warning (case D): the script creates structure from scratch\n")
        log.write("Default settings have been changed, please check settings are correct\n")
        log.write("Potential issues identified in line(s):\n")
        for k in D:
            log.write(str(k[0])+"; ")
        log.write("\n\n")
    if len(E)!=0:
        log.write("Warning (case E): the script sets the work directory; the work directory is now automatically set to the project directory when a project is opened.\n")
        log.write("If you are setting a path relative to the work directory, change it so that it is set with respect to the directory of the project. Otherwise, use an absolute path.\n")
        log.write("Potential issues identified in line(s):\n")
        for k in E:
            log.write(str(k[0])+"; ")
        log.write("\n\n")

log.close()
subprocess.Popen("notepad " + logname)


