from nilmdb.utils.printf import * import fnmatch import argparse from argparse import ArgumentDefaultsHelpFormatter as def_form def setup(self, sub): cmd = sub.add_parser("list", help="List streams", formatter_class = def_form, description=""" List streams available in the database, optionally filtering by layout or path. Wildcards are accepted. """) cmd.set_defaults(verify = cmd_list_verify, handler = cmd_list) group = cmd.add_argument_group("Stream filtering") group.add_argument("-p", "--path", metavar="PATH", default="*", help="Match only this path (-p can be omitted)") group.add_argument("path_positional", default="*", nargs="?", help=argparse.SUPPRESS) group.add_argument("-l", "--layout", default="*", help="Match only this stream layout") group = cmd.add_argument_group("Interval details") group.add_argument("-d", "--detail", action="store_true", help="Show available data time intervals") group.add_argument("-T", "--timestamp-raw", action="store_true", help="Show raw timestamps in time intervals") group.add_argument("-s", "--start", metavar="TIME", type=self.arg_time, help="Starting timestamp (free-form, inclusive)") group.add_argument("-e", "--end", metavar="TIME", type=self.arg_time, help="Ending timestamp (free-form, noninclusive)") def cmd_list_verify(self): # A hidden "path_positional" argument lets the user leave off the # "-p" when specifying the path. Handle it here. got_opt = self.args.path != "*" got_pos = self.args.path_positional != "*" if got_pos: if got_opt: self.parser.error("too many paths specified") else: self.args.path = self.args.path_positional 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_list(self): """List available streams""" streams = self.client.stream_list() if self.args.timestamp_raw: time_string = repr else: time_string = self.time_string for (path, layout) in streams: if not (fnmatch.fnmatch(path, self.args.path) and fnmatch.fnmatch(layout, self.args.layout)): continue printf("%s %s\n", path, layout) if not self.args.detail: continue printed = False for (start, end) in self.client.stream_intervals(path, self.args.start, self.args.end): printf(" [ %s -> %s ]\n", time_string(start), time_string(end)) printed = True if not printed: printf(" (no intervals)\n")