Skip to content

Commit

Permalink
feat: code quality (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
dni committed Aug 14, 2024
1 parent 7073442 commit f543b7f
Show file tree
Hide file tree
Showing 23 changed files with 3,037 additions and 132 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: CI
on:
push:
branches:
- main
pull_request:

jobs:
lint:
uses: lnbits/lnbits/.github/workflows/lint.yml@dev
tests:
runs-on: ubuntu-latest
needs: [lint]
strategy:
matrix:
python-version: ['3.9', '3.10']
steps:
- uses: actions/checkout@v4
- uses: lnbits/lnbits/.github/actions/prepare@dev
with:
python-version: ${{ matrix.python-version }}
- name: Run pytest
uses: pavelzw/pytest-action@v2
env:
LNBITS_BACKEND_WALLET_CLASS: FakeWallet
PYTHONUNBUFFERED: 1
DEBUG: true
with:
verbose: true
job-summary: true
emoji: false
click-to-expand: true
custom-pytest: poetry run pytest
report-title: 'test (${{ matrix.python-version }})'
15 changes: 7 additions & 8 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
on:
push:
tags:
- "v[0-9]+.[0-9]+.[0-9]+"
- 'v[0-9]+.[0-9]+.[0-9]+'

jobs:

release:
runs-on: ubuntu-latest
steps:
Expand Down Expand Up @@ -34,12 +33,12 @@ jobs:
- name: Create pull request in extensions repo
env:
GH_TOKEN: ${{ secrets.EXT_GITHUB }}
repo_name: "${{ github.event.repository.name }}"
tag: "${{ github.ref_name }}"
branch: "update-${{ github.event.repository.name }}-${{ github.ref_name }}"
title: "[UPDATE] ${{ github.event.repository.name }} to ${{ github.ref_name }}"
body: "https://github.com/lnbits/${{ github.event.repository.name }}/releases/${{ github.ref_name }}"
archive: "https://github.com/lnbits/${{ github.event.repository.name }}/archive/refs/tags/${{ github.ref_name }}.zip"
repo_name: '${{ github.event.repository.name }}'
tag: '${{ github.ref_name }}'
branch: 'update-${{ github.event.repository.name }}-${{ github.ref_name }}'
title: '[UPDATE] ${{ github.event.repository.name }} to ${{ github.ref_name }}'
body: 'https://github.com/lnbits/${{ github.event.repository.name }}/releases/${{ github.ref_name }}'
archive: 'https://github.com/lnbits/${{ github.event.repository.name }}/archive/refs/tags/${{ github.ref_name }}.zip'
run: |
cd lnbits-extensions
git checkout -b $branch
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
__pycache__
node_modules
.mypy_cache
.venv
12 changes: 12 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"semi": false,
"arrowParens": "avoid",
"insertPragma": false,
"printWidth": 80,
"proseWrap": "preserve",
"singleQuote": true,
"trailingComma": "none",
"useTabs": false,
"bracketSameLine": false,
"bracketSpacing": false
}
47 changes: 47 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
all: format check

format: prettier black ruff

check: mypy pyright checkblack checkruff checkprettier

prettier:
poetry run ./node_modules/.bin/prettier --write .
pyright:
poetry run ./node_modules/.bin/pyright

mypy:
poetry run mypy .

black:
poetry run black .

ruff:
poetry run ruff check . --fix

checkruff:
poetry run ruff check .

checkprettier:
poetry run ./node_modules/.bin/prettier --check .

checkblack:
poetry run black --check .

checkeditorconfig:
editorconfig-checker

test:
PYTHONUNBUFFERED=1 \
DEBUG=true \
poetry run pytest
install-pre-commit-hook:
@echo "Installing pre-commit hook to git"
@echo "Uninstall the hook with poetry run pre-commit uninstall"
poetry run pre-commit install

pre-commit:
poetry run pre-commit run --all-files


checkbundle:
@echo "skipping checkbundle"
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# LNURLDevice - <small>[LNbits](https://github.com/lnbits/lnbits) extension</small>

