Skip to content

Commit

Permalink
🎨 Use ruff to format code
Browse files Browse the repository at this point in the history
  • Loading branch information
Freed-Wu committed Apr 8, 2024
1 parent 88a3f85 commit 33d1159
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 60 deletions.
33 changes: 9 additions & 24 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
---
exclude: ^templates/

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
Expand All @@ -11,7 +13,6 @@ repos:
- id: trailing-whitespace
- id: mixed-line-ending
- id: end-of-file-fixer
exclude: ^templates/class\.txt$
- id: detect-private-key
- id: check-symlinks
- id: check-ast
Expand Down Expand Up @@ -64,8 +65,9 @@ repos:
- mdformat-toc
- mdformat-deflist
- mdformat-beautysh
- mdformat-black
- mdformat-ruff
- mdformat-config
- mdformat-web
- repo: https://github.com/DavidAnson/markdownlint-cli2
rev: v0.12.1
hooks:
Expand All @@ -77,32 +79,15 @@ repos:
hooks:
- id: update-CITATION.cff
- id: update-pyproject.toml
- repo: https://github.com/psf/black
rev: 24.3.0
hooks:
- id: black
- repo: https://github.com/PyCQA/isort
rev: 5.13.2
hooks:
- id: isort
- repo: https://github.com/pycqa/pydocstyle
rev: 6.3.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.2.1
hooks:
- id: pydocstyle
additional_dependencies:
- tomli
- id: ruff
- id: ruff-format
- repo: https://github.com/kumaraditya303/mirrors-pyright
rev: v1.1.354
rev: v1.1.350
hooks:
- id: pyright
- repo: https://github.com/PyCQA/bandit
rev: 1.7.8
hooks:
- id: bandit
args:
- -cpyproject.toml
additional_dependencies:
- tomli
- repo: https://github.com/nix-community/nixpkgs-fmt
rev: v1.3.0
hooks:
Expand Down
6 changes: 0 additions & 6 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
https://www.sphinx-doc.org/en/master/usage/configuration.html
"""

from repl_python_wakatime import __version__ as version # type: ignore
from repl_python_wakatime._metainfo import ( # type: ignore
author,
copyright,
project,
)

# -- Path setup --------------------------------------------------------------

Expand Down
11 changes: 4 additions & 7 deletions src/repl_python_wakatime/hooks/codestats.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,15 @@ def send_xp(self) -> None:

headers = {
"Content-Type": "application/json",
"User-Agent": "code-stats-python/{0}".format(__version__),
"User-Agent": f"code-stats-python/{__version__}",
"X-API-Token": self.api_key,
"Accept": "*/*",
}

# after lock is released we can send the payload
utc_now = datetime.now().astimezone().isoformat()
pulse_json = json.dumps({
"coded_at": "{0}".format(utc_now),
"coded_at": f"{utc_now}",
"xps": xp_list,
}).encode("utf-8")
req = Request(url=self.url, data=pulse_json, headers=headers)
Expand All @@ -150,7 +150,7 @@ def send_xp(self) -> None:
except URLError as e:
try:
# HTTP error
error = "{0} {1}".format(
error = "{} {}".format(
e.code, # type: ignore
e.read().decode("utf-8"), # type: ignore
)
Expand All @@ -161,10 +161,7 @@ def send_xp(self) -> None:
# SSL certificate error (eg. a public wifi redirects traffic)
error = e
except HTTPException as e:
error = "HTTPException on send data. Msg: {0}\nDoc?:{1}".format(
e.message,
e.__doc__, # type: ignore
)
error = f"HTTPException on send data. \nDoc: {e.__doc__}"
if error:
logger.error(error)

Expand Down
9 changes: 6 additions & 3 deletions src/repl_python_wakatime/hooks/wakatime.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
"""

import os
from collections.abc import Callable
from subprocess import Popen # nosec: B404
from typing import Any, Callable
from typing import Any


