Skip to content

Commit 513ae76

Browse files
authored
Adopt black as code formatter (#148)
* Replace yapf with black on pre-commit * Making pre-commit happy
1 parent 528a472 commit 513ae76

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+2308
-2882
lines changed

.flake8

Lines changed: 0 additions & 3 deletions
This file was deleted.

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
# Sphinx documentation
44
doc
5+
doc-source/compliance.rst
6+
doc-source/compliance.*.rst
7+
doc-source/modules.rst
58

69
### Emacs
710

.pre-commit-config.yaml

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,18 @@ repos:
66
- id: check-yaml
77
- id: fix-encoding-pragma
88
args: ["--remove"] # Not needed on python3
9-
- repo: https://github.com/google/yapf
10-
rev: v0.32.0
9+
- repo: https://github.com/ambv/black
10+
rev: 22.12.0
1111
hooks:
12-
- id: yapf
13-
args: [--in-place, --parallel, --recursive, --style, .yapf-config]
14-
files: "^(compliance|test)"
15-
stages: [commit]
12+
- id: black
1613
- repo: https://github.com/PyCQA/flake8
17-
rev: 5.0.4
14+
rev: 6.0.0
1815
hooks:
1916
- id: flake8
20-
additional_dependencies: [
21-
flake8-2020,
22-
flake8-broken-line,
23-
flake8-bugbear,
24-
flake8-builtins,
25-
flake8-commas,
26-
flake8-comprehensions,
27-
flake8-docstrings,
28-
flake8-eradicate,
29-
flake8-import-order,
30-
flake8-mutable,
31-
flake8-pep3101,
32-
flake8-print,
33-
flake8-quotes,
34-
flake8-string-format,
35-
flake8-use-fstring,
36-
pep8-naming
37-
]
3817
files: "^(compliance|test|demo)"
39-
stages: [commit]
4018
- repo: https://github.com/PyCQA/bandit
4119
rev: 1.7.4
4220
hooks:
4321
- id: bandit
4422
args: [--recursive]
4523
files: "^(compliance|test)"
46-
stages: [commit]

.yapf-config

Lines changed: 0 additions & 14 deletions
This file was deleted.

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- [ADDED] Documentation about using @parameterized.
1010
- [CHANGED] Unify github demo code in one single place.
1111
- [FIXED] flake8 now scans demo code too.
12+
- [CHANGED] Removed yapf in favour of black as code formatter.
1213

1314
# [1.23.0](https://github.com/ComplianceAsCode/auditree-framework/releases/tag/v1.23.0)
1415

CONTRIBUTING.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ There are some guidelines to follow when making a common fetcher or check:
4141

4242
## Code formatting and style
4343

44-
Please ensure all code contributions are formatted by `yapf` and pass all `flake8` linter requirements.
45-
CI/CD will run `yapf` and `flake8` on all new commits and reject changes if there are failures. If you
46-
run `make develop` to setup and maintain your virtual environment then `yapf` and `flake8` will be executed
44+
Please ensure all code contributions are formatted by `black` and pass all `flake8` linter requirements.
45+
CI/CD will run `black` and `flake8` on all new commits and reject changes if there are failures. If you
46+
run `make develop` to setup and maintain your virtual environment then `black` and `flake8` will be executed
4747
automatically as part of all git commits. If you'd like to run things manually you can do so locally by using:
4848

4949
```shell
@@ -81,4 +81,4 @@ example `[ADDED]`, `[CHANGED]`, etc.
8181
[Coding Standards]: https://complianceascode.github.io/auditree-framework/coding-standards.html
8282
[flake8]: https://gitlab.com/pycqa/flake8
8383
[new collab]: https://github.com/ComplianceAsCode/auditree-framework/issues/new?template=new-collaborator.md
84-
[yapf]: https://github.com/google/yapf
84+
[black]: https://github.com/psf/black

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ uninstall:
3131
pip uninstall auditree-framework
3232

3333
code-format:
34-
pre-commit run yapf --all-files
34+
pre-commit run black --all-files
3535

3636
code-lint:
3737
pre-commit run flake8 --all-files

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,32 +44,32 @@ make develop
4444

4545
#### Code style and formatting
4646

47-
This repository uses [yapf][yapf] for code formatting and [flake8][flake8] for code styling. It also
47+
This repository uses [black][black] for code formatting and [flake8][flake8] for code styling. It also
4848
uses [pre-commit][pre-commit] hooks that are integrated into the development process and the CI. When
4949
you run `make develop` you are ensuring that the pre-commit hooks are installed and updated to their
5050
latest versions for this repository. This ensures that all delivered code has been properly formatted
5151
and passes the linter rules. See the [pre-commit configuration file][pre-commit-config] for details on
52-
`yapf` and `flake8` configurations.
52+
`black` and `flake8` configurations.
5353

54-
Since `yapf` and `flake8` are installed as part of the `pre-commit` hooks, running `yapf` and `flake8`
54+
Since `black` and `flake8` are installed as part of the `pre-commit` hooks, running `black` and `flake8`
5555
manually must be done through `pre-commit`. See examples below:
5656

5757
```shell
5858
make code-format
5959
make code-lint
6060
```
6161

62-
...will run `yapf` and `flake8` on the entire repo and is equivalent to:
62+
...will run `black` and `flake8` on the entire repo and is equivalent to:
6363

6464
```shell
65-
pre-commit run yapf --all-files
65+
pre-commit run black --all-files
6666
pre-commit run flake8 --all-files
6767
```
6868

6969
...and when looking to limit execution to a subset of files do similar to:
7070

7171
```shell
72-
pre-commit run yapf --files compliance/*
72+
pre-commit run black --files compliance/*
7373
pre-commit run flake8 --files compliance/*
7474
```
7575

@@ -152,7 +152,7 @@ We have a tool called [Plant](https://github.com/ComplianceAsCode/auditree-plant
152152
[python-badge]: https://img.shields.io/badge/python-v3.6+-blue.svg
153153
[python]: https://www.python.org/downloads/
154154
[quick start guide]: https://github.com/ComplianceAsCode/auditree-framework/blob/master/doc-source/quick-start.rst
155-
[yapf]: https://github.com/google/yapf
155+
[black]: https://github.com/psf/black
156156
[lint-test]: https://github.com/ComplianceAsCode/auditree-framework/actions?query=workflow%3A%22format+%7C+lint+%7C+test%22
157157
[pypi-upload]: https://github.com/ComplianceAsCode/auditree-framework/actions?query=workflow%3A%22PyPI+upload%22
158158
[credentials]: https://complianceascode.github.io/auditree-framework/design-principles.html#credentials

compliance/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313
# limitations under the License.
1414
"""Compliance automation package."""
1515

16-
__version__ = '1.24.0'
16+
__version__ = "1.24.0"

compliance/agent.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
class ComplianceAgent:
3030
"""Compliance agent class."""
3131

32-
AGENTS_DIR = 'agents'
33-
PUBLIC_KEYS_EVIDENCE_PATH = 'raw/auditree/agent_public_keys.json'
32+
AGENTS_DIR = "agents"
33+
PUBLIC_KEYS_EVIDENCE_PATH = "raw/auditree/agent_public_keys.json"
3434

3535
def __init__(self, name=None, use_agent_dir=True):
3636
"""Construct and initialize the agent object."""
@@ -126,10 +126,9 @@ def hash_and_sign(self, data_bytes):
126126
signature = self.private_key.sign(
127127
hashed.digest(),
128128
padding.PSS(
129-
mgf=padding.MGF1(hashes.SHA256()),
130-
salt_length=padding.PSS.MAX_LENGTH
129+
mgf=padding.MGF1(hashes.SHA256()), salt_length=padding.PSS.MAX_LENGTH
131130
),
132-
Prehashed(hashes.SHA256())
131+
Prehashed(hashes.SHA256()),
133132
)
134133
return hashed.hexdigest(), base64.b64encode(signature).decode()
135134

@@ -150,9 +149,9 @@ def verify(self, data_bytes, signature_b64):
150149
data_bytes,
151150
padding.PSS(
152151
mgf=padding.MGF1(hashes.SHA256()),
153-
salt_length=padding.PSS.MAX_LENGTH
152+
salt_length=padding.PSS.MAX_LENGTH,
154153
),
155-
hashes.SHA256()
154+
hashes.SHA256(),
156155
)
157156
return True
158157
except InvalidSignature:
@@ -163,19 +162,19 @@ def from_config(cls):
163162
"""Load agent from configuration."""
164163
config = get_config()
165164
agent = cls(
166-
name=config.get('agent_name'),
167-
use_agent_dir=config.get('use_agent_dir', True)
165+
name=config.get("agent_name"),
166+
use_agent_dir=config.get("use_agent_dir", True),
168167
)
169-
private_key_path = config.get('agent_private_key')
170-
public_key_path = config.get('agent_public_key')
168+
private_key_path = config.get("agent_private_key")
169+
public_key_path = config.get("agent_public_key")
171170
if private_key_path:
172-
with open(private_key_path, 'rb') as key_file:
171+
with open(private_key_path, "rb") as key_file:
173172
agent.private_key = key_file.read()
174173
agent.public_key = agent.private_key.public_key().public_bytes(
175174
encoding=serialization.Encoding.PEM,
176-
format=serialization.PublicFormat.SubjectPublicKeyInfo
175+
format=serialization.PublicFormat.SubjectPublicKeyInfo,
177176
)
178177
elif public_key_path:
179-
with open(public_key_path, 'rb') as key_file:
178+
with open(public_key_path, "rb") as key_file:
180179
agent.public_key = key_file.read()
181180
return agent

0 commit comments

Comments
 (0)