Skip to content

Commit

Permalink
Merge pull request #128 from MichaelAquilina/black_formatted
Browse files Browse the repository at this point in the history
Format all code using black
  • Loading branch information
MichaelAquilina committed Jun 18, 2018
2 parents 123afe5 + c638d84 commit 5b86791
Show file tree
Hide file tree
Showing 36 changed files with 1,467 additions and 1,647 deletions.
2 changes: 1 addition & 1 deletion s4/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = '0.2.16'
VERSION = "0.2.16"
125 changes: 65 additions & 60 deletions s4/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,89 +17,94 @@

def main(arguments):
parser = argparse.ArgumentParser(
prog='s4',
prog="s4",
formatter_class=argparse.RawDescriptionHelpFormatter,
description=(
'Fast and cheap synchronisation of files with Amazon S3\n'
'\n'
'Version: {}\n'
'\n'
"Fast and cheap synchronisation of files with Amazon S3\n"
"\n"
"Version: {}\n"
"\n"
'To start off, add a Target with the "add" command\n'
).format(VERSION),
)
parser.add_argument(
'--log-level',
default='INFO',
choices=['DEBUG', 'INFO', 'WARNING', 'ERROR'],
"--log-level", default="INFO", choices=["DEBUG", "INFO", "WARNING", "ERROR"]
)
parser.add_argument(
'--no-colors',
action='store_true',
help='Display without colors',
"--no-colors", action="store_true", help="Display without colors"
)
parser.add_argument(
'--timestamps',
action='store_true',
help='Display timestamps for each log message',
"--timestamps",
action="store_true",
help="Display timestamps for each log message",
)
subparsers = parser.add_subparsers(dest='command')
subparsers = parser.add_subparsers(dest="command")

daemon_parser = subparsers.add_parser('daemon', help='Run S4 sync continiously', aliases=['d'])
daemon_parser.add_argument('targets', nargs='*')
daemon_parser.add_argument('--read-delay', default=1000, type=int)
daemon_parser.add_argument('--conflicts', default='ignore', choices=['1', '2', 'ignore'])
daemon_parser = subparsers.add_parser(
"daemon", help="Run S4 sync continiously", aliases=["d"]
)
daemon_parser.add_argument("targets", nargs="*")
daemon_parser.add_argument("--read-delay", default=1000, type=int)
daemon_parser.add_argument(
"--conflicts", default="ignore", choices=["1", "2", "ignore"]
)

add_parser = subparsers.add_parser('add', help='Add a new Target to synchronise', aliases=['a'])
add_parser = subparsers.add_parser(
"add", help="Add a new Target to synchronise", aliases=["a"]
)
add_parser.add_argument(
'--copy-target-credentials',
'-C',
help="Copy credentials from an existing target instead of typing them in again"
"--copy-target-credentials",
"-C",
help="Copy credentials from an existing target instead of typing them in again",
)

sync_parser = subparsers.add_parser('sync', help='Synchronise Targets with S3', aliases=['s'])
sync_parser.add_argument('targets', nargs='*')
sync_parser.add_argument('--conflicts', default=None, choices=['1', '2', 'ignore'])
sync_parser.add_argument('--dry-run', action='store_true')
sync_parser = subparsers.add_parser(
"sync", help="Synchronise Targets with S3", aliases=["s"]
)
sync_parser.add_argument("targets", nargs="*")
sync_parser.add_argument("--conflicts", default=None, choices=["1", "2", "ignore"])
sync_parser.add_argument("--dry-run", action="store_true")

edit_parser = subparsers.add_parser('edit', help='Edit Target details', aliases=['e'])
edit_parser.add_argument('target')
edit_parser = subparsers.add_parser(
"edit", help="Edit Target details", aliases=["e"]
)
edit_parser.add_argument("target")

subparsers.add_parser('targets', help='Print available Targets', aliases=['t'])
subparsers.add_parser("targets", help="Print available Targets", aliases=["t"])

