Package flumotion :: Package component :: Package bouncers :: Module icalbouncer
[hide private]

Source Code for Module flumotion.component.bouncers.icalbouncer

  1  # -*- Mode: Python;  test-case-name: flumotion.test.test_icalbouncer -*- 
  2  # vi:si:et:sw=4:sts=4:ts=4 
  3  # 
  4  # Flumotion - a streaming media server 
  5  # Copyright (C) 2004,2005,2006,2007 Fluendo, S.L. (www.fluendo.com). 
  6  # All rights reserved. 
  7   
  8  # This file may be distributed and/or modified under the terms of 
  9  # the GNU General Public License version 2 as published by 
 10  # the Free Software Foundation. 
 11  # This file is distributed without any warranty; without even the implied 
 12  # warranty of merchantability or fitness for a particular purpose. 
 13  # See "LICENSE.GPL" in the source distribution for more information. 
 14   
 15  # Licensees having purchased or holding a valid Flumotion Advanced 
 16  # Streaming Server license may use this file in accordance with the 
 17  # Flumotion Advanced Streaming Server Commercial License Agreement. 
 18  # See "LICENSE.Flumotion" in the source distribution for more information. 
 19   
 20  # Headers in this file shall remain intact. 
 21   
 22  """ 
 23  A bouncer that only lets in during an event scheduled with an ical file. 
 24  """ 
 25   
 26  from datetime import datetime 
 27   
 28  from twisted.internet import defer 
 29   
 30  from flumotion.common import keycards, messages, errors 
 31  from flumotion.common import log, documentation 
 32  from flumotion.common import eventcalendar 
 33  from flumotion.common.i18n import N_, gettexter 
 34  from flumotion.component.base import scheduler 
 35  from flumotion.component.bouncers import bouncer 
 36  from flumotion.common.keycards import KeycardGeneric 
 37   
 38  __all__ = ['IcalBouncer'] 
 39  __version__ = "$Rev: 8156 $" 
 40  T_ = gettexter() 
 41   
 42   
43 -class IcalBouncer(bouncer.Bouncer):
44 45 logCategory = 'icalbouncer' 46 keycardClasses = (KeycardGeneric, ) 47 events = [] 48
49 - def init(self):
50 self.iCalScheduler = None
51
52 - def check_properties(self, properties, addMessage):
53 54 def missingModule(moduleName): 55 m = messages.Error(T_(N_( 56 "To use the iCalendar bouncer you need to have " 57 "the '%s' module installed.\n"), moduleName), 58 mid='error-python-%s' % moduleName) 59 documentation.messageAddPythonInstall(m, moduleName) 60 addMessage(m)
61 62 if not eventcalendar.HAS_ICALENDAR: 63 missingModule('icalendar') 64 if not eventcalendar.HAS_DATEUTIL: 65 missingModule('dateutil')
66
67 - def do_setup(self):
68 props = self.config['properties'] 69 self._icsfile = props['file'] 70 71 try: 72 handle = open(self._icsfile, 'r') 73 except IOError, e: 74 m = messages.Error(T_(N_( 75 "Failed to open iCalendar file '%s'. " 76 "Check permissions on that file."), self._icsfile), 77 mid='error-icalbouncer-file') 78 self.addMessage(m) 79 return defer.fail(errors.ComponentSetupHandledError()) 80 81 try: 82 self.iCalScheduler = scheduler.ICalScheduler(handle) 83 except (ValueError, IndexError, KeyError), e: 84 m = messages.Error(T_(N_( 85 "Error parsing ical file '%s'."), self._icsfile), 86 debug=log.getExceptionMessage(e), 87 mid="error-icalbouncer-file") 88 self.addMessage(m) 89 return defer.fail(errors.ComponentSetupHandledError()) 90 91 return True
92
93 - def do_authenticate(self, keycard):
94 self.debug('authenticating keycard') 95 96 # need to check if inside an event time 97 # FIXME: think of a strategy for handling overlapping events 98 cal = self.iCalScheduler.getCalendar() 99 eventInstances = cal.getActiveEventInstances() 100 if eventInstances: 101 instance = eventInstances[0] 102 now = datetime.now(eventcalendar.UTC) 103 end = instance.end 104 duration = end - now 105 durationSecs = duration.days * 86400 + duration.seconds 106 keycard.duration = durationSecs 107 if self.addKeycard(keycard): 108 keycard.state = keycards.AUTHENTICATED 109 self.info("authenticated login, duration %d seconds", 110 durationSecs) 111 return keycard 112 keycard.state = keycards.REFUSED 113 self.info("failed in authentication, outside hours") 114 return None
115
116 - def do_stop(self):
117 # we might not have an iCalScheduler, if something went wrong 118 # during do_setup or do_check 119 if self.iCalScheduler: 120 self.iCalScheduler.cleanup()
121