lavagetto/python-etcd — pull request #1 by dsoprea (2013-12-01 06:18:43+00:00)
Title: Added prev_exist and prev_index parameters to test_and_set method.

Also fixed "prevExists" (not valid) in condition-list to be "prevExist". We now throw an error if an unrecognized condition argument is specified.

Condition-check results:

>>> c.test_and_set('/new_path', 5, prev_exist=False)
EtcdResult(action=u'create', key=u'/new_path', value=u'5', expiration=None, ttl=None, modifiedIndex=30, prevValue=None, newKey=True, dir=False, kvs=None)

>>> c.test_and_set('/new_path', 5, prev_exist=False)
Traceback (most recent call last):
File "", line 1, in
File "etcd/client.py", line 358, in test_and_set
return self.write(key, value, **kwargs)
File "etcd/client.py", line 244, in write
response = self.api_execute(path, self._MPUT, params)
File "etcd/client.py", line 503, in api_execute
etcd.EtcdError.handle(**json.loads(response.data.decode('utf-8')))
File "etcd/__init__.py", line 94, in handle
raise exc(msg)
KeyError: 'Already exists : /new_path'

>>> c.test_and_set('/new_path', 5, prev_value=10, prev_index=27, prev_exist=True)
Traceback (most recent call last):
File "", line 1, in
File "etcd/client.py", line 358, in test_and_set
return self.write(key, value, **kwargs)
File "etcd/client.py", line 244, in write
response = self.api_execute(path, self._MPUT, params)
File "etcd/client.py", line 503, in api_execute
etcd.EtcdError.handle(**json.loads(response.data.decode('utf-8')))
File "etcd/__init__.py", line 94, in handle
raise exc(msg)
ValueError: Test Failed : [10 != 5] [27 != 30]

>>> c.test_and_set('/new_path', 5, prev_value=5, prev_index=27, prev_exist=True)
Traceback (most recent call last):
File "", line 1, in
File "etcd/client.py", line 358, in test_and_set
return self.write(key, value, **kwargs)
File "etcd/client.py", line 244, in write
response = self.api_execute(path, self._MPUT, params)
File "etcd/client.py", line 503, in api_execute
etcd.EtcdError.handle(**json.loads(response.data.decode('utf-8')))
File "etcd/__init__.py", line 94, in handle
raise exc(msg)
ValueError: Test Failed : [5 != 5] [27 != 30]

>>> c.test_and_set('/new_path', 5, prev_value=5, prev_index=30, prev_exist=True)
EtcdResult(action=u'compareAndSwap', key=u'/new_path', value=u'5', expiration=None, ttl=None, modifiedIndex=31, prevValue=u'5', newKey=False, dir=False, kvs=None)