Skip to content

Commit

Permalink
build: update pre-commit config (#18)
Browse files Browse the repository at this point in the history
* Update pre-commit config

* Run pre-commit on all files. Cleanup

* Add .env to .gitignore
  • Loading branch information
raphodn authored Nov 8, 2023
1 parent fa662c4 commit 9d2e67f
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 115 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
ignore = E203, E501, W503
max-line-length = 88
exclude = .git,__pycache__,build,dist,*_pb2.py,.venv
max-doc-length = 79
max-doc-length = 79
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ ENV/
env.bak/
venv.bak/

# Environment variables
.env

# Spyder project settings
.spyderproject
.spyproject
Expand Down
18 changes: 7 additions & 11 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,28 @@ repos:
- id: end-of-file-fixer
- id: check-yaml
- id: debug-statements
- id: double-quote-string-fixer
- id: name-tests-test
- id: requirements-txt-fixer
- repo: https://github.com/asottile/setup-cfg-fmt
rev: v2.0.0
rev: v2.5.0
hooks:
- id: setup-cfg-fmt
- repo: https://github.com/asottile/reorder_python_imports
rev: v3.8.2
- repo: https://github.com/asottile/reorder-python-imports
rev: v3.12.0
hooks:
- id: reorder-python-imports
args: [--py37-plus, --add-import, 'from __future__ import annotations']
- repo: https://github.com/asottile/add-trailing-comma
rev: v2.2.3
rev: v3.1.0
hooks:
- id: add-trailing-comma
args: [--py36-plus]
- repo: https://github.com/pre-commit/mirrors-autopep8
rev: v1.6.0
- repo: https://github.com/hhatto/autopep8
rev: v2.0.4
hooks:
- id: autopep8
- repo: https://github.com/PyCQA/flake8
rev: 5.0.2
rev: 6.1.0
hooks:
- id: flake8
args: [--ignore=E501]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.971
hooks:
Expand Down
11 changes: 7 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

see [INSTALL.md](https://github.com/openfoodfacts/open-prices/blob/main/INSTALL.md)

## Install pre-commit
## Install pre-commit hooks

This repo uses [pre-commit](https://pre-commit.com/) to enforce code styling, etc. To use it:
```console
pre-commit run
This repo uses [pre-commit](https://pre-commit.com/) to enforce code styling, etc. To install it, run the following (in your virtual environment):

```
pre-commit install
```

Now `pre-commit` will run automatically on `git commit` :)
5 changes: 4 additions & 1 deletion INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Prerequisites

- Python 3.12
- Python 3.10 (lower version may be OK, but untested)
- pip

## Setup
Expand All @@ -21,6 +21,9 @@ source venv/bin/activate
# install
pip install -r requirements.txt
# environment variables
# make a copy of *.env.example* and rename it to *.env*
```

## Run locally
Expand Down
6 changes: 4 additions & 2 deletions app/api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from pathlib import Path

from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse, PlainTextResponse
from fastapi import FastAPI
from fastapi import Request
from fastapi.responses import HTMLResponse
from fastapi.responses import PlainTextResponse
from fastapi.templating import Jinja2Templates
from openfoodfacts.utils import get_logger

Expand Down
21 changes: 8 additions & 13 deletions app/config.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from enum import StrEnum
from enum import Enum

from pydantic_settings import BaseSettings


class LoggingLevel(StrEnum):
NOTSET = "NOTSET"
DEBUG = "DEBUG"
INFO = "INFO"
WARNING = "WARNING"
ERROR = "ERROR"
CRITICAL = "CRITICAL"
class LoggingLevel(Enum):
NOTSET: str = "NOTSET"
DEBUG: str = "DEBUG"
INFO: str = "INFO"
WARNING: str = "WARNING"
ERROR: str = "ERROR"
CRITICAL: str = "CRITICAL"

def to_int(self):
if self is LoggingLevel.NOTSET:
Expand All @@ -27,11 +27,6 @@ def to_int(self):


class Settings(BaseSettings):
postgres_host: str
postgres_user: str
postgres_password: str
postgres_db_name: str
postgres_port: int = 5432
sentry_dns: str | None = None
log_level: LoggingLevel = LoggingLevel.INFO

Expand Down
77 changes: 0 additions & 77 deletions app/models.py
Original file line number Diff line number Diff line change
@@ -1,77 +0,0 @@
import peewee
from playhouse.postgres_ext import BinaryJSONField, PostgresqlExtDatabase
from playhouse.shortcuts import model_to_dict

from app.config import settings

db = PostgresqlExtDatabase(
settings.postgres_db_name,
user=settings.postgres_user,
password=settings.postgres_password,
host=settings.postgres_host,
port=settings.postgres_port,
autoconnect=False,
)


class BaseModel(peewee.Model):
class Meta:
database = db
legacy_table_names = False

def to_dict(self, **kwargs):
return model_to_dict(self, **kwargs)


class Task(BaseModel):
"""Table to store moderation or data quality tasks."""

# Barcode represents the barcode of the product for which the insight was
# generated. It is prefixed by `{ORG_ID}/` for the pro platform.
barcode = peewee.CharField(max_length=100, null=False, index=True)

# Type represents the task type - must match one of the types in
# robotoff.types.TaskType.
type = peewee.CharField(max_length=256, index=True)

# Contains some additional data based on the type of the task from
# above.
data = BinaryJSONField(index=True, default=dict)

# created_at is the timestamp of when this task was created in the DB.
created_at = peewee.DateTimeField(null=True, index=True)

# Stores the timestamp of when this task was completed, either by a
# human or automatically.
completed_at = peewee.DateTimeField(null=True)

# boolean indicating if the task has been completed, either automatically
# or by a human
is_completed = peewee.BooleanField(null=True, index=True)

# If the insight was annotated manually, this field stores the username of
# the annotator (or first annotator, if multiple votes were cast).
completed_by = peewee.TextField(index=True, null=True)

# Specifies the timestamp on a task (that can be applied automatically)
# after which the task should be applied.
process_after = peewee.DateTimeField(null=True)

# If the task is about an image, this field points to that image.
image = peewee.ForeignKeyField(ImageModel, null=False, backref="tasks")

# Automatic processing is set on tasks where no human intervention is
# necessary.
automatic_processing = peewee.BooleanField(default=False, index=True)

# the server_type specifies which project we're using
server_type = peewee.CharField(
null=True,
max_length=10,
help_text="project associated with the insight, "
"one of 'off', 'obf', 'opff', 'opf', 'off-pro'",
index=True,
)

def get_product_id(self) -> ProductIdentifier:
return ProductIdentifier(self.barcode, ServerType[self.server_type])
2 changes: 1 addition & 1 deletion app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def init_sentry(sentry_dsn: str | None, integrations: list[Integration] | None =
LoggingIntegration(
level=logging.INFO, # Capture info and above as breadcrumbs
event_level=logging.WARNING, # Send warning and errors as events
)
),
)
sentry_sdk.init( # type:ignore # mypy say it's abstract
sentry_dsn,
Expand Down
2 changes: 1 addition & 1 deletion docker/prod.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
version: "3.7"

# modifications to docker-compose for production settings
# modifications to docker-compose for production settings
8 changes: 4 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
fastapi==0.103.1
uvicorn==0.23.2
jinja2==3.1.2
openfoodfacts==0.1.10
requests==2.31.0
peewee==3.17.0
pydantic-settings==2.0.3
requests==2.31.0
sentry-sdk[fastapi]==1.31.0
jinja2==3.1.2
peewee==3.17.0
uvicorn==0.23.2

0 comments on commit 9d2e67f

Please sign in to comment.