diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 000000000..27d7f13b0 --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1,2 @@ +* @analysiscenter/codeowners + diff --git a/.github/workflows/test-install.yml b/.github/workflows/test-install.yml index f8279c352..b14bd73ed 100644 --- a/.github/workflows/test-install.yml +++ b/.github/workflows/test-install.yml @@ -16,7 +16,7 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest, macos-latest, windows-latest] - python-version: [3.9, '3.10', 3.11, 3.12] + python-version: [3.9, '3.10', 3.11, 3.12, 3.13] runs-on: ${{ matrix.os }} @@ -52,7 +52,7 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest, macos-latest, windows-latest] - python-version: [3.9, '3.10', 3.11, 3.12] + python-version: [3.9, '3.10', 3.11, 3.12, 3.13] runs-on: ${{ matrix.os }} @@ -87,7 +87,7 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest, macos-latest, windows-latest] - python-version: [3.9, '3.10', 3.11, 3.12] + python-version: [3.9, '3.10', 3.11, 3.12, 3.13] runs-on: ${{ matrix.os }} @@ -151,7 +151,7 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest, macos-latest, windows-latest] - python-version: [3.9, '3.10', 3.11, 3.12] + python-version: [3.9, '3.10', 3.11, 3.12, 3.13] runs-on: ${{ matrix.os }} diff --git a/batchflow/__init__.py b/batchflow/__init__.py index 5f2b6b6b4..918200f34 100644 --- a/batchflow/__init__.py +++ b/batchflow/__init__.py @@ -23,7 +23,6 @@ from .sampler import Sampler, ConstantSampler, NumpySampler, HistoSampler, ScipySampler from .utils import save_data_to, read_data_from from .utils_random import make_rng, make_seed_sequence, spawn_seed_sequence -from .utils_telegram import TelegramMessage from .utils_transforms import Normalizer, Quantizer @@ -34,7 +33,7 @@ pylint_notebook = make_delayed_import(module='.utils_notebook', package=__name__, attribute='pylint_notebook') get_available_gpus = make_delayed_import(module='.utils_notebook', package=__name__, attribute='get_available_gpus') set_gpus = make_delayed_import(module='.utils_notebook', package=__name__, attribute='set_gpus') - +TelegramMessage = make_delayed_import(module='.utils_telegram', package=__name__, attribute='TelegramMessage') try: __version__ = version('batchflow') diff --git a/batchflow/notifier.py b/batchflow/notifier.py index 5049d473f..e7440a587 100755 --- a/batchflow/notifier.py +++ b/batchflow/notifier.py @@ -19,7 +19,6 @@ from .named_expr import NamedExpression, eval_expr from .monitor import ResourceMonitor, MONITOR_ALIASES -from .utils_telegram import TelegramMessage @@ -255,6 +254,8 @@ def __init__(self, bar='a', disable=False, frequency=1, monitors=None, graphs=No # Prepare Telegram notifications self.telegram = telegram if self.telegram or initialize_telegram: + from .utils_telegram import TelegramMessage + self.telegram_text = TelegramMessage(token=token, chat_id=chat_id, silent=silent) self.telegram_media = TelegramMessage(token=token, chat_id=chat_id, silent=silent) diff --git a/batchflow/utils_telegram.py b/batchflow/utils_telegram.py index b5a59d2b5..59e552ab2 100644 --- a/batchflow/utils_telegram.py +++ b/batchflow/utils_telegram.py @@ -1,7 +1,6 @@ """ A class to send updateable content messages to Telegram. """ import os import json -import imghdr from uuid import uuid4 from io import BytesIO from concurrent.futures import ThreadPoolExecutor @@ -148,6 +147,8 @@ def delete(self): def content_to_dict(content, group=True, **kwargs): """ Convert a content (either a path to photo or matplotlib figure) to a telegram-acceptable dictionary. """ # Convert content to a bytestring + from PIL import Image + if isinstance(content, str): bytes_ = TelegramMessage.path_to_bytes(content) else: @@ -155,7 +156,8 @@ def content_to_dict(content, group=True, **kwargs): # Create unique id of attachment, pack entities in json attach_id = f'attached{uuid4().hex}' - attach_fmt = imghdr.what(None, bytes_) + image = Image.open(BytesIO(bytes_)) + attach_fmt = image.format.lower() media_dict = {'media': f'attach://{attach_id}', 'type': 'photo'} media_dict = [media_dict] if group else media_dict diff --git a/pyproject.toml b/pyproject.toml index b0db10178..31c0a9d41 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -70,6 +70,10 @@ jupyter = [ "requests" ] +telegram = [ + "pillow>=9.4,<11.0", +] + other = [ "urllib3>=1.25" ] diff --git a/uv.lock b/uv.lock index b7a8c069f..4249d0abc 100644 --- a/uv.lock +++ b/uv.lock @@ -182,6 +182,9 @@ research = [ { name = "multiprocess" }, { name = "py-nbtools" }, ] +telegram = [ + { name = "pillow" }, +] [package.metadata] requires-dist = [ @@ -201,6 +204,7 @@ requires-dist = [ { name = "numpy", specifier = ">=1.26" }, { name = "pandas", marker = "extra == 'dev'", specifier = ">=0.24" }, { name = "pillow", marker = "extra == 'image'", specifier = ">=9.4,<11.0" }, + { name = "pillow", marker = "extra == 'telegram'", specifier = ">=9.4,<11.0" }, { name = "psutil", marker = "extra == 'monitor'" }, { name = "ptflops", marker = "extra == 'nn'", specifier = ">=0.6" }, { name = "py-nbtools", marker = "extra == 'research'", specifier = ">=0.9.14" },