<small>For more about LNBits extension check [this tutorial](https://github.com/lnbits/lnbits/wiki/LNbits-Extensions)</small>

For offline LNURL devices
Expand Down
39 changes: 21 additions & 18 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,47 @@
import asyncio
from typing import List

from fastapi import APIRouter
from loguru import logger

from lnbits.db import Database
from lnbits.helpers import template_renderer
from lnbits.tasks import create_permanent_unique_task

db = Database("ext_lnurldevice")
from .crud import db
from .tasks import wait_for_paid_invoices
from .views import lnurldevice_generic_router
from .views_api import lnurldevice_api_router
from .views_lnurl import lnurldevice_lnurl_router

lnurldevice_ext: APIRouter = APIRouter(prefix="/lnurldevice", tags=["lnurldevice"])
lnurldevice_ext.include_router(lnurldevice_generic_router)
lnurldevice_ext.include_router(lnurldevice_api_router)
lnurldevice_ext.include_router(lnurldevice_lnurl_router)

lnurldevice_static_files = [
{
"path": "/lnurldevice/static",
"name": "lnurldevice_static",
}
]


def lnurldevice_renderer():
return template_renderer(["lnurldevice/templates"])


from .lnurl import * # noqa: F401,F403
from .tasks import wait_for_paid_invoices
from .views import * # noqa: F401,F403
from .views_api import * # noqa: F401,F403


scheduled_tasks: list[asyncio.Task] = []


def lnurldevice_stop():
for task in scheduled_tasks:
try:
task.cancel()
except Exception as ex:
logger.warning(ex)


def lnurldevice_start():
from lnbits.tasks import create_permanent_unique_task

task = create_permanent_unique_task("ext_lnurldevice", wait_for_paid_invoices)
scheduled_tasks.append(task)


__all__ = [
"db",
"lnurldevice_ext",
"lnurldevice_static_files",
"lnurldevice_start",
"lnurldevice_stop",
]
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@
"description_md": "https://raw.githubusercontent.com/lnbits/lnurldevice/main/description.md",
"terms_and_conditions_md": "https://raw.githubusercontent.com/lnbits/lnurldevice/main/toc.md",
"license": "MIT"
}
}
23 changes: 16 additions & 7 deletions crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

import shortuuid
from fastapi import Request
from lnurl import encode as lnurl_encode

from lnbits.db import Database
from lnbits.helpers import urlsafe_short_hash
from lnurl import encode as lnurl_encode

from . import db
from .models import CreateLnurldevice, Lnurldevice, LnurldevicePayment

db = Database("ext_lnurldevice")


async def create_lnurldevice(data: CreateLnurldevice, req: Request) -> Lnurldevice:
if data.device == "pos" or data.device == "atm":
Expand All @@ -28,11 +29,15 @@ async def create_lnurldevice(data: CreateLnurldevice, req: Request) -> Lnurldevi
+ f"&duration={_switch.duration}"
+ f"&variable={_switch.variable}"
+ f"&comment={_switch.comment}"
+ f"&disabletime=0"
+ "&disabletime=0"
)

await db.execute(
"INSERT INTO lnurldevice.lnurldevice (id, key, title, wallet, profit, currency, device, switches) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
"""
INSERT INTO lnurldevice.lnurldevice
(id, key, title, wallet, profit, currency, device, switches)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
""",
(
lnurldevice_id,
lnurldevice_key,
Expand Down Expand Up @@ -101,7 +106,8 @@ async def get_lnurldevice(lnurldevice_id: str, req: Request) -> Optional[Lnurlde

device = Lnurldevice(**row)

# this is needed for backwards compabtibility, before the LNURL were cached inside db
# this is needed for backwards compabtibility,
# before the LNURL were cached inside db
if device.switches:
url = req.url_for("lnurldevice.lnurl_v2_params", device_id=device.id)
for _switch in device.switches:
Expand All @@ -128,7 +134,8 @@ async def get_lnurldevices(wallet_ids: List[str], req: Request) -> List[Lnurldev
(*wallet_ids,),
)

# this is needed for backwards compabtibility, before the LNURL were cached inside db
# this is needed for backwards compabtibility,
# before the LNURL were cached inside db
devices = [Lnurldevice(**row) for row in rows]

for device in devices:
Expand Down Expand Up @@ -208,6 +215,7 @@ async def get_lnurldevicepayment(
)
return LnurldevicePayment(**row) if row else None


async def get_lnurldevicepayment_by_p(
p: str,
) -> Optional[LnurldevicePayment]:
Expand All @@ -217,6 +225,7 @@ async def get_lnurldevicepayment_by_p(
)
return LnurldevicePayment(**row) if row else None


async def get_lnurlpayload(
lnurldevicepayment_payload: str,
) -> Optional[LnurldevicePayment]:
Expand Down
13 changes: 6 additions & 7 deletions description.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
For LNURL based hardware (although you could use for software), people build things like bitcoin switches, Points of Sale, and ATMs.

For LNURL based hardware (although you could use for software), people build things like bitcoin switches, Points of Sale, and ATMs.

* LNPoS: https://lnbits.github.io/lnpos
* bitcoinSwitch: https://github.com/lnbits/bitcoinSwitch
* FOSSA: https://github.com/lnbits/fossa
* BATM: https://github.com/lnbits/fossa
* OfflineSwitch: https://github.com/lnbits/fossa
- LNPoS: https://lnbits.github.io/lnpos
- bitcoinSwitch: https://github.com/lnbits/bitcoinSwitch
- FOSSA: https://github.com/lnbits/fossa
- BATM: https://github.com/lnbits/fossa
- OfflineSwitch: https://github.com/lnbits/fossa
4 changes: 2 additions & 2 deletions migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ async def m002_redux(db):
""",
(row[0], row[1], row[3], row[4], row[5], row[6]),
)
except:
return
except Exception:
pass


async def m003_redux(db):
Expand Down
59 changes: 59 additions & 0 deletions package-lock.json

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

15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "lnurldevice",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"prettier": "^3.2.5",
"pyright": "^1.1.358"
}
}
Loading

0 comments on commit f543b7f

Please sign in to comment.