def test_clone(self): m = Mock() class T1(object): pass class T2(object): pass def f1(): m.f1() def n1(): m.n1() return T1(), T2() def l1(): m.l1() def t1(obj): m.t1() def t2(obj): m.t2() # original runner1 = Runner() runner1.add(f1, first()) runner1.add(n1) runner1.add(l1, last()) runner1.add(t1, T1) runner1.add(t2, T2) # now clone and add bits def f2(): m.f2() def n2(): m.n2() def l2(): m.l2() def tn(obj): m.tn() runner2 = runner1.clone() runner2.add(f2, first()) runner2.add(n2) runner2.add(l2, last()) # make sure types stay in order runner2.add(tn, T2) # now run both, and make sure we only get what we should runner1() compare([ call.f1(), call.n1(), call.l1(), call.t1(), call.t2(), ], m.mock_calls) m.reset_mock() runner2() compare([ call.f1(), call.f2(), call.n1(), call.n2(), call.l1(), call.l2(), call.t1(), call.t2(), call.tn() ], m.mock_calls)
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())) conn.commit() log.info('Successfully added %r', filename) main = base_runner.clone() main['args'].add(args, requires=ArgumentParser) main.add(DatabaseHandler, requires=item('config', 'db')) main.add(do, requires(attr(DatabaseHandler, 'conn'), attr(Namespace, 'path'))) if __name__ == '__main__': main()
def test_debug_clone(self): runner1 = Runner(debug=object()) runner2 = runner1.clone() self.assertTrue(runner2.debug is runner1.debug)
log.exception('Something went wrong') self.conn.rollback() base_runner = Runner(ArgumentParser, base_options, parse_args, parse_config) base_runner.add(setup_logging, log_path = item(first(Config), 'log'), quiet = attr(first(Namespace), 'quiet'), verbose = attr(first(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())) conn.commit() log.info('Successfully added %r', filename) main = base_runner.clone() main.add(args, ArgumentParser) main.add(DatabaseHandler, item(Config, 'db')) main.add(do, attr(DatabaseHandler, 'conn'), attr(Namespace, 'path')) if __name__ == '__main__': main()
import logging import os from argparse import ArgumentParser, Namespace from mush import attr from psycopg2.extensions import connection as Psycopg2Connection logger = logging.getLogger(__name__) def args(parser: ArgumentParser): parser.add_argument('path', help='Path to the file to process') def insert_note(path: attr(Namespace, 'path'), conn: Psycopg2Connection): filename = os.path.basename(path) with open(path) as source: logger.debug('opened %s to insert as %s', path, filename) cursor = conn.cursor() cursor.execute('insert into notes values (%s, %s)', (filename, source.read())) logger.info('successfully inserted %s', filename) main = script.clone() main['args'].add(args) main.add(insert_note) if __name__ == '__main__': main()