Skip to content

Commit 4d1384c

Browse files
authored
Merge pull request #34 from moevm/33_pre-commit-hook
pre-commit hook with black
2 parents 25dad16 + b221280 commit 4d1384c

File tree

5 files changed

+71
-16
lines changed

5 files changed

+71
-16
lines changed

.pre-commit-config.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
repos:
2+
- repo: https://github.com/psf/black
3+
rev: 24.4.2
4+
hooks:
5+
- id: black
6+
name: Format code with Black
7+
language: python
8+
types: [python]
9+
args: [--line-length=120]
10+
stages: [pre-commit, commit]
11+
additional_dependencies: []
12+
require_serial: false
13+
verbose: true

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,19 @@
88
-- [Scripts and Yocto Layers to build VM image with all dependencies](https://github.com/moevm/vm_build_risc_v)
99

1010
-- [Sources to build and run gRPC server and worker nodes](https://github.com/moevm/grpc_server)
11+
12+
## Contribution
13+
14+
### Recommendations for working with the repository
15+
16+
#### Setting up a pre-commit hook for formatting code using Black
17+
18+
- The black configuration settings are located in .pre-commit-config.yaml
19+
20+
- After cloning the project, download all the necessary dependencies using `pip install -r requirements.txt` in a virtual environment. Run the `pre-commit install` command in the virtual environment once. Next, the hook will be triggered with each commit.
21+
22+
- If black finds a problem in the code, he will fix it, and you will need to repeat `git add .` and `git commit -m "name_commit"`
23+
24+
- You can check for a hook with `cat command.git/hooks/pre-commit` if it returns the code then the hook is installed.
25+
26+
- Additional information is described in the file [using_black.md](wiki/using_black.md)

requirements.txt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
1+
black==25.1.0
2+
cfgv==3.4.0
3+
click==8.1.8
14
coverage==7.7.0
5+
distlib==0.3.9
6+
filelock==3.18.0
27
grpcio==1.70.0
38
grpcio-tools==1.70.0
9+
identify==2.6.9
410
iniconfig==2.0.0
11+
mypy-extensions==1.0.0
12+
nodeenv==1.9.1
513
packaging==24.2
14+
pathspec==0.12.1
15+
platformdirs==4.3.7
616
pluggy==1.5.0
17+
pre_commit==4.2.0
718
protobuf==5.29.3
819
pytest==8.3.5
920
pytest-cov==6.0.0
1021
python-dotenv==1.0.1
11-
setuptools==75.8.2
22+
PyYAML==6.0.2
23+
setuptools==75.8.2
24+
virtualenv==20.29.3

src/client/client.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,22 @@
55
from src.protobuf import file_service_pb2
66
from src.protobuf import file_service_pb2_grpc
77

8-
load_dotenv(os.path.join(os.path.dirname(__file__), '..', '..', 'env', '.grpc.env'))
8+
load_dotenv(os.path.join(os.path.dirname(__file__), "..", "..", "env", ".grpc.env"))
99

1010

1111
class FileClient:
1212
def __init__(self):
13-
self.host: str = os.environ['GRPC_CLIENT_HOST']
14-
self.port: str = os.environ['GRPC_PORT']
15-
max_message_length: int = int(os.environ['GRPC_MAX_MESSAGE_LENGTH'])
13+
self.host: str = os.environ["GRPC_CLIENT_HOST"]
14+
self.port: str = os.environ["GRPC_PORT"]
15+
max_message_length: int = int(os.environ["GRPC_MAX_MESSAGE_LENGTH"])
1616

1717
options = [
18-
('grpc.max_receive_message_length', max_message_length),
19-
('grpc.max_send_message_length', max_message_length),
18+
("grpc.max_receive_message_length", max_message_length),
19+
("grpc.max_send_message_length", max_message_length),
2020
]
2121

22-
self.channel: grpc.Channel = grpc.insecure_channel(
23-
f'{self.host}:{self.port}',
24-
options=options
25-
)
22+
self.channel: grpc.Channel = grpc.insecure_channel(f"{self.host}:{self.port}", options=options)
23+
2624
self.stub: file_service_pb2_grpc.FileServiceStub = file_service_pb2_grpc.FileServiceStub(self.channel)
2725

2826
def upload_and_validate(self, file_path: str, file_type: str) -> file_service_pb2.FileResponse:
@@ -39,13 +37,11 @@ def upload_and_validate(self, file_path: str, file_type: str) -> file_service_pb
3937
4038
`file_service_pb2.FileResponse` - Server response with validation result
4139
"""
42-
with open(file_path, 'rb') as f:
40+
with open(file_path, "rb") as f:
4341
content: bytes = f.read()
4442

4543
request: file_service_pb2.FileRequest = file_service_pb2.FileRequest(
46-
filename=file_path,
47-
content=content,
48-
file_type=file_type
44+
filename=file_path, content=content, file_type=file_type
4945
)
5046

5147
return self.stub.UploadFile(request)
@@ -63,5 +59,5 @@ def test_file_upload():
6359
print(f"Binary file: Size={response.size}, Valid={response.is_valid}, Msg={response.message}")
6460

6561

66-
if __name__ == '__main__':
62+
if __name__ == "__main__":
6763
test_file_upload()

wiki/using_black.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## Useful:
2+
If a block cannot be formatted (for example, due to compatibility reasons), add a comment:
3+
```
4+
# fmt: off
5+
non_formatted_code = {
6+
"key": "value",
7+
"data": [1, 2, 3,]
8+
}
9+
# fmt: on
10+
```
11+
- \# fmt: off — before the code that should not be formatted.
12+
13+
- \# fmt: on — after this code, to enable auto-formatting again.
14+
15+
## Links:
16+
- The Black documentation: https://black.readthedocs.io
17+
- Config examples: https://github.com/pre-commit/pre-commit-hooks

0 commit comments

Comments
 (0)