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