# 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

# Tools to interact with the web port:
#  * wait for the node to start up, by polling the web port
#  * send a command and get a response

# TODO: handle 'restart' correctly by writing something into the DB to
# distinguish between the old node and the new one. Maybe.


def get_url_and_token(basedir, err):
    basedir = os.path.abspath(basedir)
    dbfile = os.path.join(basedir, "petmail.db")
    if not (os.path.isdir(basedir) and os.path.exists(dbfile)):

# 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


# Tools to interact with the web port:
#  * wait for the node to start up, by polling the web port
#  * send a command and get a response

# TODO: handle 'restart' correctly by writing something into the DB to
# distinguish between the old node and the new one. Maybe.


def get_url_and_token(basedir, err):
    basedir = os.path.abspath(basedir)
    dbfile = os.path.join(basedir, "petmail.db")
    if not (os.path.isdir(basedir) and os.path.exists(dbfile)):

        raise NoNodeError(basedir)