Skip to content

Commit

Permalink
[SW-164] Add ruff and black formatting to spot_wrapper (#84)
Browse files Browse the repository at this point in the history
## Change Overview

This PR enforces `ruff` and `black` linting for all files in `spot_wrapper`.  Pre-commit hooks have been added, and all existing code that did not meet these style requirements has been modified to do so. The configuration files added were taken from the [`spot_ros2` repository](https://github.com/bdaiinstitute/spot_ros2/). 

## Testing Done

I tested a few general Spot examples (move the arm, walk forward) that rely on `spot_wrapper` and the functionality was unchanged.
  • Loading branch information
khughes-bdai authored Jan 31, 2024
1 parent f9516d8 commit 6b68c50
Show file tree
Hide file tree
Showing 34 changed files with 502 additions and 1,032 deletions.
17 changes: 0 additions & 17 deletions .github/workflows/black.yml

This file was deleted.

20 changes: 20 additions & 0 deletions .github/workflows/util_pre-commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright (c) 2023 Boston Dynamics AI Institute, Inc. All rights reserved.

name: Util - Pre-Commit Runner

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
pre-commit:
name: util_pre-commit
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v3
- uses: pre-commit/action@v3.0.0
23 changes: 23 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright (c) 2023 Boston Dynamics AI Institute, Inc. All rights reserved.

repos:
- repo: https://github.com/charliermarsh/ruff-pre-commit
# Ruff version.
rev: 'v0.0.263'
hooks:
- id: ruff
args: ['--fix', '--config', 'pyproject.toml']
- repo: https://github.com/psf/black
rev: 23.3.0
hooks:
- id: black
language_version: python3.10
args: ['--config', 'pyproject.toml']
verbose: true
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: check-yaml
- id: check-added-large-files
- id: check-toml
- id: end-of-file-fixer
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ Copyright (c) 2020 Boston Dynamics, Inc. All rights reserved.

Downloading, reproducing, distributing or otherwise using the SDK Software
is subject to the terms and conditions of the Boston Dynamics Software
Development Kit License (20191101-BDSDK-SL).
Development Kit License (20191101-BDSDK-SL).
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@ To update requirements.txt, use
```commandline
pipreqs . --force
```

# Contributing
This repository enforces `ruff` and `black` linting. To verify that your code will pass inspection, install `pre-commit` and run:
```bash
pre-commit install
pre-commit run --all-files
```
The [Google Style Guide](https://google.github.io/styleguide/) is followed for default formatting.
48 changes: 48 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
[tool.ruff]
# Enable pycodestyle (`E`), Pyflakes (`F`), and import sorting (`I`)
select = ["E", "F", "I"]
ignore = []
fixable = ["ALL"]
unfixable = []
exclude = [
".bzr",
".direnv",
".eggs",
".git",
".hg",
".mypy_cache",
".nox",
".pants.d",
".pytype",
".ruff_cache",
".svn",
".tox",
".venv",
"__pypackages__",
"_build",
"buck-out",
"build",
"dist",
"node_modules",
"venv",
"docker/ros",
]
line-length = 120
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"
target-version = "py38"

[tool.ruff.per-file-ignores]
"__init__.py" = ["F401"]

[tool.ruff.mccabe]
max-complexity = 10

[tool.black]
line-length = 120
target-version = ['py38']
include = '\.pyi?$'
force-exclude = '''
/(
)/
'''
preview = true
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from setuptools import setup, find_packages
from setuptools import find_packages, setup

setup(
name="spot_wrapper",
Expand Down
11 changes: 4 additions & 7 deletions spot_wrapper/cam_webrtc_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,16 @@ def send_sdp_answer_to_spot_cam(self, token, offer_id, sdp_answer):
server_url = f"https://{self.hostname}:{self.sdp_port}/{self.sdp_filename}"

payload = {"id": offer_id, "sdp": base64.b64encode(sdp_answer).decode("utf8")}
r = requests.post(
server_url, verify=self.cam_ssl_cert, json=payload, headers=headers
)
r = requests.post(server_url, verify=self.cam_ssl_cert, json=payload, headers=headers)
if r.status_code != 200:
raise ValueError(r)

async def start(self):
# first get a token
try:
token = self.get_bearer_token()
except:
except Exception as e:
print(f"Could not get bearer token, mocking instead. Exception: {e}")
token = self.get_bearer_token(mock=True)

offer_id, sdp_offer = self.get_sdp_offer_from_spot_cam(token)
Expand All @@ -101,9 +100,7 @@ async def _on_ice_connection_state_change():
print(f"ICE connection state changed to: {self.pc.iceConnectionState}")

if self.pc.iceConnectionState == "checking":
self.send_sdp_answer_to_spot_cam(
token, offer_id, self.pc.localDescription.sdp.encode()
)
self.send_sdp_answer_to_spot_cam(token, offer_id, self.pc.localDescription.sdp.encode())

@self.pc.on("track")
def _on_track(track):
Expand Down
Loading

0 comments on commit 6b68c50

Please sign in to comment.