You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

78 lines
3.0 KiB

  1. from nilmdb.utils.printf import *
  2. import fnmatch
  3. import argparse
  4. from argparse import ArgumentDefaultsHelpFormatter as def_form
  5. def setup(self, sub):
  6. cmd = sub.add_parser("list", help="List streams",
  7. formatter_class = def_form,
  8. description="""
  9. List streams available in the database,
  10. optionally filtering by layout or path. Wildcards
  11. are accepted.
  12. """)
  13. cmd.set_defaults(verify = cmd_list_verify,
  14. handler = cmd_list)
  15. group = cmd.add_argument_group("Stream filtering")
  16. group.add_argument("-p", "--path", metavar="PATH", default="*",
  17. help="Match only this path (-p can be omitted)")
  18. group.add_argument("path_positional", default="*",
  19. nargs="?", help=argparse.SUPPRESS)
  20. group.add_argument("-l", "--layout", default="*",
  21. help="Match only this stream layout")
  22. group = cmd.add_argument_group("Interval details")
  23. group.add_argument("-d", "--detail", action="store_true",
  24. help="Show available data time intervals")
  25. group.add_argument("-T", "--timestamp-raw", action="store_true",
  26. help="Show raw timestamps in time intervals")
  27. group.add_argument("-s", "--start",
  28. metavar="TIME", type=self.arg_time,
  29. help="Starting timestamp (free-form, inclusive)")
  30. group.add_argument("-e", "--end",
  31. metavar="TIME", type=self.arg_time,
  32. help="Ending timestamp (free-form, noninclusive)")
  33. def cmd_list_verify(self):
  34. # A hidden "path_positional" argument lets the user leave off the
  35. # "-p" when specifying the path. Handle it here.
  36. got_opt = self.args.path != "*"
  37. got_pos = self.args.path_positional != "*"
  38. if got_pos:
  39. if got_opt:
  40. self.parser.error("too many paths specified")
  41. else:
  42. self.args.path = self.args.path_positional
  43. if self.args.start is not None and self.args.end is not None:
  44. if self.args.start >= self.args.end:
  45. self.parser.error("start must precede end")
  46. def cmd_list(self):
  47. """List available streams"""
  48. streams = self.client.stream_list()
  49. if self.args.timestamp_raw:
  50. time_string = repr
  51. else:
  52. time_string = self.time_string
  53. for (path, layout) in streams:
  54. if not (fnmatch.fnmatch(path, self.args.path) and
  55. fnmatch.fnmatch(layout, self.args.layout)):
  56. continue
  57. printf("%s %s\n", path, layout)
  58. if not self.args.detail:
  59. continue
  60. printed = False
  61. for (start, end) in self.client.stream_intervals(path, self.args.start,
  62. self.args.end):
  63. printf(" [ %s -> %s ]\n", time_string(start), time_string(end))
  64. printed = True
  65. if not printed:
  66. printf(" (no intervals)\n")