Skip to content

Commit

Permalink
Fix code based on ruff and pyupgrade feedback, add to linters
Browse files Browse the repository at this point in the history
  • Loading branch information
bergercookie committed Aug 11, 2024
1 parent 437635f commit 504e49c
Show file tree
Hide file tree
Showing 78 changed files with 1,344 additions and 1,120 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,5 @@ test-tw-gcal-sync.json
/.task_backup
/.envrc
.tool-versions
.lvimrc
a
1 change: 1 addition & 0 deletions .markdown-link-check.json
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
{
"ignorePatterns": ["https://notion.so"]
}
26 changes: 14 additions & 12 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ repos:
minimum_pre_commit_version: 2.9.2
require_serial: true
types_or: [python, pyi]

- id: pyupgrade
name: Pyupgrade
description: Automatically upgrade syntax for newer versions.
entry: pyupgrade
args: ["--py38-plus"]
types_or: [python]
language: system

# - id: mypy
# name: Mypy check
# entry: mypy
Expand All @@ -70,19 +79,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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
</p>

<a href="https://github.com/bergercookie/syncall/actions" alt="master">
<img src="https://github.com/bergercookie/syncall/actions/workflows/ci.yml/badge.svg?branch=master" /></a>
<img src="https://github.com/bergercookie/syncall/actions/workflows/tests.yml/badge.svg?branch=master" /></a>
<img src="https://github.com/bergercookie/syncall/actions/workflows/linters.yml/badge.svg?branch=master" /></a>
<a href='https://coveralls.io/github/bergercookie/syncall?branch=master'>
<img src='https://coveralls.io/repos/github/bergercookie/syncall/badge.svg?branch=master' alt='Coverage Status' /></a>
<a href="https://github.com/pre-commit/pre-commit">
Expand Down
356 changes: 189 additions & 167 deletions poetry.lock

Large diffs are not rendered by default.

45 changes: 41 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ classifiers = [
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3 :: Only",
"Intended Audience :: End Users/Desktop",
"Natural Language :: English",
Expand Down Expand Up @@ -50,7 +52,7 @@ tw_gtasks_sync = "syncall.scripts.tw_gtasks_sync:main"

# end-user dependencies --------------------------------------------------------
[tool.poetry.dependencies]
python = ">=3.8,<=3.12.5"
python = ">=3.8.1,<=3.12.5"

PyYAML = "~5.3.1"
bidict = "^0.21.4"
Expand Down Expand Up @@ -109,7 +111,8 @@ 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"
pyupgrade = "3.16"

# isort ------------------------------------------------------------------------
[tool.isort]
Expand Down Expand Up @@ -181,15 +184,49 @@ 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 = [
"ANN201",
"S101",
"D102",
"D103",
"ANN001",
"ANN202",
"D100",
"FBT001",
"ANN003",
"PGH003",
"N803",
"FBT002",
"FA100",
"TRY003",
"SLF001",
"EM102",
"D105",
"D107",
"D213",
"D203",
"EM101",
"D205",
"D400",
"D415",
"INP001",
"E501",
"SIM118",
"N806",
"ANN002",
"ANN204",
"N802",
"PLR0913",
"ANN206",
]
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
75 changes: 45 additions & 30 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

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 @@ -30,9 +39,9 @@ def __init__(
side_B: SyncSide,
converter_B_to_A: ConverterFn,
converter_A_to_B: ConverterFn,
resolution_strategy: ResolutionStrategy = AlwaysSecondRS(),
config_fname: Optional[str] = None,
ignore_keys: Tuple[Sequence[str], Sequence[str]] = tuple(),
resolution_strategy: ResolutionStrategy | None = None,
config_fname: str | None = None,
ignore_keys: tuple[Sequence[str], Sequence[str]] = (),
catch_exceptions: bool = True,
):
# Preferences manager
Expand All @@ -49,10 +58,13 @@ def __init__(
else:
logger.debug(f"Using a custom configuration file ... -> {config_fname}")

if resolution_strategy is None:
resolution_strategy = AlwaysSecondRS()

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 +131,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 +180,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,27 +214,26 @@ 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.
"""Insert an item using the given side helper.
Other side already has the item, and I'm also inserting it at this side.
"""
item_side, _ = self._get_side_instances(helper)
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 @@ -232,19 +246,19 @@ def inserter_to(self, item: Item, helper: SideHelper) -> ID:
return item_created_id

def updater_to(self, item_id: ID, item: Item, helper: SideHelper):
"""Updater."""
"""Update an item using the given side helper."""
side, _ = self._get_side_instances(helper)
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)
pickle_dump(item, serdes_dir / item_id)

def deleter_to(self, item_id: ID, helper: SideHelper):
"""Deleter."""
"""Delete an item using the given side helper."""
logger.info(f"[{helper}] Synchronising deleted item, id -> {item_id}...")
side, _ = self._get_side_instances(helper)
side.delete_single_item(item_id)
Expand All @@ -255,27 +269,28 @@ def item_getter_for(self, item_id: ID, helper: SideHelper) -> Item:
"""Item Getter."""
logger.debug(f"Fetching {helper} item for id -> {item_id}")
side, _ = self._get_side_instances(helper)
item = side.get_item(item_id)
return item
return side.get_item(item_id)

def _item_has_update(self, prev_item: Item, new_item: Item, helper: SideHelper) -> bool:
"""Determine whether the item has been updated."""
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 +309,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 504e49c

Please sign in to comment.