def test_read_all_write_one(self):
        readers = []
        for tmpfilename in self.tmpfilenames:
            readers.append(TextFileReader(tmpfilename, interval=0.2))

        transforms = [PrefixTransform('prefix_1'), PrefixTransform('prefix_2')]

        outfilename = self.tmpdirname + '/f_out'
        writers = [TextFileWriter(outfilename)]

        listener = Listener(readers, transforms, writers)
        listener.run()

        out_lines = []
        with open(outfilename, 'r') as f:
            for line in f.readlines():
                out_lines.append(line.rstrip())
        out_lines.sort()

        source_lines = []
        for f in SAMPLE_DATA:
            source_lines.extend(
                ['prefix_2 prefix_1 ' + f for f in SAMPLE_DATA[f]])
        source_lines.sort()

        logging.debug('out: %s, source: %s', out_lines, source_lines)
        self.assertEqual(out_lines, source_lines)
    def test_read_one_write_all(self):
        readers = TextFileReader(self.tmpfilenames[0])

        outfilenames = [
            self.tmpdirname + '/' + f for f in ['f1_out', 'f2_out', 'f3_out']
        ]
        writers = [TextFileWriter(ofn) for ofn in outfilenames]

        listener = Listener(readers=readers, writers=writers)
        listener.run()

        for ofn in outfilenames:
            line_num = 0
            with open(ofn, 'r') as f:
                for line in f.readlines():
                    self.assertEqual(SAMPLE_DATA['f1'][line_num],
                                     line.rstrip())
                    line_num += 1
    def test_check_format(self):

        # This should be okay - for now it warns us that check_format
        # is not implemented for ComposedWriter
        with self.assertLogs(logging.getLogger(), logging.WARNING):
            with self.assertRaises(ValueError):
                Listener([
                    TextFileReader(self.tmpfilenames[0]),
                    TextFileReader(self.tmpfilenames[1])
                ],
                         check_format=True)
Exemple #4
0
        # Parse out values for user@host:database. We count on
        # --database_password having been specified somewhere.
        (user, host_db) = new_args.write_database.split('@')
        (host, database) = host_db.split(':')
        writers.append(DatabaseWriter(database=database, host=host,
                                      user=user, password=password))

      if new_args.write_cached_data_server:
        data_server = new_args.write_cached_data_server
        writers.append(CachedDataWriter(data_server=data_server))

    ##########################
    # If we don't have any readers, read from stdin, if we don't have
    # any writers, write to stdout.
    if not readers:
      readers.append(TextFileReader())
    if not writers:
      writers.append(TextFileWriter())

    ##########################
    # Now that we've got our readers, transforms and writers defined,
    # create the Listener.
    listener = Listener(readers=readers, transforms=transforms, writers=writers,
                        stderr_writers=stderr_writers,
                        interval=all_args.interval,
                        check_format=all_args.check_format)

  ############################
  # Whichever way we created the listener, run it.
  listener.run()