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.
 
 
 

94 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 > self.args.end:
  40. self.parser.error("start is after end")
  41. if self.args.binary:
  42. if (self.args.bare or self.args.annotate or self.args.markup or
  43. self.args.timestamp_raw or self.args.count):
  44. self.parser.error("--binary cannot be combined with other options")
  45. def cmd_extract(self):
  46. streams = self.client.stream_list(self.args.path)
  47. if len(streams) != 1:
  48. self.die("error getting stream info for path %s", self.args.path)
  49. layout = streams[0][1]
  50. if self.args.timestamp_raw:
  51. time_string = nilmdb.utils.time.timestamp_to_string
  52. else:
  53. time_string = nilmdb.utils.time.timestamp_to_human
  54. if self.args.annotate:
  55. printf("# path: %s\n", self.args.path)
  56. printf("# layout: %s\n", layout)
  57. printf("# start: %s\n", time_string(self.args.start))
  58. printf("# end: %s\n", time_string(self.args.end))
  59. printed = False
  60. if self.args.binary:
  61. printer = sys.stdout.buffer.write
  62. else:
  63. printer = lambda x: print(x.decode('utf-8'))
  64. bare = self.args.bare
  65. count = self.args.count
  66. for dataline in self.client.stream_extract(self.args.path,
  67. self.args.start,
  68. self.args.end,
  69. self.args.count,
  70. self.args.markup,
  71. self.args.binary):
  72. if bare and not count:
  73. # Strip timestamp (first element). Doesn't make sense
  74. # if we are only returning a count.
  75. dataline = b' '.join(dataline.split(b' ')[1:])
  76. printer(dataline)
  77. printed = True
  78. if not printed:
  79. if self.args.annotate:
  80. printf("# no data\n")
  81. return 2
  82. return 0