Work on command line client, and some improvements to server

handling of bad URLs


git-svn-id: https://bucket.mit.edu/svn/nilm/nilmdb@10676 ddd99763-3ecb-0310-9145-efcb8ce7c51f
This commit is contained in:
Jim Paris 2012-04-03 22:21:42 +00:00
parent f29d38d9d9
commit 7bba4a80d9
5 changed files with 66 additions and 13 deletions

View File

@ -1,8 +1,9 @@
all: tool
all: test
tool:
python nilmtool.py --help
python nilmtool.py
python nilmtool.py list --help
python nilmtool.py -u asfdadsf list
lint:
pylint -f parseable nilmdb

View File

@ -45,14 +45,18 @@ class MyCurl(object):
def __init__(self, baseurl = ""):
"""If baseurl is supplied, all other functions that take
a URL can be given a relative URL instead."""
self.baseurl = baseurl
self.url = baseurl
# Verify / clean up URL
reparsed = urlparse.urlparse(baseurl).geturl()
if '://' not in reparsed:
reparsed = urlparse.urlparse("http://" + baseurl).geturl()
self.baseurl = reparsed
self.curl = pycurl.Curl()
self.curl.setopt(pycurl.SSL_VERIFYHOST, 2)
self.curl.setopt(pycurl.FOLLOWLOCATION, 1)
self.curl.setopt(pycurl.MAXREDIRS, 5)
self._setup_url()
def _setup_url(self, url, params):
def _setup_url(self, url = "", params = ""):
url = urlparse.urljoin(self.baseurl, url)
if params:
url = urlparse.urljoin(url, "?" + urllib.urlencode(params, True))
@ -92,7 +96,9 @@ class MyCurl(object):
try:
self.curl.perform()
except pycurl.error as e:
raise ServerError(502, self.url, "Curl error: " + e[1])
raise ServerError(status = "502 Error",
url = self.url,
message = e[1])
body_str = body.getvalue()
self._check_error(body_str)
return json.loads(body_str)

View File

@ -17,7 +17,9 @@ version_string = sprintf("nilmtool %s, client library %s",
version, nilmdb.Client.client_version)
def parse_opts(argv):
parser = argparse.ArgumentParser(add_help = False)
formatter = argparse.ArgumentDefaultsHelpFormatter
parser = argparse.ArgumentParser(add_help = False,
formatter_class = formatter)
group = parser.add_argument_group("General options")
group.add_argument("-h", "--help", action='help',
@ -27,11 +29,37 @@ def parse_opts(argv):
group.add_argument("-q", "--quiet", action="store_true",
help="suppress unnecessary console output")
group = parser.add_argument_group("Server options")
group = parser.add_argument_group("Server")
group.add_argument("-u", "--url", action="store",
default="http://localhost:12380/",
help="NilmDB server URL (default: %(default)s)")
sub = parser.add_subparsers(title="Commands",
dest="command",
description="Specify --help after the "
"command for command-specific options.")
# info
cmd = sub.add_parser("info", help="Server information",
formatter_class = formatter,
description="""
List information about the server, like
version.
""")
# list
cmd = sub.add_parser("list", help="List streams",
formatter_class = formatter,
description="""
List streams available in the database,
optionally filtering by type or partial path.
""")
group = cmd.add_argument_group("Stream filtering")
group.add_argument("-t", "--type", metavar="GLOB", default="*",
help="Match only this stream type")
group.add_argument("-p", "--path", metavar="GLOB", default="*",
help="Match only this path")
# group.add_argument(
@ -48,9 +76,23 @@ def parse_opts(argv):
return args
def die(formatstr, *args):
fprintf(sys.stderr, formatstr, *args)
sys.exit(-1)
def run(argv):
args = parse_opts(argv)
print args
client = nilmdb.Client(args.url)
try:
server_version = client.version()
except nilmdb.client.NilmCommError as e:
die("Error connecting to server: %s\n", str(e))
print server_version
print args.url
print args.command
# if not opt.quiet:
# printf("Server URL: %s\n", opt.url)

View File

@ -55,6 +55,10 @@ class TestClient(object):
with assert_raises(nilmdb.client.ClientError):
client.version()
# Now a real URL with no http:// prefix
client = nilmdb.Client(url = "localhost:12380")
version = client.version()
# Now use the real URL
client = nilmdb.Client(url = "http://localhost:12380/")
version = client.version()

View File

@ -53,7 +53,7 @@ class TestCmdline(object):
outfile = StringIO.StringIO()
with stdio_wrapper(infile, outfile, outfile) as s:
try:
nilmdb.cmdline.run(args = shlex.split(arg_string))
nilmdb.cmdline.run(shlex.split(arg_string))
sys.exit(0)
except SystemExit as e:
exitcode = e.code
@ -62,11 +62,11 @@ class TestCmdline(object):
def test_cmdline_basic(self):
(output, exitcode) = self.run("--help")
assert("Usage:" in output)
assert("usage:" in output)
eq_(exitcode, 0)
(output, exitcode) = self.run("--nosuchoption")
ne_(exitcode, 0)
(output, exitcode) = self.run("--url http://localhost:12380/")
eq_(exitcode, 0)
# (output, exitcode) = self.run("--url http://localhost:12380/")
# eq_(exitcode, 0)