# Path: src/petmail/scripts/runner.py
# class NoNodeError(Exception):
#     def __init__(self, basedir):
#         self.basedir = basedir
#     def __str__(self):
#         return "NoNodeError: '%s' doesn't look like a Petmail basedir, quitting" % self.basedir


import os, sys, time
import urllib, urlparse, httplib, json
from socket import error as socket_error
from StringIO import StringIO
from .. import database
from .runner import NoNodeError
    # first, create the event channel
    resp = do_http("POST", baseurl + "api/eventchannel-create",
                   json.dumps({"token": token}))
    if resp.status != 200:
        raise RuntimeError("unable to create event channel: %s %s"
                           % (resp.status, resp.reason))
    r = json.loads(resp.read().decode("utf-8"))
    esid = r["esid"]
    # we'll listen on this one
    event_resp = do_http("GET", baseurl + "api/events/%s" % esid,
                         event_stream=True)
    # and then subscribe to hear about events. I'm not 100% sure this won't
    # race (the web frontend specifically waits until a "ready" event is
    # delivered, to make sure the browser has established the EventSource
    # channel, before subscribing to anything. We're ok as long as do_http()
    # doesn't return until after it's seen the HTTP headers.
    resp = do_http("POST", baseurl + "api/eventchannel-subscribe",
                   json.dumps({"token": token,
                               "args": {"esid": esid,
                                        "topic": topic,
                                        "catchup": catchup}}))
    if resp.status != 200:
        raise RuntimeError("unable to subscribe to event channel: %s %s"
                           % (resp.status, resp.reason))

    return event_resp

class EOFError(Exception):
    pass


# Path: src/petmail/scripts/runner.py
# class NoNodeError(Exception):
#     def __init__(self, basedir):
#         self.basedir = basedir
#     def __str__(self):
#         return "NoNodeError: '%s' doesn't look like a Petmail basedir, quitting" % self.basedir


# Path: src/petmail/scripts/webwait.py
import os, sys, time
import urllib, urlparse, httplib, json
from socket import error as socket_error
from StringIO import StringIO
from .. import database
from .runner import NoNodeError

    # first, create the event channel
    resp = do_http("POST", baseurl + "api/eventchannel-create",
                   json.dumps({"token": token}))
    if resp.status != 200:
        raise RuntimeError("unable to create event channel: %s %s"
                           % (resp.status, resp.reason))
    r = json.loads(resp.read().decode("utf-8"))
    esid = r["esid"]
    # we'll listen on this one
    event_resp = do_http("GET", baseurl + "api/events/%s" % esid,
                         event_stream=True)
    # and then subscribe to hear about events. I'm not 100% sure this won't
    # race (the web frontend specifically waits until a "ready" event is
    # delivered, to make sure the browser has established the EventSource
    # channel, before subscribing to anything. We're ok as long as do_http()
    # doesn't return until after it's seen the HTTP headers.
    resp = do_http("POST", baseurl + "api/eventchannel-subscribe",
                   json.dumps({"token": token,
                               "args": {"esid": esid,
                                        "topic": topic,
                                        "catchup": catchup}}))
    if resp.status != 200:
        raise RuntimeError("unable to subscribe to event channel: %s %s"
                           % (resp.status, resp.reason))

    return event_resp

class EOFError(Exception):
    pass


def get_line(r):