From 960482827259034749146423a1e3381cc3e93690 Mon Sep 17 00:00:00 2001 From: Jeroen Janssen Date: Tue, 14 Feb 2012 19:03:02 +0100 Subject: [PATCH] Finished options refactor. --- dumbo/cmd.py | 10 ++++++++++ dumbo/core.py | 2 +- dumbo/util.py | 8 +++++--- tests/testexamples.py | 18 +++++++++--------- 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/dumbo/cmd.py b/dumbo/cmd.py index be0047f..f0f045d 100644 --- a/dumbo/cmd.py +++ b/dumbo/cmd.py @@ -68,12 +68,15 @@ def start(prog, opts, stdout=sys.stdout, stderr=sys.stderr): + + opts = Options(opts) opts += Options(configopts('common')) opts += Options(configopts('start')) pyenv = envdef('PYTHONPATH', opts['libegg'], shortcuts=dict(configopts('eggs', prog)), extrapaths=sys.path) + if not opts['prog']: opts.add('prog', prog) @@ -82,6 +85,7 @@ def start(prog, print >> sys.stderr, 'ERROR:', prog, 'does not exist' return 1 prog = '-m ' + prog + return execute("%s %s" % (sys.executable, prog), opts, pyenv, @@ -91,36 +95,42 @@ def start(prog, def cat(path, opts): + opts = Options(opts) opts += Options(configopts('common')) opts += Options(configopts('cat')) return create_filesystem(opts).cat(path, opts) def ls(path, opts): + opts = Options(opts) opts += Options(configopts('common')) opts += Options(configopts('ls')) return create_filesystem(opts).ls(path, opts) def exists(path, opts): + opts = Options(opts) opts += Options(configopts('common')) opts += Options(configopts('exists')) return create_filesystem(opts).exists(path, opts) def rm(path, opts): + opts = Options(opts) opts += Options(configopts('common')) opts += Options(configopts('rm')) return create_filesystem(opts).rm(path, opts) def put(path1, path2, opts): + opts = Options(opts) opts += Options(configopts('common')) opts += Options(configopts('put')) return create_filesystem(opts).put(path1, path2, opts) def get(path1, path2, opts): + opts = Options(opts) opts += Options(configopts('common')) opts += Options(configopts('get')) return create_filesystem(opts).get(path1, path2, opts) diff --git a/dumbo/core.py b/dumbo/core.py index dc55db0..8f693f1 100644 --- a/dumbo/core.py +++ b/dumbo/core.py @@ -361,7 +361,7 @@ def run(mapper, for output in dumpcode(inputs): print '\t'.join(output) else: - opts = opts or Options() + opts = Options(opts) if type(mapper) == str: opts.add('mapper', mapper) elif hasattr(mapper, 'opts'): diff --git a/dumbo/util.py b/dumbo/util.py index 33047d2..a57166d 100644 --- a/dumbo/util.py +++ b/dumbo/util.py @@ -81,8 +81,8 @@ def loadtext(inputs): class Options(object): """ - Class that represent a a set of options. A key can hold - more than a value and key are stored in lowercase. + Class that represents a set of options. A key can hold + more than one value and keys are stored in lowercase. """ def __init__(self, seq=None, **kwargs): @@ -93,7 +93,9 @@ def __init__(self, seq=None, **kwargs): - seq: a list of (key, value) pairs """ self._opts = defaultdict(set) - options = (seq or []) + kwargs.items() + options = seq or [] + for k, v in kwargs.iteritems(): + self.add(k, v) for k, v in options: self.add(k, v) diff --git a/tests/testexamples.py b/tests/testexamples.py index a54b6af..bc201f6 100644 --- a/tests/testexamples.py +++ b/tests/testexamples.py @@ -27,7 +27,7 @@ def tearDown(self): def testwordcount(self): opts = self.common_opts - opts += Options([('input', self.exdir+'brian.txt'), ('output', self.outfile)]) + opts += [('input', self.exdir+'brian.txt'), ('output', self.outfile)] retval = cmd.start(self.exdir+'wordcount.py', opts, stdout=self.logfile, stderr=self.logfile) self.assertEqual(0, retval) @@ -36,8 +36,8 @@ def testwordcount(self): def testoowordcount(self): opts = self.common_opts - opts += Options([('excludes', self.exdir+'excludes.txt'), - ('input', self.exdir+'brian.txt'), ('output', self.outfile)]) + opts += [('excludes', self.exdir+'excludes.txt'), + ('input', self.exdir+'brian.txt'), ('output', self.outfile)] retval = cmd.start(self.exdir+'oowordcount.py', opts, stdout=self.logfile, stderr=self.logfile) self.assertEquals(0, retval) @@ -46,7 +46,7 @@ def testoowordcount(self): def testaltwordcount(self): opts = self.common_opts - opts += Options([('input', self.exdir+'brian.txt'), ('output', self.outfile)]) + opts += [('input', self.exdir+'brian.txt'), ('output', self.outfile)] retval = cmd.start(self.exdir+'altwordcount.py', opts, stdout=self.logfile, stderr=self.logfile) self.assertEqual(0, retval) @@ -55,7 +55,7 @@ def testaltwordcount(self): def testitertwice(self): opts = self.common_opts - opts += Options([('input', self.exdir+'brian.txt'), ('output', self.outfile)]) + opts += [('input', self.exdir+'brian.txt'), ('output', self.outfile)] retval = cmd.start(self.exdir+'itertwice.py', opts, stdout=self.logfile, stderr=self.logfile) self.assertEqual(0, retval) @@ -64,9 +64,9 @@ def testitertwice(self): def testjoin(self): opts = self.common_opts - opts += Options([('input', self.exdir+'hostnames.txt'), + opts += [('input', self.exdir+'hostnames.txt'), ('input', self.exdir+'logs.txt'), - ('output', self.outfile)]) + ('output', self.outfile)] retval = cmd.start(self.exdir+'join.py', opts, stdout=self.logfile, stderr=self.logfile) self.assertEqual(0, retval) @@ -75,9 +75,9 @@ def testjoin(self): def testmulticount(self): opts = self.common_opts - opts += Options([('input', self.exdir+'brian.txt'), + opts += [('input', self.exdir+'brian.txt'), ('input', self.exdir+'eno.txt'), - ('output', self.outfile)]) + ('output', self.outfile)] retval = cmd.start(self.exdir+'multicount.py', opts, stdout=self.logfile, stderr=self.logfile) self.assertEqual(0, retval)