Skip to content

Commit

Permalink
Merge pull request #81 from MichaelAquilina/remove_colored_from_sync
Browse files Browse the repository at this point in the history
Remove colored from sync.py
  • Loading branch information
MichaelAquilina committed Oct 28, 2017
2 parents c515c3a + 275c7eb commit cd0b9da
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 19 deletions.
25 changes: 25 additions & 0 deletions s4/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

import boto3

from clint.textui import colored

from inotify_simple import INotify, flags

try:
Expand Down Expand Up @@ -385,6 +387,28 @@ def sync_command(args, config, logger):
else:
targets = args.targets

def action_callback(resolution):
if resolution.action == Resolution.UPDATE:
logger.info(
colored.yellow('Updating %s (%s => %s)'),
resolution.key,
resolution.from_client.get_uri(),
resolution.to_client.get_uri()
)
elif resolution.action == Resolution.CREATE:
logger.info(
colored.green('Creating %s (%s => %s)'),
resolution.key,
resolution.from_client.get_uri(),
resolution.to_client.get_uri()
)
elif resolution.action == Resolution.DELETE:
logger.info(
colored.red('Deleting %s on %s'),
resolution.key,
resolution.to_client.get_uri()
)

try:
for name in sorted(targets):
if name not in config['targets']:
Expand All @@ -402,6 +426,7 @@ def sync_command(args, config, logger):
update_callback=update_progress_bar,
complete_callback=hide_progress_bar,
conflict_handler=handle_conflict,
action_callback=action_callback,
)

logger.info('Syncing %s [%s <=> %s]', name, client_1.get_uri(), client_2.get_uri())
Expand Down
24 changes: 5 additions & 19 deletions s4/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import logging
import traceback

from clint.textui import colored

from s4.clients import SyncState
from s4.resolution import Resolution

Expand All @@ -17,6 +15,7 @@ def __init__(
start_callback=None,
update_callback=None,
complete_callback=None,
action_callback=None,
conflict_handler=None,
):
self.client_1 = client_1
Expand All @@ -25,6 +24,7 @@ def __init__(
self.start_callback = start_callback
self.update_callback = update_callback
self.complete_callback = complete_callback
self.action_callback = action_callback
self.conflict_handler = conflict_handler

def __repr__(self):
Expand Down Expand Up @@ -201,28 +201,14 @@ def run_resolutions(self, resolutions, dry_run=False):
try:
for key in sorted(resolutions.keys()):
resolution = resolutions[key]
if self.action_callback is not None:
self.action_callback(resolution)

if resolution.action == Resolution.UPDATE:
self.logger.info(
colored.yellow('Updating %s (%s => %s)'),
resolution.key,
resolution.from_client.get_uri(),
resolution.to_client.get_uri()
)
deferred_function = self.move_client
elif resolution.action == Resolution.CREATE:
self.logger.info(
colored.green('Creating %s (%s => %s)'),
resolution.key,
resolution.from_client.get_uri(),
resolution.to_client.get_uri()
)
deferred_function = self.move_client
elif resolution.action == Resolution.DELETE:
self.logger.info(
colored.red('Deleting %s on %s'),
resolution.key,
resolution.to_client.get_uri()
)
deferred_function = self.delete_client
else:
raise ValueError('Unknown resolution', resolution)
Expand Down
20 changes: 20 additions & 0 deletions tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,26 @@ def test_dry_run(self, local_client, s3_client):
assert not local_client.put.called
assert not s3_client.put.called

def test_action_callback(self, local_client, s3_client):
utils.set_s3_contents(s3_client, 'colors/cream', 9999)

utils.set_local_contents(local_client, 'colors/green', 3000)
utils.set_local_contents(local_client, 'colors/blue', 2000)

callback_mock = mock.MagicMock()

worker = sync.SyncWorker(
local_client,
s3_client,
action_callback=callback_mock
)
worker.sync()

assert callback_mock.call_count == 3
assert callback_mock.call_args_list[0][0][0].key == 'colors/blue'
assert callback_mock.call_args_list[1][0][0].key == 'colors/cream'
assert callback_mock.call_args_list[2][0][0].key == 'colors/green'

def test_local_with_s3(self, local_client, s3_client):
utils.set_s3_contents(s3_client, 'colors/cream', 9999, '#ddeeff')

Expand Down

0 comments on commit cd0b9da

Please sign in to comment.