def wakatime_hook(
project: str = "",
language: str = "python",
category: str = "coding",
plugin: str = "repl-python-wakatime",
filenames: list[str] = [".git"],
filenames: list[str] | None = None,
detect_func: Callable[[str], bool] = os.path.isdir,
*args: Any,
**kwargs: Any,
Expand All @@ -33,11 +34,13 @@ def wakatime_hook(
:param plugin:
:type plugin: str
:param filenames:
:type filenames: list[str]
:type filenames: list[str] | None
:param detect_func:
:type detect_func: Callable[[str], bool]
:rtype: None
"""
if filenames is None:
filenames = [".git"]
if project == "":
from ..utils.project import get_project

Expand Down
20 changes: 14 additions & 6 deletions src/repl_python_wakatime/ipython.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
==========
"""

from typing import Any, Callable
from collections.abc import Callable
from typing import Any

from IPython.terminal.interactiveshell import TerminalInteractiveShell
from IPython.terminal.prompts import ClassicPrompts, Prompts
Expand All @@ -16,7 +17,7 @@ def get_new_prompts_class(
prompts_class: type,
hook: Callable = wakatime_hook,
args: tuple = (),
kwargs: dict[str, Any] = {},
kwargs: dict[str, Any] | None = None,
) -> type:
"""Get new prompts class.
Expand All @@ -27,9 +28,11 @@ def get_new_prompts_class(
:param args:
:type args: tuple
:param kwargs:
:type kwargs: dict[str, Any]
:type kwargs: dict[str, Any] | None
:rtype: type
"""
if kwargs is None:
kwargs = {}
if isinstance(prompts_class, LazyConfigValue):
prompts_class = ClassicPrompts
shell = TerminalInteractiveShell()
Expand Down Expand Up @@ -77,7 +80,7 @@ def install_hook(
c: Config,
hook: Callable = wakatime_hook,
args: tuple = (),
kwargs: dict[str, Any] = {"plugin": "repl-ipython-wakatime"},
kwargs: dict[str, Any] | None = None,
) -> Config:
"""Install hook.
Expand All @@ -88,10 +91,15 @@ def install_hook(
:param args:
:type args: tuple
:param kwargs:
:type kwargs: dict[str, Any]
:type kwargs: dict[str, Any] | None
:rtype: Config
"""
if kwargs is None:
kwargs = {"plugin": "repl-ipython-wakatime"}
c.TerminalInteractiveShell.prompts_class = get_new_prompts_class( # type: ignore
c.TerminalInteractiveShell.prompts_class, hook, args, kwargs # type: ignore
c.TerminalInteractiveShell.prompts_class,
hook,
args,
kwargs, # type: ignore
)
return c
15 changes: 10 additions & 5 deletions src/repl_python_wakatime/ptpython.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
===========
"""

from typing import Any, Callable
from collections.abc import Callable
from typing import Any

from prompt_toolkit.formatted_text import AnyFormattedText
from ptpython.prompt_style import PromptStyle
Expand All @@ -19,7 +20,7 @@ def __init__(
prompt_style: PromptStyle,
hook: Callable = wakatime_hook,
args: tuple = (),
kwargs: dict[str, Any] = {},
kwargs: dict[str, Any] | None = None,
) -> None:
"""Init.
Expand All @@ -30,9 +31,11 @@ def __init__(
:param args:
:type args: tuple
:param kwargs:
:type kwargs: dict[str, Any]
:type kwargs: dict[str, Any] | None
:rtype: None
"""
if kwargs is None:
kwargs = {}
super().__init__()
self.prompt_style = prompt_style
self.hook = hook
Expand Down Expand Up @@ -69,7 +72,7 @@ def install_hook(
repl: PythonRepl,
hook: Callable = wakatime_hook,
args: tuple = (),
kwargs: dict[str, Any] = {"plugin": "repl-ptpython-wakatime"},
kwargs: dict[str, Any] | None = None,
hook_prefix: str = "ps1_",
) -> PythonRepl:
"""Install hook.
Expand All @@ -81,11 +84,13 @@ def install_hook(
:param args:
:type args: tuple
:param kwargs:
:type kwargs: dict[str, Any]
:type kwargs: dict[str, Any] | None
:param hook_prefix:
:type hook_prefix: str
:rtype: PythonRepl
"""
if kwargs is None:
kwargs = {"plugin": "repl-ptpython-wakatime"}
ps = Ps(repl.all_prompt_styles[repl.prompt_style], hook, args, kwargs)
length = len(hook_prefix)
names = map(
Expand Down
15 changes: 10 additions & 5 deletions src/repl_python_wakatime/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"""

import sys
from typing import Any, Callable
from collections.abc import Callable
from typing import Any

from .hooks.wakatime import wakatime_hook

Expand All @@ -16,7 +17,7 @@ def __init__(
ps1: object = None,
hook: Callable = wakatime_hook,
args: tuple = (),
kwargs: dict[str, Any] = {},
kwargs: dict[str, Any] | None = None,
) -> None:
"""Init.
Expand All @@ -27,9 +28,11 @@ def __init__(
:param args:
:type args: tuple
:param kwargs:
:type kwargs: dict[str, Any]
:type kwargs: dict[str, Any] | None
:rtype: None
"""
if kwargs is None:
kwargs = {}
if ps1:
self.ps1 = ps1
else:
Expand All @@ -56,7 +59,7 @@ def __str__(self) -> str:
def install_hook(
hook: Callable = wakatime_hook,
args: tuple = (),
kwargs: dict[str, Any] = {"plugin": "repl-python-wakatime"},
kwargs: dict[str, Any] | None = None,
) -> object:
"""Install hook.
Expand All @@ -65,8 +68,10 @@ def install_hook(
:param args:
:type args: tuple
:param kwargs:
:type kwargs: dict[str, Any]
:type kwargs: dict[str, Any] | None
:rtype: object
"""
if kwargs is None:
kwargs = {"plugin": "repl-python-wakatime"}
sys.ps1 = Ps1(hook=hook, args=args, kwargs=kwargs)
return sys.ps1
8 changes: 5 additions & 3 deletions src/repl_python_wakatime/utils/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
"""

import os
from typing import Callable
from collections.abc import Callable


def get_project(
filenames: list[str] = [".git"],
filenames: list[str] | None = None,
detect_func: Callable[[str], bool] = os.path.isdir,
) -> str:
"""Get project. Its function is like ``git rev-parse --show-toplevel``.
Expand All @@ -18,10 +18,12 @@ def get_project(
use current directory as ``project``.
:param filenames:
:type filenames: list[str]
:type filenames: list[str] | None
:param detect_func:
:type detect_func: Callable[[str], bool]
"""
if filenames is None:
filenames = [".git"]
cwd = os.getcwd()
project = cwd
oldproject = ""
Expand Down
1 change: 0 additions & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""

import pytest

from repl_python_wakatime.hooks.codestats import CodeStats
from repl_python_wakatime.utils.api import get_api_key

Expand Down

0 comments on commit 33d1159

Please sign in to comment.