Example #1
0
  def quit(ctx):
    if not ctx.pipeline:
      def callback(_):
        ctx.transport.write(str(command('QUIT')))

        expect = range(200, 300)

        def callback(recv):
          try:
            replyLine = rfc5321.replyLine.match(recv, '( replyCode, textstring )')

          except ValueError:
            asdf.callback.insert(0, callback)

            return ctx.transport.protocol.dataReceived.shift().then(recv.__add__)

          ctx.recv = recv[len(replyLine):]

          result = reply(int(replyLine.replyCode), *map(str, replyLine.textstring))
          if int(result) not in expect:
            raise result

          return result

        asdf = promise.promise().then(callback)

        return asdf(ctx.recv)

      return ctx.cascade(untwisted.partial(promise.promise.then, callback=callback))

    return client.quit(ctx)
Example #2
0
  def throw(ctx, *args, **kwds):

    # Already triggered
    if ctx.trigger:
      raise StopIteration

    ctx.trigger = True

    #raise args
    if args:
      #type, value=None, traceback=None = args
      type, value, traceback = (lambda type, value=None, traceback=None: (type, value, traceback))(*args)
      try:
        raise type, value, traceback

      except:
        pass

    import traceback

    ctx.traceback = untwisted.final(untwisted.partial(sys.stderr.write, ''.join(traceback.format_stack(sys._getframe().f_back)) + traceback.format_exc()))

    ctx.args = args
    ctx.kwds = kwds

    ctx.propagate()

    return ctx
Example #3
0
    def throw(ctx, *args, **kwds):

        # Already triggered
        if ctx.trigger:
            raise StopIteration

        ctx.trigger = True

        #raise args
        if args:
            #type, value=None, traceback=None = args
            type, value, traceback = (lambda type, value=None, traceback=None:
                                      (type, value, traceback))(*args)
            try:
                raise type, value, traceback

            except:
                pass

        import traceback

        ctx.traceback = untwisted.final(
            untwisted.partial(
                sys.stderr.write,
                ''.join(traceback.format_stack(sys._getframe().f_back)) +
                traceback.format_exc()))

        ctx.args = args
        ctx.kwds = kwds

        ctx.propagate()

        return ctx
Example #4
0
  def mnbv(ctx, e, name):
    try:
      name = map(untwisted.partial(re.findall, '\+|>|[^\s+>]+'), re.split(',', name))

    except TypeError:
      return poiu(ctx.args[name])

    a = []
    for itm in ctx.args:
      a.extend(select(itm, itm.match, *name))

    # Any selector which could match but didn't should result in an empty
    # sequence - only a selector which could never match any group should raise
    # an exception.  A selector which matches a repetition that didn't match
    # currently incorrectly raises an exception
    if not a:
      raise e

    return poiu(*filter(lambda itm: itm.match.group(itm.group), a))
Example #5
0
    def data(ctx, content):
      if not ctx.ctx.pipeline:
        content = re.sub('(^|\r\n)\.', '\\1..', content)

        if '\r\n' != content[-2:]:
          content += '\r\n'

        content += '.\r\n'

        def callback(_):
          ctx.ctx.transport.write(str(command('DATA')))

          expect = range(300, 400)

          def callback(recv):
            try:
              replyLine = rfc5321.replyLine.match(recv, '( replyCode, textstring )')

            except ValueError:
              asdf.callback.insert(0, callback)

              return ctx.ctx.transport.protocol.dataReceived.shift().then(recv.__add__)

            ctx.ctx.recv = recv[len(replyLine):]

            result = reply(int(replyLine.replyCode), *map(str, replyLine.textstring))
            if int(result) not in expect:
              raise result

            ctx.ctx.transport.write(content)

            return promise.promise()(ctx.ctx.reply())

          asdf = promise.promise().then(callback)

          return asdf(ctx.ctx.recv)

        return ctx.ctx.cascade(untwisted.partial(promise.promise.then, callback=callback))

      return client.mail.data(ctx, content)
Example #6
0
  def propagate(ctx):
    while True:
      try:
        callback = ctx.callback.pop(0)

      # No callback
      except IndexError:
        return ctx

      if isinstance(callback, promise):

        # Already triggered
        if hasattr(callback, 'args'):
          raise StopIteration

        callback.trigger = True

        try:
          callback.traceback = ctx.traceback
          del ctx.traceback

        # Don't worry about callback.traceback: Can't be without .args
        except AttributeError:
          pass

        callback.args = ctx.args
        ctx.args = callback,

        callback.kwds = ctx.kwds
        ctx.kwds = {}

        if ctx.callback:
          callback.propagate()

        # Tail call optimization
        else:
          ctx = callback

        continue

      # Skip if callback has no .throw()
      if hasattr(ctx, 'traceback'):
        try:
          callback = callback.throw

        except AttributeError:
          continue

        ctx.traceback.cancel()
        del ctx.traceback

      args = ctx.args
      del ctx.args

      kwds = ctx.kwds
      del ctx.kwds

      try:
        result = callback(*args, **kwds)

      except:
        ctx.traceback = untwisted.final(untwisted.partial(sys.stderr.write, ''.join(traceback.format_stack(sys._getframe().f_back)) + traceback.format_exc()))

        ctx.args = sys.exc_info()
        ctx.kwds = {}

        continue

      if isinstance(result, promise):

        # Tail call optimization
        #result.then(ctx)

        try:
          ctx.traceback = result.traceback
          del result.traceback

        # Don't worry about ctx.traceback: del above
        except AttributeError:
          pass

        try:
          ctx.args = result.args
          result.args = ctx,

          ctx.kwds = result.kwds
          result.kwds = {}

        # Not yet triggered
        except AttributeError:
          result.callback.append(ctx)

          return ctx

        continue

      ctx.args = result,
      ctx.kwds = {}
