예제 #1
0
class eventTest(unittest.TestCase):
    CORRELATE = Event

    def setUp(self):
        self.event  = Event()
        self.args   = None
        self.kwargs = None

    def callback(self, *args, **kwargs):
        self.args   = args
        self.kwargs = kwargs

    def callback2(self, *args, **kwargs):
        self.callback(*args, **kwargs)

    def testConstructor(self):
        event = Event()

    def testConnect(self):
        self.event.connect(self.callback)
        self.assertEqual(self.event.n_subscribers(), 1)
        self.assertRaises(AttributeError, self.event.connect, self.callback)
        self.event.connect(self.callback2)
        self.assertEqual(self.event.n_subscribers(), 2)

    def testListen(self):
        import gc
        from Exscript.util.weakmethod import WeakMethod
        def thefunction():
            pass
        ref = self.event.listen(thefunction)
        self.assert_(isinstance(ref, WeakMethod))
        self.assertEqual(self.event.n_subscribers(), 1)
        self.assertRaises(AttributeError, self.event.listen, thefunction)
        del thefunction
        gc.collect()
        self.assertEqual(self.event.n_subscribers(), 0)

    def testNSubscribers(self):
        self.assertEqual(self.event.n_subscribers(), 0)
        self.event.connect(self.callback)
        self.assertEqual(self.event.n_subscribers(), 1)
        self.event.listen(self.callback2)
        self.assertEqual(self.event.n_subscribers(), 2)

    def testIsConnected(self):
        self.assertEqual(self.event.is_connected(self.callback), False)
        self.event.connect(self.callback)
        self.assertEqual(self.event.is_connected(self.callback), True)

        self.assertEqual(self.event.is_connected(self.callback2), False)
        self.event.listen(self.callback2)
        self.assertEqual(self.event.is_connected(self.callback2), True)

    def testEmit(self):
        self.event.connect(self.callback)
        self.assertEqual(self.args,   None)
        self.assertEqual(self.kwargs, None)

        self.event.emit()
        self.assertEqual(self.args,   ())
        self.assertEqual(self.kwargs, {})

        self.event.emit('test')
        self.assertEqual(self.args,   ('test',))
        self.assertEqual(self.kwargs, {})

        self.event.emit('test', foo = 'bar')
        self.assertEqual(self.args,   ('test',))
        self.assertEqual(self.kwargs, {'foo': 'bar'})
        self.event.disconnect(self.callback)

        self.event.listen(self.callback)
        self.args   = None
        self.kwargs = None

        self.event.emit()
        self.assertEqual(self.args,   ())
        self.assertEqual(self.kwargs, {})

        self.event.emit('test')
        self.assertEqual(self.args,   ('test',))
        self.assertEqual(self.kwargs, {})

        self.event.emit('test', foo = 'bar')
        self.assertEqual(self.args,   ('test',))
        self.assertEqual(self.kwargs, {'foo': 'bar'})
        self.event.disconnect(self.callback)

    def testDisconnect(self):
        self.assertEqual(self.event.n_subscribers(), 0)
        self.event.connect(self.callback)
        self.event.connect(self.callback2)
        self.assertEqual(self.event.n_subscribers(), 2)
        self.event.disconnect(self.callback)
        self.assertEqual(self.event.n_subscribers(), 1)
        self.event.disconnect(self.callback2)
        self.assertEqual(self.event.n_subscribers(), 0)

    def testDisconnectAll(self):
        self.assertEqual(self.event.n_subscribers(), 0)
        self.event.connect(self.callback)
        self.event.connect(self.callback2)
        self.assertEqual(self.event.n_subscribers(), 2)
        self.event.disconnect_all()
        self.assertEqual(self.event.n_subscribers(), 0)
