Skip to content

Commit a8eb139

Browse files
authored
feat: code quality (#31)
* feat: code quality
1 parent d656d41 commit a8eb139

25 files changed

+3192
-237
lines changed

.github/workflows/ci.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: CI
2+
on:
3+
push:
4+
branches:
5+
- main
6+
pull_request:
7+
8+
jobs:
9+
lint:
10+
uses: lnbits/lnbits/.github/workflows/lint.yml@dev
11+
tests:
12+
runs-on: ubuntu-latest
13+
needs: [lint]
14+
strategy:
15+
matrix:
16+
python-version: ['3.9', '3.10']
17+
steps:
18+
- uses: actions/checkout@v4
19+
- uses: lnbits/lnbits/.github/actions/prepare@dev
20+
with:
21+
python-version: ${{ matrix.python-version }}
22+
- name: Run pytest
23+
uses: pavelzw/pytest-action@v2
24+
env:
25+
LNBITS_BACKEND_WALLET_CLASS: FakeWallet
26+
PYTHONUNBUFFERED: 1
27+
DEBUG: true
28+
with:
29+
verbose: true
30+
job-summary: true
31+
emoji: false
32+
click-to-expand: true
33+
custom-pytest: poetry run pytest
34+
report-title: 'test (${{ matrix.python-version }})'

.github/workflows/release.yml

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
on:
22
push:
33
tags:
4-
- "v[0-9]+.[0-9]+.[0-9]+"
4+
- 'v[0-9]+.[0-9]+.[0-9]+'
55

66
jobs:
7-
87
release:
98
runs-on: ubuntu-latest
109
steps:
@@ -34,12 +33,12 @@ jobs:
3433
- name: Create pull request in extensions repo
3534
env:
3635
GH_TOKEN: ${{ secrets.EXT_GITHUB }}
37-
repo_name: "${{ github.event.repository.name }}"
38-
tag: "${{ github.ref_name }}"
39-
branch: "update-${{ github.event.repository.name }}-${{ github.ref_name }}"
40-
title: "[UPDATE] ${{ github.event.repository.name }} to ${{ github.ref_name }}"
41-
body: "https://github.com/lnbits/${{ github.event.repository.name }}/releases/${{ github.ref_name }}"
42-
archive: "https://github.com/lnbits/${{ github.event.repository.name }}/archive/refs/tags/${{ github.ref_name }}.zip"
36+
repo_name: '${{ github.event.repository.name }}'
37+
tag: '${{ github.ref_name }}'
38+
branch: 'update-${{ github.event.repository.name }}-${{ github.ref_name }}'
39+
title: '[UPDATE] ${{ github.event.repository.name }} to ${{ github.ref_name }}'
40+
body: 'https://github.com/lnbits/${{ github.event.repository.name }}/releases/${{ github.ref_name }}'
41+
archive: 'https://github.com/lnbits/${{ github.event.repository.name }}/archive/refs/tags/${{ github.ref_name }}.zip'
4342
run: |
4443
cd lnbits-extensions
4544
git checkout -b $branch

.gitignore

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,4 @@
1-
.DS_Store
2-
._*
3-
41
__pycache__
5-
*.py[cod]
6-
*$py.class
2+
node_modules
73
.mypy_cache
8-
.vscode
9-
*-lock.json
10-
11-
*.egg
12-
*.egg-info
13-
.coverage
14-
.pytest_cache
15-
.webassets-cache
16-
htmlcov
17-
test-reports
18-
tests/data/*.sqlite3
19-
20-
*.swo
21-
*.swp
22-
*.pyo
23-
*.pyc
24-
*.env
4+
.venv

.prettierrc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"semi": false,
3+
"arrowParens": "avoid",
4+
"insertPragma": false,
5+
"printWidth": 80,
6+
"proseWrap": "preserve",
7+
"singleQuote": true,
8+
"trailingComma": "none",
9+
"useTabs": false,
10+
"bracketSameLine": false,
11+
"bracketSpacing": false
12+
}

Makefile

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
all: format check
2+
3+
format: prettier black ruff
4+
5+
check: mypy pyright checkblack checkruff checkprettier
6+
7+
prettier:
8+
poetry run ./node_modules/.bin/prettier --write .
9+
pyright:
10+
poetry run ./node_modules/.bin/pyright
11+
12+
mypy:
13+
poetry run mypy .
14+
15+
black:
16+
poetry run black .
17+
18+
ruff:
19+
poetry run ruff check . --fix
20+
21+
checkruff:
22+
poetry run ruff check .
23+
24+
checkprettier:
25+
poetry run ./node_modules/.bin/prettier --check .
26+
27+
checkblack:
28+
poetry run black --check .
29+
30+
checkeditorconfig:
31+
editorconfig-checker
32+
33+
test:
34+
PYTHONUNBUFFERED=1 \
35+
DEBUG=true \
36+
poetry run pytest
37+
install-pre-commit-hook:
38+
@echo "Installing pre-commit hook to git"
39+
@echo "Uninstall the hook with poetry run pre-commit uninstall"
40+
poetry run pre-commit install
41+
42+
pre-commit:
43+
poetry run pre-commit run --all-files
44+
45+
46+
checkbundle:
47+
@echo "skipping checkbundle"

__init__.py

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
import asyncio
2-
from typing import List
32

43
from fastapi import APIRouter
54
from loguru import logger
65

7-
from lnbits.db import Database
8-
from lnbits.helpers import template_renderer
9-
from lnbits.tasks import create_permanent_unique_task
10-
11-
from .nostr.client.client import NostrClient
12-
from .router import NostrRouter
13-
14-
db = Database("ext_nostrclient")
6+
from .crud import db
7+
from .nostr_client import all_routers, nostr_client
8+
from .tasks import check_relays, init_relays, subscribe_events
9+
from .views import nostrclient_generic_router
10+
from .views_api import nostrclient_api_router
1511

1612
nostrclient_static_files = [
1713
{
@@ -21,23 +17,11 @@
2117
]
2218

2319
nostrclient_ext: APIRouter = APIRouter(prefix="/nostrclient", tags=["nostrclient"])
24-
25-
nostr_client: NostrClient = NostrClient()
26-
27-
# we keep this in
28-
all_routers: list[NostrRouter] = []
20+
nostrclient_ext.include_router(nostrclient_generic_router)
21+
nostrclient_ext.include_router(nostrclient_api_router)
2922
scheduled_tasks: list[asyncio.Task] = []
3023

3124

32-
def nostr_renderer():
33-
return template_renderer(["nostrclient/templates"])
34-
35-
36-
from .tasks import check_relays, init_relays, subscribe_events # noqa
37-
from .views import * # noqa
38-
from .views_api import * # noqa
39-
40-
4125
async def nostrclient_stop():
4226
for task in scheduled_tasks:
4327
try:
@@ -56,7 +40,20 @@ async def nostrclient_stop():
5640

5741

5842
def nostrclient_start():
43+
from lnbits.tasks import create_permanent_unique_task
44+
5945
task1 = create_permanent_unique_task("ext_nostrclient_init_relays", init_relays)
60-
task2 = create_permanent_unique_task("ext_nostrclient_subscrive_events", subscribe_events)
46+
task2 = create_permanent_unique_task(
47+
"ext_nostrclient_subscrive_events", subscribe_events
48+
)
6149
task3 = create_permanent_unique_task("ext_nostrclient_check_relays", check_relays)
6250
scheduled_tasks.extend([task1, task2, task3])
51+
52+
53+
__all__ = [
54+
"db",
55+
"nostrclient_ext",
56+
"nostrclient_static_files",
57+
"nostrclient_stop",
58+
"nostrclient_start",
59+
]

crud.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
from typing import List, Optional
2-
31
import json
2+
from typing import Optional
3+
4+
from lnbits.db import Database
45

5-
from . import db
66
from .models import Config, Relay
77

8+
db = Database("ext_nostrclient")
9+
810

9-
async def get_relays() -> List[Relay]:
11+
async def get_relays() -> list[Relay]:
1012
rows = await db.fetchall("SELECT * FROM nostrclient.relays")
1113
return [Relay.from_row(r) for r in rows]
1214

@@ -37,27 +39,21 @@ async def create_config() -> Config:
3739
INSERT INTO nostrclient.config (json_data)
3840
VALUES (?)
3941
""",
40-
(json.dumps(config.dict())),
41-
)
42-
row = await db.fetchone(
43-
"SELECT json_data FROM nostrclient.config", ()
42+
(json.dumps(config.dict()),),
4443
)
44+
row = await db.fetchone("SELECT json_data FROM nostrclient.config", ())
4545
return json.loads(row[0], object_hook=lambda d: Config(**d))
4646

4747

4848
async def update_config(config: Config) -> Optional[Config]:
4949
await db.execute(
5050
"""UPDATE nostrclient.config SET json_data = ?""",
51-
(json.dumps(config.dict())),
52-
)
53-
row = await db.fetchone(
54-
"SELECT json_data FROM nostrclient.config", ()
51+
(json.dumps(config.dict()),),
5552
)
53+
row = await db.fetchone("SELECT json_data FROM nostrclient.config", ())
5654
return json.loads(row[0], object_hook=lambda d: Config(**d))
5755

5856

5957
async def get_config() -> Optional[Config]:
60-
row = await db.fetchone(
61-
"SELECT json_data FROM nostrclient.config", ()
62-
)
58+
row = await db.fetchone("SELECT json_data FROM nostrclient.config", ())
6359
return json.loads(row[0], object_hook=lambda d: Config(**d)) if row else None

models.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
from sqlite3 import Row
22
from typing import List, Optional
33

4-
from pydantic import BaseModel
5-
64
from lnbits.helpers import urlsafe_short_hash
5+
from pydantic import BaseModel
76

87

98
class RelayStatus(BaseModel):

nostr/bech32.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,27 +124,29 @@ def decode(hrp, addr):
124124
hrpgot, data, spec = bech32_decode(addr)
125125
if hrpgot != hrp:
126126
return (None, None)
127-
decoded = convertbits(data[1:], 5, 8, False)
127+
decoded = convertbits(data[1:], 5, 8, False) # type: ignore
128128
if decoded is None or len(decoded) < 2 or len(decoded) > 40:
129129
return (None, None)
130-
if data[0] > 16:
130+
if data[0] > 16: # type: ignore
131131
return (None, None)
132-
if data[0] == 0 and len(decoded) != 20 and len(decoded) != 32:
132+
if data[0] == 0 and len(decoded) != 20 and len(decoded) != 32: # type: ignore
133133
return (None, None)
134134
if (
135-
data[0] == 0
135+
data[0] == 0 # type: ignore
136136
and spec != Encoding.BECH32
137-
or data[0] != 0
137+
or data[0] != 0 # type: ignore
138138
and spec != Encoding.BECH32M
139139
):
140140
return (None, None)
141-
return (data[0], decoded)
141+
return (data[0], decoded) # type: ignore
142142

143143

144144
def encode(hrp, witver, witprog):
145145
"""Encode a segwit address."""
146146
spec = Encoding.BECH32 if witver == 0 else Encoding.BECH32M
147-
ret = bech32_encode(hrp, [witver] + convertbits(witprog, 8, 5), spec)
147+
wit_prog = convertbits(witprog, 8, 5)
148+
assert wit_prog
149+
ret = bech32_encode(hrp, [witver, *wit_prog], spec)
148150
if decode(hrp, ret) == (None, None):
149151
return None
150152
return ret

0 commit comments

Comments
 (0)