srikanth | 116e6e8 | 2014-08-19 07:22:37 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (c) 2013 Big Switch Networks, Inc. |
| 4 | # |
| 5 | # Licensed under the Eclipse Public License, Version 1.0 (the |
| 6 | # "License"); you may not use this file except in compliance with the |
| 7 | # License. You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.eclipse.org/legal/epl-v10.html |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 14 | # implied. See the License for the specific language governing |
| 15 | # permissions and limitations under the License. |
| 16 | # |
| 17 | |
| 18 | import os |
| 19 | import sdncon |
| 20 | |
| 21 | BSN_BASE = sdncon.SDN_ROOT |
| 22 | PERSONALITY = 'vm_personality' |
| 23 | |
| 24 | """ |
| 25 | This is a simple conditional decorator, suggested by Claudiu on Stackoverflow.com |
| 26 | """ |
| 27 | def conditionally(decorator, condition): |
| 28 | def moddec(f): |
| 29 | if not condition: |
| 30 | return f |
| 31 | return decorator(f) |
| 32 | return moddec |
| 33 | |
| 34 | def isCloudBuild(): |
| 35 | pf = os.path.join(BSN_BASE, PERSONALITY) |
| 36 | return os.path.exists(pf) and "cloud" in open(pf).read() |
| 37 | |
| 38 | def uicontext(request): |
| 39 | expiredSession = False |
| 40 | debug = False |
| 41 | try: |
| 42 | hostname = request.META['HTTP_HOST'] |
| 43 | referer = request.META['HTTP_REFERER'].split('/') |
| 44 | if referer[2] == hostname: |
| 45 | expiredSession = referer[3] not in ('account', 'logout') |
| 46 | except: |
| 47 | # Ignore any exceptions, like missing REFERER, HOST, etc. |
| 48 | pass |
| 49 | return { |
| 50 | 'isCloudBuild': isCloudBuild(), |
| 51 | 'expiredSession': expiredSession, |
| 52 | 'debug': debug, |
| 53 | } |
| 54 | |
| 55 | def abc(f): |
| 56 | def blah(*args, **kwargs): |
| 57 | print 'abc' |
| 58 | return f(*args, **kwargs) |
| 59 | return blah |
| 60 | |
| 61 | @conditionally(abc, isCloudBuild()) |
| 62 | def test(): pass |
| 63 | |
| 64 | if __name__ == '__main__': |
| 65 | test() |
| 66 | |
| 67 | |