Skip to content

Commit

Permalink
Do first batch of ruff changes
Browse files Browse the repository at this point in the history
  • Loading branch information
bergercookie committed Aug 11, 2024
1 parent 437635f commit cbd134e
Show file tree
Hide file tree
Showing 71 changed files with 974 additions and 792 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,4 @@ test-tw-gcal-sync.json
/.task_backup
/.envrc
.tool-versions
.lvimrc
17 changes: 5 additions & 12 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,12 @@ repos:
# types: [python]
# exclude: ^tests/

- id: isort
name: Isort check
entry: isort
- id: ruff
name: Ruff Linter
entry: ruff
args: ["check", "--fix"]
language: system
pass_filenames: true
types: [python]

# - id: ruff
# name: Ruff Linter
# entry: ruff
# args: ["check", "--fix"]
# language: system
# types_or: [python, pyi]
types_or: [python, pyi]

- id: poetry-check
name: Poetry check
Expand Down
323 changes: 160 additions & 163 deletions poetry.lock

Large diffs are not rendered by default.

107 changes: 104 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ coverage = { version = "^6.5.0", extras = ["toml"] }
coveralls = "^3.3.1"
check-jsonschema = "^0.14.3"
# readline = "6.2.4.1"
ruff = "^0.1.15"
ruff = "^0.5"

# isort ------------------------------------------------------------------------
[tool.isort]
Expand Down Expand Up @@ -181,15 +181,116 @@ pythonPlatform = "Linux"
# ruff -------------------------------------------------------------------------
[tool.ruff]
line-length = 95
target-version = "py310"
target-version = "py38"
force-exclude = true

exclude = []

[tool.ruff.lint]

select = ["ALL"]
ignore = []
ignore = [
# remove them in phases
"A002",
"ANN001",
"ANN002",
"ANN003",
"ANN101",
"ANN102",
"ANN201",
"ANN202",
"ANN204",
"ANN206",
"ANN401",
"ARG001",
"ARG002",
"B007",
"B008",
"B012",
"B904",
"C405",
"C408",
"C419",
"C901",
"D100",
"D100",
"D101",
"D101",
"D102",
"D102",
"D103",
"D103",
"D104",
"D104",
"D105",
"D107",
"D107",
"D203",
"D205",
"D213",
"D301",
"D400",
"D401",
"D415",
"DTZ004",
"DTZ006",
"E501",
"E711",
"E712",
"EM101",
"EM102",
"ERA001",
"EXE002",
"F403",
"F841",
"FA100",
"FA102",
"FBT001",
"FBT002",
"FIX002",
"INP001",
"N802",
"N803",
"N806",
"PGH003",
"PLR0912",
"PLR0913",
"PLR0915",
"PLR2004",
"PT006",
"PT009",
"PT012",
"PT018",
"PT027",
"PTH118",
"PTH120",
"PTH123",
"RET503",
"RET504",
"RET505",
"RET508",
"RUF012",
"S101",
"S101",
"S301",
"S506",
"S602",
"SIM108",
"SIM110",
"SIM118",
"SLF001",
"T100",
"T201",
"TCH001",
"TCH002",
"TCH003",
"TD002",
"TD003",
"TD004",
"TRY003",
"TRY004",
"UP007",
]
exclude = []

[tool.ruff.lint.flake8-annotations]
Expand Down
2 changes: 1 addition & 1 deletion syncall/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""__init__"""
"""Init module."""

# global imports ------------------------------------------------------------------------------
from syncall.__version__ import __version__ as version
Expand Down
59 changes: 36 additions & 23 deletions syncall/aggregator.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from typing_extensions import Self

if TYPE_CHECKING:
from pathlib import Path
from typing import Iterable, Sequence

from item_synchronizer.types import ID, ConverterFn, Item

from syncall.sync_side import SyncSide

from functools import partial
from pathlib import Path
from typing import Any, Dict, Iterable, Optional, Sequence, Tuple
from typing import Any, Optional

from bidict import bidict # type: ignore
from bidict import bidict # pyright: ignore[reportPrivateImportUsage]
from bubop import PrefsManager, logger, pickle_dump, pickle_load
from item_synchronizer import Synchronizer
from item_synchronizer.helpers import SideChanges
from item_synchronizer.resolution_strategy import AlwaysSecondRS, ResolutionStrategy
from item_synchronizer.types import ID, ConverterFn, Item

from syncall.app_utils import app_name
from syncall.side_helper import SideHelper
from syncall.sync_side import SyncSide


class Aggregator:
Expand All @@ -32,7 +41,7 @@ def __init__(
converter_A_to_B: ConverterFn,
resolution_strategy: ResolutionStrategy = AlwaysSecondRS(),
config_fname: Optional[str] = None,
ignore_keys: Tuple[Sequence[str], Sequence[str]] = tuple(),
ignore_keys: tuple[Sequence[str], Sequence[str]] = tuple(),
catch_exceptions: bool = True,
):
# Preferences manager
Expand All @@ -52,7 +61,7 @@ def __init__(
self.prefs_manager = PrefsManager(app_name=app_name(), config_fname=config_fname)

# Own config
self.config: Dict[str, Any] = {}
self.config: dict[str, Any] = {}

self._side_A: SyncSide = side_A
self._side_B: SyncSide = side_B
Expand Down Expand Up @@ -119,15 +128,18 @@ def side_A_fn(fn):

self.cleaned_up = False

def __enter__(self):
def __enter__(self) -> Self:
"""Enter context manager."""
self.start()
return self

def __exit__(self, *_):
def __exit__(self, *_) -> None:
"""Exit context manager."""
self.finish()

def detect_changes(self, helper: SideHelper, items: Dict[ID, Item]) -> SideChanges:
"""
def detect_changes(self, helper: SideHelper, items: dict[ID, Item]) -> SideChanges:
"""Detect changes between the two sides.
Given a fresh list of items from the SyncSide, determine which of them are new,
modified, or have been deleted since the last run.
"""
Expand Down Expand Up @@ -165,7 +177,7 @@ def detect_changes(self, helper: SideHelper, items: Dict[ID, Item]) -> SideChang

