Compare commits

...

4 Commits

Author SHA1 Message Date
00e6ba1124 Avoid ENOENT in nilmdb.utils.diskusage.du
ENOENT might show up if we're actively deleting files in the nilmdb
thread while trying to read available space from e.g. the server
thread.
2013-04-10 22:25:22 -04:00
01029230c9 Tweaks to sorting 2013-04-10 19:59:38 -04:00
ecc4e5ef9d Improve test coverage 2013-04-10 19:08:05 -04:00
23f31c472b Split sort_streams_nicely into separate file 2013-04-10 19:07:58 -04:00
5 changed files with 47 additions and 18 deletions

View File

@@ -6,7 +6,6 @@ import nilmdb.utils
import nilmdb.client.httpclient import nilmdb.client.httpclient
from nilmdb.client.errors import ClientError from nilmdb.client.errors import ClientError
import re
import time import time
import simplejson as json import simplejson as json
import contextlib import contextlib
@@ -66,12 +65,8 @@ class Client(object):
params["layout"] = layout params["layout"] = layout
if extended: if extended:
params["extended"] = 1 params["extended"] = 1
def sort_streams_nicely(x): streams = self.http.get("stream/list", params)
"""Human-friendly sort (/stream/2 before /stream/10)""" return nilmdb.utils.sort.sort_human(streams, key = lambda s: s[0])
num = lambda t: int(t) if t.isdigit() else t
key = lambda k: [ num(c) for c in re.split('([0-9]+)', k[0]) ]
return sorted(x, key = key)
return sort_streams_nicely(self.http.get("stream/list", params))
def stream_get_metadata(self, path, keys = None): def stream_get_metadata(self, path, keys = None):
params = { "path": path } params = { "path": path }

View File

@@ -13,3 +13,4 @@ import nilmdb.utils.time
import nilmdb.utils.iterator import nilmdb.utils.iterator
import nilmdb.utils.interval import nilmdb.utils.interval
import nilmdb.utils.lock import nilmdb.utils.lock
import nilmdb.utils.sort

View File

@@ -1,4 +1,5 @@
import os import os
import errno
from math import log from math import log
def human_size(num): def human_size(num):
@@ -16,10 +17,17 @@ def human_size(num):
return '1 byte' return '1 byte'
def du(path): def du(path):
"""Like du -sb, returns total size of path in bytes.""" """Like du -sb, returns total size of path in bytes. Ignore
errors that might occur if we encounter broken symlinks or
files in the process of being removed."""
try:
size = os.path.getsize(path) size = os.path.getsize(path)
if os.path.isdir(path): if os.path.isdir(path):
for thisfile in os.listdir(path): for thisfile in os.listdir(path):
filepath = os.path.join(path, thisfile) filepath = os.path.join(path, thisfile)
size += du(filepath) size += du(filepath)
return size return size
except OSError as e: # pragma: no cover
if e.errno != errno.ENOENT:
raise
return 0

18
nilmdb/utils/sort.py Normal file
View File

@@ -0,0 +1,18 @@
import re
def sort_human(items, key = None):
"""Human-friendly sort (/stream/2 before /stream/10)"""
def to_num(val):
try:
return int(val)
except Exception:
return val
def human_key(text):
if key:
text = key(text)
# Break into character and numeric chunks.
chunks = re.split(r'([0-9]+)', text)
return [ to_num(c) for c in chunks ]
return sorted(items, key = human_key)

View File

@@ -105,16 +105,19 @@ class TestClient(object):
client.http.post("/stream/list") client.http.post("/stream/list")
client = nilmdb.client.Client(url = testurl) client = nilmdb.client.Client(url = testurl)
# Create three streams # Create four streams
client.stream_create("/newton/prep", "float32_8") client.stream_create("/newton/prep", "float32_8")
client.stream_create("/newton/raw", "uint16_6") client.stream_create("/newton/raw", "uint16_6")
client.stream_create("/newton/zzz/rawnotch", "uint16_9") client.stream_create("/newton/zzz/rawnotch2", "uint16_9")
client.stream_create("/newton/zzz/rawnotch11", "uint16_9")
# Verify we got 3 streams # Verify we got 4 streams in the right order
eq_(client.stream_list(), [ ["/newton/prep", "float32_8"], eq_(client.stream_list(), [ ["/newton/prep", "float32_8"],
["/newton/raw", "uint16_6"], ["/newton/raw", "uint16_6"],
["/newton/zzz/rawnotch", "uint16_9"] ["/newton/zzz/rawnotch2", "uint16_9"],
["/newton/zzz/rawnotch11", "uint16_9"]
]) ])
# Match just one type or one path # Match just one type or one path
eq_(client.stream_list(layout="uint16_6"), eq_(client.stream_list(layout="uint16_6"),
[ ["/newton/raw", "uint16_6"] ]) [ ["/newton/raw", "uint16_6"] ])
@@ -327,6 +330,10 @@ class TestClient(object):
2525.169921875, 8350.83984375, 3724.699951171875, 2525.169921875, 8350.83984375, 3724.699951171875,
1355.3399658203125, 2039.0)) 1355.3399658203125, 2039.0))
# Just get some coverage
with assert_raises(ClientError) as e:
client.http.post("/stream/remove", { "path": "/none" })
client.close() client.close()
def test_client_06_generators(self): def test_client_06_generators(self):