Skip to content

Commit

Permalink
Finalize linting
Browse files Browse the repository at this point in the history
  • Loading branch information
MrNaif2018 committed Dec 16, 2024
1 parent 24594bd commit 6e8550c
Show file tree
Hide file tree
Showing 15 changed files with 55 additions and 42 deletions.
5 changes: 2 additions & 3 deletions api/crud/invoices.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import contextlib
import secrets
import time
from collections import defaultdict
Expand Down Expand Up @@ -245,11 +246,9 @@ async def update_invoice_payments(invoice, wallets_ids, discounts, store, produc
if randomize_selection:
symbols = defaultdict(list)
for wallet in wallets:
try:
with contextlib.suppress(Exception):
symbol = await utils.wallets.get_wallet_symbol(wallet)
symbols[symbol].append(wallet)
except Exception: # pragma: no cover
pass
coros = [
create_method_for_wallet(invoice, secrets.choice(symbols[symbol]), discounts, store, product, promocode)
for symbol in symbols
Expand Down
4 changes: 1 addition & 3 deletions api/ext/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def collect_server_settings(ssh_settings): # pragma: no cover
from api.utils.common import str_to_bool

settings = ConfiguratorServerSettings()
try:
with contextlib.suppress(Exception):
client = ssh_settings.create_ssh_client()
env = ServerEnv(client)
cryptos = CommaSeparatedStrings(env.get("BITCART_CRYPTOS", "btc"))
Expand All @@ -114,6 +114,4 @@ def collect_server_settings(ssh_settings): # pragma: no cover
installation_pack=installation_pack, additional_components=additional_components
)
client.close()
except Exception:
pass
return settings
2 changes: 1 addition & 1 deletion api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ def to_dict(self, currency, index: int = None):
return data

@classmethod
def parse_chain_id(self, url): # pragma: no cover
def parse_chain_id(cls, url): # pragma: no cover
k = url.find("@")
if k == -1:
return None
Expand Down
16 changes: 12 additions & 4 deletions api/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from contextvars import ContextVar
from typing import Annotated

import aiofiles
import fido2.features
from aiohttp import ClientSession
from bitcart import COINS, APIManager
Expand Down Expand Up @@ -151,42 +152,49 @@ def set_db_name(cls, db, info: ValidationInfo):
return db

@field_validator("datadir", mode="before")
@classmethod
def set_datadir(cls, path):
path = os.path.abspath(path)
ensure_exists(path)
return path

@field_validator("backups_dir", mode="before")
@classmethod
def set_backups_dir(cls, path):
path = os.path.abspath(path)
ensure_exists(path)
return path

@field_validator("backend_plugins_dir", mode="before")
@classmethod
def set_backend_plugins_dir(cls, path):
path = os.path.abspath(path)
ensure_exists(path)
return path

@field_validator("admin_plugins_dir", mode="before")
@classmethod
def set_admin_plugins_dir(cls, path):
path = os.path.abspath(path)
ensure_exists(path)
return path

@field_validator("store_plugins_dir", mode="before")
@classmethod
def set_store_plugins_dir(cls, path):
path = os.path.abspath(path)
ensure_exists(path)
return path

@field_validator("daemon_plugins_dir", mode="before")
@classmethod
def set_daemon_plugins_dir(cls, path):
path = os.path.abspath(path)
ensure_exists(path)
return path

@field_validator("docker_plugins_dir", mode="before")
@classmethod
def set_docker_plugins_dir(cls, path):
path = os.path.abspath(path)
ensure_exists(path)
Expand Down Expand Up @@ -301,15 +309,15 @@ async def with_db(self):
async def fetch_schema(self):
schema_path = os.path.join(self.datadir, "plugins_schema.json")
if os.path.exists(schema_path):
with open(schema_path) as f:
plugins_schema = json.loads(f.read())
async with aiofiles.open(schema_path) as f:
plugins_schema = json.loads(await f.read())
if plugins_schema["$id"] == PLUGINS_SCHEMA_URL:
self.plugins_schema = plugins_schema
return
async with ClientSession() as session, session.get(PLUGINS_SCHEMA_URL) as resp:
self.plugins_schema = await resp.json()
with open(schema_path, "w") as f:
f.write(json.dumps(self.plugins_schema))
async with aiofiles.open(schema_path, "w") as f:
await f.write(json.dumps(self.plugins_schema))

async def init(self):
sys.excepthook = excepthook_handler(self, sys.excepthook)
Expand Down
8 changes: 4 additions & 4 deletions api/utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ async def run_universal(func, *args, **kwargs):
return result


async def run_repeated(func, timeout, start_timeout=None): # pragma: no cover
if start_timeout is None:
start_timeout = timeout
async def run_repeated(func, interval, initial_delay=None): # pragma: no cover
if initial_delay is None:
initial_delay = interval
first_iter = True
while True:
await asyncio.sleep(start_timeout if first_iter else timeout)
await asyncio.sleep(initial_delay if first_iter else interval)
await run_universal(func)
first_iter = False

Expand Down
5 changes: 2 additions & 3 deletions api/utils/notifications.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import contextlib
import traceback

import apprise
Expand Down Expand Up @@ -30,15 +31,13 @@ def validate_data(provider, data): # pragma: no cover
for k, v in provider["details"][json_part].items():
if "type" in v and k in data:
field_type = v["type"]
try:
with contextlib.suppress(Exception):
if field_type == "int":
data[k] = int(data[k])
elif field_type == "float":
data[k] = float(data[k])
elif field_type == "bool":
data[k] = utils.common.str_to_bool(data[k])
except Exception:
pass
data["schema"] = provider["details"]["tokens"]["schema"]["values"][0]
return data

Expand Down
9 changes: 5 additions & 4 deletions api/views/files.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os

import aiofiles
from fastapi import APIRouter, File, HTTPException, Security, UploadFile
from fastapi.responses import RedirectResponse
from sqlalchemy import select
Expand All @@ -22,8 +23,8 @@ async def create_file(
raise HTTPException(403, "File uploads are not allowed")
file_obj = await utils.database.create_object(models.File, {"filename": file.filename}, user)
path = get_file_path(file_obj)
with open(path, "wb") as f:
f.write(await file.read())
async with aiofiles.open(path, "wb") as f:
await f.write(await file.read())
return file_obj


Expand All @@ -39,8 +40,8 @@ async def patch_file(
utils.files.safe_remove(get_file_path(item))
await utils.database.modify_object(item, {"filename": file.filename})
path = get_file_path(item)
with open(path, "wb") as f:
f.write(await file.read())
async with aiofiles.open(path, "wb") as f:
await f.write(await file.read())
return item


Expand Down
4 changes: 2 additions & 2 deletions api/views/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ async def get_log_contents(
if not settings.settings.log_file:
raise HTTPException(400, "Log file unconfigured")
try:
with open(os.path.join(settings.settings.log_dir, log)) as f:
return f.read().strip()
async with aiofiles.open(os.path.join(settings.settings.log_dir, log)) as f:
return (await f.read()).strip()
except OSError:
raise HTTPException(404, "This log doesn't exist") from None

Expand Down
4 changes: 2 additions & 2 deletions api/views/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ async def install_plugin(
manifest_path = os.path.join(plugin_path, "manifest.json")
if not os.path.exists(manifest_path):
return {"status": "error", "message": "Invalid plugin archive: missing manifest.json"}
with open(manifest_path) as f:
manifest = f.read()
async with aiofiles.open(manifest_path) as f:
manifest = await f.read()
try:
manifest = plugin_ext.parse_manifest(manifest)
except ValueError as e:
Expand Down
5 changes: 3 additions & 2 deletions api/views/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
from decimal import Decimal

import aiofiles
from fastapi import APIRouter, Depends, File, Form, HTTPException, Security, UploadFile
from pydantic import ValidationError
from sqlalchemy import select
Expand All @@ -24,8 +25,8 @@ def get_image_local_path(model_id):

async def save_image(model, image):
filename = get_image_local_path(model.id)
with open(filename, "wb") as f:
f.write(await image.read())
async with aiofiles.open(filename, "wb") as f:
await f.write(await image.read())


def parse_data(data, scheme):
Expand Down
4 changes: 1 addition & 3 deletions daemons/eth.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,11 +370,9 @@ async def on_startup(self, app):
self.trace_available = False
self.trace_queue = asyncio.Queue()
await self.create_coin(archive=True)
try:
with contextlib.suppress(Exception):
await self.archive_coin.debug_trace_block(0)
self.trace_available = True
except Exception:
pass
await super().on_startup(app)
if self.trace_available:
self.archive_limiter = AsyncLimiter(1, 1 / self.ARCHIVE_RATE_LIMIT)
Expand Down
7 changes: 4 additions & 3 deletions daemons/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ def write(self, data: str) -> None:
mode = os.stat(self.path).st_mode
except FileNotFoundError:
mode = stat.S_IREAD | stat.S_IWRITE
if not self.file_exists():
assert not os.path.exists(self.path)
if not self.file_exists() and os.path.exists(self.path):
raise DBFileException(f"File {self.path} should not exist")
os.replace(temp_path, self.path)
os.chmod(self.path, mode)
self._file_exists = True
Expand Down Expand Up @@ -301,7 +301,8 @@ def _after_upgrade_tasks(self):
self.data = StoredDict(self.data, self, [])

def _is_upgrade_method_needed(self, min_version, max_version):
assert min_version <= max_version
if min_version > max_version:
raise DBFileException(f"Invalid version range: {min_version} > {max_version}")
cur_version = self.get_version()
if cur_version > max_version:
return False
Expand Down
3 changes: 2 additions & 1 deletion daemons/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,8 @@ class MultipleProviderRPC(metaclass=ABCMeta):
RESET = object() # sentinel

def __init__(self, providers: list[AbstractRPCProvider]):
assert isinstance(providers, list)
if not isinstance(providers, list):
raise TypeError("providers must be a list")
self.providers = providers
if not self.providers:
raise ValueError("No urls provided")
Expand Down
9 changes: 8 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@ select = [
"RUF100",
"RET",
"A",
"S",
"ASYNC",
]
ignore = ["RET502", "RET503"]
ignore = ["RET502", "RET503", "S104", "S507", "ASYNC110"]
mccabe = { max-complexity = 12 }

[tool.ruff.lint.per-file-ignores]
'tests/*' = ["S"]
'scripts/*' = ["S"]
'.circleci/*' = ["S"]

[tool.ruff.lint.isort]
known-third-party = ["bitcart"]

Expand Down
12 changes: 6 additions & 6 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,18 +174,18 @@ async def test_notification_template(client, token, user):


def test_run_host(mocker):
TEST_FILE = os.path.expanduser("~/test-output")
content = f"touch {TEST_FILE}"
test_file = os.path.expanduser("~/test-output")
content = f"touch {test_file}"
# No valid ssh connection
ok, error = utils.host.run_host(content)
assert ok is False
assert not os.path.exists(TEST_FILE)
assert not os.path.exists(test_file)
assert "Connection problem" in error
assert utils.host.run_host_output(content, "good")["status"] == "error"
# Same with key file
settings.settings.ssh_settings.key_file = "something"
assert utils.host.run_host(content)[0] is False
assert not os.path.exists(TEST_FILE)
assert not os.path.exists(test_file)
settings.settings.ssh_settings.key_file = ""
mocker.patch("paramiko.SSHClient.connect", return_value=True)
mocker.patch(
Expand All @@ -197,8 +197,8 @@ def test_run_host(mocker):
assert error is None
assert utils.host.run_host_output(content, "good") == {"status": "success", "message": "good"}
time.sleep(1) # wait for command to execute (non-blocking)
assert os.path.exists(TEST_FILE)
os.remove(TEST_FILE) # Cleanup
assert os.path.exists(test_file)
os.remove(test_file) # Cleanup


def test_versiontuple():
Expand Down

0 comments on commit 6e8550c

Please sign in to comment.