Add code style guidelines to the documention, and reformat some code accordingly.

This commit is contained in:
Dan Helfman 2018-07-28 15:02:17 -07:00
parent 789bcd402a
commit d93da55ce9
4 changed files with 71 additions and 36 deletions

3
NEWS
View File

@ -2,9 +2,12 @@
* Skip before/after backup hooks when only doing --prune, --check, --list, and/or --info. * 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/ * #71: Support for XDG_CONFIG_HOME environment variable for specifying alternate user ~/.config/
path. 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. * #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, * #77: Skip non-"*.yaml" config filenames in /etc/borgmatic.d/ so as not to parse backup files,
editor swap files, etc. editor swap files, etc.
* Add code style guidelines to the documention.
1.2.0 1.2.0
* #61: Support for Borg --list option via borgmatic command-line to list all archives. * #61: Support for Borg --list option via borgmatic command-line to list all archives.

View File

@ -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! 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 ### Development
To get set up to hack on borgmatic, first clone master via HTTPS or SSH: To get set up to hack on borgmatic, first clone master via HTTPS or SSH:

View File

@ -98,7 +98,7 @@ def parse_arguments(*arguments):
args = parser.parse_args(arguments) args = parser.parse_args(arguments)
if args.json and not args.list: 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 # 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. # 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): def _run_commands(args, consistency, local_path, location, remote_path, retention, storage):
json_results = [] json_results = []
for unexpanded_repository in location['repositories']: for unexpanded_repository in location['repositories']:
_run_commands_on_repository(args, consistency, json_results, local_path, location, remote_path, retention, _run_commands_on_repository(
storage, unexpanded_repository) args, consistency, json_results, local_path, location, remote_path, retention, storage,
unexpanded_repository,
)
if args.json: if args.json:
sys.stdout.write(json.dumps(json_results)) sys.stdout.write(json.dumps(json_results))
def _run_commands_on_repository(args, consistency, json_results, local_path, location, remote_path, retention, storage, def _run_commands_on_repository(
unexpanded_repository): # pragma: no cover args, consistency, json_results, local_path, location, remote_path,
retention, storage, unexpanded_repository,
): # pragma: no cover
repository = os.path.expanduser(unexpanded_repository) repository = os.path.expanduser(unexpanded_repository)
dry_run_label = ' (dry run; not making any changes)' if args.dry_run else '' dry_run_label = ' (dry run; not making any changes)' if args.dry_run else ''
if args.prune: if args.prune:

View File

@ -1,38 +1,52 @@
from borgmatic.commands import borgmatic
from flexmock import flexmock
import json import json
import pytest
import sys import sys
from flexmock import flexmock
import pytest
from borgmatic.commands import borgmatic
def test__run_commands_handles_multiple_json_outputs_in_array(): def test__run_commands_handles_multiple_json_outputs_in_array():
# THEN (
(flexmock(borgmatic) flexmock(borgmatic)
.should_receive("_run_commands_on_repository") .should_receive('_run_commands_on_repository')
.times(3) .times(3)
.replace_with(lambda args, consistency, json_results, local_path, location, remote_path, retention, storage, .replace_with(
unexpanded_repository: json_results.append({"whatever": unexpanded_repository})) 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") flexmock(sys.stdout)
.with_args(json.dumps(json.loads(''' .should_call("write")
[ .with_args(
{"whatever": "fake_repo1"}, json.dumps(
{"whatever": "fake_repo2"}, json.loads(
{"whatever": "fake_repo3"} '''
] [
'''))) {"whatever": "fake_repo1"},
) {"whatever": "fake_repo2"},
{"whatever": "fake_repo3"}
]
''',
)
)
)
)
borgmatic._run_commands(args=flexmock(json=True), borgmatic._run_commands(
consistency=None, args=flexmock(json=True),
local_path=None, consistency=None,
location={"repositories": [ local_path=None,
"fake_repo1", location={'repositories': [
"fake_repo2", 'fake_repo1',
"fake_repo3" 'fake_repo2',
]}, 'fake_repo3'
remote_path=None, ]},
retention=None, remote_path=None,
storage=None) retention=None,
storage=None,
)