subparsers.add_parser('version', help='Print S4 Version', aliases=['v'])
subparsers.add_parser("version", help="Print S4 Version", aliases=["v"])

ls_parser = subparsers.add_parser('ls', help="Display list of files for a Target")
ls_parser.add_argument('target')
ls_parser.add_argument('--sort-by', '-s', choices=['key', 'local', 's3'], default='key')
ls_parser.add_argument('--descending', '-d', action='store_true')
ls_parser = subparsers.add_parser("ls", help="Display list of files for a Target")
ls_parser.add_argument("target")
ls_parser.add_argument(
"--sort-by", "-s", choices=["key", "local", "s3"], default="key"
)
ls_parser.add_argument("--descending", "-d", action="store_true")
ls_parser.add_argument(
'--all', '-A',
dest='show_all',
action='store_true',
help='show deleted files',
"--all", "-A", dest="show_all", action="store_true", help="show deleted files"
)

remove_parser = subparsers.add_parser('rm', help="Remove a Target")
remove_parser.add_argument('target')
remove_parser = subparsers.add_parser("rm", help="Remove a Target")
remove_parser.add_argument("target")

args = parser.parse_args(arguments)

if args.log_level == 'DEBUG':
log_format = '%(levelname)s:%(module)s:%(lineno)s %(message)s'
if args.log_level == "DEBUG":
log_format = "%(levelname)s:%(module)s:%(lineno)s %(message)s"
else:
log_format = '%(message)s'
log_format = "%(message)s"

if args.timestamps:
log_format = '%(asctime)s: ' + log_format
log_format = "%(asctime)s: " + log_format

logging.basicConfig(format=log_format, level=args.log_level)

# shut boto up
logging.getLogger('boto3').setLevel(logging.CRITICAL)
logging.getLogger('botocore').setLevel(logging.CRITICAL)
logging.getLogger('nose').setLevel(logging.CRITICAL)
logging.getLogger('s3transfer').setLevel(logging.CRITICAL)
logging.getLogger('filelock').setLevel(logging.CRITICAL)
logging.getLogger("boto3").setLevel(logging.CRITICAL)
logging.getLogger("botocore").setLevel(logging.CRITICAL)
logging.getLogger("nose").setLevel(logging.CRITICAL)
logging.getLogger("s3transfer").setLevel(logging.CRITICAL)
logging.getLogger("filelock").setLevel(logging.CRITICAL)

logger = logging.getLogger(__name__)
logger.setLevel(args.log_level)
Expand All @@ -108,31 +113,31 @@ def main(arguments):

try:
command = None
if args.command in ('version', 'v'):
if args.command in ("version", "v"):
print(VERSION)
return
elif args.command in ('sync', 's'):
elif args.command in ("sync", "s"):
command = SyncCommand(args, config, logger)
elif args.command in ('targets', 't'):
elif args.command in ("targets", "t"):
command = TargetsCommand(args, config, logger)
elif args.command in ('add', 'a'):
elif args.command in ("add", "a"):
command = AddCommand(args, config, logger)
elif args.command in ('edit', 'e'):
elif args.command in ("edit", "e"):
command = EditCommand(args, config, logger)
elif args.command == 'ls':
elif args.command == "ls":
command = LsCommand(args, config, logger)
elif args.command == 'rm':
elif args.command == "rm":
command = RmCommand(args, config, logger)
elif args.command in ('daemon', 'd'):
elif args.command in ("daemon", "d"):
command = DaemonCommand(args, config, logger)

if command:
try:
command.run()
except Exception as e:
logger.error('An unhandled error has occurred: %s', e)
logger.error("An unhandled error has occurred: %s", e)
# Only display a scary stack trace to the user if in DEBUG mode
if args.log_level == 'DEBUG':
if args.log_level == "DEBUG":
raise e
else:
parser.print_help()
Expand Down
42 changes: 21 additions & 21 deletions s4/clients/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@


