def test_csvfile(self):
        from pikos.api import csvfile

        # default usage
        recorder = csvfile()
        self.assertIsInstance(recorder, CSVFileRecorder)
        self.assertEqual(recorder._filename, 'monitor_records.csv')
        self.assertIsNone(recorder._handle)

        # with a filter
        recorder = csvfile(self.filename, filter_=my_filter)
        self.assertIsInstance(recorder, CSVFileRecorder)
        self.assertEqual(recorder._filename, self.filename)
        self.assertIs(recorder._filter, my_filter)
Example #2
0
def main():
    description = "Execute the python script inside the pikos monitor " \
                  "context."
    parser = argparse.ArgumentParser(description=description)
    parser.add_argument('monitor', choices=MONITORS.keys(),
                        help='The monitor to use')
    parser.add_argument('-o', '--output', type=argparse.FileType('wb'),
                        help='Output results to a file')
    parser.add_argument('--buffered', action='store_false',
                        help='Use a buffered stream.')
    parser.add_argument('--recording', choices=['screen', 'text', 'csv'],
                        help='Select the type of recording to use.',
                        default='screen'),
    parser.add_argument('--focused-on', help='Provide the module path(s) of '
                        'the method where recording will be focused. '
                        'Comma separated list of importable functions',
                        default=None),
    parser.add_argument('script', help='The script to run.')
    args = parser.parse_args()

    stream = args.output if args.output is not None else sys.stdout

    if args.recording == 'text':
        recorder = textfile()
    elif args.recording == 'csv':
        if not args.buffered:
            msg = ('Unbuffered output is not supported for csv recording.'
                   'The default options for the CSVWriter will be used.')
            warnings.warn(msg)
        recorder = csvfile()
    else:
        recorder = screen()

    script = args.script
    sys.path.insert(0, os.path.dirname(script))
    focus_on = get_focused_on(script, focused_on=args.focused_on)
    monitor = MONITORS[args.monitor](recorder=recorder, focus_on=focus_on)
    run_code_under_monitor(args.script, monitor)