def run_with(main, conn, argv): runner = Runner(ArgumentParser) runner.extend(main.clone(added_using='args')) runner.add(lambda parser: parser.parse_args(argv), requires=ArgumentParser) runner.add(lambda: conn) runner.extend(main.clone(start_label='body')) runner()
def test_extend_with_runners(self): m = Mock() class T1(object): pass class T2(object): pass t1 = T1() t2 = T2() def job1(): m.job1() return t1 @requires(T1) def job2(obj): m.job2(obj) return t2 @requires(T2) def job3(obj): m.job3(obj) runner1 = Runner(job1) runner2 = Runner(job2) runner3 = Runner(job3) runner = Runner(runner1) runner.extend(runner2, runner3) runner() compare([ call.job1(), call.job2(t1), call.job3(t2), ], m.mock_calls)
class DatabaseHandler: def __init__(self, db_path): self.conn = sqlite3.connect(db_path) def __enter__(self): return self def __exit__(self, type, obj, tb): if type: log.exception('Something went wrong') self.conn.rollback() base_runner = Runner(ArgumentParser) base_runner.add(base_options, label='args') base_runner.extend(parse_args, parse_config) base_runner.add( setup_logging, requires(log_path=item('config', 'log'), quiet=attr(Namespace, 'quiet'), verbose=attr(Namespace, 'verbose'))) def args(parser): parser.add_argument('path', help='Path to the file to process') def do(conn, path): filename = os.path.basename(path) with open(path) as source: conn.execute('insert into notes values (?, ?)',
def test_debug(self): class T1(object): pass class T2(object): pass class T3(object): pass def makes_t1(): pass @requires(T1) def makes_t2(obj): pass @requires(T2) def makes_t3(obj): pass def user(obj1, obj2): pass expected = '''\ Added {makes_t1} to 'normal' period for {nonetype} with Requirements() Current call order: For {nonetype}: normal: {makes_t1} requires Requirements() Added {makes_t2} to 'normal' period for {T1} with Requirements(T1) Current call order: For {nonetype}: normal: {makes_t1} requires Requirements() For {T1}: normal: {makes_t2} requires Requirements(T1) Added {makes_t3} to 'normal' period for {T2} with Requirements(T2) Current call order: For {nonetype}: normal: {makes_t1} requires Requirements() For {T1}: normal: {makes_t2} requires Requirements(T1) For {T2}: normal: {makes_t3} requires Requirements(T2) Added {user} to 'normal' period for {T3} with Requirements(T3, T1) Current call order: For {nonetype}: normal: {makes_t1} requires Requirements() For {T1}: normal: {makes_t2} requires Requirements(T1) For {T2}: normal: {makes_t3} requires Requirements(T2) For {T3}: normal: {user} requires Requirements(T3, T1) '''.format(nonetype=repr(None.__class__), makes_t1=repr(makes_t1), makes_t2=repr(makes_t2), makes_t3=repr(makes_t3), user=repr(user), T1=repr(T1), T2=repr(T2), T3=repr(T3)) with OutputCapture() as output: runner1 = Runner(makes_t1, debug=True) runner1.extend(makes_t2, makes_t3) runner1.add(user, T3, T1) compare(expected, output.captured) actual = StringIO() runner2 = Runner(makes_t1, debug=actual) runner2.extend(makes_t2, makes_t3) runner2.add(user, T3, T1) compare(expected, actual.getvalue())
handler.setLevel(logging.DEBUG if verbose else logging.INFO) log.addHandler(handler) class DatabaseHandler: def __init__(self, db_path): self.conn = sqlite3.connect(db_path) def __enter__(self): return self def __exit__(self, type, obj, tb): if type: log.exception('Something went wrong') self.conn.rollback() base_runner = Runner(ArgumentParser) base_runner.add(base_options, label='args') base_runner.extend(parse_args, parse_config) base_runner.add(setup_logging, requires( log_path = item('config', 'log'), quiet = attr(Namespace, 'quiet'), verbose = attr(Namespace, 'verbose') )) def args(parser): parser.add_argument('path', help='Path to the file to process') def do(conn, path): filename = os.path.basename(path) with open(path) as source: conn.execute('insert into notes values (?, ?)', (filename, source.read()))