Browse Source

Bring get_stream_info into nilmtools.filter module; use it

tags/nilmtools-1.0
Jim Paris 11 years ago
parent
commit
7e88da3c26
2 changed files with 31 additions and 31 deletions
  1. +3
    -11
      src/decimate_auto.py
  2. +28
    -20
      src/filter.py

+ 3
- 11
src/decimate_auto.py View File

@@ -5,14 +5,6 @@ import nilmtools.decimate
import nilmdb.client
import argparse

def get_stream_info(client, path):
"""Return a nilmtools.filter.StreamInfo object about the given
path, or None if it doesn't exist"""
streams = client.stream_list(path, extended = True)
if len(streams) != 1:
return None
return nilmtools.filter.StreamInfo(client.geturl(), streams[0])

def main(argv = None):
parser = argparse.ArgumentParser(
formatter_class = argparse.RawDescriptionHelpFormatter,
@@ -34,7 +26,7 @@ def main(argv = None):
# Pull out info about the base stream
client = nilmdb.client.Client(args.url)

info = get_stream_info(client, args.path)
info = nilmtools.filter.get_stream_info(client, args.path)
if not info:
raise Exception("path " + args.path + " not found")

@@ -60,7 +52,7 @@ def main(argv = None):
new_path = "%s~decim-%d" % (args.path, factor)

# Create the stream if needed
new_info = get_stream_info(client, new_path)
new_info = nilmtools.filter.get_stream_info(client, new_path)
if not new_info:
print "Creating stream", new_path
client.stream_create(new_path, decimated_type)
@@ -72,7 +64,7 @@ def main(argv = None):
new_path])

# Update info using the newly decimated stream
info = get_stream_info(client, new_path)
info = nilmtools.filter.get_stream_info(client, new_path)

if __name__ == "__main__":
main()

+ 28
- 20
src/filter.py View File

@@ -20,16 +20,16 @@ import numpy as np
import cStringIO

class MissingDestination(Exception):
def __init__(self, src, dest):
def __init__(self, args, src, dest):
self.args = args
self.src = src
self.dest = dest
Exception.__init__(self, "destination path " + dest.path + " not found")

class StreamInfo(object):
def __init__(self, url, info, interhost = False):
def __init__(self, url, info):
self.url = url
self.info = info
self.interhost = interhost
try:
self.path = info[0]
self.layout = info[1]
@@ -43,15 +43,26 @@ class StreamInfo(object):
except IndexError, TypeError:
pass

def string(self, interhost):
"""Return stream info as a string. If interhost is true,
include the host URL."""
if interhost:
return sprintf("[%s] ", self.url) + str(self)
return str(self)

def __str__(self):
"""Print stream info as a string"""
res = ""
if self.interhost:
res = sprintf("[%s] ", self.url)
res += sprintf("%s (%s), %.2fM rows, %.2f hours",
"""Return stream info as a string."""
return sprintf("%s (%s), %.2fM rows, %.2f hours",
self.path, self.layout, self.rows / 1e6,
self.seconds / 3600.0)
return res

def get_stream_info(client, path):
"""Return a StreamInfo object about the given path, or None if it
doesn't exist"""
streams = client.stream_list(path, extended = True)
if len(streams) != 1:
return None
return StreamInfo(client.geturl(), streams[0])

class Filter(object):

@@ -129,20 +140,17 @@ class Filter(object):
raise Exception("source and destination path must be different")

# Open and print info about the streams
src = self._client_src.stream_list(args.srcpath, extended = True)
if len(src) != 1:
self.src = get_stream_info(self._client_src, args.srcpath)
if not self.src:
raise Exception("source path " + args.srcpath + " not found")
self.src = StreamInfo(args.url, src[0], self.interhost)

dest = self._client_dest.stream_list(args.destpath, extended = True)
if len(dest) != 1:
raise MissingDestination(self.src,
StreamInfo(args.dest_url, [args.destpath],
self.interhost))
self.dest = StreamInfo(args.dest_url, dest[0], self.interhost)
self.dest = get_stream_info(self._client_dest, args.destpath)
if not self.dest:
raise MissingDestination(args, self.src,
StreamInfo(args.dest_url, [args.destpath]))

print "Source:", self.src
print " Dest:", self.dest
print "Source:", self.src.string(self.interhost)
print " Dest:", self.dest.string(self.interhost)

if args.dry_run:
for interval in self.intervals():


Loading…
Cancel
Save