Browse Source

Clean up imports to separate client and server more.

"import nilmdb" doesn't do much; "import nilmdb.client" or "import
nilmdb.server" is now required.
tags/nilmdb-1.3.0
Jim Paris 11 years ago
parent
commit
232a3876c2
17 changed files with 59 additions and 58 deletions
  1. +4
    -2
      nilmdb/__init__.py
  2. +0
    -1
      nilmdb/client/client.py
  3. +0
    -1
      nilmdb/client/httpclient.py
  4. +3
    -2
      nilmdb/cmdline/cmdline.py
  5. +0
    -1
      nilmdb/cmdline/create.py
  6. +0
    -1
      nilmdb/cmdline/destroy.py
  7. +1
    -1
      nilmdb/cmdline/info.py
  8. +0
    -1
      nilmdb/cmdline/insert.py
  9. +0
    -1
      nilmdb/cmdline/remove.py
  10. +2
    -2
      nilmdb/scripts/nilmdb_server.py
  11. +1
    -1
      nilmdb/server/bulkdata.py
  12. +1
    -1
      nilmdb/server/nilmdb.py
  13. +1
    -1
      nilmdb/server/pyrocket.py
  14. +1
    -1
      nilmdb/server/server.py
  15. +26
    -23
      tests/test_client.py
  16. +7
    -6
      tests/test_cmdline.py
  17. +12
    -12
      tests/test_nilmdb.py

+ 4
- 2
nilmdb/__init__.py View File

@@ -1,7 +1,9 @@
"""Main NilmDB import""" """Main NilmDB import"""


from nilmdb.server import NilmDB, Server
from nilmdb.client import Client
# These aren't imported automatically, because loading the server
# stuff isn't always necessary.
#from nilmdb.server import NilmDB, Server
#from nilmdb.client import Client


from nilmdb._version import get_versions from nilmdb._version import get_versions
__version__ = get_versions()['version'] __version__ = get_versions()['version']


+ 0
- 1
nilmdb/client/client.py View File

@@ -2,7 +2,6 @@


"""Class for performing HTTP client requests via libcurl""" """Class for performing HTTP client requests via libcurl"""


import nilmdb
import nilmdb.utils import nilmdb.utils
import nilmdb.client.httpclient import nilmdb.client.httpclient
from nilmdb.client.errors import ClientError from nilmdb.client.errors import ClientError


+ 0
- 1
nilmdb/client/httpclient.py View File

@@ -1,6 +1,5 @@
"""HTTP client library""" """HTTP client library"""


import nilmdb
import nilmdb.utils import nilmdb.utils
from nilmdb.client.errors import ClientError, ServerError, Error from nilmdb.client.errors import ClientError, ServerError, Error




+ 3
- 2
nilmdb/cmdline/cmdline.py View File

@@ -1,6 +1,7 @@
"""Command line client functionality""" """Command line client functionality"""


import nilmdb
import nilmdb.client

from nilmdb.utils.printf import * from nilmdb.utils.printf import *
from nilmdb.utils import datetime_tz from nilmdb.utils import datetime_tz
import nilmdb.utils.time import nilmdb.utils.time
@@ -84,7 +85,7 @@ class Cmdline(object):
if "verify" in self.args: if "verify" in self.args:
self.args.verify(self) self.args.verify(self)


self.client = nilmdb.Client(self.args.url)
self.client = nilmdb.client.Client(self.args.url)


# Make a test connection to make sure things work, # Make a test connection to make sure things work,
# unless the particular command requests that we don't. # unless the particular command requests that we don't.


+ 0
- 1
nilmdb/cmdline/create.py View File

@@ -1,5 +1,4 @@
from nilmdb.utils.printf import * from nilmdb.utils.printf import *
import nilmdb
import nilmdb.client import nilmdb.client


from argparse import RawDescriptionHelpFormatter as raw_form from argparse import RawDescriptionHelpFormatter as raw_form


+ 0
- 1
nilmdb/cmdline/destroy.py View File

@@ -1,5 +1,4 @@
from nilmdb.utils.printf import * from nilmdb.utils.printf import *
import nilmdb
import nilmdb.client import nilmdb.client


from argparse import ArgumentDefaultsHelpFormatter as def_form from argparse import ArgumentDefaultsHelpFormatter as def_form


+ 1
- 1
nilmdb/cmdline/info.py View File

