-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestcsv.py
158 lines (134 loc) · 4.99 KB
/
testcsv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/python
import xlrd
import requests
import json
import csv
class InputStruct:
SecZone = None
Site = None
UnLocCode = None
rows = []
output_dir = "."
filename = "updatedtagging1_report.csv"
#UN_Location_Code="UN-Location-Code"
#security_Zone="security-Zone"
data =[{'id': '%s',
"attributes":{
"Security-Zone":"%s",
"Site":"","UN-Location-Code":"%s"
}
}
]
#data2={}
data = json.dumps(data)
url = 'http://ma-gncstd-lapp08.corp.apple.com:8080/api/v1/networks/'
#baseurl= 'https://groot-api-ut.corp.apple.com/'#uat
#baseurl = 'https://groot-api-ut.corp.apple.com/'#prod
headers1 = {'content-type': 'application/json'}
# Do the HTTP put request
#r = requests.put(url, data=data, headers=headers)
def do_process():
fname = "/Users/raja/Desktop/Reportdevicetagvalue.xlsx"
xl_workbook = xlrd.open_workbook(fname)
# List sheet names, and pull a sheet by name
sheet_names = xl_workbook.sheet_names()
#print "SDFDS"
#print 'Sheet Names', sheet_names, xl_workbook
xl_sheet = xl_workbook.sheet_by_index(0)
#print ('Sheet name: %s' % xl_sheet.name)
row = xl_sheet.row(0) # 1st row
from xlrd.sheet import ctype_text
headers = []
#print '(Column #) type:value'
for idx, cell_obj in enumerate(row):
cell_type_str = ctype_text.get(cell_obj.ctype, 'unknown type')
headers.append(cell_obj.value)
#print headers
inputstructDict={}
structKey=""
for row_idx in range(1, xl_sheet.nrows): # Iterate through rows
#print ('Row: %s' % row_idx) # Print row number
#Condition 1
data2 = {}
#structKey=''
structValue = InputStruct()
structValue.SecZone=[]
structValue.Site=[]
structValue.SecZone=[]
structValue.UnLocCode=[]
# data2["attributes"]={}
cell_obj = xl_sheet.cell(row_idx, 0) # Get cell object by row, col
#print cell_obj
ips = cell_obj.value.split("/");
#print cell_obj
data2['id']=cell_obj.value
structKey=cell_obj.value
#print structKey
if inputstructDict.has_key(structKey):
structValue = inputstructDict[structKey]
#print structValue
#print ipsx`
if (ips[1]=="30" or ips[1]=="31"):
#data['id']=cell_obj.value
#data['attributes']['Security-Zone']= "Routing"
#data2['id']=cell_obj.value
#data2['attributes']['Security-Zone']= ["Routing"]
#structValue.SecZone.append("Routing")
if "Routing" not in structValue.SecZone:
structValue.SecZone=["Routing"]
#r = requests.put(url, data=data, headers=headers)
#print ips[1]
# Call api with routing
#Condition 2
cell_obj = xl_sheet.cell(row_idx,12)
#unlc_cell_obj = xl_sheet.cell(row_idx, 2)
#print unlc_cell_obj
#Site_cell_obj = xl_sheet.cell(row_ idx, 3)
#print Site_cell_obj
if(cell_obj.value == "OK"):
cell_obj2 = xl_sheet.cell(row_idx,3)
if cell_obj2.value not in structValue.Site:
#print cell_obj2.value
#data[0].attributes.Site=cell_obj2.value
#data2['attributes']['Site']= [cell_obj2.value]
structValue.Site.append(cell_obj2.value)
#Call the api with cell_obj2.value
#Condition 3
cell_obj = xl_sheet.cell(row_idx,4)
if(len(cell_obj.value)>0):
cell_obj2 = xl_sheet.cell(row_idx,2);
if cell_obj2.value not in structValue.UnLocCode:
#print cell_obj2.value
#data2['attributes']['UN-Location-Code']=[cell_obj2.value]
structValue.UnLocCode.append(cell_obj2.value)
#data[0].attributes.UN-Location-Code=cell_obj2.value
#data['attributes']['UN-Location-Code']=cell_obj2.value
#Call the api with cell_obj2.value
data2={}
data2["attributes"]={}
data2['id']=structKey
data2['attributes']['Security-Zone']=structValue.SecZone
data2['attributes']['Site']=structValue.Site
data2['attributes']['UN-Location-Code']=structValue.UnLocCode
inputstructDict[structKey]=structValue
data2= [data2]
data2 = json.dumps(data2)
#print data2
#data2=[]
r = requests.put(url, data=data2, headers=headers1)
#print r
if r.status_code != 201:
print('Status:', r.status_code, 'Problem with the request. Exiting.')
#print data2
rows.append(['Problem with the request. Exiting #{}'.format(data2)])
#exit()
else:
# Report success
print('Successfully added attributes #{}'.format(data2))
rows.append(['Successfully added attributes #{}'.format(data2)])
with open(filename, "wb") as csv_file:
csv_writer = csv.writer(
csv_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)
csv_writer.writerows(rows)
if __name__ == "__main__":
do_process()