pep8 fixes on core library

This commit is contained in:
Nathan Fritz 2010-10-20 19:43:53 -07:00
parent ce69213a1e
commit c4699b92e6
4 changed files with 46 additions and 44 deletions

View file

@ -128,7 +128,8 @@ class ClientXMPP(BaseXMPP):
self.sessionstarted = False self.sessionstarted = False
self.bound = False self.bound = False
self.bindfail = False self.bindfail = False
self.schedule("session timeout checker", 15, self._session_timeout_check) self.schedule("session timeout checker", 15,
self._session_timeout_check)
def _session_timeout_check(self): def _session_timeout_check(self):
if not self.session_started_event.isSet(): if not self.session_started_event.isSet():

View file

@ -2,7 +2,8 @@
SleekXMPP: The Sleek XMPP Library SleekXMPP: The Sleek XMPP Library
Copyright (C) 2010 Nathanael C. Fritz Copyright (C) 2010 Nathanael C. Fritz
This file is part of SleekXMPP. This file is part of SleekXMPP.
See the file LICENSE for copying permission. See the file LICENSE for copying permission.
""" """
__all__ = ['xep_0004', 'xep_0030', 'xep_0033', 'xep_0045', 'xep_0050', 'xep_0078', 'xep_0085', 'xep_0092', 'xep_0199', 'gmail_notify', 'xep_0060'] __all__ = ['xep_0004', 'xep_0030', 'xep_0033', 'xep_0045', 'xep_0050',
'xep_0078', 'xep_0085', 'xep_0092', 'xep_0199', 'gmail_notify', 'xep_0060']

View file

@ -2,25 +2,25 @@
SleekXMPP: The Sleek XMPP Library SleekXMPP: The Sleek XMPP Library
Copyright (C) 2010 Nathanael C. Fritz Copyright (C) 2010 Nathanael C. Fritz
This file is part of SleekXMPP. This file is part of SleekXMPP.
See the file LICENSE for copying permission. See the file LICENSE for copying permission.
""" """
class base_plugin(object): class base_plugin(object):
def __init__(self, xmpp, config): def __init__(self, xmpp, config):
self.xep = 'base' self.xep = 'base'
self.description = 'Base Plugin' self.description = 'Base Plugin'
self.xmpp = xmpp self.xmpp = xmpp
self.config = config self.config = config
self.post_inited = False self.post_inited = False
self.enable = config.get('enable', True) self.enable = config.get('enable', True)
if self.enable: if self.enable:
self.plugin_init() self.plugin_init()
def plugin_init(self): def plugin_init(self):
pass pass
def post_init(self): def post_init(self):
self.post_inited = True self.post_inited = True

View file