class SyncState(object):
UPDATED = 'UPDATED'
CREATED = 'CREATED'
DELETED = 'DELETED'
CONFLICT = 'CONFLICT'
NOCHANGES = 'NOCHANGES'
DOESNOTEXIST = 'DOESNOTEXIST'
UPDATED = "UPDATED"
CREATED = "CREATED"
DELETED = "DELETED"
CONFLICT = "CONFLICT"
NOCHANGES = "NOCHANGES"
DOESNOTEXIST = "DOESNOTEXIST"

def __init__(self, state, local_timestamp, remote_timestamp):
self.state = state
Expand All @@ -32,13 +32,13 @@ def __eq__(self, other):
if not isinstance(other, SyncState):
return False
return (
self.state == other.state and
self.local_timestamp == other.local_timestamp and
self.remote_timestamp == other.remote_timestamp
self.state == other.state
and self.local_timestamp == other.local_timestamp
and self.remote_timestamp == other.remote_timestamp
)

def __repr__(self):
return 'SyncState<{}, local={}, remote={}>'.format(
return "SyncState<{}, local={}, remote={}>".format(
self.state, self.get_local_datetime(), self.get_remote_datetime()
)

Expand All @@ -50,7 +50,7 @@ def __init__(self, fp, total_size, timestamp):
self.timestamp = timestamp

def __repr__(self):
return 'SyncObject<{}, {}, {}>'.format(self.fp, self.total_size, self.timestamp)
return "SyncObject<{}, {}, {}>".format(self.fp, self.total_size, self.timestamp)


def get_sync_state(index_local, real_local, remote):
Expand All @@ -72,7 +72,7 @@ def get_sync_state(index_local, real_local, remote):
elif index_local < real_local:
return SyncState(SyncState.UPDATED, real_local, remote)
elif index_local > real_local:
return SyncState(SyncState.CONFLICT, index_local, remote) # corruption?
return SyncState(SyncState.CONFLICT, index_local, remote) # corruption?
else:
return SyncState(SyncState.NOCHANGES, real_local, remote)

Expand All @@ -81,7 +81,7 @@ class SyncClient(object):
def get_client_name(self):
raise NotImplementedError()

def get_uri(self, key=''):
def get_uri(self, key=""):
raise NotImplementedError()

def lock(self, timeout=10):
Expand Down Expand Up @@ -140,15 +140,15 @@ def update_index(self):

for key in keys:
index[key] = {
'remote_timestamp': self.get_remote_timestamp(key),
'local_timestamp': self.get_real_local_timestamp(key),
"remote_timestamp": self.get_remote_timestamp(key),
"local_timestamp": self.get_real_local_timestamp(key),
}
self.index = index

def update_index_entry(self, key):
self.index[key] = {
'remote_timestamp': self.get_remote_timestamp(key),
'local_timestamp': self.get_real_local_timestamp(key),
"remote_timestamp": self.get_remote_timestamp(key),
"local_timestamp": self.get_real_local_timestamp(key),
}

def flush_index(self):
Expand All @@ -162,7 +162,9 @@ def get_action(self, key):
index_local_timestamp = self.get_index_local_timestamp(key)
real_local_timestamp = self.get_real_local_timestamp(key)
remote_timestamp = self.get_remote_timestamp(key)
return get_sync_state(index_local_timestamp, real_local_timestamp, remote_timestamp)
return get_sync_state(
index_local_timestamp, real_local_timestamp, remote_timestamp
)

def get_all_actions(self):
real_local_timestamps = self.get_all_real_local_timestamps()
Expand All @@ -178,9 +180,7 @@ def get_all_actions(self):
remote_timestamp = remote_timestamps.get(key)

results[key] = get_sync_state(
index_local_timestamp,
real_local_timestamp,
remote_timestamp,
index_local_timestamp, real_local_timestamp, remote_timestamp
)

return results

0 comments on commit 5b86791

Please sign in to comment.