Skip to content

Commit

Permalink
use ruff lint and fmt code
Browse files Browse the repository at this point in the history
  • Loading branch information
hanxi committed Jun 14, 2024
1 parent e484164 commit 88f0ce7
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 55 deletions.
29 changes: 27 additions & 2 deletions pdm.lock

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

26 changes: 26 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,29 @@ requires = ["pdm-backend"]
build-backend = "pdm.backend"

[tool.pdm]
[tool.pdm.dev-dependencies]
lint = [
"ruff>=0.4.8",
]
[tool.ruff]
select = [
"B", # flake8-bugbear
"C4", # flake8-comprehensions
"E", # pycodestyle - Error
"F", # Pyflakes
"I", # isort
"W", # pycodestyle - Warning
"UP", # pyupgrade
]
ignore = [
"E501", # line-too-long
"W191", # tab-indentation
]
include = ["**/*.py", "**/*.pyi", "**/pyproject.toml"]

[tool.ruff.pydocstyle]
convention = "google"

[tool.pdm.scripts]
lint = "ruff check ."
fmt = "ruff format ."
18 changes: 18 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,24 @@ requests==2.31.0 \
rich==13.7.1 \
--hash=sha256:4edbae314f59eb482f54e9e30bf00d33350aaa94f4bfcd4e9e3110e64d0d7222 \
--hash=sha256:9be308cb1fe2f1f57d67ce99e95af38a1e2bc71ad9813b0e247cf7ffbcc3a432
ruff==0.4.8 \
--hash=sha256:14019a06dbe29b608f6b7cbcec300e3170a8d86efaddb7b23405cb7f7dcaf780 \
--hash=sha256:16d717b1d57b2e2fd68bd0bf80fb43931b79d05a7131aa477d66fc40fbd86268 \
--hash=sha256:284c2e3f3396fb05f5f803c9fffb53ebbe09a3ebe7dda2929ed8d73ded736deb \
--hash=sha256:384154a1c3f4bf537bac69f33720957ee49ac8d484bfc91720cc94172026ceed \
--hash=sha256:6d795d7639212c2dfd01991259460101c22aabf420d9b943f153ab9d9706e6a9 \
--hash=sha256:6ea874950daca5697309d976c9afba830d3bf0ed66887481d6bca1673fc5b66a \
--hash=sha256:704977a658131651a22b5ebeb28b717ef42ac6ee3b11e91dc87b633b5d83142b \
--hash=sha256:72584676164e15a68a15778fd1b17c28a519e7a0622161eb2debdcdabdc71883 \
--hash=sha256:7663a6d78f6adb0eab270fa9cf1ff2d28618ca3a652b60f2a234d92b9ec89066 \
--hash=sha256:9678d5c9b43315f323af2233a04d747409d1e3aa6789620083a82d1066a35199 \
--hash=sha256:a7354f921e3fbe04d2a62d46707e569f9315e1a613307f7311a935743c51a764 \
--hash=sha256:aad360893e92486662ef3be0a339c5ca3c1b109e0134fcd37d534d4be9fb8de3 \
--hash=sha256:d05f8d6f0c3cce5026cecd83b7a143dcad503045857bc49662f736437380ad45 \
--hash=sha256:e14a3a095d07560a9d6769a72f781d73259655919d9b396c650fc98a8157555d \
--hash=sha256:e9d5ce97cacc99878aa0d084c626a15cd21e6b3d53fd6f9112b7fc485918e1fa \
--hash=sha256:eeceb78da8afb6de0ddada93112869852d04f1cd0f6b80fe464fd4e35c330913 \
--hash=sha256:fc95aac2943ddf360376be9aa3107c8cf9640083940a8c5bd824be692d2216dc
typing-extensions==4.9.0; python_version < "3.11" \
--hash=sha256:23478f88c37f27d76ac8aee6c905017a143b0b1b886c3c9f66bc2fd94f9f5783 \
--hash=sha256:af72aea155e91adfc61c3ae9e0e342dbc0cba726d6cba4b6c72c1f34e47291cd
Expand Down
5 changes: 2 additions & 3 deletions xiaomusic/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import argparse
import json
import os
from dataclasses import dataclass, field
from typing import Any, Iterable
from dataclasses import dataclass

