blob: c807189ffe70d35fd5f7defe310fd458617794b3 [file] [log] [blame]
Suchitra.Vemurifdb220a2016-10-19 14:09:53 -07001import pexpect,os
2import time
3import json
4import collections
5import sys
6import robot
7import os.path
8from os.path import expanduser
9
10class utils(object):
11
12 @staticmethod
13 def listToDict(alist, intListIndex):
14 dictInfo = alist[int(intListIndex)]
15 return dictInfo
16
17 @staticmethod
18 def jsonToList(strFile, strListName):
19 data = json.loads(open(strFile).read())
20 #print "data...",data
21 dataList = data[strListName]
22 return dataList
23
Suchitra.Vemuri85220062016-10-25 10:44:11 -070024 '''
25 @method compare_dict
26 @Description: validates if contents of dict1 exists in dict2
27 @params: dict1 = input_data entered through api
28 dict2 = retrieved data from GET method
29 returns True if contents of dict1 exists in dict2
30 '''
31
32 @staticmethod
33 def compare_dict(dict1, dict2):
34 print "input_data", dict1
35 print "get data", dict2
36 if dict1 == None or dict2 == None:
37 return False
38
39 if type(dict1) is not dict or type(dict2) is not dict:
40 return False
41
42 for key1,value1 in dict1.items():
43 try:
44 if key1 in dict2:
45 for key2, value2 in value1.items():
46 if value2 != dict2[key1][key2]:
47 return False
48 except:
49 print "Additional items"
50 return True
51
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -070052 '''
53 @method search_dictionary
54 @Description: Searches for a key in the provided nested dictionary
55 @params: input_dict = dictionary to be searched
56 search_key = name of the key to be searched for
57 returns two values: search_key value and status of the search.
58 True if found (False when not found)
59
60 '''
61 def search_dictionary(self,input_dict, search_key):
62 input_keys = input_dict.keys()
63 key_value = ''
64 found = False
65 for key in input_keys:
66 if key == search_key:
67 key_value = input_dict[key]
68 found = True
69 break
70 elif type(input_dict[key]) == dict:
71 key_value, found = self.search_dictionary(input_dict[key],search_key)
72 if found == True:
73 break
Suchitra.Vemuri8be18802016-11-16 16:59:54 -080074 elif type(input_dict[key]) == list:
75 if not input_dict[key]:
76 found = False
77 break
78 for item in input_dict[key]:
79 if isinstance(item, dict):
80 key_value, found = self.search_dictionary(item, search_key)
81 if found == True:
82 break
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -070083 return key_value,found
84 '''
Suchitra.Vemuri8be18802016-11-16 16:59:54 -080085 @method getDictFromListOfDict
86 return key_value,found
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -070087 @Description: Searches for the dictionary in the provided list of dictionaries
88 that matches the value of the key provided
89 @params : List of dictionaries(getResponse Data from the URL),
90 SearchKey - Key that needs to be searched for (ex: account_num)
91 searchKeyValue - Value of the searchKey (ex: 21)
92 @Returns: Dictionary returned when match found for searchKey with the corresponding
93 searchKeyValue provided
94 '''
95
96 def getDictFromListOfDict(self, getJsonDataList, searchKey, searchKeyValue):
97 return_dict = {}
98 result = ''
99 for data in getJsonDataList:
100 return_dict = {}
101 found = False
102 input_keys = data.keys()
103 for key in input_keys:
104 if key == searchKey and str(data[key]) == str(searchKeyValue):
105 found = True
106 return_dict = data
107 break
108 elif type(data[key]) == dict:
109 result, found = self.search_dictionary(data[key],searchKey)
110 if found == True and str(result) == str(searchKeyValue):
111 return_dict = data
112 break
113 elif type(data[key]) == list:
114 for item in data[key]:
115 if isinstance(item, dict):
116 result, found = self.search_dictionary(data[key], searchKey)
117 if found == True and str(result) == str(searchKeyValue):
118 return_dict = data
119 break
120 if return_dict:
121 break
122 return return_dict
123
124
125 '''
126 @method getFieldValueFromDict
127 @params : search_dict - Dictionary to be searched
128 field - Key to be searched for (ex: account_num)
129 @Returns: Returns the value of the Key that was provided
130 '''
131 def getFieldValueFromDict(self,search_dict, field):
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800132 print "search_dict", search_dict, "field...", field
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700133 results = ''
134 found = False
135 input_keys = search_dict.keys()
136 for key in input_keys:
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800137 print "key...", key
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700138 if key == field:
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800139 print "entered if..."
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700140 results = search_dict[key]
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800141 print "results...", results
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800142 if not results:
143 found = True
144 break
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700145 elif type(search_dict[key]) == dict:
146 results, found = self.search_dictionary(search_dict[key],field)
147 if found == True:
148 break
149 elif type(search_dict[key]) == list:
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800150 if not search_dict[key]:
151 found = False
152 break
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700153 for item in search_dict[key]:
154 if isinstance(item, dict):
155 results, found = self.search_dictionary(item, field)
156 if found == True:
157 break
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800158 if results:
159 break
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700160
161 return results
162
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800163 def setFieldValueInDict(self,input_dict,field,field_value):
164 input_dict[field]=field_value
165 return input_dict
166
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700167'''
Suchitra.Vemurifdb220a2016-10-19 14:09:53 -0700168#Test
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800169dict_list = {
170 "humanReadableName": "cordSubscriber-17",
171 "id": 17,
172 "features": {
173 "uplink_speed": 1000000000,
174 "downlink_speed": 1000000000,
175 "status": "enabled"
176 },
177 "identity": {
178 "account_num": "20",
179 "name": "My House"
180 },
181 "related": {}
182 }
183input_dict = {
184 "s_tag" : "111",
185 "c_tag" : "222",
186 "subscriber" : ""
187 }
188new_value = 3
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700189test = utils()
Suchitra.Vemurifdb220a2016-10-19 14:09:53 -0700190#data=test.jsonToList("Subscribers.json","SubscriberInfo")
191#print test.jsonToList("Subscribers.json","SubscriberInfo")
192#print "index 1...",test.listToDict(data,1)
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800193#result = test.getDictFromListOfDict(dict_list,"email",21)
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800194#result = test.getFieldValueFromDict(dict_list,"id")
195#result = test.getDictFromListOfDict(dict_list,"account_num",21)
196result = test.setFieldValueInDict(input_dict,"subscriber",new_value)
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700197print "finalllllll result....", result
198'''