@@ -1,4 +1,4 @@
import nilmdb
import nilmdb.client
from nilmdb.utils.printf import * from nilmdb.utils.printf import *
from nilmdb.utils import human_size from nilmdb.utils import human_size




+ 0
- 1
nilmdb/cmdline/insert.py View File

@@ -1,5 +1,4 @@
from nilmdb.utils.printf import * from nilmdb.utils.printf import *
import nilmdb
import nilmdb.client import nilmdb.client
import nilmdb.utils.timestamper as timestamper import nilmdb.utils.timestamper as timestamper
import nilmdb.utils.time import nilmdb.utils.time


+ 0
- 1
nilmdb/cmdline/remove.py View File

@@ -1,5 +1,4 @@
from nilmdb.utils.printf import * from nilmdb.utils.printf import *
import nilmdb
import nilmdb.client import nilmdb.client


def setup(self, sub): def setup(self, sub):


+ 2
- 2
nilmdb/scripts/nilmdb_server.py View File

@@ -41,8 +41,8 @@ def main():


# Create database object. Needs to be serialized before passing # Create database object. Needs to be serialized before passing
# to the Server. # to the Server.
db = nilmdb.utils.serializer_proxy(nilmdb.NilmDB)(args.database,
sync = not args.nosync)
db = nilmdb.utils.serializer_proxy(
nilmdb.server.NilmDB)(args.database, sync = not args.nosync)


# Configure the server # Configure the server
if args.quiet: if args.quiet:


+ 1
- 1
nilmdb/server/bulkdata.py View File

@@ -4,9 +4,9 @@
# nilmdb.py, but will pull the parent nilmdb module instead. # nilmdb.py, but will pull the parent nilmdb module instead.
from __future__ import absolute_import from __future__ import absolute_import
from __future__ import division from __future__ import division
import nilmdb
from nilmdb.utils.printf import * from nilmdb.utils.printf import *
from nilmdb.utils.time import float_time_to_string as ftts from nilmdb.utils.time import float_time_to_string as ftts
import nilmdb.utils


import os import os
import cPickle as pickle import cPickle as pickle


+ 1
- 1
nilmdb/server/nilmdb.py View File

@@ -10,7 +10,7 @@ Manages both the SQL database and the table storage backend.
# Need absolute_import so that "import nilmdb" won't pull in # Need absolute_import so that "import nilmdb" won't pull in
# nilmdb.py, but will pull the parent nilmdb module instead. # nilmdb.py, but will pull the parent nilmdb module instead.
from __future__ import absolute_import from __future__ import absolute_import
import nilmdb
import nilmdb.utils
from nilmdb.utils.printf import * from nilmdb.utils.printf import *
from nilmdb.server.interval import (Interval, DBInterval, from nilmdb.server.interval import (Interval, DBInterval,
IntervalSet, IntervalError) IntervalSet, IntervalError)


+ 1
- 1
nilmdb/server/pyrocket.py View File

@@ -5,11 +5,11 @@
# This is slow! Use the C version instead. # This is slow! Use the C version instead.


from __future__ import absolute_import from __future__ import absolute_import
import nilmdb
import struct import struct
import cStringIO import cStringIO
import itertools import itertools
from . import layout as _layout from . import layout as _layout
import nilmdb.utils
from nilmdb.utils.time import float_time_to_string as ftts from nilmdb.utils.time import float_time_to_string as ftts


ERR_UNKNOWN = 0 ERR_UNKNOWN = 0


+ 1
- 1
nilmdb/server/server.py View File

@@ -3,7 +3,7 @@
# Need absolute_import so that "import nilmdb" won't pull in # Need absolute_import so that "import nilmdb" won't pull in
# nilmdb.py, but will pull the nilmdb module instead. # nilmdb.py, but will pull the nilmdb module instead.
from __future__ import absolute_import from __future__ import absolute_import
import nilmdb
import nilmdb.server
from nilmdb.utils.printf import * from nilmdb.utils.printf import *
from nilmdb.server.errors import NilmDBError from nilmdb.server.errors import NilmDBError




+ 26
- 23
tests/test_client.py View File

@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-


import nilmdb
import nilmdb.server
import nilmdb.client

from nilmdb.utils.printf import * from nilmdb.utils.printf import *
from nilmdb.utils import timestamper from nilmdb.utils import timestamper
from nilmdb.client import ClientError, ServerError from nilmdb.client import ClientError, ServerError
@@ -33,11 +35,12 @@ def setup_module():
recursive_unlink(testdb) recursive_unlink(testdb)


