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.
 
 
 

70 lines
2.7 KiB

  1. from __future__ import absolute_import
  2. from nilmdb.utils.printf import *
  3. import nilmdb.client
  4. import fnmatch
  5. import argparse
  6. from argparse import ArgumentDefaultsHelpFormatter as def_form
  7. def setup(self, sub):
  8. cmd = sub.add_parser("list", help="List streams",
  9. formatter_class = def_form,
  10. description="""
  11. List streams available in the database,
  12. optionally filtering by layout or path. Wildcards
  13. are accepted.
  14. """)
  15. cmd.set_defaults(preconnect = cmd_preconnect,
  16. handler = cmd_list)
  17. group = cmd.add_argument_group("Stream filtering")
  18. group.add_argument("-p", "--path", metavar="PATH", default="*",
  19. help="Match only this path (-p can be omitted)")
  20. group.add_argument("path_positional", default="*",
  21. nargs="?", help=argparse.SUPPRESS)
  22. group.add_argument("-l", "--layout", default="*",
  23. help="Match only this stream layout")
  24. group = cmd.add_argument_group("Interval details")
  25. group.add_argument("-d", "--detail", action="store_true",
  26. help="Show available data time intervals")
  27. group.add_argument("-s", "--start",
  28. metavar="TIME", type=self.arg_time,
  29. help="Starting timestamp (free-form)")
  30. group.add_argument("-e", "--end",
  31. metavar="TIME", type=self.arg_time,
  32. help="Ending timestamp (free-form)")
  33. def cmd_preconnect(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. def cmd_list(self):
  44. """List available streams"""
  45. streams = self.client.stream_list()
  46. for (path, layout) in streams:
  47. if not (fnmatch.fnmatch(path, self.args.path) and
  48. fnmatch.fnmatch(layout, self.args.layout)):
  49. continue
  50. printf("%s %s\n", path, layout)
  51. if not self.args.detail:
  52. continue
  53. printed = False
  54. for (start, end) in self.client.stream_intervals(path, self.args.start,
  55. self.args.end):
  56. printf(" [ %s -> %s ]\n",
  57. self.time_string(start),
  58. self.time_string(end))
  59. printed = True
  60. if not printed:
  61. printf(" (no intervals)\n")