-
Notifications
You must be signed in to change notification settings - Fork 4
/
ns_def.py
1642 lines (1360 loc) · 84.9 KB
/
ns_def.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''
SPDX-License-Identifier: Apache-2.0
Copyright 2023 Cisco Systems, Inc. and its affiliates
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
'''
import tkinter as tk ,tkinter.ttk as ttk,tkinter.filedialog, tkinter.messagebox
import sys, os, shutil , unicodedata,subprocess,datetime,random
import openpyxl
import math ,ipaddress ,yaml
from pptx import *
import platform
def get_l3_segments(self):
'''get values of Master Data'''
# parameter
ws_l3_name = 'Master_Data_L3'
excel_maseter_file = self.inFileTxt_L3_3_1.get()
self.result_get_l2_broadcast_domains = get_l2_broadcast_domains.run(self, excel_maseter_file) ## 'self.update_l2_table_array, device_l2_boradcast_domain_array, device_l2_directly_l3vport_array, device_l2_other_array, marged_l2_broadcast_group_array'
#print('--- get_l3_segments ---')
#print('--- self.target_l2_broadcast_group_array ---')
#print(self.target_l2_broadcast_group_array)
self.l3_table_array = convert_master_to_array(ws_l3_name, excel_maseter_file, '<<L3_TABLE>>')
#print('--- self.l3_table_array ---')
#print(self.l3_table_array)
updated_l3_table_array = []
for index, tmp_l3_table_array in enumerate(self.l3_table_array):
str(tmp_l3_table_array).replace(' ', '')
if index >= 2:
if len(tmp_l3_table_array[1]) == 5:
if ',' in str(tmp_l3_table_array[1][4]):
#print('--- tmp_l3_table_array ', str(tmp_l3_table_array))
tmp_tmp_l3_table_array = str(tmp_l3_table_array[1][4]).split(',')
for tmp_add_array in tmp_tmp_l3_table_array:
tmp_tmp_tmp_l3_table_array = tmp_l3_table_array
tmp_tmp_tmp_l3_table_array[1][4] = tmp_add_array
#print('--- tmp_tmp_tmp_l3_table_array ', tmp_tmp_tmp_l3_table_array)
updated_l3_table_array.append([tmp_tmp_tmp_l3_table_array[1][0], tmp_tmp_tmp_l3_table_array[1][1], tmp_tmp_tmp_l3_table_array[1][2], tmp_tmp_tmp_l3_table_array[1][3], tmp_tmp_tmp_l3_table_array[1][4]])
else:
updated_l3_table_array.append(tmp_l3_table_array[1])
elif len(tmp_l3_table_array[1]) == 4:
updated_l3_table_array.append([tmp_l3_table_array[1][0], tmp_l3_table_array[1][1], tmp_l3_table_array[1][2], tmp_l3_table_array[1][3], ''])
elif len(tmp_l3_table_array[1]) == 3:
updated_l3_table_array.append([tmp_l3_table_array[1][0], tmp_l3_table_array[1][1], tmp_l3_table_array[1][2], '', ''])
#print('--- updated_l3_table_array ---')
#print(updated_l3_table_array)
'''get segment with target area'''
l3_segment_group_array = []
for tmp_target_l2_broadcast_group_array in self.target_l2_broadcast_group_array:
tmp_l3_segment_group_array = []
for tmp_tmp_target_l2_broadcast_group_array in tmp_target_l2_broadcast_group_array[1]:
for tmp_updated_l3_table_array in updated_l3_table_array:
if tmp_tmp_target_l2_broadcast_group_array[0] == tmp_updated_l3_table_array[1] and tmp_tmp_target_l2_broadcast_group_array[1] == tmp_updated_l3_table_array[2]:
tmp_l3_segment_group_array.append(tmp_updated_l3_table_array)
if tmp_l3_segment_group_array != []:
l3_segment_group_array.append(tmp_l3_segment_group_array)
#print('--- l3_segment_group_array ---')
#print(l3_segment_group_array)
return l3_segment_group_array
def return_os_slash():
slash_type = '\\'+'\\'
os_type = platform.platform()
#print(os_type)
if 'macOS'.casefold() in os_type.casefold() or 'Linux'.casefold() in os_type.casefold():
slash_type = '/'
return (slash_type)
def get_backup_filename(full_filepath):
now = datetime.datetime.now(datetime.timezone(datetime.timedelta(hours=9)))
yyyymmddhhss = str(now.strftime('%Y%m%d%H%M%S'))
#print(yyyymmddhhss)
filename = os.path.basename(full_filepath)
iDir = os.path.abspath(os.path.dirname(full_filepath))
basename_without_ext = os.path.splitext(os.path.basename(full_filepath))[0]
basename_ext = os.path.splitext(os.path.basename(full_filepath))[1]
backup_full_filepath = iDir + return_os_slash() + basename_without_ext + '_' +yyyymmddhhss + basename_ext
shutil.copyfile(full_filepath, backup_full_filepath)
print('--- Backup Master file ---', backup_full_filepath)
return (backup_full_filepath)
def messagebox_file_open(full_filepath):
if return_os_slash() == '\\\\': # add ver 2.1.1 for bug fix on Mac OS
filename = os.path.basename(full_filepath)
ret = tkinter.messagebox.askyesno('Complete', 'Do you want to open the created file?\n\n' + filename)
if ret == True:
subprocess.Popen(full_filepath, shell=True)
def check_file_type(full_filepath):
count_check_tag = 0
if full_filepath.endswith('.pptx'):
input_ppt = Presentation(full_filepath)
for i, sld in enumerate(input_ppt.slides, start=1):
for shp in sld.shapes:
if 'AUTO_SHAPE' in str(shp.shape_type) and str(shp.text) != '':
### check contain IF tag adjustments[0] = 0.xx445
try:
if shp.adjustments[0] == 0.99445 or shp.adjustments[0] == 0.50445: #check IF tag
count_check_tag += 1
if count_check_tag >= 2:
return (['ERROR','Please enter a PPT file that does not contain IF tags'])
except Exception as e:
#print('[info] Exception handling with check_file_type')
flag_exception_dummy = True
return_type_array = ['PPT_SKECH','PPT_SKECH']
elif full_filepath.endswith('.xlsx'):
return_type_array = ['ERROR', 'Please enter a EXCEL file compatible with NS']
input_excel = openpyxl.load_workbook(full_filepath)
# check ws name
ws_list = input_excel.get_sheet_names()
for sheet_name in ws_list:
if sheet_name == 'Master_Data':
return_type_array = ['EXCEL_MASTER', ws_list]
elif sheet_name == 'L1 Table':
return_type_array = ['EXCEL_DEVICE', ws_list]
elif full_filepath.endswith('.yaml'):
return_type_array = ['ERROR', 'Please enter a backup file of CML']
with open(str(full_filepath), 'r') as yml:
config = yaml.safe_load(yml)
for tmp_config in config:
if tmp_config == 'lab':
return_type_array = ['YAML_CML', config]
else:
return_type_array = ['ERROR', 'Please enter a file compatible with NS']
return return_type_array
def num2alpha(num): #input number output alphabet
if num<=26:
return chr(64+num).lower()
elif num%26==0:
return num2alpha(num//26-1)+chr(90).lower()
else:
return num2alpha(num//26)+chr(64+num%26).lower()
def get_ip_address_set(change_tmp_ip_address_array):
host = ipaddress.ip_interface(change_tmp_ip_address_array)
tmp_network = str(host.network)
tmp_ip = str(host.ip)
tmp_mask = host.network.prefixlen
tmp_ip_array = tmp_ip.split('.')
if tmp_mask >= 24:
return_ip = '.'+ tmp_ip_array[3]
elif tmp_mask >= 16:
return_ip = '.' + tmp_ip_array[2] + '.' + tmp_ip_array[3]
elif tmp_mask >= 8:
return_ip = '.' + tmp_ip_array[1] + '.' + tmp_ip_array[2] + '.' + tmp_ip_array[3]
else:
return_ip = tmp_ip
return ([change_tmp_ip_address_array,tmp_network,return_ip ]) # [ip_address,network_address,mask,host_address]
def get_description_width_hight(font_size,description):
par_char_ratio = 0.0095
font_hight_ratio = 0.018
per_char_width = font_size * par_char_ratio
font_size_hight = font_size * font_hight_ratio
result = [get_east_asian_width_count(description) * per_char_width,font_size_hight ]
return (result)
### write excel meta file ###
def write_excel_meta(master_excel_meta, excel_file_path, worksheet_name, section_write_to,offset_row, offset_column):
'''
:param master_excel_meta: tuple master data
:param excel_file_path: file path of excel master data
:param worksheet_name: worksheet name to write
:param section_write_to: decide the value of start row and column
:return: none
'''
#print(excel_file_path, worksheet_name, section_write_to)
wb = openpyxl.load_workbook(excel_file_path)
wb.active = wb[worksheet_name]
#worksheet backup
#wb.copy_worksheet(wb[worksheet_name])
#find the row and column of section
flag_section = False
empty_count = 0
row_count = 0
if section_write_to == '_template_':
flag_section = True
row_count = 1
while flag_section == False:
row_count += 1
if wb.active.cell(row_count, 1).value == section_write_to:
flag_section = True
elif '<<N/A>>' == section_write_to:
flag_section = True
row_count = 1
elif wb.active.cell(row_count, 1).value == None:
empty_count += 1
if empty_count > 10000:
flag_section = True
print('---ERROR and STOP--- can not find ---> %s ' % section_write_to)
exit()
#insert number of row
num_insert_row = 2
for i in master_excel_meta:
if i[0] > num_insert_row:
num_insert_row = i[0]
wb.active.insert_rows(row_count + 1 + offset_row, amount=num_insert_row - 1)
#write each cell
for num_wp_up in master_excel_meta:
wb.active.cell(num_wp_up[0] + row_count - 1 + offset_row, num_wp_up[1] + offset_column).value = master_excel_meta[num_wp_up]
# save excel file
wb.save(excel_file_path)
wb.close()
### overwrite excel meta file ###
def overwrite_excel_meta(master_excel_meta, excel_file_path, worksheet_name, section_write_to,offset_row, offset_column):
'''
:param master_excel_meta: tuple master data
:param excel_file_path: file path of excel master data
:param worksheet_name: worksheet name to write
:param section_write_to: deside the value of start row and column
:return: none
'''
#print(excel_file_path, worksheet_name, section_write_to)
wb = openpyxl.load_workbook(excel_file_path)
wb.active = wb[worksheet_name]
#worksheet backup
#wb.copy_worksheet(wb[worksheet_name])
#find the row and column of section
flag_section = False
empty_count = 0
row_count = 0
if section_write_to == '_template_':
flag_section = True
row_count = 1
while flag_section == False:
row_count += 1
if wb.active.cell(row_count, 1).value == section_write_to:
flag_section = True
elif '<<N/A>>' == section_write_to:
flag_section = True
row_count = 1
elif wb.active.cell(row_count, 1).value == None:
empty_count += 1
if empty_count > 10000:
flag_section = True
print('---ERROR and STOP--- can not find ---> %s ' % section_write_to)
exit()
#write each cell
for num_wp_up in master_excel_meta:
wb.active.cell(num_wp_up[0] + row_count - 1 + offset_row, num_wp_up[1] + offset_column).value = master_excel_meta[num_wp_up]
# save excel file
wb.save(excel_file_path)
wb.close()
### return shapes of tuple format ###
def return_shape_tuple(current_shape_array ,start_row):
'''
:param current_shape_array): current shape array
start_row : input row number in the tuple
'''
#sort top value
current_shape_array = sorted(current_shape_array, reverse=False, key=lambda x: x[3]) # sort for top
tmp_grid_array = []
grid_array = []
threshold_shape_top = current_shape_array[0][3]
threshold_shape_down = current_shape_array[0][3] + current_shape_array[0][5]
#make y grid array
flag_next_y_grid = False
for i in range(len(current_shape_array)):
if flag_next_y_grid == True:
threshold_shape_top = current_shape_array[i-1][3]
threshold_shape_down = current_shape_array[i-1][3] + current_shape_array[i-1][5]
flag_next_y_grid = False
if current_shape_array[i][3] <= threshold_shape_down and (current_shape_array[i][3] + current_shape_array[i][5]) >= threshold_shape_top:
tmp_grid_array.append(current_shape_array[i])
if i == len(current_shape_array) - 1:
grid_array.append(tmp_grid_array)
else:
grid_array.append(tmp_grid_array)
tmp_grid_array = []
tmp_grid_array.append(current_shape_array[i])
flag_next_y_grid = True
if i == len(current_shape_array) - 1:
grid_array.append([current_shape_array[i]])
# sort left value
master_grid_array = []
for tmp_grid_array in grid_array:
tmp_grid_array = sorted(tmp_grid_array, reverse=False, key=lambda x: x[2]) # sort for top
master_grid_array.append(tmp_grid_array)
'''Added automatic horizontal axis placement function at Ver 2.2.2 '''
# Remove items containing '_AIR_' in the second element
master_grid_array = [[item for item in sublist if '_AIR_' not in item[1]] for sublist in master_grid_array]
updated_master_grid_array = []
device_name_array = []
kari_master_grid_array = []
for tmp_master_grid_array in master_grid_array:
for tmp_tmp_master_grid_array in tmp_master_grid_array:
device_name_array.append(tmp_tmp_master_grid_array[1])
kari_master_grid_array.append(tmp_tmp_master_grid_array)
#print('--- device_name_array ---')
#print(device_name_array)
vertical_key_array = []
used_device_array = []
for tmp_device_name_array in device_name_array:
if tmp_device_name_array not in used_device_array:
#print('#####tmp_device_name_array,used_device_array,vertical_key_array',tmp_device_name_array,used_device_array,vertical_key_array)
target = tmp_device_name_array
result = None
for kari_kari_master_grid_array in kari_master_grid_array:
if kari_kari_master_grid_array[1] == target:
result = kari_kari_master_grid_array
break
if result not in vertical_key_array and result[1] not in used_device_array:
vertical_key_array.append(result)
used_device_array.append(result)
except_array = []
for sublist in master_grid_array:
except_array.extend(sublist)
flag_1st_match = False
#re-make at ver 2.3.4
for item in sublist:
if ((result[2] < item[2] + item[4] and result[2] > item[2]) or \
(result[2] + result[4] > item[2] and result[2] + result[4] < item[2] + item[4]) or \
(result[2] < item[2] and result[2] + result[4] > item[2] + item[4]) or \
(result[2] > item[2] and result[2] + result[4] < item[2] + item[4]) or \
(result[2] == item[2] and result[2] + result[4] == item[2] + item[4] and result[1] != item[1])) and \
(item[1] not in used_device_array) and (flag_1st_match == False or item not in except_array):
used_device_array.append(item[1])
except_array.remove(item)
flag_1st_match = True
# Remove duplicates by converting lists to tuples, adding to a set, and converting back to lists
vertical_key_array = [list(t) for t in set(tuple(item) for item in vertical_key_array)]
#print(vertical_key_array, len(vertical_key_array))
vertical_key_array_2 = []
for tmp_vertical_key_array in vertical_key_array:
vertical_key_array_2.append(
[tmp_vertical_key_array[0], '_AIR_', tmp_vertical_key_array[2], tmp_vertical_key_array[3],
tmp_vertical_key_array[4], tmp_vertical_key_array[5], tmp_vertical_key_array[6], tmp_vertical_key_array[7],
tmp_vertical_key_array[2] + int(tmp_vertical_key_array[4] * 0.5)])
vertical_key_array_2 = sorted(vertical_key_array_2, key=lambda x: x[8])
#print('####vertical_key_array_2 ####')
#print(vertical_key_array_2)
for tmp_master_grid_array in master_grid_array:
#print('####### tmp_master_grid_array ########')
#print(tmp_master_grid_array)
import copy
updated_vertical_key_array_2 = copy.deepcopy(vertical_key_array_2)
for i, tmp_vertical_key_array_2 in enumerate(vertical_key_array_2):
for tmp_tmp_master_grid_array in tmp_master_grid_array:
if tmp_tmp_master_grid_array not in updated_vertical_key_array_2:
if tmp_vertical_key_array_2[2] + tmp_vertical_key_array_2[4] > tmp_tmp_master_grid_array[2] and tmp_vertical_key_array_2[2] < tmp_tmp_master_grid_array[2]:
updated_vertical_key_array_2[i] = tmp_tmp_master_grid_array
break
elif tmp_vertical_key_array_2[2] > tmp_tmp_master_grid_array[2] and tmp_vertical_key_array_2[2] + tmp_vertical_key_array_2[4] < tmp_tmp_master_grid_array[2] + tmp_tmp_master_grid_array[4]:
updated_vertical_key_array_2[i] = tmp_tmp_master_grid_array
break
elif tmp_vertical_key_array_2[2] + tmp_vertical_key_array_2[4] >= tmp_tmp_master_grid_array[2] + tmp_tmp_master_grid_array[4] and tmp_vertical_key_array_2[2] < tmp_tmp_master_grid_array[2] + tmp_tmp_master_grid_array[4]:
updated_vertical_key_array_2[i] = tmp_tmp_master_grid_array
break
elif tmp_vertical_key_array_2[2] < tmp_tmp_master_grid_array[2] and tmp_vertical_key_array_2[2] + tmp_vertical_key_array_2[4] > tmp_tmp_master_grid_array[2] + tmp_tmp_master_grid_array[4]:
updated_vertical_key_array_2[i] = tmp_tmp_master_grid_array
break
elif tmp_vertical_key_array_2[2] == tmp_tmp_master_grid_array[2] and tmp_vertical_key_array_2[4] == tmp_tmp_master_grid_array[4]:
updated_vertical_key_array_2[i] = tmp_tmp_master_grid_array
break
#print(updated_vertical_key_array_2,len(updated_vertical_key_array_2))
updated_master_grid_array.append(updated_vertical_key_array_2)
#print('--- updated_master_grid_array ---')
#print(updated_master_grid_array)
master_grid_array = copy.deepcopy(updated_master_grid_array)
#print('--- master_grid_array ---')
#print(master_grid_array)
'''Addition completed(ver 2.2.2)'''
# make the tuple format
tuple_grid_array = {}
tuple_grid_array[start_row, 1] = master_grid_array[0][0][0]
num_row = start_row -1
num_column =1
for tmp_y in master_grid_array:
num_row += 1
for tmp_x in tmp_y:
num_column += 1
tuple_grid_array[num_row, num_column] = tmp_x[1]
tuple_grid_array[num_row, num_column + 1] = '<END>'
num_column = 1
tuple_grid_array[num_row + 1, 1] = '<END>'
return(tuple_grid_array)
### return width size of each folder ###
def get_folder_width_size(master_folder_tuple,master_style_shape_tuple,master_shape_tuple,min_tag_inches):
#add parameter at ver2.1 for large size
folder_width_ratio = 0.5 # add at ver 2.1 for large size
master_width_size_folder = []
master_width_size_y_grid = []
master_hight_size_y_grid = []
master_folder_size = []
folder_num_list = []
for tmp_master_folder_tuple in master_folder_tuple:
if tmp_master_folder_tuple[1] == 1 and master_folder_tuple[tmp_master_folder_tuple] != '<SET_WIDTH>':
folder_num_list.append(tmp_master_folder_tuple[0])
#print(master_folder_tuple)
current_folder_start_row = 1
current_folder_end_row = 1
for folder_num in folder_num_list:
#print('------------' , folder_num)
i = 0
for tmp_master_folder_tuple in master_folder_tuple:
if tmp_master_folder_tuple[0] == folder_num and tmp_master_folder_tuple[1] != 1:
if master_folder_tuple[tmp_master_folder_tuple] == '':
master_width_size_folder.append([folder_num, [['_empty_']]])
master_folder_size.append([folder_num, [['_empty_']]])
#print([folder_num, [['_empty_']]])
i += 1
else:
i += 1
flag_shape_start = False
for tmp_master_shape_tuple in master_shape_tuple:
if tmp_master_shape_tuple[1] == 1 and master_shape_tuple[tmp_master_shape_tuple] == '<END>' and flag_shape_start == True:
current_folder_end_row = tmp_master_shape_tuple[0] - 1
break
if tmp_master_shape_tuple[1] == 1 and master_folder_tuple[tmp_master_folder_tuple] == master_shape_tuple[tmp_master_shape_tuple]:
current_folder_start_row = tmp_master_shape_tuple[0]
flag_shape_start = True
#print(master_folder_tuple[tmp_master_folder_tuple],current_folder_start_row,current_folder_end_row)
tmp_folder_size =[]
current_level = 0
current_max_width = 0
current_max_hight = 0
tmp_hight = 0
for r in range(current_folder_start_row,current_folder_end_row+1):
tmp_width = 0
tmp_count_shape = 0
current_max_hight = 0
for tmp_master_shape_tuple in master_shape_tuple:
if tmp_master_shape_tuple[0] == r and tmp_master_shape_tuple[1] != 1:
for tmp_master_style_shape_tuple in master_style_shape_tuple:
if master_style_shape_tuple[tmp_master_style_shape_tuple[0],1] == master_shape_tuple[tmp_master_shape_tuple]:
# sum width in a level in a folder
tmp_width += master_style_shape_tuple[tmp_master_style_shape_tuple[0], 2]
tmp_count_shape += 1
#get max value in a level in a folder
if current_max_hight < (master_style_shape_tuple[tmp_master_style_shape_tuple[0], 3] + (min_tag_inches * 2.5)):
current_max_hight = (master_style_shape_tuple[tmp_master_style_shape_tuple[0], 3] + (min_tag_inches * 2.5))
break
current_level +=1
#print(current_level,tmp_width,tmp_hight,tmp_count_shape)
current_level_inches_width = min_tag_inches * 18 + tmp_width + ((tmp_count_shape-1) * (min_tag_inches * 4 )) # ver2.2.1(a) chage , min_tag_inches * 2 ->18
#print('----current_level_inches_hight ---- ',master_folder_tuple[tmp_master_folder_tuple],master_style_shape_tuple[tmp_master_style_shape_tuple[0], 3],current_level,current_max_hight)
tmp_hight += current_max_hight
if current_max_width < current_level_inches_width:
current_max_width = current_level_inches_width
tmp_hight += 1.0 # add up down buffer for a hight in a folder
#print(master_folder_tuple[tmp_master_folder_tuple],current_max_width,tmp_hight)
tmp_folder_size.append([master_folder_tuple[tmp_master_folder_tuple],current_max_width * folder_width_ratio,tmp_hight]) # add folder_width_ratio at ver 2.1 for large size
master_width_size_folder.append([folder_num,tmp_folder_size])
#print([folder_num,tmp_folder_size])
master_folder_size.append([folder_num,tmp_folder_size])
if i == 0:
master_width_size_folder.append([folder_num, [['_empty_']]])
master_folder_size.append([folder_num, [['_empty_']]])
#print([folder_num, [['_empty_']]])
#Add _empty_ value
for folder_num in folder_num_list:
tmp_sum_width = 0
empty_count = 0
shape_count = 0
for tmp_master_min_size_folder in master_width_size_folder:
if tmp_master_min_size_folder[0] == folder_num and tmp_master_min_size_folder[1][0][0] != '_empty_':
tmp_sum_width += tmp_master_min_size_folder[1][0][1]
elif tmp_master_min_size_folder[0] == folder_num and tmp_master_min_size_folder[1][0][0] == '_empty_':
empty_count += 1
shape_count += 1
master_width_size_y_grid.append([folder_num,(tmp_sum_width + (((tmp_sum_width / shape_count) * 0.1) *empty_count)),((tmp_sum_width / shape_count) * 0.2)])
#print('---- empty -----',((((tmp_sum_width / shape_count) * 0.1) *empty_count)))
#GET best width of slide (inches)
slide_max_width_inches = 0
for tmp_master_min_size_y_grid in master_width_size_y_grid:
if slide_max_width_inches < tmp_master_min_size_y_grid[1]:
slide_max_width_inches = tmp_master_min_size_y_grid[1]
#GET best hight of slide (inches)
slide_max_hight_inches = 0
#print('---master_folder_size--- ', master_folder_size)
for tmp_master_min_size_y_grid in master_width_size_y_grid:
tmp_max_hight_y_grid = 0
flag_only_wp = True
#print('----tmp_master_min_size_y_grid----',tmp_master_min_size_y_grid)
for tmp_master_folder_size in master_folder_size:
if tmp_master_min_size_y_grid[0] == tmp_master_folder_size[0] and tmp_master_folder_size[1][0][0] != '_empty_':
if tmp_max_hight_y_grid < tmp_master_folder_size[1][0][2]:
tmp_max_hight_y_grid = tmp_master_folder_size[1][0][2]
if '_wp_' not in str(tmp_master_folder_size[1][0][0]):
flag_only_wp = False
if flag_only_wp == True:
tmp_max_hight_y_grid = tmp_max_hight_y_grid * 1 # Change hight ratio Ver 1.1
if tmp_max_hight_y_grid == 0:
tmp_max_hight_y_grid = 0.5 # only empty level is 0.5 inches
#print('tmp_max_hight_y_grid ----- ',tmp_max_hight_y_grid )
master_hight_size_y_grid.append([tmp_master_min_size_y_grid[0],tmp_max_hight_y_grid])
slide_max_hight_inches += tmp_max_hight_y_grid
#print('----slide_max_width_inches----',slide_max_width_inches)
#print('----master_width_size_y_grid----',master_width_size_y_grid)
#print('----master_folder_size----',master_folder_size)
#print('----slide_max_hight_inches----',slide_max_hight_inches)
#print('----master_hight_size_y_gri----',master_hight_size_y_grid)
return([slide_max_width_inches, master_width_size_y_grid, master_folder_size, slide_max_hight_inches, master_hight_size_y_grid])
def get_root_folder_tuple(self,master_folder_size_array,tmp_folder_name):
self.root_left = 0.28
self.root_top = 1.42
self.root_width = math.ceil(master_folder_size_array[0] * 12) / 10 #ver2.2.0 change, 10 -> 12
self.root_hight = math.ceil(master_folder_size_array[3] * 12) / 10 #ver1.1 change, 10 -> 12
ppt_min_width = 6 # inches 13.4
ppt_min_hight = 4 # inches 7.5
master_root_folder_tuple = {}
master_root_folder_tuple[2, 3] = 1
master_root_folder_tuple[2, 4] = 1
master_root_folder_tuple[2, 5] = self.root_left
master_root_folder_tuple[2, 6] = self.root_top
if self.root_width < (ppt_min_width - (self.root_left * 2)):
master_root_folder_tuple[2, 7] = (ppt_min_width - (self.root_left * 2))
else:
master_root_folder_tuple[2, 7] = self.root_width
if self.root_hight < (ppt_min_hight - (self.root_top * 1.5)):
master_root_folder_tuple[2, 8] = (ppt_min_hight - (self.root_top * 1.5))
else:
master_root_folder_tuple[2, 8] = self.root_hight
master_root_folder_tuple[2, 2] = '[L1]' + tmp_folder_name
return(master_root_folder_tuple)
### return tuple for def - return_shape_tuple - ###
def convert_array_to_tuple(tmp_master_data_array):
template_master_data_tuple = {}
for tmp_tmp_master_data_array in tmp_master_data_array:
i = 1
for tmp_tmp_tmp_master_data_array in tmp_tmp_master_data_array[1]:
template_master_data_tuple[tmp_tmp_master_data_array[0],i] = tmp_tmp_tmp_master_data_array
i += 1
return(template_master_data_tuple)
### return array for def - return_array - ###
def convert_tuple_to_array(tmp_master_data_tuple):
tmp_master_data_array = []
for tmp_tmp_master_data_tuple in tmp_master_data_tuple:
tmp_master_data_array.append([tmp_tmp_master_data_tuple[0],tmp_tmp_master_data_tuple[1],tmp_master_data_tuple[tmp_tmp_master_data_tuple]])
# sort row -> column
tmp_master_data_array = sorted(tmp_master_data_array, reverse=False, key=lambda x:( x[0],x[1])) # sort row -> column
#print(tmp_master_data_array)
master_data_array = []
tmp_tmp_array = []
flag_first = True
for tmp_array in tmp_master_data_array:
if flag_first == True:
tmp_num = tmp_array[0]
#tmp_tmp_array.append(tmp_array[2])
flag_first = False
if flag_first == False:
if tmp_num == tmp_array[0]:
tmp_tmp_array.append(tmp_array[2])
else:
master_data_array.append([tmp_num ,tmp_tmp_array])
tmp_num = tmp_array[0]
tmp_tmp_array = []
tmp_tmp_array.append(tmp_array[2])
master_data_array.append([tmp_num, tmp_tmp_array])
return(master_data_array)
### return folder and wp name array from master excel file ###
def get_folder_wp_array_from_master(ws_name, ppt_meta_file):
input_ppt_mata_excel = openpyxl.load_workbook(ppt_meta_file)
input_ppt_mata_excel.active = input_ppt_mata_excel[ws_name]
# GET Folder names
flag_finish = False
current_row = 1
folder_name_array = []
wp_name_array = []
while flag_finish == False:
if input_ppt_mata_excel.active.cell(current_row, 1).value == '<<POSITION_SHAPE>>':
#print(input_ppt_mata_excel.active.cell(current_row, 1).value)
start_row = current_row
if input_ppt_mata_excel.active.cell(current_row, 1).value == '<<STYLE_SHAPE>>':
#print(input_ppt_mata_excel.active.cell(current_row, 1).value)
end_row = current_row - 1
flag_finish = True
current_row += 1
for i in range(start_row + 1, end_row+1):
if str(input_ppt_mata_excel.active.cell(i, 1).value) != 'None' and str(input_ppt_mata_excel.active.cell(i, 1).value) != '<END>' \
and '_wp_' not in str(input_ppt_mata_excel.active.cell(i, 1).value):
folder_name_array.append(str(input_ppt_mata_excel.active.cell(i, 1).value))
elif '_wp_' in str(input_ppt_mata_excel.active.cell(i, 1).value):
wp_name_array.append(str(input_ppt_mata_excel.active.cell(i, 1).value))
input_ppt_mata_excel.close()
return([folder_name_array, wp_name_array])
### copy excel sheet to own file ###
def copy_excel_sheet(ws_name, ppt_meta_file, copy_sheet_name):
input_ppt_mata_excel = openpyxl.load_workbook(ppt_meta_file)
# check tmp_ws_name already exits
ws_list = input_ppt_mata_excel.get_sheet_names()
if copy_sheet_name in ws_list:
ws = input_ppt_mata_excel.remove(input_ppt_mata_excel[copy_sheet_name])
# copy
ws = input_ppt_mata_excel.copy_worksheet(input_ppt_mata_excel[ws_name])
ws.title = copy_sheet_name
input_ppt_mata_excel.save(ppt_meta_file)
input_ppt_mata_excel.close()
return()
### remove excel sheet ###
def remove_excel_sheet(ppt_meta_file, copy_sheet_name):
input_ppt_mata_excel = openpyxl.load_workbook(ppt_meta_file)
# check tmp_ws_name already exits
ws_list = input_ppt_mata_excel.get_sheet_names()
if copy_sheet_name in ws_list:
ws = input_ppt_mata_excel.remove(input_ppt_mata_excel[copy_sheet_name])
input_ppt_mata_excel.save(ppt_meta_file)
input_ppt_mata_excel.close()
return()
### create excel sheet ###
def create_excel_sheet(ppt_meta_file, sheet_name):
input_ppt_mata_excel = openpyxl.load_workbook(ppt_meta_file)
# check tmp_ws_name already exits
ws_list = input_ppt_mata_excel.get_sheet_names()
if sheet_name in ws_list:
ws = input_ppt_mata_excel.remove(input_ppt_mata_excel[sheet_name])
ws = input_ppt_mata_excel.create_sheet(sheet_name)
else:
ws = input_ppt_mata_excel.create_sheet(sheet_name)
input_ppt_mata_excel.save(ppt_meta_file)
input_ppt_mata_excel.close()
return()
#convert from master to array
def convert_master_to_array(ws_name, ppt_meta_file,section_name):
input_ppt_mata_excel = openpyxl.load_workbook(ppt_meta_file)
input_ppt_mata_excel.active = input_ppt_mata_excel[ws_name]
# GET Folder names
flag_finish = False
flag_get_start_row = False
current_row = 1
empty_count = 0
start_row = 1
while flag_finish == False:
if input_ppt_mata_excel.active.cell(current_row, 1).value == section_name:
#print(input_ppt_mata_excel.active.cell(current_row, 1).value)
start_row = current_row
flag_get_start_row = True
current_row += 1
if '<<' in str(input_ppt_mata_excel.active.cell(current_row , 1).value) and '>>' in str(input_ppt_mata_excel.active.cell(current_row, 1).value)\
and flag_get_start_row == True:
#print(input_ppt_mata_excel.active.cell(current_row, 1).value)
end_row = current_row - 1
flag_finish = True
if str(input_ppt_mata_excel.active.cell(current_row, 1).value) == 'None':
empty_count += 1
else:
empty_count = 0
### Add IF section_name == '<<POSITION_TAG>>' for large map at 2.3.0
if empty_count >= 100 and section_name == '<<POSITION_TAG>>':
flag_finish = True
end_row = current_row
elif empty_count >= 3000:
flag_finish = True
end_row = current_row
current_row += 1
#print(start_row,end_row)
return_array = []
for tmp_row in range(start_row,end_row+1):
tmp_array = []
current_row_array = []
flag_column_end = False
tmp_column = 1
tmp_empty_count = 0
while flag_column_end == False:
if str(input_ppt_mata_excel.active.cell(tmp_row, tmp_column).value) != 'None' and tmp_empty_count == 0:
current_row_array.append(input_ppt_mata_excel.active.cell(tmp_row, tmp_column).value)
if str(input_ppt_mata_excel.active.cell(tmp_row, tmp_column).value) == 'None':
tmp_empty_count += 1
tmp_array.append('')
if str(input_ppt_mata_excel.active.cell(tmp_row, tmp_column).value) != 'None' and tmp_empty_count != 0:
tmp_empty_count = 0
for m in tmp_array:
current_row_array.append(m)
current_row_array.append(input_ppt_mata_excel.active.cell(tmp_row, tmp_column).value)
tmp_array = []
if tmp_empty_count >= 100:
flag_column_end = True
tmp_column += 1
if len(current_row_array) != 0:
return_array.append([tmp_row - start_row +1,current_row_array])
return(return_array)
#convert from excel table to array
def convert_excel_to_array(ws_name, excel_file, start_row):
input_ppt_mata_excel = openpyxl.load_workbook(excel_file)
input_ppt_mata_excel.active = input_ppt_mata_excel[ws_name]
# GET Folder names
flag_finish = False
current_row = 1
empty_count = 0
while flag_finish == False:
if str(input_ppt_mata_excel.active.cell(current_row, 1).value) == 'None' and str(input_ppt_mata_excel.active.cell(current_row, 2).value) == 'None':
empty_count += 1
else:
empty_count = 0
if empty_count >= 100:
flag_finish = True
end_row = current_row
current_row += 1
#print(start_row,end_row)
return_array = []
for tmp_row in range(start_row,end_row+1):
tmp_array = []
current_row_array = []
flag_column_end = False
tmp_column = 1
tmp_empty_count = 0
while flag_column_end == False:
if str(input_ppt_mata_excel.active.cell(tmp_row, tmp_column).value) != 'None' and tmp_empty_count == 0:
current_row_array.append(input_ppt_mata_excel.active.cell(tmp_row, tmp_column).value)
if str(input_ppt_mata_excel.active.cell(tmp_row, tmp_column).value) == 'None':
tmp_empty_count += 1
tmp_array.append('')
if str(input_ppt_mata_excel.active.cell(tmp_row, tmp_column).value) != 'None' and tmp_empty_count != 0:
tmp_empty_count = 0
for m in tmp_array:
current_row_array.append(m)
current_row_array.append(input_ppt_mata_excel.active.cell(tmp_row, tmp_column).value)
tmp_array = []
if tmp_empty_count >= 100:
flag_column_end = True
tmp_column += 1
if len(current_row_array) != 0:
return_array.append([tmp_row - start_row +1,current_row_array])
input_ppt_mata_excel.close()
return(return_array)
def clear_section_sheet(tmp_ws_name, ppt_meta_file, clear_section_taple):
wb = openpyxl.load_workbook(ppt_meta_file)
wb.active = wb[tmp_ws_name]
#GET section row and column
section_name = 'N/A'
for tmp_clear_section_taple in clear_section_taple:
if '<<' in str(clear_section_taple[tmp_clear_section_taple]) and '>>' in str(clear_section_taple[tmp_clear_section_taple]):
section_name = clear_section_taple[tmp_clear_section_taple]
break
flag_get_section = False
i = 1
while flag_get_section == False:
if wb.active.cell(i,1).value == section_name:
start_row = i
flag_get_section = True
break
i += 1
if i > 1000000:
print('EEROR cannot find section name -- ',section_name)
exit()
for tmp_clear_section_taple in clear_section_taple:
if str(clear_section_taple[tmp_clear_section_taple]) != str(section_name):
wb.active.cell(tmp_clear_section_taple[0] + start_row -1, tmp_clear_section_taple[1]).value = ''
wb.save(ppt_meta_file)
wb.close()
return ('clear_section_sheet')
def clear_tag_in_position_line(tmp_ws_name, ppt_meta_file, clear_section_taple):
wb = openpyxl.load_workbook(ppt_meta_file)
wb.active = wb[tmp_ws_name]
#GET section row and column
section_name = 'N/A'
for tmp_clear_section_taple in clear_section_taple:
if '<<' in str(clear_section_taple[tmp_clear_section_taple]) and '>>' in str(clear_section_taple[tmp_clear_section_taple]):
section_name = clear_section_taple[tmp_clear_section_taple]
break
flag_get_section = False
i = 1
while flag_get_section == False:
if wb.active.cell(i,1).value == section_name:
start_row = i
flag_get_section = True
break
i += 1
if i > 1000000:
print('EEROR cannot find section name -- ',section_name)
exit()
for tmp_clear_section_taple in clear_section_taple:
if str(clear_section_taple[tmp_clear_section_taple]) != str(section_name) and (tmp_clear_section_taple[1] == 3 or tmp_clear_section_taple[1] == 4):
wb.active.cell(tmp_clear_section_taple[0] + start_row -1, tmp_clear_section_taple[1]).value = ''
wb.save(ppt_meta_file)
wb.close()
return ('clear_tag_in_position_line')
#get shape name in the folder and sort by tuple type
def get_shape_folder_tuple(position_shape_tuple):
return_tuple = {}
current_folder_name = ''
for tmp_position_shape_tuple in position_shape_tuple:
if tmp_position_shape_tuple[0] != 1 and position_shape_tuple[tmp_position_shape_tuple] != '<END>':
if position_shape_tuple[tmp_position_shape_tuple[0],1] != '' and tmp_position_shape_tuple[1] == 1:
#print(position_shape_tuple[tmp_position_shape_tuple])
current_folder_name = position_shape_tuple[tmp_position_shape_tuple]
if tmp_position_shape_tuple[1] != 1:
return_tuple[position_shape_tuple[tmp_position_shape_tuple]] = current_folder_name
return (return_tuple)
### convert value from interface name . exsample Gigabit Ethernet 0/0 -> 1001000
def get_if_value(if_name):
sum_num = 0
if_name = if_name.rstrip()
if ' ' in if_name:
split_if_name = split_portname(if_name)
if '/' in split_if_name[1] or '.' in split_if_name[1]: #update replace '.' to '/' for Network Sketcher ver 2.0
split_if_name[1] = split_if_name[1].replace('.','/')
each_num = split_if_name[1].split('/')
#print(split_if_name[1],len(each_num))
tmp_add_value = '1'
for i in range(0,int(len(each_num))):
tmp_add_value += '000'
tmp_num = int(tmp_add_value)
#print('tmp_num ', tmp_num)
for n in range(0,int(len(each_num))):
sum_num += (int(each_num[n]) + 1) * tmp_num
tmp_num = tmp_num/1000
#print(split_if_name[1],sum_num)
if_value = sum_num
else:
if_value = split_if_name[1]
else:
if_value = -1
return (int(if_value))
def split_portname(if_name):
#reduce space' ' in if name
if_name_split = str(if_name).split(' ')
tmp_if_name_split = str(if_name).replace(str(if_name_split[-1]), '')
if len(if_name_split) != 1:
name = tmp_if_name_split.replace(' ','')
num = str(if_name_split[-1])
else:
name = tmp_if_name_split.replace(' ','')
num = ''
return_array = [name, num]
return (return_array)
def check_file_open(file_fullpath):
if os.path.exists(file_fullpath):
try:
os.rename(file_fullpath, file_fullpath) #can't rename an open file so an error will be thrown
return False
except:
#tkinter.messagebox.showwarning(title="File is being opened", message="Please close the file below." + '\n\n' + file_fullpath)
print('[WARNING] The file you are writing to may have been left open; Windows may display this message even when there is no problem.')