@@ -12,7 +12,31 @@ class KeyStore(object):
1212 # =================================================
1313 # "private" mathods
1414 # =================================================
15-
15+
16+ # get the list of values for a given key
17+ @staticmethod
18+ def _get (item ):
19+ item = item .rstrip ('/' )
20+ values = list ()
21+
22+ # does the request contain a wild card value?
23+ if "/*/" in item :
24+ parts = item .split ("*" )
25+ left = parts [0 ].split ()[- 1 ]
26+ right = parts [1 ].split ()[0 ] if parts [1 ].split () else ''
27+
28+ temp_vals = KeyStore .get (left )
29+ if (isinstance (temp_vals , basestring )):
30+ temp_vals = ast .literal_eval (temp_vals )
31+ for temp_val in temp_vals :
32+ if left + temp_val + right in KeyStore .db :
33+ values .append (temp_val )
34+ else :
35+ if item in KeyStore .db :
36+ values = KeyStore .db [item ]
37+
38+ return values
39+
1640 # =================================================
1741 # "public" methods
1842 # =================================================
@@ -27,8 +51,9 @@ def add(item):
2751 (key , value ) = item .rsplit ('/' , 1 )
2852 values = list ()
2953 if key in KeyStore .db :
30- values = KeyStore .get (key )
31- values = ast .literal_eval (values )
54+ values = KeyStore ._get (key )
55+ if (isinstance (values , basestring )):
56+ values = ast .literal_eval (values )
3257 if value not in values :
3358 values .append (value )
3459 KeyStore .db [key ] = values
@@ -37,26 +62,19 @@ def add(item):
3762 # return a list of values for a given key
3863 @staticmethod
3964 def get (item ):
40- item = item .rstrip ('/' )
41- values = list ()
42-
43- # does the request contain a wild card value?
44- if "/*/" in item :
45- parts = item .split ("*" )
46- left = parts [0 ].split ()[- 1 ]
47- right = parts [1 ].split ()[0 ] if parts [1 ].split () else ''
48-
49- #(left, right) = item.split("*", 1)
50- temp_vals = KeyStore .get (left )
51- temp_vals = ast .literal_eval (temp_vals )
52- for temp_val in temp_vals :
53- if left + temp_val + right in KeyStore .db :
54- values .append (temp_val )
55- else :
56- if item in KeyStore .db :
57- values = KeyStore .db [item ]
58-
59- return values
65+ result = list ()
66+
67+ # are we processin just one lookup?
68+ if (isinstance (item , basestring )):
69+ result = KeyStore ._get (item )
70+ if (isinstance (result , basestring )):
71+ result = ast .literal_eval (result )
72+ # or are we processing 2 lookups?
73+ elif (isinstance (item , list )):
74+ for k in item :
75+ r2 = KeyStore .get (k )
76+ result = result .extend (r2 )
77+ return sorted (set (result ))
6078
6179 # remove a given key or value
6280 @staticmethod
@@ -114,8 +132,8 @@ def load(filename):
114132 KeyStore .add ("host/3.3.3.3/port/22" )
115133 KeyStore .add ("host/4.4.4.4/port/25" )
116134 print "-------------------------------------------------------------------"
117- KeyStore .debug ()
118- print KeyStore .dump ()
135+ # KeyStore.debug()
136+ # print KeyStore.dump()
119137
120138 print KeyStore .get ("host/*/port/80" )
121139 print KeyStore .get ("host" )
0 commit comments