예제 #2
0
class eventTest(unittest.TestCase):
    CORRELATE = Event

    def setUp(self):
        self.event = Event()
        self.args = None
        self.kwargs = None

    def callback(self, *args, **kwargs):
        self.args = args
        self.kwargs = kwargs

    def callback2(self, *args, **kwargs):
        self.callback(*args, **kwargs)

    def testConstructor(self):
        event = Event()

    def testConnect(self):
        self.event.connect(self.callback)
        self.assertEqual(self.event.n_subscribers(), 1)
        self.assertRaises(AttributeError, self.event.connect, self.callback)
        self.event.connect(self.callback2)
        self.assertEqual(self.event.n_subscribers(), 2)

    def testListen(self):
        import gc
        from Exscript.util.weakmethod import WeakMethod

        def thefunction():
            pass

        ref = self.event.listen(thefunction)
        self.assertIsInstance(ref, WeakMethod)
        self.assertEqual(self.event.n_subscribers(), 1)
        self.assertRaises(AttributeError, self.event.listen, thefunction)
        del thefunction
        gc.collect()
        self.assertEqual(self.event.n_subscribers(), 0)

    def testNSubscribers(self):
        self.assertEqual(self.event.n_subscribers(), 0)
        self.event.connect(self.callback)
        self.assertEqual(self.event.n_subscribers(), 1)
        self.event.listen(self.callback2)
        self.assertEqual(self.event.n_subscribers(), 2)

    def testIsConnected(self):
        self.assertEqual(self.event.is_connected(self.callback), False)
        self.event.connect(self.callback)
        self.assertEqual(self.event.is_connected(self.callback), True)

        self.assertEqual(self.event.is_connected(self.callback2), False)
        self.event.listen(self.callback2)
        self.assertEqual(self.event.is_connected(self.callback2), True)

    def testEmit(self):
        self.event.connect(self.callback)
        self.assertEqual(self.args, None)
        self.assertEqual(self.kwargs, None)

        self.event.emit()
        self.assertEqual(self.args, ())
        self.assertEqual(self.kwargs, {})

        self.event.emit('test')
        self.assertEqual(self.args, ('test', ))
        self.assertEqual(self.kwargs, {})

        self.event.emit('test', foo='bar')
        self.assertEqual(self.args, ('test', ))
        self.assertEqual(self.kwargs, {'foo': 'bar'})
        self.event.disconnect(self.callback)

        self.event.listen(self.callback)
        self.args = None
        self.kwargs = None

        self.event.emit()
        self.assertEqual(self.args, ())
        self.assertEqual(self.kwargs, {})

        self.event.emit('test')
        self.assertEqual(self.args, ('test', ))
        self.assertEqual(self.kwargs, {})

        self.event.emit('test', foo='bar')
        self.assertEqual(self.args, ('test', ))
        self.assertEqual(self.kwargs, {'foo': 'bar'})
        self.event.disconnect(self.callback)

    def testDisconnect(self):
        self.assertEqual(self.event.n_subscribers(), 0)
        self.event.connect(self.callback)
        self.event.connect(self.callback2)
        self.assertEqual(self.event.n_subscribers(), 2)
        self.event.disconnect(self.callback)
        self.assertEqual(self.event.n_subscribers(), 1)
        self.event.disconnect(self.callback2)
        self.assertEqual(self.event.n_subscribers(), 0)

    def testDisconnectAll(self):
        self.assertEqual(self.event.n_subscribers(), 0)
        self.event.connect(self.callback)
        self.event.connect(self.callback2)
        self.assertEqual(self.event.n_subscribers(), 2)
        self.event.disconnect_all()
        self.assertEqual(self.event.n_subscribers(), 0)
