Unify action API

This commit is contained in:
Markus Unterwaditzer 2014-06-18 15:16:28 +02:00
parent 82af0fad2e
commit 80f8301f71

View file

@ -127,7 +127,9 @@ def sync(storage_a, storage_b, status, conflict_resolution=None,
action(storages, status, conflict_resolution) action(storages, status, conflict_resolution)
def action_upload(ident, source, dest): def action_upload(ident, dest):
source = 'a' if dest == 'b' else 'b'
def inner(storages, status, conflict_resolution): def inner(storages, status, conflict_resolution):
source_storage, source_list, source_ident_to_href = storages[source] source_storage, source_list, source_ident_to_href = storages[source]
dest_storage, dest_list, dest_ident_to_href = storages[dest] dest_storage, dest_list, dest_ident_to_href = storages[dest]
@ -154,7 +156,9 @@ def action_upload(ident, source, dest):
return inner return inner
def action_update(ident, source, dest): def action_update(ident, dest):
source = 'a' if dest == 'b' else 'b'
def inner(storages, status, conflict_resolution): def inner(storages, status, conflict_resolution):
source_storage, source_list, source_ident_to_href = storages[source] source_storage, source_list, source_ident_to_href = storages[source]
dest_storage, dest_list, dest_ident_to_href = storages[dest] dest_storage, dest_list, dest_ident_to_href = storages[dest]
@ -224,12 +228,10 @@ def action_conflict_resolve(ident):
raise SyncConflict(ident=ident, href_a=href_a, href_b=href_b) raise SyncConflict(ident=ident, href_a=href_a, href_b=href_b)
elif conflict_resolution == 'a wins': elif conflict_resolution == 'a wins':
sync_logger.info('...{} wins.'.format(a_storage)) sync_logger.info('...{} wins.'.format(a_storage))
action_update(ident, 'a', 'b')(storages, status, action_update(ident, 'b')(storages, status, conflict_resolution)
conflict_resolution)
elif conflict_resolution == 'b wins': elif conflict_resolution == 'b wins':
sync_logger.info('...{} wins.'.format(b_storage)) sync_logger.info('...{} wins.'.format(b_storage))
action_update(ident, 'b', 'a')(storages, status, action_update(ident, 'a')(storages, status, conflict_resolution)
conflict_resolution)
else: else:
raise ValueError('Invalid conflict resolution mode: {}' raise ValueError('Invalid conflict resolution mode: {}'
.format(conflict_resolution)) .format(conflict_resolution))
@ -255,18 +257,18 @@ def get_actions(storages, status):
if a and b: # missing status if a and b: # missing status
yield action_conflict_resolve(ident) yield action_conflict_resolve(ident)
elif a and not b: # new item was created in a elif a and not b: # new item was created in a
yield action_upload(ident, 'a', 'b') yield action_upload(ident, 'b')
elif not a and b: # new item was created in b elif not a and b: # new item was created in b
yield action_upload(ident, 'b', 'a') yield action_upload(ident, 'a')
else: else:
_, status_etag_a, _, status_etag_b = status[ident] _, status_etag_a, _, status_etag_b = status[ident]
if a and b: if a and b:
if a['etag'] != status_etag_a and b['etag'] != status_etag_b: if a['etag'] != status_etag_a and b['etag'] != status_etag_b:
yield action_conflict_resolve(ident) yield action_conflict_resolve(ident)
elif a['etag'] != status_etag_a: # item was updated in a elif a['etag'] != status_etag_a: # item was updated in a
yield action_update(ident, 'a', 'b') yield action_update(ident, 'b')
elif b['etag'] != status_etag_b: # item was updated in b elif b['etag'] != status_etag_b: # item was updated in b
yield action_update(ident, 'b', 'a') yield action_update(ident, 'a')
elif a and not b: # was deleted from b elif a and not b: # was deleted from b
yield action_delete(ident, 'a') yield action_delete(ident, 'a')
elif not a and b: # was deleted from a elif not a and b: # was deleted from a