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.
 
 
 

95 lines
3.8 KiB

  1. from nilmdb.utils.printf import *
  2. import nilmdb.client
  3. import sys
  4. def setup(self, sub):
  5. cmd = sub.add_parser("extract", help="Extract data",
  6. description="""
  7. Extract data from a stream.
  8. """)
  9. cmd.set_defaults(verify = cmd_extract_verify,
  10. handler = cmd_extract)
  11. group = cmd.add_argument_group("Data selection")
  12. group.add_argument("path",
  13. help="Path of stream, e.g. /foo/bar",
  14. ).completer = self.complete.path
  15. group.add_argument("-s", "--start", required=True,
  16. metavar="TIME", type=self.arg_time,
  17. help="Starting timestamp (free-form, inclusive)",
  18. ).completer = self.complete.time
  19. group.add_argument("-e", "--end", required=True,
  20. metavar="TIME", type=self.arg_time,
  21. help="Ending timestamp (free-form, noninclusive)",
  22. ).completer = self.complete.time
  23. group = cmd.add_argument_group("Output format")
  24. group.add_argument("-B", "--binary", action="store_true",
  25. help="Raw binary output")
  26. group.add_argument("-b", "--bare", action="store_true",
  27. help="Exclude timestamps from output lines")
  28. group.add_argument("-a", "--annotate", action="store_true",
  29. help="Include comments with some information "
  30. "about the stream")
  31. group.add_argument("-m", "--markup", action="store_true",
  32. help="Include comments with interval starts and ends")
  33. group.add_argument("-T", "--timestamp-raw", action="store_true",
  34. help="Show raw timestamps in annotated information")
  35. group.add_argument("-c", "--count", action="store_true",
  36. help="Just output a count of matched data points")
  37. return cmd
  38. def cmd_extract_verify(self):
  39. if self.args.start is not None and self.args.end is not None:
  40. if self.args.start > self.args.end:
  41. self.parser.error("start is after end")
  42. if self.args.binary:
  43. if (self.args.bare or self.args.annotate or self.args.markup or
  44. self.args.timestamp_raw or self.args.count):
  45. self.parser.error("--binary cannot be combined with other options")
  46. def cmd_extract(self):
  47. streams = self.client.stream_list(self.args.path)
  48. if len(streams) != 1:
  49. self.die("error getting stream info for path %s", self.args.path)
  50. layout = streams[0][1]
  51. if self.args.timestamp_raw:
  52. time_string = nilmdb.utils.time.timestamp_to_string
  53. else:
  54. time_string = nilmdb.utils.time.timestamp_to_human
  55. if self.args.annotate:
  56. printf("# path: %s\n", self.args.path)
  57. printf("# layout: %s\n", layout)
  58. printf("# start: %s\n", time_string(self.args.start))
  59. printf("# end: %s\n", time_string(self.args.end))
  60. printed = False
  61. if self.args.binary:
  62. printer = sys.stdout.write
  63. else:
  64. printer = print
  65. bare = self.args.bare
  66. count = self.args.count
  67. for dataline in self.client.stream_extract(self.args.path,
  68. self.args.start,
  69. self.args.end,
  70. self.args.count,
  71. self.args.markup,
  72. self.args.binary):
  73. if bare and not count:
  74. # Strip timestamp (first element). Doesn't make sense
  75. # if we are only returning a count.
  76. dataline = ' '.join(dataline.split(' ')[1:])
  77. printer(dataline)
  78. printed = True
  79. if not printed:
  80. if self.args.annotate:
  81. printf("# no data\n")
  82. return 2
  83. return 0