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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
|
#!/usr/bin/python
#
# Copyright 2009 Google Inc. All Rights Reserved.
#
# 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.
#
"""Validation and type conversion functions."""
__author__ = 'api.sgrinberg@gmail.com (Stan Grinberg)'
from aw_api import MAX_TARGET_NAMESPACE
from aw_api import Utils
from aw_api import SanityCheck as glob_sanity_check
from aw_api.Errors import ValidationError
def GetPyClass(name, web_services):
"""Return Python class for a given class name.
Args:
name: str name of the Python class to return.
web_services: module for web service.
Returns:
Python class.
"""
for index in xrange(MAX_TARGET_NAMESPACE):
try:
pyclass = eval('web_services.ns%s.%s_Def(\'%s\').pyclass' % (index, name,
name))
break
except AttributeError:
if index == MAX_TARGET_NAMESPACE - 1:
version = web_services.__dict__['__name__'].split('.')[2]
msg = ('Given API version, %s, is not compatible with \'%s\' class.' %
(version, name))
raise ValidationError(msg)
return pyclass
def IsPyClass(obj):
"""Return True if a given object is a Python class, False otherwise.
Args:
obj: object an object to check.
Returns:
bool True if a given object is a Python class, False otherwise.
"""
if (hasattr(obj, 'typecode') and
str(obj.typecode.pyclass).find('_Holder') > -1):
return True
return False
def ValidateAccountInfoV13(acct_info):
"""Validate AccountInfo object.
Args:
acct_info: dict AccountInfo object.
"""
glob_sanity_check.ValidateTypes(((acct_info, dict),))
for key in acct_info:
if key in ('defaultNetworkTargeting',):
glob_sanity_check.ValidateTypes(((acct_info[key], list),))
network_types = []
for sub_key in acct_info[key]:
glob_sanity_check.ValidateTypes(((sub_key, (str, unicode)),))
network_types.append(sub_key)
acct_info[key] = {'networkTypes': network_types}
elif key in ('emailPromotionsPreferences',):
glob_sanity_check.ValidateTypes(((acct_info[key], dict),))
for sub_key in acct_info[key]:
glob_sanity_check.ValidateTypes(
((acct_info[key][sub_key], (str, unicode)),))
else:
glob_sanity_check.ValidateTypes(((acct_info[key], (str, unicode)),))
def ValidateAdGroupV13(ad_group):
"""Validate AdGroup object.
Args:
ad_group: dict AdGroup object.
"""
glob_sanity_check.ValidateTypes(((ad_group, dict),))
for key in ad_group:
glob_sanity_check.ValidateTypes(((ad_group[key], (str, unicode)),))
def ValidateOptimizerSettingsV13(optimizer):
"""Validate Optimizer object.
An Optimizer object is one of BudgetOptimizerSettings,
ConversionOptimizerSettings.
Args:
optimizer: dict Optimizer object.
"""
glob_sanity_check.ValidateTypes(((optimizer, dict),))
for key in optimizer:
glob_sanity_check.ValidateTypes(((optimizer[key], (str, unicode)),))
def ValidateGeoTargetV13(target):
"""Validate GeoTarget object.
Args:
target: dict GeoTarget object.
Returns:
dict updated GeoTarget object.
"""
glob_sanity_check.ValidateTypes(((target, dict),))
for key in target:
if target[key] == 'None': continue
if key in ('targetAll',):
glob_sanity_check.ValidateTypes(((target[key], (str, unicode)),))
data = target[key]
else:
glob_sanity_check.ValidateTypes(((target[key], dict),))
geo_target = target[key]
for sub_key in geo_target:
glob_sanity_check.ValidateTypes(((geo_target[sub_key], list),))
for item in geo_target[sub_key]:
if sub_key in ('circles',):
circle = {}
for sub_sub_key in item:
glob_sanity_check.ValidateTypes(((item[sub_sub_key],
(str, unicode)),))
circle[sub_sub_key] = item[sub_sub_key]
item = circle
else:
glob_sanity_check.ValidateTypes(((item, (str, unicode)),))
# If value is an empty list, remove key from the dictionary.
if not geo_target[sub_key]:
geo_target = Utils.UnLoadDictKeys(geo_target, [sub_key])
data = geo_target
target[key] = data
return target
def ValidateLanguageTargetV13(targets):
"""Validate LanguageTarget object.
Args:
targets: list LanguageTarget objects.
Returns:
list updated LanguageTarget objects.
"""
glob_sanity_check.ValidateTypes(((targets, list),))
languages = []
for item in targets:
glob_sanity_check.ValidateTypes(((item, (str, unicode)),))
languages.append({'languages': item})
return languages
def ValidateNetworkTargetV13(targets):
"""Validate NetworkTarget object.
Args:
targets: list NetworkTarget objects.
Returns:
list updated NetworkTarget objects.
"""
glob_sanity_check.ValidateTypes(((targets, list),))
networks = []
for item in targets:
glob_sanity_check.ValidateTypes(((item, (str, unicode)),))
networks.append({'networkTypes': item})
return networks
def ValidateAdScheduleV13(schedule):
"""Validate AdSchedule object.
Args:
schedule: dict AdSchedule object.
Returns:
dict updated AdSchedule object.
"""
glob_sanity_check.ValidateTypes(((schedule, dict),))
for key in schedule:
if schedule[key] == 'None': continue
if key in ('intervals',):
glob_sanity_check.ValidateTypes(((schedule[key], list),))
for item in schedule[key]:
glob_sanity_check.ValidateTypes(((item, dict),))
interval = {}
for sub_key in item:
glob_sanity_check.ValidateTypes(((item[sub_key], (str, unicode)),))
interval[sub_key] = item[sub_key]
item = interval
data = schedule[key]
else:
glob_sanity_check.ValidateTypes(((schedule[key], (str, unicode)),))
data = schedule[key]
schedule[key] = data
return schedule
def ValidateCampaignV13(campaign):
"""Validate Campaign object.
Args:
campaign: dict Campaign object.
"""
glob_sanity_check.ValidateTypes(((campaign, dict),))
for key in campaign:
if key in ('budgetOptimizerSettings', 'conversionOptimizerSettings'):
ValidateOptimizerSettingsV13(campaign[key])
elif key in ('geoTargeting',):
campaign[key] = ValidateGeoTargetV13(campaign[key])
elif key in ('languageTargeting',):
data = campaign[key]
if 'languages' in campaign[key]:
data = campaign[key]['languages']
campaign[key] = ValidateLanguageTargetV13(data)
elif key in ('networkTargeting',):
data = campaign[key]
if 'networkTypes' in campaign[key]:
data = campaign[key]['networkTypes']
campaign[key] = ValidateNetworkTargetV13(data)
elif key in ('schedule',):
campaign[key] = ValidateAdScheduleV13(campaign[key])
else:
glob_sanity_check.ValidateTypes(((campaign[key], (str, unicode)),))
def ValidateSeedKeywordV13(seed):
"""Validate SeedKeyword object.
Args:
seed: dict SeedKeyword object.
"""
glob_sanity_check.ValidateTypes(((seed, dict),))
for key in seed:
glob_sanity_check.ValidateTypes(((seed[key], (str, unicode)),))
def ValidateDefinedReportJobV13(job, web_services):
"""Validate DefinedReportJob object.
Args:
job: dict DefinedReportJob object.
web_services: module for web services.
Returns:
DefinedReportJob instance.
"""
report_type = GetPyClass('DefinedReportJob', web_services)
new_job = report_type()
for key in job:
if job[key] == 'None': continue
if key in ('adGroups', 'adGroupStatuses', 'aggregationTypes', 'campaigns',
'campaignStatuses', 'clientEmails', 'keywords',
'keywordStatuses', 'selectedColumns'):
glob_sanity_check.ValidateTypes(((job[key], list),))
for item in job[key]:
glob_sanity_check.ValidateTypes(((item, (str, unicode)),))
else:
glob_sanity_check.ValidateTypes(((job[key], (str, unicode)),))
new_job.__dict__.__setitem__('_%s' % key, job[key])
return new_job
def ValidateDemographicsTargetV13(demo):
"""Validate DemographicsTarget object.
Args:
demo: dict DemographicsTarget object.
"""
glob_sanity_check.ValidateTypes(((demo, dict),))
for key in demo:
glob_sanity_check.ValidateTypes(((demo[key], (str, unicode)),))
def ValidateLanguageGeoTargetingV13(targeting):
"""Validate LanguageGeoTargeting object.
Args:
targeting: dict LanguageGeoTargeting object.
"""
glob_sanity_check.ValidateTypes(((targeting, dict),))
for key in targeting:
glob_sanity_check.ValidateTypes(((targeting[key], list),))
for sub_key in targeting[key]:
glob_sanity_check.ValidateTypes(((sub_key, (str, unicode)),))
def ValidateAdGroupRequestV13(request):
"""Validate AdGroupRequest object.
Args:
request: dict AdGroupRequest object.
"""
glob_sanity_check.ValidateTypes(((request, dict),))
for key in request:
if key in ('keywordRequests',):
glob_sanity_check.ValidateTypes(((request[key], list),))
for item in request[key]:
ValidateKeywordRequestV13(item)
else:
glob_sanity_check.ValidateTypes(((request[key], (str, unicode)),))
def ValidateCampaignRequestV13(request):
"""Validate CampaignRequest object.
Args:
request: dict CampaignRequest object.
"""
glob_sanity_check.ValidateTypes(((request, dict),))
for key in request:
if key in ('adGroupRequests',):
glob_sanity_check.ValidateTypes(((request[key], list),))
for item in request[key]:
ValidateAdGroupRequestV13(item)
elif key in ('geoTargeting',):
request[key] = ValidateGeoTargetV13(request[key])
elif key in ('languageTargeting',):
request[key] = ValidateLanguageTargetV13(request[key])
elif key in ('networkTargeting',):
request[key] = ValidateNetworkTargetV13(request[key])
else:
glob_sanity_check.ValidateTypes(((request[key], (str, unicode)),))
def ValidateKeywordTrafficRequestV13(request):
"""Validate KeywordTrafficRequest object.
Args:
request: dict KeywordTrafficRequest object.
"""
glob_sanity_check.ValidateTypes(((request, dict),))
for key in request:
glob_sanity_check.ValidateTypes(((request[key], (str, unicode)),))
def ValidateKeywordRequestV13(request):
"""Validate KeywordRequest object.
Args:
request: dict KeywordRequest object.
"""
glob_sanity_check.ValidateTypes(((request, dict),))
for key in request:
glob_sanity_check.ValidateTypes(((request[key], (str, unicode)),))
def ValidateImage(image, web_services):
"""Validate Image object.
Args:
image: dict Image object.
web_services: module for web services.
Returns:
Image instance.
"""
if IsPyClass(image):
return image
glob_sanity_check.ValidateTypes(((image, dict),))
new_image = GetPyClass('Image', web_services)
for key in image:
if image[key] == 'None': continue
if key in ('dimensions',):
glob_sanity_check.ValidateTypes(((image[key], list),))
dimensions = []
for item in image[key]:
dimensions.append(ValidateMapEntry(item,
'Media_Size_DimensionsMapEntry',
web_services))
data = dimensions
elif key in ('urls',):
glob_sanity_check.ValidateTypes(((image[key], list),))
urls = []
for item in image[key]:
urls.append(ValidateMapEntry(item,
'Media_Size_StringMapEntry',
web_services))
data = urls
elif key in ('extendedCapabilities',):
glob_sanity_check.ValidateTypes(((image[key], list),))
capabilities = []
for item in image[key]:
capabilities.append(ValidateMapEntry(item,
'Media_MediaExtendedCapabilityType_Media_MediaExtendedCapabilityStateMapEntry',
web_services))
data = capabilities
else:
glob_sanity_check.ValidateTypes(((image[key], (str, unicode)),))
data = image[key]
new_image.__dict__.__setitem__('_%s' % key, data)
return new_image
def ValidateDimensions(dimensions, web_services):
"""Validate Dimensions object.
Args:
dimensions: dict Dimensions object.
web_services: module for web services.
Returns:
Dimensions instance.
"""
if IsPyClass(dimensions):
return dimensions
glob_sanity_check.ValidateTypes(((dimensions, dict),))
new_dimensions = GetPyClass('Dimensions', web_services)
for key in dimensions:
if dimensions[key] == 'None': continue
glob_sanity_check.ValidateTypes(((dimensions[key], (str, unicode)),))
new_dimensions.__dict__.__setitem__('_%s' % key, dimensions[key])
return new_dimensions
def ValidateMapEntry(entry, type, web_services):
"""Validate MapEntry object.
MapEntry object is one of Media_MediaExtendedCapabilityType_Media_MediaExtendedCapabilityStateMapEntry,
Media_Size, Media_Size_StringMapEntry, Type_AttributeMapEntry.
Args:
entry: dict MapEntry object.
web_services: module for web services.
Returns:
XxxMapEntry instance.
"""
if IsPyClass(entry):
return entry
glob_sanity_check.ValidateTypes(((entry, dict),))
new_entry = GetPyClass(type, web_services)
for key in entry:
if entry[key] == 'None': continue
if type in ('Media_Size_DimensionsMapEntry',) and key in ('value',):
data = ValidateDimensions(entry[key], web_services)
else:
glob_sanity_check.ValidateTypes(((entry[key], (str, unicode)),))
data = entry[key]
new_entry.__dict__.__setitem__('_%s' % key, data)
return new_entry
def ValidateVideo(video, web_services):
"""Validate Video object.
Args:
image: dict Video object.
web_services: module for web services.
Returns:
Video instance.
"""
if IsPyClass(video):
return video
glob_sanity_check.ValidateTypes(((video, dict),))
new_video = GetPyClass('Video', web_services)
for key in video:
if video[key] == 'None': continue
if key in ('dimensions',):
glob_sanity_check.ValidateTypes(((video[key], list),))
dimensions = []
for item in video[key]:
dimensions.append(ValidateMapEntry(item,
'Media_Size_DimensionsMapEntry',
web_services))
data = dimensions
elif key in ('urls',):
glob_sanity_check.ValidateTypes(((video[key], list),))
urls = []
for item in video[key]:
urls.append(ValidateMapEntry(item,
'Media_Size_StringMapEntry',
web_services))
data = urls
elif key in ('extendedCapabilities',):
glob_sanity_check.ValidateTypes(((video[key], list),))
capabilities = []
for item in video[key]:
capabilities.append(ValidateMapEntry(item,
'Media_MediaExtendedCapabilityType_Media_MediaExtendedCapabilityStateMapEntry',
web_services))
data = capabilities
else:
glob_sanity_check.ValidateTypes(((video[key], (str, unicode)),))
data = video[key]
new_video.__dict__.__setitem__('_%s' % key, data)
return new_video
def ValidateAudio(audio, web_services):
"""Validate Audio object.
Args:
audio: dict Audio object.
web_services: module for web services.
Returns:
Audio instance.
"""
if IsPyClass(audio):
return audio
glob_sanity_check.ValidateTypes(((audio, dict),))
new_audio = GetPyClass('Audio', web_services)
for key in audio:
if audio[key] == 'None': continue
if key in ('dimensions',):
glob_sanity_check.ValidateTypes(((audio[key], list),))
dimensions = []
for item in audio[key]:
dimensions.append(ValidateMapEntry(item,
'Media_Size_DimensionsMapEntry',
web_services))
data = dimensions
elif key in ('urls',):
glob_sanity_check.ValidateTypes(((audio[key], list),))
urls = []
for item in audio[key]:
urls.append(ValidateMapEntry(item,
'Media_Size_StringMapEntry',
web_services))
data = urls
elif key in ('extendedCapabilities',):
glob_sanity_check.ValidateTypes(((audio[key], list),))
capabilities = []
for item in audio[key]:
capabilities.append(ValidateMapEntry(item,
'Media_MediaExtendedCapabilityType_Media_MediaExtendedCapabilityStateMapEntry',
web_services))
data = capabilities
else:
glob_sanity_check.ValidateTypes(((audio[key], (str, unicode)),))
data = audio[key]
new_audio.__dict__.__setitem__('_%s' % key, data)
return new_audio
def ValidateTemplateElement(element, web_services):
"""Validate TemplateElement object.
Args:
element: dict TemplateElement object.
web_services: module for web services.
Returns:
TemplateElement instance.
"""
if IsPyClass(element):
return element
glob_sanity_check.ValidateTypes(((element, dict),))
new_element = GetPyClass('TemplateElement', web_services)
for key in element:
if element[key] == 'None': continue
if key in ('fields',):
glob_sanity_check.ValidateTypes(((element[key], list),))
fields = []
for item in element[key]:
fields.append(ValidateTemplateElementField(item, web_services))
data = fields
else:
glob_sanity_check.ValidateTypes(((element[key], (str, unicode)),))
data = element[key]
new_element.__dict__.__setitem__('_%s' % key, data)
return new_element
def ValidateMedia(media, web_services):
"""Validate Media object.
Media object is on of Image, Video.
Args:
field: dict media object.
web_services: module for web services.
Returns:
Media updated media object.
"""
if 'data' in media:
new_media = ValidateImage(media, web_services)
else:
new_media = ValidateVideo(media, web_services)
return new_media
def ValidateTemplateElementField(field, web_services):
"""Validate TemplateElementField object.
Args:
field: dict TemplateElementField object.
web_services: module for web services.
Returns:
TemplateElementField instance.
"""
if IsPyClass(field):
return field
glob_sanity_check.ValidateTypes(((field, dict),))
new_field = GetPyClass('TemplateElementField', web_services)
for key in field:
if field[key] == 'None': continue
if key in ('fieldMedia',):
data = ValidateMedia(field[key], web_services)
else:
glob_sanity_check.ValidateTypes(((field[key], (str, unicode)),))
data = field[key]
new_field.__dict__.__setitem__('_%s' % key, data)
return new_field
def ValidateAd(operator, ad, web_services):
"""Validate Ad object.
An Ad object is one of DeprecatedAd, MobileAd, MobileImageAd, ImageAd,
LocalBusinessAd, TemplateAd, TextAd,
Args:
operator: str operator to use.
ad: dict ad object.
web_services: module for web services.
Returns:
dict/Ad updated ad object or Ad instance.
"""
if IsPyClass(ad):
return ad
glob_sanity_check.ValidateTypes(((ad, dict),))
if operator in ('ADD', ''):
if 'adType' in ad:
new_ad = GetPyClass(ad['adType'], web_services)
elif 'type' in ad:
new_ad = GetPyClass(ad['type'], web_services)
elif 'Ad_Type' in ad:
new_ad = GetPyClass(ad['Ad_Type'], web_services)
else:
msg = 'The \'adType\' or \'type\' of the ad is missing.'
raise ValidationError(msg)
elif operator in ('SET', 'REMOVE'):
new_ad = GetPyClass('Ad', web_services)
for key in ad:
if ad[key] == 'None': continue
if key in ('productImage', 'image', 'businessImage', 'customIcon', 'icon'):
data = ValidateImage(ad[key], web_services)
elif key in ('video',):
data = ValidateVideo(ad[key], web_services)
elif key in ('markupLanguages', 'mobileCarriers'):
glob_sanity_check.ValidateTypes(((ad[key], list),))
for item in ad[key]:
glob_sanity_check.ValidateTypes(((item, (str, unicode)),))
data = ad[key]
elif key in ('target',):
data = ValidateProximityTarget(ad[key], web_services)
elif key in ('adUnionId',):
data = ValidateEntityId(ad[key], 'AdUnionId', web_services)
elif key in ('templateElements',):
glob_sanity_check.ValidateTypes(((ad[key], list),))
elements = []
for item in ad[key]:
elements.append(ValidateTemplateElement(item, web_services))
data = elements
elif key in ('dimensions',):
data = ValidateDimensions(ad[key], web_services)
else:
glob_sanity_check.ValidateTypes(((ad[key], (str, unicode)),))
data = ad[key]
new_ad.__dict__.__setitem__('_%s' % key, data)
return new_ad
def ValidateProximityTarget(target, web_services):
"""Validate ProximityTarget object.
Args:
target: dict ProximityTarget object.
web_services: module for web services.
Returns:
ProximityTarget instance.
"""
if IsPyClass(target):
return target
glob_sanity_check.ValidateTypes(((target, dict),))
new_target = GetPyClass('ProximityTarget', web_services)
for key in target:
if target[key] == 'None': continue
if key in ('geoPoint',):
data = ValidateGeoPoint(target[key], web_services)
elif key in ('address',):
data = ValidateAddress(target[key], web_services)
else:
glob_sanity_check.ValidateTypes(((target[key], (str, unicode)),))
data = target[key]
new_target.__dict__.__setitem__('_%s' % key, data)
return new_target
def ValidateCriterion(operator, criterion, web_services):
"""Validate Criterion object.
A Criterion object is one of Keyword, Website, Placement.
Args:
operator: str operator to use.
criterion: dict Criterion object.
web_services: module for web services.
Returns:
dict/Criterion updated criterion object or Criterion instance.
"""
if IsPyClass(criterion):
return criterion
glob_sanity_check.ValidateTypes(((criterion, dict),))
if operator in ('ADD', ''):
if 'criterionType' in criterion:
new_criterion = GetPyClass(criterion['criterionType'], web_services)
elif 'type' in criterion:
new_criterion = GetPyClass(criterion['type'], web_services)
elif 'Criterion_Type' in criterion:
new_criterion = GetPyClass(criterion['Criterion_Type'], web_services)
else:
msg = 'The \'criterionType\' or \'type\' of the criterion is missing.'
raise ValidationError(msg)
elif operator in ('SET', 'REMOVE'):
new_criterion = GetPyClass('Criterion', web_services)
for key in criterion:
if criterion[key] == 'None': continue
glob_sanity_check.ValidateTypes(((criterion[key], (str, unicode)),))
new_criterion.__dict__.__setitem__('_%s' % key, criterion[key])
return new_criterion
def ValidateDateRange(range, web_services):
"""Validate DateRange object.
Args:
range: dict DateRange object.
web_services: module for web services.
Returns:
DateRange instance.
"""
if IsPyClass(range):
return range
glob_sanity_check.ValidateTypes(((range, dict),))
new_range = GetPyClass('DateRange', web_services)
for key in range:
if range[key] == 'None': continue
glob_sanity_check.ValidateTypes(((range[key], (str, unicode)),))
new_range.__dict__.__setitem__('_%s' % key, range[key])
return new_range
def ValidateMoney(amount, web_services):
"""Validate Money object.
Args:
amount: dict Money object.
web_services: module for web services.
Returns:
Money instance.
"""
if IsPyClass(amount):
return amount
glob_sanity_check.ValidateTypes(((amount, dict),))
money = GetPyClass('Money', web_services)
for key in amount:
if amount[key] == 'None': continue
glob_sanity_check.ValidateTypes(((amount[key], (str, unicode)),))
money.__dict__.__setitem__('_%s' % key, amount[key])
return money
def ValidateBudget(budget, web_services):
"""Validate Budget object.
Args:
budget: dict Budget object.
Returns:
Budget instance.
"""
if IsPyClass(budget):
return budget
glob_sanity_check.ValidateTypes(((budget, dict),))
new_budget = GetPyClass('Budget', web_services)
for key in budget:
if budget[key] == 'None': continue
if key in ('amount',):
budget[key] = ValidateMoney(budget[key], web_services)
else:
glob_sanity_check.ValidateTypes(((budget[key], (str, unicode)),))
new_budget.__dict__.__setitem__('_%s' % key, budget[key])
return new_budget
def ValidateBid(bid, web_services):
"""Validate Bid object.
Args:
bid: dict Bid object.
web_services: module for web services.
Returns:
Bid instance.
"""
if IsPyClass(bid):
return bid
glob_sanity_check.ValidateTypes(((bid, dict),))
new_bid = GetPyClass('Bid', web_services)
for key in bid:
if bid[key] == 'None': continue
if key in ('amount',):
data = ValidateMoney(bid[key], web_services)
else:
glob_sanity_check.ValidateTypes(((bid[key], (str, unicode)),))
data = bid[key]
new_bid.__dict__.__setitem__('_%s' % key, data)
return new_bid
def ValidateBids(bids, web_services):
"""Validate Bids object.
A Bids object is on of AdGroupBids, AdGroupCriterionBids,
BudgetOptimizerAdGroupBids, BudgetOptimizerAdGroupCriterionBids,
ConversionOptimizerAdGroupBids, ConversionOptimizerAdGroupCriterionBids,
ManualCPCAdGroupBids, ManualCPCAdGroupCriterionBids, ManualCPMAdGroupBids,
ManualCPMAdGroupCriterionBids, PositionPreferenceAdGroupCriterionBids.
Args:
bids: dict Bids object.
web_services: module for web services.
Returns:
XxxBids instance.
"""
if IsPyClass(bids):
return bids
glob_sanity_check.ValidateTypes(((bids, dict),))
if 'type' in bids:
new_bids = GetPyClass(bids['type'], web_services)
elif 'AdGroupBids_Type' in bids:
new_bids = GetPyClass(bids['AdGroupBids_Type'], web_services)
elif 'AdGroupCriterionBids_Type' in bids:
new_bids = GetPyClass(bids['AdGroupCriterionBids_Type'], web_services)
else:
msg = 'The \'type\' of the bid is missing.'
raise ValidationError(msg)
for key in bids:
if bids[key] == 'None': continue
if key in ('proxyBid', 'maxCpc', 'maxCpm', 'proxyMaxCpc',
'proxyKeywordMaxCpc', 'proxySiteMaxCpc', 'targetCpa',
'keywordMaxCpc', 'keywordContentMaxCpc', 'siteMaxCpc',
'targetCpa'):
data = ValidateBid(bids[key], web_services)
elif key in ('positionPreferenceBids',):
glob_sanity_check.ValidateTypes(((bids[key], dict),))
new_bids = GetPyClass('PositionPreferenceAdGroupCriterionBids',
web_services)
for sub_key in bids[key]:
if sub_key in ('proxyMaxCpc',):
data = ValidateBid(bids[key][sub_key], web_services)
else:
glob_sanity_check.ValidateTypes(((bids[key][sub_key],
(str, unicode)),))
data = bids[key][sub_key]
new_bids.__dict__.__setitem__('_%s' % sub_key, data)
data = new_bids
else:
glob_sanity_check.ValidateTypes(((bids[key], (str, unicode)),))
data = bids[key]
new_bids.__dict__.__setitem__('_%s' % key, data)
return new_bids
def ValidateExemptionRequest(exemption):
"""Validate ExemptionRequest object.
Args:
exemption: dict ExemptionRequest object.
"""
glob_sanity_check.ValidateTypes(((exemption, dict),))
for key in exemption:
if key in ('key',):
glob_sanity_check.ValidateTypes(((exemption[key], dict),))
for sub_key in exemption[key]:
if (isinstance(exemption[key][sub_key], tuple) and
not exemption[key][sub_key]):
continue
glob_sanity_check.ValidateTypes(((exemption[key][sub_key],
(str, unicode)),))
def ValidateGeoPoint(geo_point, web_services):
"""Validate GeoPoint object.
Args:
geo_point: dict GeoPoint object.
web_services: module for web services.
Returns:
GeoPoint instance.
"""
if IsPyClass(geo_point):
return geo_point
glob_sanity_check.ValidateTypes(((geo_point, dict),))
new_geo_point = GetPyClass('GeoPoint', web_services)
for key in geo_point:
if geo_point[key] == 'None': continue
glob_sanity_check.ValidateTypes(((geo_point[key], (str, unicode)),))
new_geo_point.__dict__.__setitem__('_%s' % key, geo_point[key])
return new_geo_point
def ValidateAddress(address, web_services):
"""Validate Address object.
Args:
address: dict Address object.
web_services: module for web services.
Returns:
Address instance.
"""
if IsPyClass(address):
return address
glob_sanity_check.ValidateTypes(((address, dict),))
new_address = GetPyClass('Address', web_services)
for key in address:
if address[key] == 'None': continue
if address[key]:
glob_sanity_check.ValidateTypes(((address[key], (str, unicode)),))
new_address.__dict__.__setitem__('_%s' % key, address[key])
return new_address
def ValidateTarget(target, web_services):
"""Validate Target object.
A Target object is one of AdScheduleTarget, AgeTarget, CityTarget,
CountryTarget, DemographicTarget, GenderTarget, GeoTarget, LanguageTarget,
MetroTarget, NetworkTarget, PlatformTarget, PolygonTarget, ProvinceTarget,
ProximityTarget, Target.
Args:
target: list a target object.
web_services: module for web services.
Returns:
XxxTarget instance.
"""
if IsPyClass(target):
return target
glob_sanity_check.ValidateTypes(((target, dict),))
if 'type' in target:
new_target = GetPyClass(target['type'], web_services)
elif 'Target_Type' in target:
new_target = GetPyClass(target['Target_Type'], web_services)
else:
msg = 'The \'type\' of the target is missing.'
raise ValidationError(msg)
for key in target:
if target[key] == 'None': continue
if key in ('vertices',):
glob_sanity_check.ValidateTypes(((target[key], list),))
geo_points = []
for item in target[key]:
geo_points.append(ValidateGeoPoint(item, web_services))
data = geo_points
elif key in ('address',):
data = ValidateAddress(target[key], web_services)
elif key in ('geoPoint',):
data = ValidateGeoPoint(target[key], web_services)
else:
glob_sanity_check.ValidateTypes(((target[key], (str, unicode)),))
data = target[key]
new_target.__dict__.__setitem__('_%s' % key, data)
return new_target
def ValidateEntityId(id, type, web_services):
"""Validate XxxId object.
The XxxId object is one of AdUnionId, EntityId, TempAdUnionId.
Args:
id: dict EntityId object.
type: string desired type to set for this entity id.
web_services: module for web services.
Returns:
XxxId instance.
"""
if IsPyClass(id):
return id
glob_sanity_check.ValidateTypes(((id, dict),))
new_id = GetPyClass(type, web_services)
for key in id:
if id[key] == 'None': continue
glob_sanity_check.ValidateTypes(((id[key], (str, unicode)),))
new_id.__dict__.__setitem__('_%s' % key, id[key])
return new_id
def ValidateJobOperation(operation, web_services):
"""Validate JobOperation object.
Args:
operation: dict JobOperation object.
web_services: module for web services.
Returns:
JobOperation instance.
"""
if IsPyClass(operation):
return operation
glob_sanity_check.ValidateTypes(((operation, dict),))
if 'type' not in operation:
msg = 'A job operation type is missing.'
raise ValidationError(msg)
operation_type = '%sOperation' % operation['type']
new_operation = GetPyClass(operation_type, web_services)
operation = ValidateOperation(operation, web_services)
for key in operation:
new_operation.__dict__.__setitem__('_%s' % key, operation[key])
return new_operation
def ValidateOperationStream(stream, web_services):
"""Validate OperationStream object.
Args:
stream: dict OperationStream object.
web_services: module for web services.
Returns:
OperationStream instance.
"""
if IsPyClass(stream):
return stream
glob_sanity_check.ValidateTypes(((stream, dict),))
new_stream = GetPyClass('OperationStream', web_services)
for key in stream:
if stream[key] == 'None': continue
if key in ('operations',):
glob_sanity_check.ValidateTypes(((stream[key], list),))
ops = []
for item in stream[key]:
ops.append(ValidateJobOperation(item, web_services))
data = ops
elif key in ('scopingEntityId',):
data = ValidateEntityId(stream[key], 'EntityId', web_services)
else:
glob_sanity_check.ValidateTypes(((stream[key], (str, unicode)),))
data = stream[key]
new_stream.__dict__.__setitem__('_%s' % key, data)
return new_stream
def ValidateBulkMutateRequest(bmr, web_services):
"""Validate BulkMutateRequest object.
Args:
bmr: dict BulkMutateRequest object.
web_services: module for web services.
Returns:
BulkMutateRequest instance.
"""
if IsPyClass(bmr):
return bmr
glob_sanity_check.ValidateTypes(((bmr, dict),))
new_bmr = GetPyClass('BulkMutateRequest', web_services)
for key in bmr:
if bmr[key] == 'None': continue
if key in ('operationStreams',):
glob_sanity_check.ValidateTypes(((bmr[key], list),))
streams = []
for item in bmr[key]:
stream = ValidateOperationStream(item, web_services)
streams.append(stream)
data = streams
else:
glob_sanity_check.ValidateTypes(((bmr[key], (str, unicode)),))
data = bmr[key]
new_bmr.__dict__.__setitem__('_%s' % key, data)
return new_bmr
def ValidateAdExtension(extension, web_services):
"""Validate AdExtension object.
Args:
extension: dict AdExtension object.
web_services: module for web services.
Returns:
AdExtension instance.
"""
if IsPyClass(extension):
return extension
glob_sanity_check.ValidateTypes(((extension, dict),))
if 'type' in extension:
new_extension = GetPyClass(extension['type'], web_services)
else:
new_extension = GetPyClass('AdExtension', web_services)
for key in extension:
if extension[key] == 'None': continue
if key in ('address',):
data = ValidateAddress(extension[key], web_services)
elif key in ('geoPoint',):
data = ValidateGeoPoint(extension[key], web_services)
else:
glob_sanity_check.ValidateTypes(((extension[key], (str, unicode)),))
data = extension[key]
new_extension.__dict__.__setitem__('_%s' % key, data)
return new_extension
def ValidateOverrideInfo(info, web_services):
"""Validate OverrideInfo object.
Args:
info: dict OverrideInfo object.
web_services: module for web services.
Returns:
OverrideInfo instance.
"""
if IsPyClass(info):
return info
glob_sanity_check.ValidateTypes(((info, dict),))
new_info = GetPyClass('OverrideInfo', web_services)
for key in info:
if info[key] == 'None': continue
glob_sanity_check.ValidateTypes(((info[key], (str, unicode)),))
info.__dict__.__setitem__('_%s' % key, info[key])
return new_info
def ValidateLongComparisonOperation(operation, web_services):
"""Validate LongComparisonOperation object.
Args:
operation: dict LongComparisonOperation object.
web_services: module for web services.
Returns:
LongComparisonOperation instance.
"""
if IsPyClass(operation):
return operation
glob_sanity_check.ValidateTypes(((operation, dict),))
new_operation = GetPyClass('LongComparisonOperation', web_services)
for key in operation:
if operation[key] == 'None': continue
glob_sanity_check.ValidateTypes(((operation[key], (str, unicode)),))
new_operation.__dict__.__setitem__('_%s' % key, operation[key])
return new_operation
def ValidateKeyword(keyword, web_services):
"""Validate Keyword object.
Args:
keyword: dict Keyword object.
web_services: module for web services.
Returns:
Keyword instance.
"""
if IsPyClass(keyword):
return keyword
glob_sanity_check.ValidateTypes(((keyword, dict),))
new_keyword = GetPyClass('Keyword', web_services)
for key in keyword:
if keyword[key] == 'None': continue
glob_sanity_check.ValidateTypes(((keyword[key], (str, unicode)),))
new_keyword.__dict__.__setitem__('_%s' % key, keyword[key])
return new_keyword
def ValidateCountryTarget(target, web_services):
"""Validate CountryTarget object.
Args:
target: dict CountryTarget object.
web_services: module for web services.
Returns:
CountryTarget instance.
"""
if IsPyClass(target):
return target
glob_sanity_check.ValidateTypes(((target, dict),))
new_target = GetPyClass('CountryTarget', web_services)
for key in target:
if target[key] == 'None': continue
glob_sanity_check.ValidateTypes(((target[key], (str, unicode)),))
new_target.__dict__.__setitem__('_%s' % key, target[key])
return new_target
def ValidateLanguageTarget(target, web_services):
"""Validate LanguageTarget object.
Args:
target: dict LanguageTarget object.
web_services: module for web services.
Returns:
LanguageTarget instance.
"""
if IsPyClass(target):
return target
glob_sanity_check.ValidateTypes(((target, dict),))
new_target = GetPyClass('LanguageTarget', web_services)
for key in target:
if target[key] == 'None': continue
glob_sanity_check.ValidateTypes(((target[key], (str, unicode)),))
new_target.__dict__.__setitem__('_%s' % key, target[key])
return new_target
def ValidatePaging(paging, web_services):
"""Validate Paging object.
Args:
paging: dict Paging object.
web_services: module for web services.
Returns:
Paging instance.
"""
if IsPyClass(paging):
return paging
glob_sanity_check.ValidateTypes(((paging, dict),))
new_paging = GetPyClass('Paging', web_services)
for key in paging:
if paging[key] == 'None': continue
glob_sanity_check.ValidateTypes(((paging[key], (str, unicode)),))
new_paging.__dict__.__setitem__('_%s' % key, paging[key])
return new_paging
def ValidateSearchParameter(param, web_services):
"""Validate SearchParameter object.
A SearchParameter is one of AdTypeSearchParameter,
AverageTargetedMonthlySearchesSearchParameter, CompetitionSearchParameter,
CountryTargetSearchParameter, ExcludedKeywordSearchParameter,
GlobalMonthlySearchesSearchParameter, IncludeAdultContentSearchParameter,
KeywordCategoryIdSearchParameter, KeywordMatchTypeSearchParameter,
LanguageTargetSearchParameter, MobileSearchParameter,
NgramGroupsSearchParameter, PlacementTypeSearchParameter,
RelatedToKeywordSearchParameter, RelatedToUrlSearchParameter,
SeedAdGroupIdSearchParameter
Args:
search_parameter: dict SearchParameter object.
web_services: module for web services.
Returns:
XxxSearchParameter instance.
"""
if IsPyClass(param):
return param
glob_sanity_check.ValidateTypes(((param, dict),))
if 'type' in param:
new_param = GetPyClass(param['type'], web_services)
elif 'SearchParameter_Type' in param:
new_param = GetPyClass('SearchParameter_Type', web_services)
else:
msg = 'The \'type\' of the search parameter is missing.'
raise ValidationError(msg)
for key in param:
if param[key] == 'None': continue
if key in ('adTypes', 'levels', 'keywordMatchTypes', 'ngramGroups',
'categoryIds', 'placementTypes', 'urls'):
glob_sanity_check.ValidateTypes(((param[key], list),))
items = []
for item in param[key]:
glob_sanity_check.ValidateTypes(((item, (str, unicode)),))
items.append(item)
data = items
elif key in ('operation',):
data = ValidateLongComparisonOperation(param[key], web_services)
elif key in ('keywords',):
glob_sanity_check.ValidateTypes(((param[key], list),))
kws = []
for item in param[key]:
kws.append(ValidateKeyword(item, web_services))
data = kws
elif key in ('countryTargets',):
glob_sanity_check.ValidateTypes(((param[key], list),))
targets = []
for item in param[key]:
targets.append(ValidateCountryTarget(item, web_services))
data = targets
elif key in ('languageTargets',):
glob_sanity_check.ValidateTypes(((param[key], list),))
targets = []
for item in param[key]:
targets.append(ValidateLanguageTarget(item, web_services))
data = targets
else:
glob_sanity_check.ValidateTypes(((param[key], (str, unicode)),))
data = param[key]
new_param.__dict__.__setitem__('_%s' % key, data)
return new_param
def ValidateBiddingStrategy(strategy, web_services):
"""Validate BiddingStrategy object.
A BiddingStrategy is one of BudgetOptimizer, ConversionOptimizer, ManualCPC,
ManualCPM.
Args:
strategy: dict BiddingStrategy object.
web_services: module for web services.
Returns:
BiddingStrategy instance.
"""
if IsPyClass(strategy):
return strategy
glob_sanity_check.ValidateTypes(((strategy, dict),))
if 'type' in strategy:
new_strategy = GetPyClass(strategy['type'], web_services)
elif 'BiddingStrategy_Type' in strategy:
new_strategy = GetPyClass(strategy['BiddingStrategy_Type'], web_services)
else:
msg = 'The \'type\' of the bidding transition is missing.'
raise ValidationError(msg)
for key in strategy:
if strategy[key] == 'None': continue
if key in ('bidCeiling',):
data = ValidateMoney(strategy[key], web_services)
else:
glob_sanity_check.ValidateTypes(((strategy[key], (str, unicode)),))
data = strategy[key]
new_strategy.__dict__.__setitem__('_%s' % key, data)
return new_strategy
def ValidateFrequencyCap(cap, web_services):
"""Validate FrequencyCap object.
Args:
paging: dict frequency cap object.
web_services: module for web services.
Returns:
FrequencyCap instance.
"""
if IsPyClass(cap):
return cap
glob_sanity_check.ValidateTypes(((cap, dict),))
new_cap = GetPyClass('FrequencyCap', web_services)
for key in cap:
if cap[key] == 'None': continue
glob_sanity_check.ValidateTypes(((cap[key], (str, unicode)),))
new_cap.__dict__.__setitem__('_%s' % key, cap[key])
return new_cap
def ValidateOperation(operation, web_services):
"""Validate Operation object.
An Operation object is one of AdExtensionOverrideOperation,
AdGroupAdOperation, AdGroupCriterionOperation, AdGroupOperation,
AdParamOperation, CampaignAdExtensionOperation, CampaignCriterionOperation,
CampaignOperation, CampaignTargetOperation, JobOperation,
LbcListingDataOperation, LongComparisonOperation.
Args:
operation: dict operation object.
web_services: module for web services.
Returns:
dict updated Operation object.
"""
if IsPyClass(operation):
return operation
glob_sanity_check.ValidateTypes(((operation, dict),))
caller = {
'name': web_services.__name__.split('.')[-1].split('Service')[0],
'typed': False
}
if 'type' in operation: caller['name'] = operation['type']
# Custom handler for services that require concrete types for ADD and SET
# operators.
if caller['name'] in ('AdGroupCriterion', 'BulkMutateJob',
'CampaignAdExtension', 'CampaignCriterion',
'CampaignTarget'):
caller['typed'] = True
operator = ''
for key in operation:
if key in ('operator',):
glob_sanity_check.ValidateTypes(((operation[key], (str, unicode)),))
operator = operation[key]
elif key in ('operand',):
glob_sanity_check.ValidateTypes(((operation[key], dict),))
operand = operation[key]
if (caller['typed'] and 'type' in operand) or 'type' in operation:
caller['typed'] = True
if 'type' in operation and 'type' in operand:
type = operand['type']
elif 'type' in operation:
type = operation['type']
else:
type = operand['type']
new_operand = GetPyClass(type, web_services)
elif not caller['typed']:
pass
else:
msg = 'The \'type\' of the operand is missing.'
raise ValidationError(msg)
for sub_key in operand:
if sub_key in ('ad',):
data = ValidateAd(operator, operand[sub_key], web_services)
elif sub_key in ('bids',):
data = ValidateBids(operand[sub_key], web_services)
elif sub_key in ('criterion',):
data = ValidateCriterion(operator, operand[sub_key], web_services)
elif sub_key in ('minBids',):
glob_sanity_check.ValidateTypes(((operand[sub_key], list),))
bids = []
for item in operand[sub_key]:
bids.append(ValidateBid(item, web_services))
data = bids
elif sub_key in ('budget',):
data = ValidateBudget(operand[sub_key], web_services)
elif sub_key in ('biddingStrategy',):
data = ValidateBiddingStrategy(operand[sub_key], web_services)
elif sub_key in ('frequencyCap',):
data = ValidateFrequencyCap(operand[sub_key], web_services)
elif sub_key in ('targets',):
glob_sanity_check.ValidateTypes(((operand[sub_key], list),))
targets = []
for item in operand[sub_key]:
targets.append(ValidateTarget(item, web_services))
data = targets
elif sub_key in ('request',):
data = ValidateBulkMutateRequest(operand[sub_key], web_services)
elif sub_key in ('adExtension',):
data = ValidateAdExtension(operand[sub_key], web_services)
elif sub_key in ('overrideInfo',):
data = ValidateOverrideInfo(operand[sub_key], web_services)
elif sub_key in ('customerIds',):
glob_sanity_check.ValidateTypes(((operand[sub_key], list),))
for item in operand[sub_key]:
glob_sanity_check.ValidateTypes(((item, (str, unicode)),))
else:
data = operand[sub_key]
if caller['typed']:
new_operand.__dict__.__setitem__('_%s' % sub_key, data)
else:
operand[sub_key] = data
if caller['typed']:
new_operand.__dict__.__setitem__('_%s' % key, operand)
operation[key] = new_operand
elif key in ('exemptionRequests',):
glob_sanity_check.ValidateTypes(((operation[key], list),))
for item in operation[key]:
ValidateExemptionRequest(item)
data = operation[key]
elif key in ('biddingTransition',):
glob_sanity_check.ValidateTypes(((operation[key], dict),))
for sub_key in operation[key]:
if sub_key in ('targetBiddingStrategy',):
operation[key][sub_key] = \
ValidateBiddingStrategy(operation[key][sub_key], web_services)
elif sub_key in ('explicitAdGroupBids',):
operation[key][sub_key] = \
ValidateBids(operation[key][sub_key], web_services)
else:
glob_sanity_check.ValidateTypes(((operation[key][sub_key],
(str, unicode)),))
return operation
def ValidateSelector(selector, web_services):
"""Validate Selector object.
A Selector object is one of AccountSelector, AdExtensionOverrideSelector,
AdGroupAdSelector, AdGroupCriterionSelector, AdGroupSelector, AdParamSelector,
AdStatsSelector, BulkMutateJobSelector, CampaignAdExtensionSelector,
CampaignCriterionSelector, CampaignSelector, CampaignTargetSelector,
GeoLocationSelector, InfoSelector, JobSelector, StatsSelector,
TargetingIdeaSelector.
Args:
selector: dict selector object.
web_services: module for web services.
"""
glob_sanity_check.ValidateTypes(((selector, dict),))
for key in selector:
if key in ('idFilters',):
glob_sanity_check.ValidateTypes(((selector[key], list),))
for item in selector[key]:
glob_sanity_check.ValidateTypes(((item, dict),))
for sub_key in item:
glob_sanity_check.ValidateTypes(((item[sub_key], (str, unicode)),))
elif key in ('statsSelector',):
glob_sanity_check.ValidateTypes(((selector[key], dict),))
for sub_key in selector[key]:
ValidateDateRange(selector[key][sub_key], web_services)
elif key in ('dateRange',):
ValidateDateRange(selector[key], web_services)
elif key in ('adIds', 'adExtensionIds', 'adGroupIds', 'campaignIds',
'criteriaId', 'jobIds', 'ids', 'clientEmails',
'customerJobKeys', 'requestedAttributeTypes', 'jobStatuses',
'userStatuses', 'statuses', 'campaignStatuses'):
glob_sanity_check.ValidateTypes(((selector[key], list),))
for item in selector[key]:
glob_sanity_check.ValidateTypes(((item, (str, unicode)),))
elif key in ('searchParameters'):
glob_sanity_check.ValidateTypes(((selector[key], list),))
params = []
for item in selector[key]:
params.append(ValidateSearchParameter(item, web_services))
selector[key] = params
elif key in ('paging',):
ValidatePaging(selector[key], web_services)
elif key in ('addresses',):
glob_sanity_check.ValidateTypes(((selector[key], list),))
addresses = []
for item in selector[key]:
addresses.append(ValidateAddress(item, web_services))
selector[key] = addresses
else:
glob_sanity_check.ValidateTypes(((selector[key], (str, unicode)),))
|