|
- from nilmdb.utils.printf import *
- import nilmdb
- import nilmdb.client
- import nilmdb.utils.timestamper as timestamper
-
- import sys
-
- def setup(self, sub):
- cmd = sub.add_parser("insert", help="Insert data",
- description="""
- Insert data into a stream.
- """)
- cmd.set_defaults(handler = cmd_insert)
- cmd.add_argument("-q", "--quiet", action='store_true',
- help='suppress unnecessary messages')
-
- group = cmd.add_argument_group("Timestamping",
- description="""
- If timestamps are already provided in the
- input date, use --none. Otherwise,
- provide --start, or use --filename to
- try to deduce timestamps from the file.
-
- Set the TZ environment variable to change
- the default timezone.
- """)
-
- group.add_argument("-r", "--rate", type=float,
- help="""
- If needed, rate in Hz (required when using --start)
- """)
- exc = group.add_mutually_exclusive_group()
- exc.add_argument("-s", "--start",
- metavar="TIME", type=self.arg_time,
- help="Starting timestamp (free-form)")
- exc.add_argument("-f", "--filename", action="store_true",
- help="""
- Use filenames to determine start time
- (default, if filenames are provided)
- """)
- exc.add_argument("-n", "--none", action="store_true",
- help="Timestamp is already present, don't add one")
-
- group = cmd.add_argument_group("Required parameters")
- group.add_argument("path",
- help="Path of stream, e.g. /foo/bar")
- group.add_argument("file", nargs="*", default=['-'],
- help="File(s) to insert (default: - (stdin))")
-
- def cmd_insert(self):
- # Find requested stream
- streams = self.client.stream_list(self.args.path)
- if len(streams) != 1:
- self.die("error getting stream info for path %s", self.args.path)
-
- if self.args.start and len(self.args.file) != 1:
- self.die("error: --start can only be used with one input file")
-
- for filename in self.args.file:
- if filename == '-':
- infile = sys.stdin
- else:
- try:
- infile = open(filename, "r")
- except IOError:
- self.die("error opening input file %s", filename)
-
- # Build a timestamper for this file
- if self.args.none:
- ts = timestamper.TimestamperNull(infile)
- else:
- if self.args.start:
- start = self.args.start
- else:
- try:
- start = self.parse_time(filename)
- except ValueError:
- self.die("error extracting time from filename '%s'",
- filename)
-
- if not self.args.rate:
- self.die("error: --rate is needed, but was not specified")
- rate = self.args.rate
-
- ts = timestamper.TimestamperRate(infile, start, rate)
-
- # Print info
- if not self.args.quiet:
- printf("Input file: %s\n", filename)
- printf("Timestamper: %s\n", str(ts))
-
- # Insert the data
- try:
- self.client.stream_insert(self.args.path, ts)
- except nilmdb.client.Error as e:
- # TODO: It would be nice to be able to offer better errors
- # here, particularly in the case of overlap, which just shows
- # ugly bracketed ranges of 16-digit numbers and a mangled URL.
- # Need to consider adding something like e.prettyprint()
- # that is smarter about the contents of the error.
- self.die("error inserting data: %s", str(e))
-
- return
|