class CLI: def __init__(self): self.data = Data() def start(self, argv=None): args = docopt(__doc__, argv=argv, version=pork.__version__) self._process(args['KEY'], args['VALUE'], args['--delete']) def _process(self, key, value, delete): if key and delete: self.data.delete(key) elif key and value: self.data.set(key, value) elif key: v = self.data.get(key) if v is not None: print v elif self.data.is_empty(): print __doc__.strip() else: items = self.data.list() padding = len(max(items, key=len)) for k, v in self.data.list().iteritems(): print "%s: %s" % (k.rjust(padding), v)
def it_fails_silently_if_it_cannot_save(self): data = Data() with patch("__builtin__.open", side_effect=ValueError): data.set('foo', 'bar') assert True
def it_returns_the_data_dict(self): data = Data() data.set('foo', 'bar') assert data.list() == {'foo': 'bar'}
def it_is_empty_if_there_are_no_keys(self): data = Data() assert data.is_empty()
def it_deletes_existing_keys(self): data = Data() data.set('foo', 'bar') data.delete('foo') assert data.get('foo') is None
def it_sets_and_gets_keys(self): data = Data() data.set('foo', 'bar') assert data.get('foo') == 'bar'
def it_loads_json_data_from_file(self): with patch("__builtin__.open", mock_open(read_data='{"foo":"bar"}'), create=True) as m: data = Data() assert data.get('foo') == 'bar'
def __init__(self): self.data = Data()