Пример #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)
Пример #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
Пример #3
0
 def it_returns_the_data_dict(self):
     data = Data()
     data.set('foo', 'bar')
     assert data.list() == {'foo': 'bar'}
Пример #4
0
 def it_is_empty_if_there_are_no_keys(self):
     data = Data()
     assert data.is_empty()
Пример #5
0
 def it_deletes_existing_keys(self):
     data = Data()
     data.set('foo', 'bar')
     data.delete('foo')
     assert data.get('foo') is None
Пример #6
0
 def it_sets_and_gets_keys(self):
     data = Data()
     data.set('foo', 'bar')
     assert data.get('foo') == 'bar'
Пример #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'
Пример #8
0
 def __init__(self):
     self.data = Data()
Пример #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
Пример #10
0
 def it_returns_the_data_dict(self):
     data = Data()
     data.set('foo', 'bar')
     assert data.list() == {'foo': 'bar'}
Пример #11
0
 def it_is_empty_if_there_are_no_keys(self):
     data = Data()
     assert data.is_empty()
Пример #12
0
 def it_deletes_existing_keys(self):
     data = Data()
     data.set('foo', 'bar')
     data.delete('foo')
     assert data.get('foo') is None
Пример #13
0
 def it_sets_and_gets_keys(self):
     data = Data()
     data.set('foo', 'bar')
     assert data.get('foo') == 'bar'
Пример #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'