Skip to content

Commit

Permalink
Use Ruff instead of flake8 and isort and bandit
Browse files Browse the repository at this point in the history
Signed-off-by: Aurélien Bompard <[email protected]>
  • Loading branch information
abompard committed Mar 29, 2024
1 parent 5cff54a commit 4f7ffaa
Show file tree
Hide file tree
Showing 21 changed files with 177 additions and 382 deletions.
5 changes: 0 additions & 5 deletions .flake8

This file was deleted.

23 changes: 5 additions & 18 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,10 @@ repos:
rev: 24.3.0
hooks:
- id: black
args: ["--check", "--diff"]

- repo: https://github.com/pycqa/flake8
rev: 7.0.0
# Ruff
- repo: https://github.com/charliermarsh/ruff-pre-commit
# Ruff version.
rev: v0.3.3
hooks:
- id: flake8

- repo: https://github.com/pycqa/isort
rev: 5.13.2
hooks:
- id: isort
args: ["-c", "--df"]

- repo: https://github.com/PyCQA/bandit
rev: 1.7.8
hooks:
- id: bandit
args: ["-ll", "-x", "*/tests/*"]
# All files in one go to get a single report
require_serial: true
- id: ruff
46 changes: 15 additions & 31 deletions datanommer.commands/datanommer/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
import importlib.metadata
import itertools
import json
import logging
import time
Expand All @@ -37,9 +38,7 @@ def get_config(config_path=None):
conf = fedora_messaging_config.conf["consumer_config"]
for key in ("datanommer_sqlalchemy_url", "alembic_ini"):
if key not in conf:
raise click.ClickException(
f"{key} not defined in the fedora-messaging config"
)
raise click.ClickException(f"{key} not defined in the fedora-messaging config")
return conf


Expand Down Expand Up @@ -67,12 +66,8 @@ def create(config_path):

@click.command()
@config_option
@click.option(
"--since", default=None, help="Only after datetime, ex 2013-02-14T08:05:59.87"
)
@click.option(
"--before", default=None, help="Only before datetime, ex 2013-02-14T08:05:59.87"
)
@click.option("--since", default=None, help="Only after datetime, ex 2013-02-14T08:05:59.87")
@click.option("--before", default=None, help="Only before datetime, ex 2013-02-14T08:05:59.87")
def dump(config_path, since, before):
"""Dump the contents of the datanommer database as JSON.
Expand All @@ -90,22 +85,20 @@ def dump(config_path, since, before):
if before:
try:
before = datetime.fromisoformat(before)
except ValueError:
raise click.ClickException("Invalid date format")
except ValueError as e:
raise click.ClickException("Invalid date format") from e

query = query.where(m.Message.timestamp <= before)

if since:
try:
since = datetime.fromisoformat(since)
except ValueError:
raise click.ClickException("Invalid date format")
except ValueError as e:
raise click.ClickException("Invalid date format") from e

query = query.where(m.Message.timestamp >= since)

results = [
json.dumps(msg.as_fedora_message_dict()) for msg in m.session.scalars(query)
]
results = [json.dumps(msg.as_fedora_message_dict()) for msg in m.session.scalars(query)]
click.echo(f"[{','.join(results)}]")


Expand Down Expand Up @@ -184,20 +177,14 @@ def stats(config_path, topic, category):

@click.command()
@config_option
@click.option(
"--topic", default=None, help="Show the latest for only a specific topic."
)
@click.option(
"--category", default=None, help="Show the latest for only a specific category."
)
@click.option("--topic", default=None, help="Show the latest for only a specific topic.")
@click.option("--category", default=None, help="Show the latest for only a specific category.")
@click.option(
"--overall",
is_flag=True,
help="Show only the latest message out of all message types.",
)
@click.option(
"--timestamp", is_flag=True, help="Show only the timestamp of the message(s)."
)
@click.option("--timestamp", is_flag=True, help="Show only the timestamp of the message(s).")
@click.option(
"--timesince",
is_flag=True,
Expand Down Expand Up @@ -300,13 +287,10 @@ def latest(config_path, topic, category, overall, timestamp, timesince, human):
queries = [select(m.Message).where(m.Message.category == category)]
elif not overall:
# If no args..
categories_query = (
select(m.Message.category).distinct().order_by(m.Message.category)
)
categories_query = select(m.Message.category).distinct().order_by(m.Message.category)
categories = m.session.scalars(categories_query)
queries = [
select(m.Message).where(m.Message.category == category)
for category in categories
select(m.Message).where(m.Message.category == category) for category in categories
]
else:
# Show only the single latest message, regardless of type.
Expand Down Expand Up @@ -334,7 +318,7 @@ def formatter(key, val):
return f'{{"{key}": {json.dumps(val.as_fedora_message_dict())}}}'

results = []
for result in sum((list(m.session.scalars(query)) for query in queries), []):
for result in itertools.chain.from_iterable(m.session.scalars(query) for query in queries):
results.append(formatter(result.category, result))

click.echo(f"[{','.join(results)}]")
4 changes: 1 addition & 3 deletions datanommer.commands/datanommer/commands/extract_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@

@click.command()
@config_option
@click.option(
"--topic", default=None, help="Only extract users for messages of a specific topic."
)
@click.option("--topic", default=None, help="Only extract users for messages of a specific topic.")
@click.option(
"--category",
default=None,
Expand Down
94 changes: 28 additions & 66 deletions datanommer.commands/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions datanommer.commands/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ python = "^3.10"
"datanommer.models" = "^1.0.0"
fedora-messaging = ">=2.1.0"

[tool.poetry.dev-dependencies]
[tool.poetry.group.dev.dependencies]
pre-commit = "*"
"datanommer.models" = {path = "../datanommer.models", develop = true}
black = "*"
isort = "*"
flake8 = "*"
ruff = "*"
pytest = "*"
liccheck = "*"
pytest-cov = "*"
Expand Down
Loading

0 comments on commit 4f7ffaa

Please sign in to comment.