f18fdac41cb218c693786aa40ecb50fa9f49b738
[sfa.git] / archive / util / util.py
1 from excep import *
2 SR_SUFFIX = '_srr'
3 CR_SUFFIX = '_crr'
4
5 global_sr_tree = None
6 global_cr_tree = None
7
8 def set_tree_globals(tree1, tree2):
9     global global_sr_tree
10     global global_cr_tree
11     global_sr_tree = tree1
12     global_cr_tree = tree2
13
14 def get_tree_globals():
15     return (global_sr_tree, global_cr_tree)
16     
17 #function converts a hierarchical name from geni format to array of strings
18 def geni_to_arr(name):        
19     arrayName = []
20     try:
21         parts = name.split(".")
22         for i in range(len(parts)):
23             arrayName.append(parts[i])
24         return arrayName
25     except:
26         raise MalformedHrnException(name)
27
28 #used to parse the function name and the parameters specified in "operation_request"
29 def msg_to_params(str):        
30     try:
31         return eval(str)
32     except:
33         raise InvalidRPCParams(str)
34
35 #returns the authority hrn of a given 'hrn'
36 def obtain_authority(hrn):
37         parts = hrn.split(".")
38         auth_str = ''
39         if len(parts) > 1:
40                 auth_str = parts[0]+''
41                 for i in range(1, len(parts)-1):
42                         auth_str = auth_str + '.' + parts[i]
43         return auth_str
44
45 #returns the last element of an hrn
46 def get_leaf(hrn):
47     parts = hrn.split(".")
48     return parts[len(parts)-1]
49
50 #checks whether the 'hrn_auth' is an authority of 'hrn'
51 def check_authority(hrn, hrn_auth):
52     arr = geni_to_arr(hrn)
53     arr_auth = geni_to_arr(hrn_auth)
54     try:
55         for i in range(len(arr_auth)):
56             if arr[i] != arr_auth[i]:
57                 return False
58     except: 
59         return False
60     return True
61     
62 def hrn_to_tablename(hrn,type):
63     hrn = hrn.replace(".","$")
64     if type == 'slc':
65         hrn = hrn + SR_SUFFIX
66     else:
67         hrn = hrn + CR_SUFFIX
68     return  hrn
69