# Start web app on a custom port # Start web app on a custom port
test_db = nilmdb.utils.serializer_proxy(nilmdb.NilmDB)(testdb, sync = False)
test_server = nilmdb.Server(test_db, host = "127.0.0.1",
port = 32180, stoppable = False,
fast_shutdown = True,
force_traceback = True)
test_db = nilmdb.utils.serializer_proxy(
nilmdb.server.NilmDB)(testdb, sync = False)
test_server = nilmdb.server.Server(test_db, host = "127.0.0.1",
port = 32180, stoppable = False,
fast_shutdown = True,
force_traceback = True)
test_server.start(blocking = False) test_server.start(blocking = False)


def teardown_module(): def teardown_module():
@@ -50,24 +53,24 @@ class TestClient(object):


def test_client_01_basic(self): def test_client_01_basic(self):
# Test a fake host # Test a fake host
client = nilmdb.Client(url = "http://localhost:1/")
client = nilmdb.client.Client(url = "http://localhost:1/")
with assert_raises(nilmdb.client.ServerError): with assert_raises(nilmdb.client.ServerError):
client.version() client.version()
client.close() client.close()


# Then a fake URL on a real host # Then a fake URL on a real host
client = nilmdb.Client(url = "http://localhost:32180/fake/")
client = nilmdb.client.Client(url = "http://localhost:32180/fake/")
with assert_raises(nilmdb.client.ClientError): with assert_raises(nilmdb.client.ClientError):
client.version() client.version()
client.close() client.close()


# Now a real URL with no http:// prefix # Now a real URL with no http:// prefix
client = nilmdb.Client(url = "localhost:32180")
client = nilmdb.client.Client(url = "localhost:32180")
version = client.version() version = client.version()
client.close() client.close()


# Now use the real URL # Now use the real URL
client = nilmdb.Client(url = testurl)
client = nilmdb.client.Client(url = testurl)
version = client.version() version = client.version()
eq_(distutils.version.LooseVersion(version), eq_(distutils.version.LooseVersion(version),
distutils.version.LooseVersion(test_server.version)) distutils.version.LooseVersion(test_server.version))
@@ -79,7 +82,7 @@ class TestClient(object):


def test_client_02_createlist(self): def test_client_02_createlist(self):
# Basic stream tests, like those in test_nilmdb:test_stream # Basic stream tests, like those in test_nilmdb:test_stream
client = nilmdb.Client(url = testurl)
client = nilmdb.client.Client(url = testurl)


# Database starts empty # Database starts empty
eq_(client.stream_list(), []) eq_(client.stream_list(), [])
@@ -100,7 +103,7 @@ class TestClient(object):
for x in range(10): for x in range(10):
with assert_raises(ClientError): with assert_raises(ClientError):
client.http.post("/stream/list") client.http.post("/stream/list")
client = nilmdb.Client(url = testurl)
client = nilmdb.client.Client(url = testurl)


# Create three streams # Create three streams
client.stream_create("/newton/prep", "float32_8") client.stream_create("/newton/prep", "float32_8")
@@ -131,7 +134,7 @@ class TestClient(object):
client.close() client.close()


def test_client_03_metadata(self): def test_client_03_metadata(self):
client = nilmdb.Client(url = testurl)
client = nilmdb.client.Client(url = testurl)


# Set / get metadata # Set / get metadata
eq_(client.stream_get_metadata("/newton/prep"), {}) eq_(client.stream_get_metadata("/newton/prep"), {})
@@ -175,7 +178,7 @@ class TestClient(object):
client.close() client.close()


def test_client_04_insert(self): def test_client_04_insert(self):
client = nilmdb.Client(url = testurl)
client = nilmdb.client.Client(url = testurl)


# Limit _max_data to 1 MB, since our test file is 1.5 MB # Limit _max_data to 1 MB, since our test file is 1.5 MB
old_max_data = nilmdb.client.client.StreamInserter._max_data old_max_data = nilmdb.client.client.StreamInserter._max_data
@@ -280,7 +283,7 @@ class TestClient(object):


def test_client_05_extractremove(self): def test_client_05_extractremove(self):
# Misc tests for extract and remove. Most of them are in test_cmdline. # Misc tests for extract and remove. Most of them are in test_cmdline.
client = nilmdb.Client(url = testurl)
client = nilmdb.client.Client(url = testurl)