return side_changes

def sync(self):
def sync(self) -> None:
"""Entrypoint method."""
items_A = {
str(item[self._helper_A.id_key]): item for item in self._side_A.get_all_items()
Expand Down Expand Up @@ -199,17 +211,16 @@ def sync(self):
# synchronize
self._synchronizer.sync(changes_A=changes_A, changes_B=changes_B)

def start(self):
"""Initialization actions."""
def start(self) -> None:
"""Initialize the aggregator."""
self._side_A.start()
self._side_B.start()

def finish(self):
"""Finalization actions."""
def finish(self) -> None:
"""Finalize the aggregator."""
self._side_A.finish()
self._side_B.finish()

# InserterFn = Callable[[Item], ID]
def inserter_to(self, item: Item, helper: SideHelper) -> ID:
"""Inserter.
Expand All @@ -219,7 +230,7 @@ def inserter_to(self, item: Item, helper: SideHelper) -> ID:
serdes_dir, _ = self._get_serdes_dirs(helper)
logger.info(
f"[{helper.other}] Inserting item [{self._summary_of(item, helper):10}] at"
f" {helper}..."
f" {helper}...",
)

item_created = item_side.add_item(item)
Expand All @@ -237,7 +248,7 @@ def updater_to(self, item_id: ID, item: Item, helper: SideHelper):
serdes_dir, _ = self._get_serdes_dirs(helper)
logger.info(
f"[{helper.other}] Updating item [{self._summary_of(item, helper):10}] at"
f" {helper}..."
f" {helper}...",
)

side.update_item(item_id, **item)
Expand All @@ -263,19 +274,21 @@ def _item_has_update(self, prev_item: Item, new_item: Item, helper: SideHelper)
side, _ = self._get_side_instances(helper)

return not side.items_are_identical(
prev_item, new_item, ignore_keys=[helper.id_key, *helper.ignore_keys]
prev_item,
new_item,
ignore_keys=[helper.id_key, *helper.ignore_keys],
)

def _get_ids_map(self, helper: SideHelper):
return self._B_to_A_map if helper is self._helper_B else self._B_to_A_map.inverse

def _get_serdes_dirs(self, helper: SideHelper) -> Tuple[Path, Path]:
def _get_serdes_dirs(self, helper: SideHelper) -> tuple[Path, Path]:
serdes_dir = self.config[f"{helper}_serdes"]
other_serdes_dir = self.config[f"{helper.other}_serdes"]

return serdes_dir, other_serdes_dir

def _get_side_instances(self, helper: SideHelper) -> Tuple[SyncSide, SyncSide]:
def _get_side_instances(self, helper: SideHelper) -> tuple[SyncSide, SyncSide]:
side = self._side_B if helper is self._helper_B else self._side_A
other_side = self._side_A if helper is self._helper_B else self._side_B

Expand All @@ -294,7 +307,7 @@ def full_path(id_: ID) -> Path:
except FileNotFoundError:
logger.warning(f"File doesn't exist, this may indicate an error -> {p}")
logger.opt(exception=True).debug(
f"File doesn't exist, this may indicate an error -> {p}"
f"File doesn't exist, this may indicate an error -> {p}",
)

def _summary_of(self, item: Item, helper: SideHelper, short=True) -> str:
Expand Down
Loading

0 comments on commit cbd134e

Please sign in to comment.