|
- from __future__ import print_function
- from nilmdb.utils.printf import *
- import nilmdb
- import nilmdb.client
- import sys
-
- def setup(self, sub):
- cmd = sub.add_parser("extract", help="Extract data",
- description="""
- Extract data from a stream.
- """)
- cmd.set_defaults(verify = cmd_extract_verify,
- handler = cmd_extract)
-
- group = cmd.add_argument_group("Data selection")
- group.add_argument("path",
- help="Path of stream, e.g. /foo/bar")
- group.add_argument("-s", "--start", required=True,
- metavar="TIME", type=self.arg_time,
- help="Starting timestamp (free-form, inclusive)")
- group.add_argument("-e", "--end", required=True,
- metavar="TIME", type=self.arg_time,
- help="Ending timestamp (free-form, noninclusive)")
-
- group = cmd.add_argument_group("Output format")
- group.add_argument("-b", "--bare", action="store_true",
- help="Exclude timestamps from output lines")
- group.add_argument("-a", "--annotate", action="store_true",
- help="Include comments with some information "
- "about the stream")
- group.add_argument("-T", "--timestamp-raw", action="store_true",
- help="Show raw timestamps in annotated information")
- group.add_argument("-c", "--count", action="store_true",
- help="Just output a count of matched data points")
-
- def cmd_extract_verify(self):
- if self.args.start is not None and self.args.end is not None:
- if self.args.start > self.args.end:
- self.parser.error("start is after end")
-
- def cmd_extract(self):
- streams = self.client.stream_list(self.args.path)
- if len(streams) != 1:
- self.die("error getting stream info for path %s", self.args.path)
- layout = streams[0][1]
-
- if self.args.timestamp_raw:
- time_string = repr
- else:
- time_string = self.time_string
-
- if self.args.annotate:
- printf("# path: %s\n", self.args.path)
- printf("# layout: %s\n", layout)
- printf("# start: %s\n", time_string(self.args.start))
- printf("# end: %s\n", time_string(self.args.end))
-
- printed = False
- for dataline in self.client.stream_extract(self.args.path,
- self.args.start,
- self.args.end,
- self.args.count):
- if self.args.bare and not self.args.count:
- # Strip timestamp (first element). Doesn't make sense
- # if we are only returning a count.
- dataline = ' '.join(dataline.split(' ')[1:])
- print(dataline)
- printed = True
- if not printed:
- if self.args.annotate:
- printf("# no data\n")
- return 2
-
- return 0
|