def test_textfile(self):
        from pikos.api import textfile

        # default usage
        recorder = textfile()
        self.assertIsInstance(recorder, TextFileRecorder)
        self.assertEqual(recorder._filename, 'monitor_records.log')
        self.assertIsNone(recorder._stream)

        # with a filter
        recorder = textfile(self.filename, filter_=my_filter)
        self.assertIsInstance(recorder, TextFileRecorder)
        self.assertEqual(recorder._filename, self.filename)
        self.assertIsNone(recorder._stream)
        self.assertIs(recorder._filter, my_filter)
Beispiel #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)