first step to merge senslab upstream:
[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
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         if rules.sorted_rule_list:
34             contexts = rules.contexts
35             request_context = context_callback(hrn, origin_hrn, contexts)
36             rules.set_context(request_context)
37             newrspec = rules.apply(rspec)
38         else:
39             newrspec = rspec
40         return newrspec
41
42 except:
43     
44     from sfa.util.sfalogging import logger
45     def run_sfatables (_,__,___, rspec, ____=None):
46         logger.warning("Cannot import sfatables.runtime, please install package sfa-sfatables")
47         return rspec