Skip to content

Commit 88e49d3

Browse files
authored
Merge pull request #53 from codeguru42/51-black
51 black
2 parents 2c2fd5c + b8e5760 commit 88e49d3

File tree

17 files changed

+378
-84
lines changed

17 files changed

+378
-84
lines changed

.github/workflows/build.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ jobs:
1010
- name: Checkout repo
1111
uses: actions/checkout@v3
1212

13+
- name: black
14+
uses: psf/black@stable
15+
1316
- name: Configure AWS credentials
1417
uses: aws-actions/configure-aws-credentials@v1-node16
1518
with:

.pre-commit-config.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# See https://pre-commit.com for more information
2+
# See https://pre-commit.com/hooks.html for more hooks
3+
repos:
4+
- repo: https://github.com/pre-commit/pre-commit-hooks
5+
rev: v3.2.0
6+
hooks:
7+
- id: trailing-whitespace
8+
- id: end-of-file-fixer
9+
- id: check-added-large-files
10+
- repo: https://github.com/psf/black
11+
rev: 23.3.0
12+
hooks:
13+
- id: black

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Go Capture API
22

33
![Build](https://github.com/codeguru42/go-capture-api/actions/workflows/build.yaml/badge.svg)
4+
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
45

56
Go Capture API is a REST API that will generate an SGF file from an image of a Go position.
67

@@ -9,6 +10,12 @@ Go Capture API is a REST API that will generate an SGF file from an image of a G
910
* Python 3.11+
1011
* Docker and Docker Compose
1112

13+
## Commit hooks
14+
15+
```shell
16+
pre-commit install
17+
```
18+
1219
## Run
1320

1421
```shell

go_capture/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from .tasks import app as celery_app
22

3-
__all__ = ('celery_app',)
3+
__all__ = ("celery_app",)

go_capture/asgi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111

1212
from django.core.asgi import get_asgi_application
1313

14-
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'go_capture.settings')
14+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "go_capture.settings")
1515

1616
application = get_asgi_application()

go_capture/settings.py

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -23,62 +23,62 @@
2323
# Quick-start development settings - unsuitable for production
2424
# See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/
2525

26-
SECRET_KEY = env.str('DJANGO_SECRET_KEY')
26+
SECRET_KEY = env.str("DJANGO_SECRET_KEY")
2727

2828
# SECURITY WARNING: don't run with debug turned on in production!
29-
DEBUG = env.bool('DJANGO_DEBUG', False)
29+
DEBUG = env.bool("DJANGO_DEBUG", False)
3030

31-
ALLOWED_HOSTS = env.list('DJANGO_ALLOWED_HOSTS')
31+
ALLOWED_HOSTS = env.list("DJANGO_ALLOWED_HOSTS")
3232

3333

3434
# Application definition
3535

3636
INSTALLED_APPS = [
37-
'django.contrib.admin',
38-
'django.contrib.auth',
39-
'django.contrib.contenttypes',
40-
'django.contrib.sessions',
41-
'django.contrib.messages',
42-
'django.contrib.staticfiles',
37+
"django.contrib.admin",
38+
"django.contrib.auth",
39+
"django.contrib.contenttypes",
40+
"django.contrib.sessions",
41+
"django.contrib.messages",
42+
"django.contrib.staticfiles",
4343
]
4444

4545
MIDDLEWARE = [
46-
'django.middleware.security.SecurityMiddleware',
47-
'django.contrib.sessions.middleware.SessionMiddleware',
48-
'django.middleware.common.CommonMiddleware',
49-
'django.contrib.auth.middleware.AuthenticationMiddleware',
50-
'django.contrib.messages.middleware.MessageMiddleware',
51-
'django.middleware.clickjacking.XFrameOptionsMiddleware',
46+
"django.middleware.security.SecurityMiddleware",
47+
"django.contrib.sessions.middleware.SessionMiddleware",
48+
"django.middleware.common.CommonMiddleware",
49+
"django.contrib.auth.middleware.AuthenticationMiddleware",
50+
"django.contrib.messages.middleware.MessageMiddleware",
51+
"django.middleware.clickjacking.XFrameOptionsMiddleware",
5252
]
5353

54-
ROOT_URLCONF = 'go_capture.urls'
54+
ROOT_URLCONF = "go_capture.urls"
5555

5656
TEMPLATES = [
5757
{
58-
'BACKEND': 'django.template.backends.django.DjangoTemplates',
59-
'DIRS': [],
60-
'APP_DIRS': True,
61-
'OPTIONS': {
62-
'context_processors': [
63-
'django.template.context_processors.debug',
64-
'django.template.context_processors.request',
65-
'django.contrib.auth.context_processors.auth',
66-
'django.contrib.messages.context_processors.messages',
58+
"BACKEND": "django.template.backends.django.DjangoTemplates",
59+
"DIRS": [],
60+
"APP_DIRS": True,
61+
"OPTIONS": {
62+
"context_processors": [
63+
"django.template.context_processors.debug",
64+
"django.template.context_processors.request",
65+
"django.contrib.auth.context_processors.auth",
66+
"django.contrib.messages.context_processors.messages",
6767
],
6868
},
6969
},
7070
]
7171

72-
WSGI_APPLICATION = 'go_capture.wsgi.application'
72+
WSGI_APPLICATION = "go_capture.wsgi.application"
7373

7474

7575
# Database
7676
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases
7777

7878
DATABASES = {
79-
'default': {
80-
'ENGINE': 'django.db.backends.sqlite3',
81-
'NAME': BASE_DIR / 'db.sqlite3',
79+
"default": {
80+
"ENGINE": "django.db.backends.sqlite3",
81+
"NAME": BASE_DIR / "db.sqlite3",
8282
}
8383
}
8484

@@ -88,26 +88,26 @@
8888

8989
AUTH_PASSWORD_VALIDATORS = [
9090
{
91-
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
91+
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
9292
},
9393
{
94-
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
94+
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
9595
},
9696
{
97-
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
97+
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
9898
},
9999
{
100-
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
100+
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
101101
},
102102
]
103103

104104

105105
# Internationalization
106106
# https://docs.djangoproject.com/en/4.1/topics/i18n/
107107

108-
LANGUAGE_CODE = 'en-us'
108+
LANGUAGE_CODE = "en-us"
109109

110-
TIME_ZONE = 'UTC'
110+
TIME_ZONE = "UTC"
111111

112112
USE_I18N = True
113113

@@ -117,21 +117,21 @@
117117
# Static files (CSS, JavaScript, Images)
118118
# https://docs.djangoproject.com/en/4.1/howto/static-files/
119119

120-
STATIC_URL = 'static/'
120+
STATIC_URL = "static/"
121121

122122
# Default primary key field type
123123
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field
124124

125-
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
125+
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
126126

127-
MEDIA_URL = 'media/'
128-
MEDIA_ROOT = BASE_DIR / 'media'
127+
MEDIA_URL = "media/"
128+
MEDIA_ROOT = BASE_DIR / "media"
129129
MEDIA_ROOT.mkdir(exist_ok=True)
130130

131-
IMAGES_DIR = MEDIA_ROOT / 'images'
131+
IMAGES_DIR = MEDIA_ROOT / "images"
132132
IMAGES_DIR.mkdir(exist_ok=True)
133133

134-
SGF_DIR = MEDIA_ROOT / 'sgf'
134+
SGF_DIR = MEDIA_ROOT / "sgf"
135135
SGF_DIR.mkdir(exist_ok=True)
136136

137-
FIREBASE_CREDENTIALS_FILE = BASE_DIR / env.str('FIREBASE_CREDENTIALS_FILE')
137+
FIREBASE_CREDENTIALS_FILE = BASE_DIR / env.str("FIREBASE_CREDENTIALS_FILE")

go_capture/sgf/find_stones.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def draw_patches(image, coords, color):
5656

5757
def get_clusters(board):
5858
pixels = np.float32(board.reshape(-1, 3))
59-
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 200, .1)
59+
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 200, 0.1)
6060
flags = cv2.KMEANS_RANDOM_CENTERS
6161
_, labels, palette = cv2.kmeans(pixels, 6, None, criteria, 10, flags)
6262
width, height, _ = board.shape

go_capture/sgf/make_sgf.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ def write_stones(file, stones):
44

55

66
def make_sgf(file, black, white):
7-
file.write('(;FF[4]\n')
8-
file.write('GM[1]\n')
9-
file.write(';AB')
7+
file.write("(;FF[4]\n")
8+
file.write("GM[1]\n")
9+
file.write(";AB")
1010
write_stones(file, black)
11-
file.write('\n;AW')
11+
file.write("\n;AW")
1212
write_stones(file, white)
13-
file.write('\n)\n')
13+
file.write("\n)\n")

go_capture/sgf/perspective.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ def perspective_transform(image, corners):
2424

2525
# Construct new points to obtain top-down view of image in
2626
# top_r, top_l, bottom_l, bottom_r order
27-
dimensions = np.array([[0, 0], [width - 1, 0], [width - 1, height - 1], [0, height - 1]], dtype="float32")
27+
dimensions = np.array(
28+
[[0, 0], [width - 1, 0], [width - 1, height - 1], [0, height - 1]],
29+
dtype="float32",
30+
)
2831

2932
# Convert to Numpy format
3033
ordered_corners = np.array((top_l, top_r, bottom_r, bottom_l), dtype="float32")
@@ -40,7 +43,9 @@ def get_corners(image):
4043
copy = image.copy()
4144
gray = cv2.cvtColor(copy, cv2.COLOR_BGR2GRAY)
4245
blur = cv2.medianBlur(gray, 3)
43-
thresh = cv2.adaptiveThreshold(blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 3)
46+
thresh = cv2.adaptiveThreshold(
47+
blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 3
48+
)
4449
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
4550
sorted_contours = sorted(contours, key=cv2.contourArea, reverse=True)
4651
return sorted_contours[0]

go_capture/tasks.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
from go_capture.sgf.process_image import process_image
1010

1111
# Set the default Django settings module for the 'celery' program.
12-
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'go_capture.settings')
12+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "go_capture.settings")
1313

14-
app = Celery('go_capture')
14+
app = Celery("go_capture")
1515

1616
# Using a string here means the worker doesn't have to serialize
1717
# the configuration object to child processes.
1818
# - namespace='CELERY' means all celery-related configuration keys
1919
# should have a `CELERY_` prefix.
20-
app.config_from_object('django.conf:settings', namespace='CELERY')
20+
app.config_from_object("django.conf:settings", namespace="CELERY")
2121

2222
# Load task modules from all registered Django apps.
2323
app.autodiscover_tasks()
@@ -28,14 +28,14 @@
2828

2929
@app.task(bind=True)
3030
def debug_task(self):
31-
print(f'Request: {self.request!r}')
31+
print(f"Request: {self.request!r}")
3232

3333

3434
@app.task
3535
def process_image_task(image_filename, fcm_token):
36-
print(f'token: {fcm_token}')
37-
print(f'Processing {image_filename}')
38-
with open(image_filename, 'rb') as image_file:
36+
print(f"token: {fcm_token}")
37+
print(f"Processing {image_filename}")
38+
with open(image_filename, "rb") as image_file:
3939
sgf_buffer = io.StringIO()
4040
process_image(image_file, sgf_buffer)
4141

@@ -44,7 +44,7 @@ def process_image_task(image_filename, fcm_token):
4444
print(sgf_data)
4545
message = messaging.Message(
4646
data={
47-
'sgf': sgf_data,
47+
"sgf": sgf_data,
4848
},
4949
token=fcm_token,
5050
)
@@ -53,4 +53,4 @@ def process_image_task(image_filename, fcm_token):
5353
# registration token.
5454
response = messaging.send(message)
5555
# Response is a message ID string.
56-
print('Successfully sent message:', response)
56+
print("Successfully sent message:", response)

0 commit comments

Comments
 (0)