Exemplo n.º 1
0
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)
Exemplo n.º 2
0
 def it_fails_silently_if_it_cannot_save(self):
     data = Data()
     with patch("__builtin__.open", side_effect=ValueError):
         data.set('foo', 'bar')
     assert True
Exemplo n.º 3
0
 def it_returns_the_data_dict(self):
     data = Data()
     data.set('foo', 'bar')
     assert data.list() == {'foo': 'bar'}
Exemplo n.º 4
0
 def it_is_empty_if_there_are_no_keys(self):
     data = Data()
     assert data.is_empty()
Exemplo n.º 5
0
 def it_deletes_existing_keys(self):
     data = Data()
     data.set('foo', 'bar')
     data.delete('foo')
     assert data.get('foo') is None
Exemplo n.º 6
0
 def it_sets_and_gets_keys(self):
     data = Data()
     data.set('foo', 'bar')
     assert data.get('foo') == 'bar'
Exemplo n.º 7
0
 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'
Exemplo n.º 8
0
 def __init__(self):
     self.data = Data()
Exemplo n.º 9
0
 def it_fails_silently_if_it_cannot_save(self):
     data = Data()
     with patch("__builtin__.open", side_effect=ValueError):
         data.set('foo', 'bar')
     assert True
Exemplo n.º 10
0
 def it_returns_the_data_dict(self):
     data = Data()
     data.set('foo', 'bar')
     assert data.list() == {'foo': 'bar'}
Exemplo n.º 11
0
 def it_is_empty_if_there_are_no_keys(self):
     data = Data()
     assert data.is_empty()
Exemplo n.º 12
0
 def it_deletes_existing_keys(self):
     data = Data()
     data.set('foo', 'bar')
     data.delete('foo')
     assert data.get('foo') is None
Exemplo n.º 13
0
 def it_sets_and_gets_keys(self):
     data = Data()
     data.set('foo', 'bar')
     assert data.get('foo') == 'bar'
Exemplo n.º 14
0
 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'