import re
#import csv
tag = re.compile(r'nav-\w+')

with open ('/Users/ymccarter/PycharmProjects/Mihon/Make-Life-Easy/DefCreation/awsdef4.yaml', 'r') as myfile:
    data = myfile.readlines()


print(tag.search('My number is  Value: nav-navigatorauthgateway'))

fw = open('deftag.txt', 'w')

deftag = []
for line in data:
    print(line)
    if re.search(tag, line):
        line=line.replace('        Value: ','')
        deftag.append(line)
        fw.write(line)
print(deftag)



#2nd example:

"""
Author Yukie McCarter
Date 9/30/18
This python program specifically convert NPInboud.yml to CSV value
1. using Openpyxl module
2. Create the List for each item  Cidr, Destination service, Protocol (tcp or udp and so on) And FromPort and ToPort list
3.Sending column in order

"""

"""working 
with open ('NP-Prod-Inbound.yml', 'r') as myfile: #open the YML file and correct data
    data = myfile.readlines()
    #print (data)
Cidr=[]
Destination=[]
protocol = []
IpProtocol=[]
FromPort=[]
ToPort=[]
for line in data:
    if "CidrIp" in line:
        line=line.replace('CidrIp: ','')
        Cidr.append(line)
    if "Fn::ImportValue: " in line:
        line=line.replace("Fn::ImportValue: ","")
        Destination.append(line)
    if "IpProtocol: " in line:
        line=line.replace('IpProtocol: ','')
        IpProtocol.append(line)
    if "FromPort: " in line:
        line=line.replace("FromPort: ","")
        FromPort.append(line)
    if "ToPort: " in line:
        line=line.replace("ToPort: ","")
        ToPort.append(line)


from openpyxl import Workbook
#nfc_east = ('DAL', 'WAS', 'PHI', 'NYG')
wb = Workbook()
ws = wb.active
ws['A1']='Source Cidr'
ws['B1']='Destination Service'
ws['C1']='IpProtocol'
ws['D1']='FromPort'
ws['E1']='ToPort'
for row, i in enumerate(Cidr):
    column_cell = 'A'
    ws[column_cell+str(row+2)] = str(i)
for row, i in enumerate(Destination):
    column_cell = 'B'
    ws[column_cell+str(row+2)] = str(i)
for row, i in enumerate(IpProtocol):
    column_cell = 'C'
    ws[column_cell+str(row+2)] = str(i)
for row, i in enumerate(FromPort):
    column_cell = 'D'
    ws[column_cell+str(row+2)] = str(i)
for row, i in enumerate(ToPort):
    column_cell = 'E'
    ws[column_cell+str(row+2)] = str(i)


wb.save("NpProd-Inbound-Cidr.xlsx")


"""
def Yml2Createlist(file,savedfile): #function's calling item is YAML filename and Excel workbook name
    with open (file, 'r') as myfile: #open the YML file and correct data
        data = myfile.readlines()
        #print (data)
    Cidr=[] #list of Cidr address
    Destination=[] #List of Destination service
    IpProtocol=[] #list of protocol tcp, udp, gre, and so on
    FromPort=[] #list of FromPort
    ToPort=[] #list of ToPort
    for line in data: #Appending the value to each data with cleanup the format.
        if "CidrIp" in line:
            line=line.replace('CidrIp: ','')
            Cidr.append(line)
        if "Fn::ImportValue: " in line:
            line=line.replace("Fn::ImportValue: ","")
            Destination.append(line)
        if "IpProtocol: " in line:
            line=line.replace('IpProtocol: ','')
            IpProtocol.append(line)
        if "FromPort: " in line:
            line=line.replace("FromPort: ","")
            line=line.replace("'","")
            FromPort.append(line)
        if "ToPort: " in line:
            line=line.replace("ToPort: ","")
            line=line.replace("'","")
            ToPort.append(line)

        from openpyxl import Workbook #open the excel file
        # nfc_east = ('DAL', 'WAS', 'PHI', 'NYG')
        wb = Workbook() #defined 'wb' as command
        ws = wb.active #activated it
        ws['A1'] = 'Source Cidr' #creating title of column
        ws['B1'] = 'Destination Service' #creating title of column
        ws['C1'] = 'IpProtocol'#creating title of column
        ws['D1'] = 'FromPort'#creating title of column
        ws['E1'] = 'ToPort'#creating title of column
        for row, i in enumerate(Cidr):
            column_cell = 'A'
            ws[column_cell + str(row + 2)] = str(i)
        for row, i in enumerate(Destination):
            column_cell = 'B'
            ws[column_cell + str(row + 2)] = str(i)
        for row, i in enumerate(IpProtocol):
            column_cell = 'C'
            ws[column_cell + str(row + 2)] = str(i)
        for row, i in enumerate(FromPort):
            column_cell = 'D'
            ws[column_cell + str(row + 2)] = str(i)
        for row, i in enumerate(ToPort):
            column_cell = 'E'
            ws[column_cell + str(row + 2)] = str(i)

        wb.save(savedfile)

Yml2Createlist('NP-Prod-Inbound.yml',"NpProd-Inbound-Cidr.xlsx")