From 7e88da3c26124229a180b71be53146d11cb4daf7 Mon Sep 17 00:00:00 2001 From: Jim Paris Date: Tue, 2 Apr 2013 18:04:28 -0400 Subject: [PATCH] Bring get_stream_info into nilmtools.filter module; use it --- src/decimate_auto.py | 14 +++---------- src/filter.py | 48 ++++++++++++++++++++++++++------------------ 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/src/decimate_auto.py b/src/decimate_auto.py index 1d0a506..ad1f27c 100755 --- a/src/decimate_auto.py +++ b/src/decimate_auto.py @@ -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() diff --git a/src/filter.py b/src/filter.py index f593e6a..0a78bc8 100644 --- a/src/filter.py +++ b/src/filter.py @@ -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():