Skip to content

Commit

Permalink
Add and enforce linter and code formatter (#89)
Browse files Browse the repository at this point in the history
* Add linting with ruff

* Remove accidentally commited metrics file

* Add wheel
  • Loading branch information
stchris authored Jun 8, 2023
1 parent a3c8850 commit 526094f
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 9 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ jobs:
sudo rm -f /etc/boto.cfg
sudo apt-get -qq update
sudo apt-get install -y libicu-dev
pip install pytest pytest-env pytest-cov pytest-mock wheel
make dev
pip install -e ".[dev]"
- name: Run the code format check
run: make format-check
- name: Run the linter
run: make lint
- name: Run the tests
run: |
make test
Expand All @@ -36,4 +40,4 @@ jobs:
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.pypi_password }}
password: ${{ secrets.pypi_password }}
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,23 @@ install:
pip install -q -e .
pip install -q twine coverage nose moto boto3

dev:
python3 -m pip install --upgrade pip
python3 -m pip install -q -r requirements.txt
python3 -m pip install -q -r requirements-dev.txt

test:
docker-compose run --rm shell pytest --cov=servicelayer

lint:
ruff check .

format:
black .

format-check:
black --check .

shell:
docker-compose run --rm shell

Expand Down
7 changes: 7 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
black==23.3.0
ruff==0.0.270
pytest==7.3.1
pytest-env==0.8.1
pytest-cov==4.1.0
pytest-mock==3.10.0
wheel==0.40.0
44 changes: 44 additions & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Enable pycodestyle (`E`) and Pyflakes (`F`) codes by default.
select = ["E", "F"]
ignore = []

# Allow autofix for all enabled rules (when `--fix`) is provided.
fixable = ["A", "B", "C", "D", "E", "F", "G", "I", "N", "Q", "S", "T", "W", "ANN", "ARG", "BLE", "COM", "DJ", "DTZ", "EM", "ERA", "EXE", "FBT", "ICN", "INP", "ISC", "NPY", "PD", "PGH", "PIE", "PL", "PT", "PTH", "PYI", "RET", "RSE", "RUF", "SIM", "SLF", "TCH", "TID", "TRY", "UP", "YTT"]
unfixable = []

# Exclude a variety of commonly ignored directories.
exclude = [
".bzr",
".direnv",
".eggs",
".git",
".git-rewrite",
".hg",
".mypy_cache",
".nox",
".pants.d",
".pytype",
".ruff_cache",
".svn",
".tox",
".venv",
"__pypackages__",
"_build",
"buck-out",
"build",
"dist",
"node_modules",
"venv",
]

# Same as Black.
line-length = 88

# Allow unused variables when underscore-prefixed.
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"

target-version = "py38"

[mccabe]
# Unlike Flake8, default to a complexity level of 10.
max-complexity = 10
5 changes: 4 additions & 1 deletion servicelayer/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ def _upsert_values(self, conn, row):
istmt = upsert(self.table).values(row)
stmt = istmt.on_conflict_do_update(
index_elements=["key"],
set_=dict(value=istmt.excluded.value, timestamp=istmt.excluded.timestamp,),
set_=dict(
value=istmt.excluded.value,
timestamp=istmt.excluded.timestamp,
),
)
conn.execute(stmt)

Expand Down
12 changes: 8 additions & 4 deletions servicelayer/taskqueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,9 @@ def cleanup_dataset_status(cls, conn):
def should_execute(self, task_id):
"""Should a task be executed?
When a the processing of a task is cancelled, there is no way to tell RabbitMQ to drop it.
So we store that state in Redis and make the worker check with Redis before executing a task.
When a the processing of a task is cancelled, there is no way
to tell RabbitMQ to drop it. So we store that state in Redis
and make the worker check with Redis before executing a task.
"""
attempt = 1
while True:
Expand Down Expand Up @@ -369,7 +370,8 @@ def ack_message(self, task, channel):
skip_ack = task.context.get("skip_ack")
if skip_ack:
log.info(
f"Skipping acknowledging message {task.delivery_tag} for task_id {task.task_id}"
f"Skipping acknowledging message {task.delivery_tag}"
"for task_id {task.task_id}"
)
else:
log.info(
Expand All @@ -390,7 +392,9 @@ def run(self):
signal.signal(signal.SIGTERM, self.on_signal)

# worker threads
process = lambda: self.process(blocking=True)
def process():
return self.process(blocking=True)

threads = []
for _ in range(self.num_threads):
thread = threading.Thread(target=process)
Expand Down
8 changes: 6 additions & 2 deletions servicelayer/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ def process(self, blocking=True, interval=INTERVAL):
task = Stage.get_task(self.conn, stages, timeout=interval)
if task is None:
if not blocking:
# If we get a null task, retry to fetch a task a bunch of times before quitting
# If we get a null task, retry to fetch a task
# a bunch of times before quitting
if retries >= TASK_FETCH_RETRY:
log.info("Worker thread is exiting")
return self.exit_code
Expand All @@ -111,7 +112,10 @@ def run(self, blocking=True, interval=INTERVAL):
signal.signal(signal.SIGINT, self._handle_signal)
signal.signal(signal.SIGTERM, self._handle_signal)
self.init_internal()
process = lambda: self.process(blocking=blocking, interval=interval)

def process():
return self.process(blocking=blocking, interval=interval)

if not self.num_threads:
return process()
log.info("Worker has %d threads.", self.num_threads)
Expand Down

0 comments on commit 526094f

Please sign in to comment.