def autologin(flush = True, attempts = 1): """ Wraps the given function such that conn.login() is executed before calling it. Example:: @autologin(attempts = 2) def my_func(job, host, conn): pass # Do something. Exscript.util.start.quickrun('myhost', my_func) @type flush: bool @param flush: Whether to flush the last prompt from the buffer. @type attempts: int @param attempts: The number of login attempts if login fails. @rtype: function @return: The wrapped function. """ def decorator(function): def decorated(job, host, conn, *args, **kwargs): failed = 0 while True: try: conn.login(flush = flush) except LoginFailure, e: failed += 1 if failed >= attempts: raise continue break return function(job, host, conn, *args, **kwargs) copy_labels(function, decorated) return decorated
def autologin(flush=True, attempts=1): """ Wraps the given function such that conn.login() is executed before calling it. Example:: @autologin(attempts = 2) def my_func(job, host, conn): pass # Do something. Exscript.util.start.quickrun('myhost', my_func) @type flush: bool @param flush: Whether to flush the last prompt from the buffer. @type attempts: int @param attempts: The number of login attempts if login fails. @rtype: function @return: The wrapped function. """ def decorator(function): def decorated(job, host, conn, *args, **kwargs): failed = 0 while True: try: conn.login(flush=flush) except LoginFailure, e: failed += 1 if failed >= attempts: raise continue break return function(job, host, conn, *args, **kwargs) copy_labels(function, decorated) return decorated
def _decorate(flush=True, attempts=1, only_authenticate=False): """ Wraps the given function such that conn.login() or conn.authenticate() is executed. Doing the real work for autologin and autoauthenticate to minimize code duplication. :type flush: bool :param flush: Whether to flush the last prompt from the buffer. :type attempts: int :param attempts: The number of login attempts if login fails. :type only_authenticate: bool :param only_authenticate: login or only authenticate (don't authorize)? :rtype: function :return: The wrapped function. """ def decorator(function): def decorated(job, host, conn, *args, **kwargs): failed = 0 while True: try: if only_authenticate: conn.authenticate(flush=flush) else: conn.login(flush=flush) except LoginFailure, e: failed += 1 if failed >= attempts: raise continue break return function(job, host, conn, *args, **kwargs) copy_labels(function, decorated) return decorated
def bind(function, *args, **kwargs): """ Wraps the given function such that when it is called, the given arguments are passed in addition to the connection argument. @type function: function @param function: The function that's ought to be wrapped. @type args: list @param args: Passed on to the called function. @type kwargs: dict @param kwargs: Passed on to the called function. @rtype: function @return: The wrapped function. """ def decorated(*inner_args, **inner_kwargs): kwargs.update(inner_kwargs) return function(*(inner_args + args), **kwargs) copy_labels(function, decorated) return decorated
def _decorate(flush=True, attempts=1, only_authenticate=False): """ Wraps the given function such that conn.login() or conn.authenticate() is executed. Doing the real work for autologin and autoauthenticate to minimize code duplication. @type flush: bool @param flush: Whether to flush the last prompt from the buffer. @type attempts: int @param attempts: The number of login attempts if login fails. @type only_authenticate: bool @param only_authenticate: login or only authenticate (don't authorize)? @rtype: function @return: The wrapped function. """ def decorator(function): def decorated(job, host, conn, *args, **kwargs): failed = 0 while True: try: if only_authenticate: conn.authenticate(flush=flush) else: conn.login(flush=flush) except LoginFailure, e: failed += 1 if failed >= attempts: raise continue break return function(job, host, conn, *args, **kwargs) copy_labels(function, decorated) return decorated