@ -17,7 +17,7 @@ class StateMachine(object):
def __init__(self, states=[]): def __init__(self, states=[]):
self.lock = threading.Lock() self.lock = threading.Lock()
self.notifier = threading.Event() self.notifier = threading.Event()
self.__states= [] self.__states = []
self.addStates(states) self.addStates(states)
self.__default_state = self.__states[0] self.__default_state = self.__states[0]
self.__current_state = self.__default_state self.__current_state = self.__default_state
@ -28,11 +28,11 @@ class StateMachine(object):
for state in states: for state in states:
if state in self.__states: if state in self.__states:
raise IndexError("The state '%s' is already in the StateMachine." % state) raise IndexError("The state '%s' is already in the StateMachine." % state)
self.__states.append( state ) self.__states.append(state)
finally: self.lock.release() finally: self.lock.release()
def transition(self, from_state, to_state, wait=0.0, func=None, args=[], kwargs={} ): def transition(self, from_state, to_state, wait=0.0, func=None, args=[], kwargs={}):
''' '''
Transition from the given `from_state` to the given `to_state`. Transition from the given `from_state` to the given `to_state`.
This method will return `True` if the state machine is now in `to_state`. It This method will return `True` if the state machine is now in `to_state`. It
@ -64,23 +64,23 @@ class StateMachine(object):
`func( *args, **kwargs )`. `func( *args, **kwargs )`.
''' '''
return self.transition_any( (from_state,), to_state, wait=wait, return self.transition_any((from_state,), to_state, wait=wait,
func=func, args=args, kwargs=kwargs ) func=func, args=args, kwargs=kwargs)
def transition_any(self, from_states, to_state, wait=0.0, func=None, args=[], kwargs={} ): def transition_any(self, from_states, to_state, wait=0.0, func=None, args=[], kwargs={}):
''' '''
Transition from any of the given `from_states` to the given `to_state`. Transition from any of the given `from_states` to the given `to_state`.
''' '''
if not (isinstance(from_states,tuple) or isinstance(from_states,list)): if not (isinstance(from_states,tuple) or isinstance(from_states,list)):
raise ValueError( "from_states should be a list or tuple" ) raise ValueError("from_states should be a list or tuple")
for state in from_states: for state in from_states:
if not state in self.__states: if not state in self.__states:
raise ValueError( "StateMachine does not contain from_state %s." % state ) raise ValueError("StateMachine does not contain from_state %s." % state)
if not to_state in self.__states: if not to_state in self.__states:
raise ValueError( "StateMachine does not contain to_state %s." % to_state ) raise ValueError("StateMachine does not contain to_state %s." % to_state)
start = time.time() start = time.time()
while not self.lock.acquire(False): while not self.lock.acquire(False):
@ -110,10 +110,10 @@ class StateMachine(object):
if not return_val: return return_val if not return_val: return return_val
log.debug(' ==== TRANSITION %s -> %s', self.__current_state, to_state) log.debug(' ==== TRANSITION %s -> %s', self.__current_state, to_state)
self._set_state( to_state ) self._set_state(to_state)
return return_val # some 'true' value returned by func or True if func was None return return_val # some 'true' value returned by func or True if func was None
else: else:
log.error( "StateMachine bug!! The lock should ensure this doesn't happen!" ) log.error("StateMachine bug!! The lock should ensure this doesn't happen!")
return False return False
finally: finally:
self.notifier.set() # notify any waiting threads that the state has changed. self.notifier.set() # notify any waiting threads that the state has changed.
@ -149,18 +149,18 @@ class StateMachine(object):
''' '''
if not from_state in self.__states: if not from_state in self.__states:
raise ValueError( "StateMachine does not contain from_state %s." % from_state ) raise ValueError("StateMachine does not contain from_state %s." % from_state)
if not to_state in self.__states: if not to_state in self.__states:
raise ValueError( "StateMachine does not contain to_state %s." % to_state ) raise ValueError("StateMachine does not contain to_state %s." % to_state)
return _StateCtx(self, from_state, to_state, wait) return _StateCtx(self, from_state, to_state, wait)
def ensure(self, state, wait=0.0, block_on_transition=False ): def ensure(self, state, wait=0.0, block_on_transition=False):
''' '''
Ensure the state machine is currently in `state`, or wait until it enters `state`. Ensure the state machine is currently in `state`, or wait until it enters `state`.
''' '''
return self.ensure_any( (state,), wait=wait, block_on_transition=block_on_transition ) return self.ensure_any((state,), wait=wait, block_on_transition=block_on_transition)
def ensure_any(self, states, wait=0.0, block_on_transition=False): def ensure_any(self, states, wait=0.0, block_on_transition=False):
@ -180,7 +180,7 @@ class StateMachine(object):
for state in states: for state in states:
if not state in self.__states: if not state in self.__states:
raise ValueError( "StateMachine does not contain state '%s'" % state ) raise ValueError("StateMachine does not contain state '%s'" % state)
# if we're in the middle of a transition, determine whether we should # if we're in the middle of a transition, determine whether we should
# 'fall back' to the 'current' state, or wait for the new state, in order to # 'fall back' to the 'current' state, or wait for the new state, in order to
@ -229,13 +229,13 @@ class StateMachine(object):
return self.__current_state == state return self.__current_state == state
def __str__(self): def __str__(self):
return "".join(( "StateMachine(", ','.join(self.__states), "): ", self.__current_state )) return "".join(("StateMachine(", ','.join(self.__states), "): ", self.__current_state))
class _StateCtx: class _StateCtx:
def __init__( self, state_machine, from_state, to_state, wait ): def __init__(self, state_machine, from_state, to_state, wait):
self.state_machine = state_machine self.state_machine = state_machine
self.from_state = from_state self.from_state = from_state
self.to_state = to_state self.to_state = to_state
@ -244,30 +244,30 @@ class _StateCtx:
def __enter__(self): def __enter__(self):
start = time.time() start = time.time()
while not self.state_machine[ self.from_state ] or not self.state_machine.lock.acquire(False): while not self.state_machine[self.from_state] or not self.state_machine.lock.acquire(False):
# detect timeout: # detect timeout:
remainder = start + self.wait - time.time() remainder = start + self.wait - time.time()
if remainder > 0: self.state_machine.notifier.wait(remainder) if remainder > 0: self.state_machine.notifier.wait(remainder)
else: else:
log.debug('StateMachine timeout while waiting for state: %s', self.from_state ) log.debug('StateMachine timeout while waiting for state: %s', self.from_state)
return False return False
self._locked = True # lock has been acquired at this point self._locked = True # lock has been acquired at this point
self.state_machine.notifier.clear() self.state_machine.notifier.clear()
log.debug('StateMachine entered context in state: %s', log.debug('StateMachine entered context in state: %s',
self.state_machine.current_state() ) self.state_machine.current_state())
return True return True
def __exit__(self, exc_type, exc_val, exc_tb): def __exit__(self, exc_type, exc_val, exc_tb):
if exc_val is not None: if exc_val is not None:
log.exception( "StateMachine exception in context, remaining in state: %s\n%s:%s", log.exception("StateMachine exception in context, remaining in state: %s\n%s:%s",
self.state_machine.current_state(), exc_type.__name__, exc_val ) self.state_machine.current_state(), exc_type.__name__, exc_val)
if self._locked: if self._locked:
if exc_val is None: if exc_val is None:
log.debug(' ==== TRANSITION %s -> %s', log.debug(' ==== TRANSITION %s -> %s',
self.state_machine.current_state(), self.to_state) self.state_machine.current_state(), self.to_state)
self.state_machine._set_state( self.to_state ) self.state_machine._set_state(self.to_state)
self.state_machine.notifier.set() self.state_machine.notifier.set()
self.state_machine.lock.release() self.state_machine.lock.release()