Skip to content

Commit

Permalink
fix linter warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
adbenitez committed Jan 28, 2024
1 parent e4feb80 commit c4586df
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 30 deletions.
17 changes: 12 additions & 5 deletions deltabot_cli/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ class AttrDict(dict):
"""Dictionary that allows accessing values using the "dot notation" as attributes."""

def __init__(self, *args, **kwargs) -> None:
super().__init__({_camel_to_snake(key): to_attrdict(value) for key, value in dict(*args, **kwargs).items()})
super().__init__(
{
_camel_to_snake(key): to_attrdict(value)
for key, value in dict(*args, **kwargs).items()
}
)

def __getattr__(self, attr):
if attr in self:
Expand Down Expand Up @@ -79,7 +84,7 @@ def run_client_cli(
Extra keyword arguments are passed to the internal Rpc object.
"""
from .client import Client
from .client import Client # noqa

_run_cli(Client, hooks, argv, **kwargs)

Expand All @@ -93,7 +98,7 @@ def run_bot_cli(
Extra keyword arguments are passed to the internal Rpc object.
"""
from .client import Bot
from .client import Bot # noqa

_run_cli(Bot, hooks, argv, **kwargs)

Expand All @@ -104,7 +109,7 @@ def _run_cli(
argv: Optional[list] = None,
**kwargs,
) -> None:
from .rpc import Rpc
from .rpc import Rpc # noqa

if argv is None:
argv = sys.argv
Expand All @@ -129,7 +134,9 @@ def _run_cli(
if not rpc.is_configured(accid):
assert args.email, "Account is not configured and email must be provided"
assert args.password, "Account is not configured and password must be provided"
configure_thread = Thread(run=client.configure, args=(accid, args.email, args.password))
configure_thread = Thread(
target=client.configure, args=(accid, args.email, args.password)
)
configure_thread.start()
client.run_forever()

Expand Down
6 changes: 4 additions & 2 deletions deltabot_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ def get_or_create_account(self, rpc: Rpc, addr: str) -> int:
return accid

def get_account(self, rpc: Rpc, addr: str) -> int:
"""Get account id for address, if no account exists with the given address, zero is returned."""
"""Get account id for address.
If no account exists with the given address, zero is returned.
"""
for accid in rpc.get_all_account_ids():
if addr == self.get_address(rpc, accid):
return accid
Expand Down Expand Up @@ -190,7 +192,7 @@ def _serve_cmd(cli: BotCli, bot: Bot, args: Namespace) -> None:
logging.error(f"account {accid} not configured")
if len(addrs) != 0:
logging.info(f"Listening at: {', '.join(addrs)}")
cli._on_start(bot, args)
cli._on_start(bot, args) # noqa
while True:
try:
bot.run_forever()
Expand Down
12 changes: 1 addition & 11 deletions deltabot_cli/client.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
"""Event loop implementations offering high level event handling/hooking."""

import logging
from typing import (
TYPE_CHECKING,
Callable,
Dict,
Iterable,
Optional,
Set,
Tuple,
Type,
Union,
)
from typing import Callable, Dict, Iterable, Optional, Set, Tuple, Type, Union

from ._utils import (
AttrDict,
Expand Down
3 changes: 3 additions & 0 deletions deltabot_cli/const.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
"""Constants and enumerations."""

# pylama:ignore=C0115
from enum import Enum, IntEnum

COMMAND_PREFIX = "/"
Expand Down
26 changes: 16 additions & 10 deletions deltabot_cli/rpc.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""JSON-RPC communication with Delta Chat core."""

import itertools
import json
import logging
Expand All @@ -12,24 +14,26 @@


class JsonRpcError(Exception):
pass
"""An error occurred in your request to the JSON-RPC API."""


class Result(Event):
class _Result(Event):
def __init__(self) -> None:
self._value: Any = None
super().__init__()

def set(self, value: Any) -> None:
def set(self, value: Any) -> None: # noqa
self._value = value
super().set()

def wait(self) -> Any:
def wait(self) -> Any: # noqa
super().wait()
return self._value


class Rpc:
"""Access to the Delta Chat JSON-RPC API."""

def __init__(self, accounts_dir: Optional[str] = None, **kwargs):
"""The given arguments will be passed to subprocess.Popen()"""
if accounts_dir:
Expand All @@ -42,7 +46,7 @@ def __init__(self, accounts_dir: Optional[str] = None, **kwargs):
self.process: subprocess.Popen
self.id_iterator: Iterator[int]
# Map from request ID to the result.
self.pending_results: Dict[int, Result]
self.pending_results: Dict[int, _Result]
self.request_queue: Queue[Any]
self.closing: bool
self.reader_thread: Thread
Expand All @@ -51,16 +55,15 @@ def __init__(self, accounts_dir: Optional[str] = None, **kwargs):
def start(self) -> None:
if sys.version_info >= (3, 11):
# Prevent subprocess from capturing SIGINT.
kwargs = dict(process_group=0)
kwargs = {"process_group": 0, **self._kwargs}
else:
# `process_group` is not supported before Python 3.11.
kwargs = dict(preexec_fn=os.setpgrp) # noqa: PLW1509
self.process = subprocess.Popen(
kwargs = {"preexec_fn": os.setpgrp, **self._kwargs} # noqa: PLW1509
self.process = subprocess.Popen( # noqa: R1732
"deltachat-rpc-server",
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
**kwargs,
**self._kwargs,
)
self.id_iterator = itertools.count(start=1)
self.pending_results = {}
Expand All @@ -75,6 +78,7 @@ def close(self) -> None:
"""Terminate RPC server process and wait until the reader loop finishes."""
self.closing = True
self.stop_io_for_all_accounts()
assert self.process.stdin
self.process.stdin.close()
self.reader_thread.join()
self.request_queue.put(None)
Expand All @@ -89,6 +93,7 @@ def __exit__(self, _exc_type, _exc, _tb):

def reader_loop(self) -> None:
try:
assert self.process.stdout
while True:
line = self.process.stdout.readline()
if not line: # EOF
Expand All @@ -105,6 +110,7 @@ def reader_loop(self) -> None:
def writer_loop(self) -> None:
"""Writer loop ensuring only a single thread writes requests."""
try:
assert self.process.stdin
while True:
request = self.request_queue.get()
if not request:
Expand All @@ -126,7 +132,7 @@ def method(*args) -> Any:
"params": args,
"id": request_id,
}
result = self.pending_results[request_id] = Result()
result = self.pending_results[request_id] = _Result()
self.request_queue.put(request)
response = result.wait()

Expand Down
2 changes: 1 addition & 1 deletion pylama.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[pylama]
linters=mccabe,pyflakes,pylint,isort,mypy
ignore=C0116,R0902
ignore=C0116,R0902,R0913,W1203,W0718
skip=.*,build/*,tests/*,*/flycheck_*
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ dev = [
]

[tool.black]
line-length = 120
line-length = 100

[tool.isort]
profile = "black"
Expand Down

0 comments on commit c4586df

Please sign in to comment.