#! /usr/bin/python3

# !/usr/local/bin/python  deprecated for Raspberry Pi
# IX_textnames.py  renamed and enhanced to n.py
# by D@CC on 2022CMar22
####################################################################
# first line (shebang) must be #! /usr/bin/python3                 #
# place the n.py and the package into Desktop                      #
#                                                                  #
# Usage:  >$ cd Desktop/                                           #
#         >sudo chmod +x n.py                                      #
#         >$ python3 ../n.py  < ../adcRead_pkg.py                  #
# or:     >$ cd Desktop                                            #
#         >$ ./n.py --acdhimgs < adcRead_pkg.py                    #
# NB In terminal, the above should list the names of the           #
#       files that exist in the package named in stdin.            #
#                                                                  #
# but due to the Unix trick line 1 "#!/usr...                      #
# and the last line if __name__ ...                                #
# the command line below works (afer chmod ...):                   #
# >$ ./n.py <adcRead_pkg.py                                        #
####################################################################
import sys
# from textpack import marker            # use common separator key but
marker = '#' + ':'*10 + 'textpak=>'      # the IX Marker is different
mlen = len(marker)                       # file names appear after markers

def names():
    cmdList0="[n]"  #default value
    argStr=str(sys.argv[1:])
    #print("argStr1:"+argStr)
    alen=len(argStr)
    argExt=argStr[alen-5:alen-2]
    #print("argExt:"+argExt)
    isBadArg=False
    if argExt==".py": isBadArg=True              #flag a Bad Argument
    if argExt.find("pkg")>-1 : isBadArg=True     #flag a Bad Argument
    #if end
    if isBadArg==True:
        continueDoh=True
        print('#::: Be sure to precede the Pkg name with a "<".')   
        return #from names    
    else:
        #print("argStr2:"+argStr)  
        #print("cmdList02:"+cmdList0)
        if False:
            continueDoh=True
        else:
            #print("argStr3:"+argStr)
            #for cmdList in sys.argv[1:]:
            if argStr=="[]": 
                cmdList=cmdList0
            else:
                cmdList=argStr
            #if end
            #print("cmdList3:"+cmdList)
            if cmdList.find("h")>-1 :
                print("#:::: Help for python n.py [IX pkg names list pgm]")
                print("#:::: Syntax: ./n.py [args] <pkg")
                print('#::::    args = -acdhimgs or null')
                print("#:::: a:all,c:class,d:def,h:help,i:import,m:marker,g:global,s:source:")
                print("#:::: NB use IX_textpack.py to create an IX package")
                print('#:::: NB marker is "#::::::::::'+'textpak=>"')
                print("#:::: (Cudos to:O'Reilly for textpack.py)")
                return   #from names 
            #if end
            if cmdList.find("a")>-1 :
               cmdList="-cdings"     #a means list all
            #endif
            if cmdList.find("<")>-1 :
                print("#:::: ERROR precede the < with a space")
                return   #from names 
            #if end
            print("#::: Enhanced n.py [IX pkg names list prg -h for help]:")
            
            if True:
                markCnt=0
                for line in sys.stdin.readlines(  ):     # for all input lines
                    #print("before first line")
                    #if line=="": break
                    #if line==None: break
                    markIn=line[0:mlen]

                    #if cmdList.find("m")>-1 :           # always show markers
                    if(line[:mlen]== marker):            # is it a marker line
                        markCnt+=1
                        llen=len(line)
                        filename=line[mlen:llen-1]       # get filename without LF
                        print(filename)
                    #if end
                    ##if end
                    isY=False
                    if cmdList.find("c")>-1 :
                        if line.find("class ")>-1: isY=True           # is it an class line
                        if isY :
                            llen=len(line)
                            pline="  "+line[:llen-1]       # get line without LF
                            print(pline)
                        #if end
                    #if end
                    isY=False
                    if cmdList.find("i")>-1 :
                        if line.find("import ")>-1: isY=True           # is it an import line
                        if isY :
                            llen=len(line)
                            pline=" "+line[:llen-1]       # get line without LF
                            print(pline)
                        #if end
                    #if end
                    isY=False
                    if cmdList.find("g")>-1 :
                        if line.find("global ")>-1: isY=True           # is it a global line
                        if line.find("global(")>-1: isY=True           # is it a global line
                        if isY :
                            llen=len(line)
                            pline="  "+line[:llen-1]       # get line without LF
                            print(pline)
                        #if end
                    #if end
                    isY=False
                    if cmdList.find("s")>-1 :
                        if line.find("source:")>-1: isY=True           # is it a source line
                        if line.find("Source:")>-1: isY=True           # is it a Source line
                        if isY :
                            llen=len(line)
                            pline="  "+line[:llen-1]       # get line without LF
                            print(pline)
                        #if end
                    #if end
                    isY=False
                    if cmdList.find("d")>-1 :
                        if line.find("def " )>-1: isY=True           # is it a def line
                        if line.find("#def ")>-1: isY=False
                        if isY :
                            llen=len(line)
                            pline="  "+line[:llen-1]       # get line without LF
                            print(pline)
                        #if end
                    #if end
                #for end
                if markCnt==0 : print("#::: WARNING: pkg has no markers")
            print("#::: end of list by n.py")
        #if end
    #if end
#def end names

if __name__ == '__main__' : names( )
#end of n.py