From d93da55ce9b7e1c5ed048b1f7265499f40045ef3 Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Sat, 28 Jul 2018 15:02:17 -0700 Subject: [PATCH] Add code style guidelines to the documention, and reformat some code accordingly. --- NEWS | 3 + README.md | 14 ++++ borgmatic/commands/borgmatic.py | 14 ++-- .../tests/unit/commands/test_borgmatic.py | 76 +++++++++++-------- 4 files changed, 71 insertions(+), 36 deletions(-) diff --git a/NEWS b/NEWS index 18241c465..820f793a7 100644 --- a/NEWS +++ b/NEWS @@ -2,9 +2,12 @@ * Skip before/after backup hooks when only doing --prune, --check, --list, and/or --info. * #71: Support for XDG_CONFIG_HOME environment variable for specifying alternate user ~/.config/ path. + * #74: Support for Borg --list --json options via borgmatic command-line to list all archives in + JSON format, ideal for programmatic consumption. * #38, #76: Upgrade ruamel.yaml compatibility version range and fix support for Python 3.7. * #77: Skip non-"*.yaml" config filenames in /etc/borgmatic.d/ so as not to parse backup files, editor swap files, etc. + * Add code style guidelines to the documention. 1.2.0 * #61: Support for Borg --list option via borgmatic command-line to list all archives. diff --git a/README.md b/README.md index d816ffd88..75d623a55 100644 --- a/README.md +++ b/README.md @@ -326,6 +326,20 @@ to discuss your idea. We also accept Pull Requests on GitHub, if that's more your thing. In general, contributions are very welcome. We don't bite! +### Code style + +Start with [PEP 8](https://www.python.org/dev/peps/pep-0008/). But then, apply +the following deviations from PEP 8: + + * For strings, prefer single quotes over double quotes. + * Limit all lines to a maximum of 100 characters. + * Use trailing commas within multiline values or argument lists. + * Within multiline constructs: + * Use standard four-space indentation. Don't align indentation with an opening + delimeter. + * Put opening and closing delimeters on lines separate from their contents. + + ### Development To get set up to hack on borgmatic, first clone master via HTTPS or SSH: diff --git a/borgmatic/commands/borgmatic.py b/borgmatic/commands/borgmatic.py index c8993ba16..3a22e4d48 100644 --- a/borgmatic/commands/borgmatic.py +++ b/borgmatic/commands/borgmatic.py @@ -98,7 +98,7 @@ def parse_arguments(*arguments): args = parser.parse_args(arguments) if args.json and not args.list: - raise ValueError("The --json option can only be used with the --list option") + raise ValueError('The --json option can only be used with the --list option') # If any of the action flags are explicitly requested, leave them as-is. Otherwise, assume # defaults: Mutate the given arguments to enable the default actions. @@ -143,14 +143,18 @@ def run_configuration(config_filename, args): # pragma: no cover def _run_commands(args, consistency, local_path, location, remote_path, retention, storage): json_results = [] for unexpanded_repository in location['repositories']: - _run_commands_on_repository(args, consistency, json_results, local_path, location, remote_path, retention, - storage, unexpanded_repository) + _run_commands_on_repository( + args, consistency, json_results, local_path, location, remote_path, retention, storage, + unexpanded_repository, + ) if args.json: sys.stdout.write(json.dumps(json_results)) -def _run_commands_on_repository(args, consistency, json_results, local_path, location, remote_path, retention, storage, - unexpanded_repository): # pragma: no cover +def _run_commands_on_repository( + args, consistency, json_results, local_path, location, remote_path, + retention, storage, unexpanded_repository, +): # pragma: no cover repository = os.path.expanduser(unexpanded_repository) dry_run_label = ' (dry run; not making any changes)' if args.dry_run else '' if args.prune: diff --git a/borgmatic/tests/unit/commands/test_borgmatic.py b/borgmatic/tests/unit/commands/test_borgmatic.py index cab0b0f34..f53728e21 100644 --- a/borgmatic/tests/unit/commands/test_borgmatic.py +++ b/borgmatic/tests/unit/commands/test_borgmatic.py @@ -1,38 +1,52 @@ -from borgmatic.commands import borgmatic -from flexmock import flexmock import json -import pytest import sys +from flexmock import flexmock +import pytest + +from borgmatic.commands import borgmatic + def test__run_commands_handles_multiple_json_outputs_in_array(): - # THEN - (flexmock(borgmatic) - .should_receive("_run_commands_on_repository") - .times(3) - .replace_with(lambda args, consistency, json_results, local_path, location, remote_path, retention, storage, - unexpanded_repository: json_results.append({"whatever": unexpanded_repository})) - ) + ( + flexmock(borgmatic) + .should_receive('_run_commands_on_repository') + .times(3) + .replace_with( + lambda args, consistency, json_results, local_path, location, remote_path, retention, + storage, + unexpanded_repository: json_results.append({"whatever": unexpanded_repository}) + ) + ) - (flexmock(sys.stdout) - .should_call("write") - .with_args(json.dumps(json.loads(''' - [ - {"whatever": "fake_repo1"}, - {"whatever": "fake_repo2"}, - {"whatever": "fake_repo3"} - ] - '''))) - ) + ( + flexmock(sys.stdout) + .should_call("write") + .with_args( + json.dumps( + json.loads( + ''' + [ + {"whatever": "fake_repo1"}, + {"whatever": "fake_repo2"}, + {"whatever": "fake_repo3"} + ] + ''', + ) + ) + ) + ) - borgmatic._run_commands(args=flexmock(json=True), - consistency=None, - local_path=None, - location={"repositories": [ - "fake_repo1", - "fake_repo2", - "fake_repo3" - ]}, - remote_path=None, - retention=None, - storage=None) + borgmatic._run_commands( + args=flexmock(json=True), + consistency=None, + local_path=None, + location={'repositories': [ + 'fake_repo1', + 'fake_repo2', + 'fake_repo3' + ]}, + remote_path=None, + retention=None, + storage=None, + )