Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: big cleanup & deprecations for core.common module #380

Merged
merged 14 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions my/arbtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# NOTE likely also needs libyajl2 from apt or elsewhere?


from dataclasses import dataclass
from pathlib import Path
from typing import Sequence, Iterable, List, Optional

Expand All @@ -22,8 +23,9 @@ def inputs() -> Sequence[Path]:
return get_files(user_config.logfiles)


from .core import dataclass, Json, PathIsh, datetime_aware
from .core.compat import fromisoformat

from my.core import Json, PathIsh, datetime_aware
from my.core.compat import fromisoformat


@dataclass
Expand Down
2 changes: 1 addition & 1 deletion my/bluemaestro.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
Stats,
influxdb,
)
from my.core.common import mcachew
from my.core.cachew import mcachew
from my.core.error import unwrap
from my.core.pandas import DataFrameT, as_dataframe
from my.core.sqlite import sqlite_connect_immutable
Expand Down
3 changes: 2 additions & 1 deletion my/browser/active_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

REQUIRES = ["browserexport", "sqlite_backup"]

from dataclasses import dataclass

from my.config import browser as user_config
from my.core import Paths, dataclass
from my.core import Paths


@dataclass
Expand Down
2 changes: 1 addition & 1 deletion my/browser/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
make_logger,
stat,
)
from my.core.common import mcachew
from my.core.cachew import mcachew

from browserexport.merge import read_and_merge, Visit

Expand Down
3 changes: 2 additions & 1 deletion my/bumble/android.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ class Message(_BaseMessage):

import json
from typing import Union
from ..core import Res, assert_never
from ..core import Res
import sqlite3
from ..core.sqlite import sqlite_connect_immutable, select
from my.core.compat import assert_never

EntitiesRes = Res[Union[Person, _Message]]

Expand Down
4 changes: 2 additions & 2 deletions my/calendar/holidays.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from functools import lru_cache
from typing import Union

from ..core.time import zone_to_countrycode
from my.core import Stats
from my.core.time import zone_to_countrycode


@lru_cache(1)
Expand Down Expand Up @@ -46,7 +47,6 @@ def is_workday(d: DateIsh) -> bool:
return not is_holiday(d)


from ..core.common import Stats
def stats() -> Stats:
# meh, but not sure what would be a better test?
res = {}
Expand Down
3 changes: 1 addition & 2 deletions my/codeforces.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from typing import Dict, Iterator, Sequence

from my.core import get_files, Res, datetime_aware
from my.core.common import assert_never

from my.config import codeforces as config # type: ignore[attr-defined]

Expand Down Expand Up @@ -73,7 +72,7 @@ def parse(self) -> Iterator[Res[Competition]]:
# these contain only contests the user participated in
yield from self._parse_competitions(path)
else:
raise RuntimeError("shouldn't happen") # TODO switch to compat.assert_never
raise RuntimeError(f"shouldn't happen: {path.name}")


def data() -> Iterator[Res[Competition]]:
Expand Down
3 changes: 1 addition & 2 deletions my/coding/commits.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@


from my.core import PathIsh, LazyLogger, make_config
from my.core.cachew import cache_dir
from my.core.common import mcachew
from my.core.cachew import cache_dir, mcachew
from my.core.warnings import high


Expand Down
34 changes: 23 additions & 11 deletions my/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
# this file only keeps the most common & critical types/utility functions
from typing import TYPE_CHECKING

from .common import get_files, PathIsh, Paths
from .common import Json
from .common import warn_if_empty
from .common import stat, Stats
from .common import datetime_naive, datetime_aware
from .common import assert_never
from .stats import stat, Stats
from .types import (
Json,
datetime_aware,
datetime_naive,
)
from .compat import assert_never
from .utils.itertools import warn_if_empty

from .cfg import make_config
from .error import Res, unwrap
from .logging import make_logger, LazyLogger
from .logging import (
make_logger,
)
from .util import __NOT_HPI_MODULE__


# just for brevity in modules
# todo not sure about these.. maybe best to rely on regular imports.. perhaps compare?
from dataclasses import dataclass
from pathlib import Path
LazyLogger = make_logger # TODO deprecate this in favor of make_logger


if not TYPE_CHECKING:
# we used to keep these here for brevity, but feels like it only adds confusion,
# e.g. suggest that we perhaps somehow modify builtin behaviour or whatever
# so best to prefer explicit behaviour
from dataclasses import dataclass
from pathlib import Path


__all__ = [
Expand All @@ -26,7 +38,7 @@
'warn_if_empty',
'stat', 'Stats',
'datetime_aware', 'datetime_naive',
'assert_never',
'assert_never', # TODO maybe deprecate from use in my.core? will be in stdlib soon

'make_config',

Expand Down
17 changes: 7 additions & 10 deletions my/core/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,8 @@ def modules_check(*, verbose: bool, list_all: bool, quick: bool, for_modules: Li

import contextlib

from .common import quick_stats
from .util import get_stats, HPIModule
from .stats import guess_stats
from .util import HPIModule
from .stats import get_stats, quick_stats
from .error import warn_my_config_import_error

mods: Iterable[HPIModule]
Expand Down Expand Up @@ -276,11 +275,8 @@ def modules_check(*, verbose: bool, list_all: bool, quick: bool, for_modules: Li
continue

info(f'{click.style("OK", fg="green")} : {m:<50}')
# first try explicitly defined stats function:
stats = get_stats(m)
if stats is None:
# then try guessing.. not sure if should log somehow?
stats = guess_stats(m, quick=quick)
# TODO add hpi 'stats'? instead of doctor? not sure
stats = get_stats(m, guess=True)

if stats is None:
eprint(" - no 'stats' function, can't check the data")
Expand All @@ -291,6 +287,7 @@ def modules_check(*, verbose: bool, list_all: bool, quick: bool, for_modules: Li

try:
kwargs = {}
# todo hmm why wouldn't they be callable??
if callable(stats) and 'quick' in inspect.signature(stats).parameters:
kwargs['quick'] = quick
with quick_context:
Expand Down Expand Up @@ -488,8 +485,8 @@ def _locate_functions_or_prompt(qualified_names: List[str], prompt: bool = True)


def _warn_exceptions(exc: Exception) -> None:
from my.core.common import LazyLogger
logger = LazyLogger('CLI', level='warning')
from my.core import make_logger
logger = make_logger('CLI', level='warning')

logger.exception(f'hpi query: {exc}')

Expand Down
2 changes: 1 addition & 1 deletion my/core/cachew.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .common import assert_subpackage; assert_subpackage(__name__)
from .internal import assert_subpackage; assert_subpackage(__name__)

from contextlib import contextmanager
import logging
Expand Down
Loading
Loading