Skip to content
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
5 changes: 3 additions & 2 deletions telebot/asyncio_storage/pickle_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ def create_dir(self):
"""
Create directory .save-handlers.
"""
dirs, filename = os.path.split(self.file_path)
os.makedirs(dirs, exist_ok=True)
dirs = os.path.dirname(self.file_path)
if dirs:
os.makedirs(dirs, exist_ok=True)
if not os.path.isfile(self.file_path):
with open(self.file_path, "wb") as file:
pickle.dump({}, file)
Expand Down
5 changes: 3 additions & 2 deletions telebot/handler_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ def load_handlers(self, filename=None, del_file_after_loading=True):

@staticmethod
def dump_handlers(handlers, filename, file_mode="wb"):
dirs = filename.rsplit('/', maxsplit=1)[0]
os.makedirs(dirs, exist_ok=True)
dirs = os.path.dirname(filename)
if dirs:
os.makedirs(dirs, exist_ok=True)

with open(filename + ".tmp", file_mode) as file:
if apihelper.CUSTOM_SERIALIZER is None:
Expand Down
5 changes: 3 additions & 2 deletions telebot/storage/pickle_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ def create_dir(self):
"""
Create directory .save-handlers.
"""
dirs, filename = os.path.split(self.file_path)
os.makedirs(dirs, exist_ok=True)
dirs = os.path.dirname(self.file_path)
if dirs:
os.makedirs(dirs, exist_ok=True)
if not os.path.isfile(self.file_path):
with open(self.file_path, "wb") as file:
pickle.dump({}, file)
Expand Down
8 changes: 8 additions & 0 deletions tests/test_handler_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,11 @@ def start(message):

telegram_bot.process_new_updates([update_type])
assert update_type.message.text == 'entered start'


def test_dump_handlers_without_directory(tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
FileHandlerBackend.dump_handlers({1: ['handler']}, 'step.save')

assert os.path.isfile('step.save')
assert not os.path.isdir('step.save')
22 changes: 22 additions & 0 deletions tests/test_pickle_storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import os

import pytest

from telebot.storage import StatePickleStorage


def test_state_pickle_storage_accepts_filename_without_directory(tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
StatePickleStorage(file_path="states.pkl")

assert os.path.isfile("states.pkl")


def test_async_state_pickle_storage_accepts_filename_without_directory(tmp_path, monkeypatch):
pytest.importorskip("aiofiles")
from telebot.asyncio_storage import StatePickleStorage as AsyncStatePickleStorage

monkeypatch.chdir(tmp_path)
AsyncStatePickleStorage(file_path="states.pkl")

assert os.path.isfile("states.pkl")
Loading