from xiaomusic.utils import validate_proxy

Expand All @@ -13,7 +12,7 @@
HARDWARE_COMMAND_DICT = {
# hardware: (tts_command, wakeup_command, volume_command)
"LX06": ("5-1", "5-5", "2-1"),
"L05B": ("5-3", "5-4", "2-1"),
"L05B": ("5-3", "5-4", "2-1"),
"S12": ("5-1", "5-5", "2-1"), # 第一代小爱,型号MDZ-25-DA
"S12A": ("5-1", "5-5", "2-1"),
"LX01": ("5-1", "5-5", "2-1"),
Expand Down
30 changes: 19 additions & 11 deletions xiaomusic/httpserver.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
#!/usr/bin/env python3
import os
import sys
import traceback
import asyncio
from threading import Thread

from flask import Flask, request, send_from_directory
from waitress import serve
from threading import Thread

from xiaomusic.config import (
KEY_WORD_DICT,
)

from xiaomusic import (
__version__,
)
from xiaomusic.config import (
KEY_WORD_DICT,
)

# 隐藏 flask 启动告警
# https://gist.github.com/jerblack/735b9953ba1ab6234abb43174210d356
#from flask import cli
#cli.show_server_banner = lambda *_: None
# from flask import cli
# cli.show_server_banner = lambda *_: None

app = Flask(__name__)
host = "0.0.0.0"
Expand All @@ -33,29 +29,34 @@
def allcmds():
return KEY_WORD_DICT


@app.route("/getversion", methods=["GET"])
def getversion():
log.debug("getversion %s", __version__)
return {
"version": __version__,
}


@app.route("/getvolume", methods=["GET"])
def getvolume():
volume = xiaomusic.get_volume_ret()
return {
"volume": volume,
}


@app.route("/searchmusic", methods=["GET"])
def searchmusic():
name = request.args.get('name')
name = request.args.get("name")
return xiaomusic.searchmusic(name)


@app.route("/playingmusic", methods=["GET"])
def playingmusic():
return xiaomusic.playingmusic()


@app.route("/", methods=["GET"])
def redirect_to_index():
return send_from_directory("static", "index.html")
Expand All @@ -71,6 +72,7 @@ async def do_cmd():
return {"ret": "OK"}
return {"ret": "Unknow cmd"}


@app.route("/getsetting", methods=["GET"])
async def getsetting():
config = xiaomusic.getconfig()
Expand All @@ -88,31 +90,37 @@ async def getsetting():
}
return data


@app.route("/savesetting", methods=["POST"])
async def savesetting():
data = request.get_json()
log.info(data)
await xiaomusic.saveconfig(data)
return "save success"


@app.route("/musiclist", methods=["GET"])
async def musiclist():
return xiaomusic.get_music_list()


@app.route("/curplaylist", methods=["GET"])
async def curplaylist():
return xiaomusic.get_cur_play_list()


def static_path_handler(filename):
log.debug(filename)
log.debug(static_path)
absolute_path = os.path.abspath(static_path)
log.debug(absolute_path)
return send_from_directory(absolute_path, filename)


def run_app():
serve(app, host=host, port=port)


def StartHTTPServer(_port, _static_path, _xiaomusic):
global port, static_path, xiaomusic, log
port = _port
Expand Down
7 changes: 3 additions & 4 deletions xiaomusic/utils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#!/usr/bin/env python3
from __future__ import annotations

import os
import difflib
import re
import socket
from collections.abc import AsyncIterator
from http.cookies import SimpleCookie
from typing import AsyncIterator
from urllib.parse import urlparse
import difflib

from requests.utils import cookiejar_from_dict

Expand Down Expand Up @@ -62,6 +60,7 @@ def validate_proxy(proxy_str: str) -> bool:

return True


# 模糊搜索
def fuzzyfinder(user_input, collection):
return difflib.get_close_matches(user_input, collection, 10, cutoff=0.1)
Loading

0 comments on commit 88f0ce7

Please sign in to comment.