for x in client.stream_extract("/newton/prep", 999123, 999124): for x in client.stream_extract("/newton/prep", 999123, 999124):
raise AssertionError("shouldn't be any data for this request") raise AssertionError("shouldn't be any data for this request")
@@ -296,7 +299,7 @@ class TestClient(object):
def test_client_06_generators(self): def test_client_06_generators(self):
# A lot of the client functionality is already tested by test_cmdline, # A lot of the client functionality is already tested by test_cmdline,
# but this gets a bit more coverage that cmdline misses. # but this gets a bit more coverage that cmdline misses.
client = nilmdb.Client(url = testurl)
client = nilmdb.client.Client(url = testurl)


# Trigger a client error in generator # Trigger a client error in generator
start = datetime_tz.datetime_tz.smartparse("20120323T2000") start = datetime_tz.datetime_tz.smartparse("20120323T2000")
@@ -332,7 +335,7 @@ class TestClient(object):
# Pokes around in client.http internals a bit to look at the # Pokes around in client.http internals a bit to look at the
# response headers. # response headers.


client = nilmdb.Client(url = testurl)
client = nilmdb.client.Client(url = testurl)
http = client.http http = client.http


# Use a warning rather than returning a test failure for the # Use a warning rather than returning a test failure for the
@@ -371,7 +374,7 @@ class TestClient(object):
# Try both with and without posting JSON # Try both with and without posting JSON
for post_json in (False, True): for post_json in (False, True):
# Basic Unicode tests # Basic Unicode tests
client = nilmdb.Client(url = testurl, post_json = post_json)
client = nilmdb.client.Client(url = testurl, post_json = post_json)


# Delete streams that exist # Delete streams that exist
for stream in client.stream_list(): for stream in client.stream_list():
@@ -420,20 +423,20 @@ class TestClient(object):
"probably not closing properly.") "probably not closing properly.")
if test == 1: if test == 1:
# explicit close # explicit close
client = nilmdb.Client(url = testurl)
client = nilmdb.client.Client(url = testurl)
with assert_raises(ClientError) as e: with assert_raises(ClientError) as e:
client.stream_remove("/newton/prep", 123, 120) client.stream_remove("/newton/prep", 123, 120)
client.close() # remove this to see the failure client.close() # remove this to see the failure
elif test == 2: elif test == 2:
# use the context manager # use the context manager
with nilmdb.Client(url = testurl) as c:
with nilmdb.client.Client(url = testurl) as c:
with assert_raises(ClientError) as e: with assert_raises(ClientError) as e:
c.stream_remove("/newton/prep", 123, 120) c.stream_remove("/newton/prep", 123, 120)


def test_client_10_context(self): def test_client_10_context(self):
# Test using the client's stream insertion context manager to # Test using the client's stream insertion context manager to
# insert data. # insert data.
client = nilmdb.Client(testurl)
client = nilmdb.client.Client(testurl)


client.stream_create("/context/test", "uint16_1") client.stream_create("/context/test", "uint16_1")
with client.stream_insert_context("/context/test") as ctx: with client.stream_insert_context("/context/test") as ctx:
@@ -513,7 +516,7 @@ class TestClient(object):
# Empty intervals are ok! If recording detection events # Empty intervals are ok! If recording detection events
# by inserting rows into the database, we want to be able to # by inserting rows into the database, we want to be able to
# have an interval where no events occurred. Test them here. # have an interval where no events occurred. Test them here.
client = nilmdb.Client(testurl)
client = nilmdb.client.Client(testurl)
client.stream_create("/empty/test", "uint16_1") client.stream_create("/empty/test", "uint16_1")


def info(): def info():
@@ -607,7 +610,7 @@ class TestClient(object):
# Check that connections are persistent when they should be. # Check that connections are persistent when they should be.
# This is pretty hard to test; we have to poke deep into # This is pretty hard to test; we have to poke deep into
# the Requests library. # the Requests library.
with nilmdb.Client(url = testurl) as c:
with nilmdb.client.Client(url = testurl) as c:
def connections(): def connections():
try: try:
poolmanager = c.http._last_response.connection.poolmanager poolmanager = c.http._last_response.connection.poolmanager


+ 7
- 6
tests/test_cmdline.py View File

@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-


import nilmdb
import nilmdb.server

