def script(body, args=None): script = Runner() script.add(ArgumentParser) script.add(common_arguments) if args: script.add(args, requires=ArgumentParser) script.add(parse_args) script.add(load_config, requires=attr(Namespace, 'config'), returns='config') script.add(adjust_config) script.add( setup_logging, requires( log_path=item('config', 'log_path'), quiet=attr(Namespace, 'quiet'), verbose=attr(Namespace, 'verbose'), )) script.add(log_details) script.add( handle_database, requires( name=item('config', 'db', 'name'), user=item('config', 'db', 'user'), password=item('config', 'db', 'password'), )) script.add(body) return script
def make_runner(do): runner = Runner(ArgumentParser) runner.add(options, requires=ArgumentParser) runner.add(parse_args, requires=ArgumentParser) runner.add(parse_config, requires=Namespace) runner.add( setup_logging, requires(log_path=item('config', 'log'), quiet=attr(Namespace, 'quiet'), verbose=attr(Namespace, 'verbose'))) runner.add(DatabaseHandler, requires=item('config', 'db')) runner.add( do, requires(attr(DatabaseHandler, 'conn'), attr(Namespace, 'path'))) return runner
def make_runner(do): runner = Runner(ArgumentParser) runner.add(options, ArgumentParser) runner.add(parse_args, last(ArgumentParser)) runner.add(parse_config, first(Namespace)) runner.add(setup_logging, log_path = item(first(Config), 'log'), quiet = attr(first(Namespace), 'quiet'), verbose = attr(first(Namespace), 'verbose')) runner.add(DatabaseHandler, item(Config, 'db')) runner.add(do, attr(DatabaseHandler, 'conn'), attr(Namespace, 'path')) return runner
def make_runner(do): runner = Runner(ArgumentParser) runner.add(options, requires=ArgumentParser) runner.add(parse_args, requires=ArgumentParser) runner.add(parse_config, requires=Namespace) runner.add(setup_logging, requires( log_path = item('config', 'log'), quiet = attr(Namespace, 'quiet'), verbose = attr(Namespace, 'verbose') )) runner.add(DatabaseHandler, requires=item('config', 'db')) runner.add( do, requires(attr(DatabaseHandler, 'conn'), attr(Namespace, 'path')) ) return runner
def test_when_how(self): w = first(attr(Type1, 'foo')) compare(repr(w), 'first(Type1.foo)') self.assertTrue(isinstance(w, when)) h = w.type compare(h.type, Type1) compare(h.names, ('foo', )) self.assertTrue(isinstance(h, how))
def test_how_when(self): h = attr(first(Type1), 'foo') compare(repr(h), 'first(Type1).foo') compare(h.names, ('foo',), ) self.assertTrue(isinstance(h, how)) w = h.type self.assertTrue(isinstance(w, when)) compare(w.type, Type1)
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)
def test_nested(self): class T(object): foo = dict(baz='bar') m = Mock() def job1(): m.job1() return T() def job2(obj): m.job2(obj) runner = Runner() runner.add(job1) runner.add(job2, item(attr(T, 'foo'), 'baz')) runner() compare([ call.job1(), call.job2('bar'), ], m.mock_calls)
def test_attr(self): class T(object): foo = 'bar' m = Mock() def job1(): m.job1() return T() def job2(obj): m.job2(obj) runner = Runner() runner.add(job1) runner.add(job2, attr(T, 'foo')) runner() compare([ call.job1(), call.job2('bar'), ], m.mock_calls)
def test_attr_multiple(self): class T2: bar = 'baz' class T: foo = T2() m = Mock() def job1(): m.job1() return T() def job2(obj): m.job2(obj) runner = Runner() runner.add(job1) runner.add(job2, attr(T, 'foo', 'bar')) runner() compare([ call.job1(), call.job2('baz'), ], m.mock_calls)
def test_how_when(self): m = Mock() class T(dict): foo = 'bar' @requires(item(first(T), 'baz')) def f_t(o): m.f_t(o) @requires(T) def n_t(o): m.n_t(o.__class__) @requires(attr(last(T), 'foo')) def l_t(o): m.l_t(o) def make_t(): t = T() t['baz'] = 'bob' return t Runner(l_t, n_t, f_t, make_t)() compare([ call.f_t('bob'), call.n_t(T), call.l_t('bar'), ], m.mock_calls)
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())) conn.commit() log.info('Successfully added %r', filename)
def test_attr(self): o = attr(Type1, 'the secret') compare(repr(o), "Type1.the secret") compare(o.type, Type1) compare(o.names, ('the secret', )) self.assertTrue(isinstance(o, how))
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_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()
from .components import (common_arguments, parse_args, load_config, adjust_config, setup_logging, handle_database, log_details) from argparse import ArgumentParser, Namespace from mush import Runner, attr, item, requires script = Runner() script.add(ArgumentParser) script.add(common_arguments) script.add_label('args') script.add(parse_args) script.add(load_config, requires=attr(Namespace, 'config'), returns='config') script.add(adjust_config) script.add_label('adjust_config') script.add( setup_logging, requires( log_path=item('config', 'log_path'), quiet=attr(Namespace, 'quiet'), verbose=attr(Namespace, 'verbose'), )) script.add(log_details) script.add( handle_database, requires(
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())) conn.commit() log.info('Successfully added %r', filename)