Merge branch 'master' into senslab2
[sfa.git] / sfa / util / sfatablesRuntime.py
1 # sfa should not depend on sfatables
2 # if the sfatables.runtime import fails, just define run_sfatables as identity
3 import sys
4 try:
5     from sfatables.runtime import SFATablesRules
6
7     def fetch_context(slice_hrn, user_hrn, contexts):
8         """
9         Returns the request context required by sfatables. At some point, this
10         mechanism should be changed to refer to "contexts", which is the
11         information that sfatables is requesting. But for now, we just return
12         the basic information needed in a dict.
13         """
14         base_context = {'sfa':{'user':{'hrn':user_hrn}, 'slice':{'hrn':slice_hrn}}}
15         return base_context
16
17     def run_sfatables(chain, hrn, origin_hrn, rspec, context_callback = None ):
18         """
19         Run the rspec through sfatables
20         @param chain Name of rule chain
21         @param hrn Object's hrn
22         @param origin_hrn Original caller's hrn
23         @param rspec Incoming rspec
24         @param context_callback Callback used to generate the request context  
25         
26         @return rspec
27         """
28         if not context_callback:
29             context_callback = fetch_context
30     
31         chain = chain.upper()
32         rules = SFATablesRules(chain)
33         print>>sys.stderr, " \r\n \r\n \t\t \t sfaTablesRuntime.py run_sfatables context_callback %s  chain %s rules %s " %(context_callback,chain, rules )
34         if rules.sorted_rule_list:
35             contexts = rules.contexts
36             request_context = context_callback(hrn, origin_hrn, contexts)
37             rules.set_context(request_context)
38             newrspec = rules.apply(rspec)
39         else:
40             newrspec = rspec
41         return newrspec
42
43 except:
44     
45     from sfa.util.logging import logger
46     def run_sfatables (_,__,___, rspec, ____=None):
47         logger.warning("Cannot import sfatables.runtime, please install package sfa-sfatables")
48         return rspec