from nilmdb.utils.printf import * from nilmdb.utils.printf import *
import nilmdb.cmdline import nilmdb.cmdline
from nilmdb.utils import datetime_tz from nilmdb.utils import datetime_tz
@@ -22,14 +23,14 @@ testdb = "tests/cmdline-testdb"
def server_start(max_results = None, bulkdata_args = {}): def server_start(max_results = None, bulkdata_args = {}):
global test_server, test_db global test_server, test_db
# Start web app on a custom port # Start web app on a custom port
test_db = nilmdb.utils.serializer_proxy(nilmdb.NilmDB)(
test_db = nilmdb.utils.serializer_proxy(nilmdb.server.NilmDB)(
testdb, sync = False, testdb, sync = False,
max_results = max_results, max_results = max_results,
bulkdata_args = bulkdata_args) bulkdata_args = bulkdata_args)
test_server = nilmdb.Server(test_db, host = "127.0.0.1",
port = 32180, stoppable = False,
fast_shutdown = True,
force_traceback = False)
test_server = nilmdb.server.Server(test_db, host = "127.0.0.1",
port = 32180, stoppable = False,
fast_shutdown = True,
force_traceback = False)
test_server.start(blocking = False) test_server.start(blocking = False)


def server_stop(): def server_stop():


+ 12
- 12
tests/test_nilmdb.py View File

@@ -1,4 +1,4 @@
import nilmdb
import nilmdb.server


from nose.tools import * from nose.tools import *
from nose.tools import assert_raises from nose.tools import assert_raises
@@ -29,11 +29,11 @@ class Test00Nilmdb(object): # named 00 so it runs first
recursive_unlink(testdb) recursive_unlink(testdb)


with assert_raises(IOError): with assert_raises(IOError):
nilmdb.NilmDB("/nonexistant-db/foo")
nilmdb.server.NilmDB("/nonexistant-db/foo")


db = nilmdb.NilmDB(testdb)
db = nilmdb.server.NilmDB(testdb)
db.close() db.close()
db = nilmdb.NilmDB(testdb, sync=False)
db = nilmdb.server.NilmDB(testdb, sync=False)
db.close() db.close()


# test timer, just to get coverage # test timer, just to get coverage
@@ -46,7 +46,7 @@ class Test00Nilmdb(object): # named 00 so it runs first
in_("test: ", capture.getvalue()) in_("test: ", capture.getvalue())


def test_stream(self): def test_stream(self):
db = nilmdb.NilmDB(testdb, sync=False)
db = nilmdb.server.NilmDB(testdb, sync=False)
eq_(db.stream_list(), []) eq_(db.stream_list(), [])


# Bad path # Bad path
@@ -104,7 +104,7 @@ class Test00Nilmdb(object): # named 00 so it runs first


class TestBlockingServer(object): class TestBlockingServer(object):
def setUp(self): def setUp(self):
self.db = serializer_proxy(nilmdb.NilmDB)(testdb, sync=False)
self.db = serializer_proxy(nilmdb.server.NilmDB)(testdb, sync=False)


def tearDown(self): def tearDown(self):
self.db.close() self.db.close()
@@ -113,11 +113,11 @@ class TestBlockingServer(object):
# Server should fail if the database doesn't have a "_thread_safe" # Server should fail if the database doesn't have a "_thread_safe"
# property. # property.
with assert_raises(KeyError): with assert_raises(KeyError):
nilmdb.Server(object())
nilmdb.server.Server(object())


# Start web app on a custom port # Start web app on a custom port
self.server = nilmdb.Server(self.db, host = "127.0.0.1",
port = 32180, stoppable = True)
self.server = nilmdb.server.Server(self.db, host = "127.0.0.1",
port = 32180, stoppable = True)


# Run it # Run it
event = threading.Event() event = threading.Event()
@@ -145,9 +145,9 @@ class TestServer(object):


def setUp(self): def setUp(self):
# Start web app on a custom port # Start web app on a custom port
self.db = serializer_proxy(nilmdb.NilmDB)(testdb, sync=False)
self.server = nilmdb.Server(self.db, host = "127.0.0.1",
port = 32180, stoppable = False)
self.db = serializer_proxy(nilmdb.server.NilmDB)(testdb, sync=False)
self.server = nilmdb.server.Server(self.db, host = "127.0.0.1",
port = 32180, stoppable = False)
self.server.start(blocking = False) self.server.start(blocking = False)


def tearDown(self): def tearDown(self):


Loading…
Cancel
Save