예제 #3
0
class Account(object):
    """
    This class represents a user account.
    """
    def __init__(self, name, password='', password2=None, key=None):
        """
        Constructor.

        The authorization password is only required on hosts that
        separate the authentication from the authorization procedure.
        If an authorization password is not given, it defaults to the
        same value as the authentication password.

        :type  name: string
        :param name: A username.
        :type  password: string
        :param password: The authentication password.
        :type  password2: string
        :param password2: The authorization password, if required.
        :type  key: PrivateKey
        :param key: A private key, if required.
        """
        self.acquired_event = Event()
        self.released_event = Event()
        self.changed_event = Event()
        self.name = name
        self.password = password
        self.authorization_password = password2
        self.key = key
        self.synclock = multiprocessing.Condition(multiprocessing.Lock())
        self.lock = multiprocessing.Lock()

    def __enter__(self):
        self.acquire()
        return self

    def __exit__(self, thetype, value, traceback):
        self.release()

    def context(self):
        """
        When you need a 'with' context for an already-acquired account.
        """
        return Context(self)

    def acquire(self, signal=True):
        """
        Locks the account.

        :type  signal: bool
        :param signal: Whether to emit the acquired_event signal.
        """
        with self.synclock:
            while not self.lock.acquire(False):
                self.synclock.wait()
            if signal:
                self.acquired_event(self)
            self.synclock.notify_all()

    def release(self, signal=True):
        """
        Unlocks the account.

        :type  signal: bool
        :param signal: Whether to emit the released_event signal.
        """
        with self.synclock:
            self.lock.release()
            if signal:
                self.released_event(self)
            self.synclock.notify_all()

    def set_name(self, name):
        """
        Changes the name of the account.

        :type  name: string
        :param name: The account name.
        """
        self.name = name
        self.changed_event.emit(self)

    def get_name(self):
        """
        Returns the name of the account.

        :rtype:  string
        :return: The account name.
        """
        return self.name

    def set_password(self, password):
        """
        Changes the password of the account.

        :type  password: string
        :param password: The account password.
        """
        self.password = password
        self.changed_event.emit(self)

    def get_password(self):
        """
        Returns the password of the account.

        :rtype:  string
        :return: The account password.
        """
        return self.password

    def set_authorization_password(self, password):
        """
        Changes the authorization password of the account.

        :type  password: string
        :param password: The new authorization password.
        """
        self.authorization_password = password
        self.changed_event.emit(self)

    def get_authorization_password(self):
        """
        Returns the authorization password of the account.

        :rtype:  string
        :return: The account password.
        """
        return self.authorization_password or self.password

    def get_key(self):
        """
        Returns the key of the account, if any.

        :rtype:  PrivateKey|None
        :return: A key object.
        """
        return self.key
예제 #4
0
파일: Account.py 프로젝트: 0x24bin/exscript
class Account(object):
    """
    This class represents a user account.
    """

    def __init__(self, name, password = '', password2 = None, key = None):
        """
        Constructor.

        The authorization password is only required on hosts that
        separate the authentication from the authorization procedure.
        If an authorization password is not given, it defaults to the
        same value as the authentication password.

        @type  name: string
        @param name: A username.
        @type  password: string
        @param password: The authentication password.
        @type  password2: string
        @param password2: The authorization password, if required.
        @type  key: PrivateKey
        @param key: A private key, if required.
        """
        self.acquired_event         = Event()
        self.released_event         = Event()
        self.changed_event          = Event()
        self.name                   = name
        self.password               = password
        self.authorization_password = password2
        self.key                    = key
        self.synclock               = threading.Condition(threading.Lock())
        self.lock                   = threading.Lock()

    def __enter__(self):
        self.acquire()
        return self

    def __exit__(self, thetype, value, traceback):
        self.release()

    def context(self):
        """
        When you need a 'with' context for an already-acquired account.
        """
        return Context(self)

    def acquire(self, signal = True):
        """
        Locks the account.

        @type  signal: bool
        @param signal: Whether to emit the acquired_event signal.
        """
        with self.synclock:
            while not self.lock.acquire(False):
                self.synclock.wait()
            if signal:
                self.acquired_event(self)
            self.synclock.notify_all()

    def release(self, signal = True):
        """
        Unlocks the account.

        @type  signal: bool
        @param signal: Whether to emit the released_event signal.
        """
        with self.synclock:
            self.lock.release()
            if signal:
                self.released_event(self)
            self.synclock.notify_all()

    def set_name(self, name):
        """
        Changes the name of the account.

        @type  name: string
        @param name: The account name.
        """
        self.name = name
        self.changed_event.emit(self)

    def get_name(self):
        """
        Returns the name of the account.

        @rtype:  string
        @return: The account name.
        """
        return self.name

    def set_password(self, password):
        """
        Changes the password of the account.

        @type  password: string
        @param password: The account password.
        """
        self.password = password
        self.changed_event.emit(self)

    def get_password(self):
        """
        Returns the password of the account.

        @rtype:  string
        @return: The account password.
        """
        return self.password

    def set_authorization_password(self, password):
        """
        Changes the authorization password of the account.

        @type  password: string
        @param password: The new authorization password.
        """
        self.authorization_password = password
        self.changed_event.emit(self)

    def get_authorization_password(self):
        """
        Returns the authorization password of the account.

        @rtype:  string
        @return: The account password.
        """
        return self.authorization_password or self.password

    def get_key(self):
        """
        Returns the key of the account, if any.

        @rtype:  PrivateKey|None
        @return: A key object.
        """
        return self.key