Example #7
0
def timeout(secs, *args, **kwds):
  ctx = promise.promise()

  reactor.callLater(secs, untwisted.partial(ctx, *args or (None,), **kwds))

  return ctx
Example #8
0
  def replace(ctx, replace, subject, *args, **kwds):
    a = []
    b = False

    # Guaranteed to match *any* string
    pattern = re.compile('([^,]*?)\((.*?)\)|([^,]*)')

    for itm in args:

      # Zero length match only between commas
      match = pattern.match(itm)
      while True:
        try:
          c = re.findall('\+|>|[^\s+>]+', match.group(1))

        except TypeError:
          c = re.findall('\+|>|[^\s+>]+', match.group(3))
          c.append(())

          a.append(c)

          b = True

        else:
          d = map(untwisted.compose(lambda *args: args, untwisted.partial(re.findall, '\+|>|[^\s+>]+')), re.split(',', match.group(2)))
          if c:
            c.append(d)

            a.append(c)

            b = True

          else:
            a.extend(d)

        if not len(itm) > match.end():
          break

        match = pattern.match(itm, match.end() + 1)

    _, pattern, start, d, e = ctx.zxcv(0, '', subject, *a)

    if callable(replace):
      if b:
        def f(match):
          for itm in e:
            itm.match = getattr(itm, 'match', match)

          return replace(poiu(*filter(lambda itm: match.group(itm.group), e)))

      else:
        f = iter(d)
        g = iter(d[1:])
        while True:
          try:
            f.next()[1].sibling = g.next()

          except StopIteration:
            break

        f = lambda match: replace(poiu(asdf(0, *d, match=match)))

    else:
      f = replace

    try:
      return re.sub(pattern, f, subject, kwds['count'])

    except KeyError:
      return re.sub(pattern, f, subject)
Example #9
0
  def lkjh(ctx, jhgf, subject, *args):
    a = []
    b = False

    # Guaranteed to match *any* string
    pattern = re.compile('([^,]*?)\((.*?)\)|([^,]*)')

    for itm in args:

      # Zero length match only between commas
      match = pattern.match(itm)
      while True:
        try:
          c = re.findall('\+|>|[^\s+>]+', match.group(1))

        except TypeError:
          c = re.findall('\+|>|[^\s+>]+', match.group(3))
          c.append(())

          a.append(c)

          b = True

        else:
          d = map(untwisted.compose(lambda *args: args, untwisted.partial(re.findall, '\+|>|[^\s+>]+')), re.split(',', match.group(2)))
          if c:
            c.append(d)

            a.append(c)

            b = True

          else:
            a.extend(d)

        if not len(itm) > match.end():
          break

        match = pattern.match(itm, match.end() + 1)

    _, pattern, start, d, e = ctx.zxcv(0, '', subject, *a)

    match = jhgf(re.compile(pattern))(subject, start)
    if not match:
      raise ValueError

    if b:
      for itm in e:
        itm.match = getattr(itm, 'match', match)

      return poiu(*filter(lambda itm: itm.match.group(itm.group), e))

    f = iter(d)
    g = iter(d[1:])
    while True:
      try:
        f.next()[1].sibling = g.next()

      except StopIteration:
        break

    return poiu(asdf(0, *d, match=match))
Example #10
0
    def propagate(ctx):
        while True:
            try:
                callback = ctx.callback.pop(0)

            # No callback
            except IndexError:
                return ctx

            if isinstance(callback, promise):

                # Already triggered
                if hasattr(callback, 'args'):
                    raise StopIteration

                callback.trigger = True

                try:
                    callback.traceback = ctx.traceback
                    del ctx.traceback

                # Don't worry about callback.traceback: Can't be without .args
                except AttributeError:
                    pass

                callback.args = ctx.args
                ctx.args = callback,

                callback.kwds = ctx.kwds
                ctx.kwds = {}

                if ctx.callback:
                    callback.propagate()

                # Tail call optimization
                else:
                    ctx = callback

                continue

            # Skip if callback has no .throw()
            if hasattr(ctx, 'traceback'):
                try:
                    callback = callback.throw

                except AttributeError:
                    continue

                ctx.traceback.cancel()
                del ctx.traceback

            args = ctx.args
            del ctx.args

            kwds = ctx.kwds
            del ctx.kwds

            try:
                result = callback(*args, **kwds)

            except:
                ctx.traceback = untwisted.final(
                    untwisted.partial(
                        sys.stderr.write,
                        ''.join(traceback.format_stack(
                            sys._getframe().f_back)) + traceback.format_exc()))

                ctx.args = sys.exc_info()
                ctx.kwds = {}

                continue

            if isinstance(result, promise):

                # Tail call optimization
                #result.then(ctx)

                try:
                    ctx.traceback = result.traceback
                    del result.traceback

                # Don't worry about ctx.traceback: del above
                except AttributeError:
                    pass

                try:
                    ctx.args = result.args
                    result.args = ctx,

                    ctx.kwds = result.kwds
                    result.kwds = {}

                # Not yet triggered
                except AttributeError:
                    result.callback.append(ctx)

                    return ctx

                continue

            ctx.args = result,
            ctx.kwds = {}