Skip to content

Commit

Permalink
Finished options refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeroen Janssen committed Feb 14, 2012
1 parent 28d2482 commit 9604828
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
10 changes: 10 additions & 0 deletions dumbo/cmd.py
Expand Up @@ -68,12 +68,15 @@ def start(prog,
opts, opts,
stdout=sys.stdout, stdout=sys.stdout,
stderr=sys.stderr): stderr=sys.stderr):

opts = Options(opts)
opts += Options(configopts('common')) opts += Options(configopts('common'))
opts += Options(configopts('start')) opts += Options(configopts('start'))


pyenv = envdef('PYTHONPATH', opts['libegg'], pyenv = envdef('PYTHONPATH', opts['libegg'],
shortcuts=dict(configopts('eggs', prog)), shortcuts=dict(configopts('eggs', prog)),
extrapaths=sys.path) extrapaths=sys.path)

if not opts['prog']: if not opts['prog']:
opts.add('prog', prog) opts.add('prog', prog)


Expand All @@ -82,6 +85,7 @@ def start(prog,
print >> sys.stderr, 'ERROR:', prog, 'does not exist' print >> sys.stderr, 'ERROR:', prog, 'does not exist'
return 1 return 1
prog = '-m ' + prog prog = '-m ' + prog

return execute("%s %s" % (sys.executable, prog), return execute("%s %s" % (sys.executable, prog),
opts, opts,
pyenv, pyenv,
Expand All @@ -91,36 +95,42 @@ def start(prog,




def cat(path, opts): def cat(path, opts):
opts = Options(opts)
opts += Options(configopts('common')) opts += Options(configopts('common'))
opts += Options(configopts('cat')) opts += Options(configopts('cat'))
return create_filesystem(opts).cat(path, opts) return create_filesystem(opts).cat(path, opts)




def ls(path, opts): def ls(path, opts):
opts = Options(opts)
opts += Options(configopts('common')) opts += Options(configopts('common'))
opts += Options(configopts('ls')) opts += Options(configopts('ls'))
return create_filesystem(opts).ls(path, opts) return create_filesystem(opts).ls(path, opts)




def exists(path, opts): def exists(path, opts):
opts = Options(opts)
opts += Options(configopts('common')) opts += Options(configopts('common'))
opts += Options(configopts('exists')) opts += Options(configopts('exists'))
return create_filesystem(opts).exists(path, opts) return create_filesystem(opts).exists(path, opts)




def rm(path, opts): def rm(path, opts):
opts = Options(opts)
opts += Options(configopts('common')) opts += Options(configopts('common'))
opts += Options(configopts('rm')) opts += Options(configopts('rm'))
return create_filesystem(opts).rm(path, opts) return create_filesystem(opts).rm(path, opts)




def put(path1, path2, opts): def put(path1, path2, opts):
opts = Options(opts)
opts += Options(configopts('common')) opts += Options(configopts('common'))
opts += Options(configopts('put')) opts += Options(configopts('put'))
return create_filesystem(opts).put(path1, path2, opts) return create_filesystem(opts).put(path1, path2, opts)




def get(path1, path2, opts): def get(path1, path2, opts):
opts = Options(opts)
opts += Options(configopts('common')) opts += Options(configopts('common'))
opts += Options(configopts('get')) opts += Options(configopts('get'))
return create_filesystem(opts).get(path1, path2, opts) return create_filesystem(opts).get(path1, path2, opts)
Expand Down
2 changes: 1 addition & 1 deletion dumbo/core.py
Expand Up @@ -361,7 +361,7 @@ def run(mapper,
for output in dumpcode(inputs): for output in dumpcode(inputs):
print '\t'.join(output) print '\t'.join(output)
else: else:
opts = opts or Options() opts = Options(opts)
if type(mapper) == str: if type(mapper) == str:
opts.add('mapper', mapper) opts.add('mapper', mapper)
elif hasattr(mapper, 'opts'): elif hasattr(mapper, 'opts'):
Expand Down
8 changes: 5 additions & 3 deletions dumbo/util.py
Expand Up @@ -81,8 +81,8 @@ def loadtext(inputs):


class Options(object): class Options(object):
""" """
Class that represent a a set of options. A key can hold Class that represents a set of options. A key can hold
more than a value and key are stored in lowercase. more than one value and keys are stored in lowercase.
""" """


def __init__(self, seq=None, **kwargs): def __init__(self, seq=None, **kwargs):
Expand All @@ -93,7 +93,9 @@ def __init__(self, seq=None, **kwargs):
- seq: a list of (key, value) pairs - seq: a list of (key, value) pairs
""" """
self._opts = defaultdict(set) 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: for k, v in options:
self.add(k, v) self.add(k, v)


Expand Down
18 changes: 9 additions & 9 deletions tests/testexamples.py
Expand Up @@ -27,7 +27,7 @@ def tearDown(self):


def testwordcount(self): def testwordcount(self):
opts = self.common_opts 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, retval = cmd.start(self.exdir+'wordcount.py', opts,
stdout=self.logfile, stderr=self.logfile) stdout=self.logfile, stderr=self.logfile)
self.assertEqual(0, retval) self.assertEqual(0, retval)
Expand All @@ -36,8 +36,8 @@ def testwordcount(self):


def testoowordcount(self): def testoowordcount(self):
opts = self.common_opts opts = self.common_opts
opts += Options([('excludes', self.exdir+'excludes.txt'), opts += [('excludes', self.exdir+'excludes.txt'),
('input', self.exdir+'brian.txt'), ('output', self.outfile)]) ('input', self.exdir+'brian.txt'), ('output', self.outfile)]
retval = cmd.start(self.exdir+'oowordcount.py', opts, retval = cmd.start(self.exdir+'oowordcount.py', opts,
stdout=self.logfile, stderr=self.logfile) stdout=self.logfile, stderr=self.logfile)
self.assertEquals(0, retval) self.assertEquals(0, retval)
Expand All @@ -46,7 +46,7 @@ def testoowordcount(self):


def testaltwordcount(self): def testaltwordcount(self):
opts = self.common_opts 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, retval = cmd.start(self.exdir+'altwordcount.py', opts,
stdout=self.logfile, stderr=self.logfile) stdout=self.logfile, stderr=self.logfile)
self.assertEqual(0, retval) self.assertEqual(0, retval)
Expand All @@ -55,7 +55,7 @@ def testaltwordcount(self):


def testitertwice(self): def testitertwice(self):
opts = self.common_opts 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, retval = cmd.start(self.exdir+'itertwice.py', opts,
stdout=self.logfile, stderr=self.logfile) stdout=self.logfile, stderr=self.logfile)
self.assertEqual(0, retval) self.assertEqual(0, retval)
Expand All @@ -64,9 +64,9 @@ def testitertwice(self):


def testjoin(self): def testjoin(self):
opts = self.common_opts opts = self.common_opts
opts += Options([('input', self.exdir+'hostnames.txt'), opts += [('input', self.exdir+'hostnames.txt'),
('input', self.exdir+'logs.txt'), ('input', self.exdir+'logs.txt'),
('output', self.outfile)]) ('output', self.outfile)]
retval = cmd.start(self.exdir+'join.py', opts, retval = cmd.start(self.exdir+'join.py', opts,
stdout=self.logfile, stderr=self.logfile) stdout=self.logfile, stderr=self.logfile)
self.assertEqual(0, retval) self.assertEqual(0, retval)
Expand All @@ -75,9 +75,9 @@ def testjoin(self):


def testmulticount(self): def testmulticount(self):
opts = self.common_opts opts = self.common_opts
opts += Options([('input', self.exdir+'brian.txt'), opts += [('input', self.exdir+'brian.txt'),
('input', self.exdir+'eno.txt'), ('input', self.exdir+'eno.txt'),
('output', self.outfile)]) ('output', self.outfile)]
retval = cmd.start(self.exdir+'multicount.py', opts, retval = cmd.start(self.exdir+'multicount.py', opts,
stdout=self.logfile, stderr=self.logfile) stdout=self.logfile, stderr=self.logfile)
self.assertEqual(0, retval) self.assertEqual(0, retval)
Expand Down

0 comments on commit 9604828

Please sign in to comment.