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.
 
 
 

104 lines
4.0 KiB

  1. from nilmdb.utils.printf import *
  2. import nilmdb
  3. import nilmdb.client
  4. import nilmdb.utils.timestamper as timestamper
  5. import sys
  6. def setup(self, sub):
  7. cmd = sub.add_parser("insert", help="Insert data",
  8. description="""
  9. Insert data into a stream.
  10. """)
  11. cmd.set_defaults(handler = cmd_insert)
  12. cmd.add_argument("-q", "--quiet", action='store_true',
  13. help='suppress unnecessary messages')
  14. group = cmd.add_argument_group("Timestamping",
  15. description="""
  16. If timestamps are already provided in the
  17. input date, use --none. Otherwise,
  18. provide --start, or use --filename to
  19. try to deduce timestamps from the file.
  20. Set the TZ environment variable to change
  21. the default timezone.
  22. """)
  23. group.add_argument("-r", "--rate", type=float,
  24. help="""
  25. If needed, rate in Hz (required when using --start)
  26. """)
  27. exc = group.add_mutually_exclusive_group()
  28. exc.add_argument("-s", "--start",
  29. metavar="TIME", type=self.arg_time,
  30. help="Starting timestamp (free-form)")
  31. exc.add_argument("-f", "--filename", action="store_true",
  32. help="""
  33. Use filenames to determine start time
  34. (default, if filenames are provided)
  35. """)
  36. exc.add_argument("-n", "--none", action="store_true",
  37. help="Timestamp is already present, don't add one")
  38. group = cmd.add_argument_group("Required parameters")
  39. group.add_argument("path",
  40. help="Path of stream, e.g. /foo/bar")
  41. group.add_argument("file", nargs="*", default=['-'],
  42. help="File(s) to insert (default: - (stdin))")
  43. def cmd_insert(self):
  44. # Find requested stream
  45. streams = self.client.stream_list(self.args.path)
  46. if len(streams) != 1:
  47. self.die("error getting stream info for path %s", self.args.path)
  48. if self.args.start and len(self.args.file) != 1:
  49. self.die("error: --start can only be used with one input file")
  50. for filename in self.args.file:
  51. if filename == '-':
  52. infile = sys.stdin
  53. else:
  54. try:
  55. infile = open(filename, "r")
  56. except IOError:
  57. self.die("error opening input file %s", filename)
  58. # Build a timestamper for this file
  59. if self.args.none:
  60. ts = timestamper.TimestamperNull(infile)
  61. else:
  62. if self.args.start:
  63. start = self.args.start
  64. else:
  65. try:
  66. start = self.parse_time(filename)
  67. except ValueError:
  68. self.die("error extracting time from filename '%s'",
  69. filename)
  70. if not self.args.rate:
  71. self.die("error: --rate is needed, but was not specified")
  72. rate = self.args.rate
  73. ts = timestamper.TimestamperRate(infile, start, rate)
  74. # Print info
  75. if not self.args.quiet:
  76. printf("Input file: %s\n", filename)
  77. printf("Timestamper: %s\n", str(ts))
  78. # Insert the data
  79. try:
  80. self.client.stream_insert(self.args.path, ts)
  81. except nilmdb.client.Error as e:
  82. # TODO: It would be nice to be able to offer better errors
  83. # here, particularly in the case of overlap, which just shows
  84. # ugly bracketed ranges of 16-digit numbers and a mangled URL.
  85. # Need to consider adding something like e.prettyprint()
  86. # that is smarter about the contents of the error.
  87. self.die("error inserting data: %s", str(e))
  88. return