Просмотр исходного кода

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

Dan Helfman 6 лет назад
Родитель
Сommit
d93da55ce9
4 измененных файлов с 71 добавлено и 36 удалено
  1. 3 0
      NEWS
  2. 14 0
      README.md
  3. 9 5
      borgmatic/commands/borgmatic.py
  4. 45 31
      borgmatic/tests/unit/commands/test_borgmatic.py

+ 3 - 0
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.

+ 14 - 0
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:

+ 9 - 5
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:

+ 45 - 31
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,
+    )