Skip to content

Commit

Permalink
remove usage of py in favor of pathlib
Browse files Browse the repository at this point in the history
  • Loading branch information
hpk42 committed May 9, 2022
1 parent 1d7d201 commit cffa101
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
25 changes: 12 additions & 13 deletions python/src/deltachat/testplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import pytest
import requests
import py
import pathlib

from . import Account, const, account_hookimpl, get_core_info
from .events import FFIEventLogger, FFIEventTracker
Expand Down Expand Up @@ -180,7 +180,7 @@ def get_liveconfig_producer(self):
pytest.fail("more than {} live accounts requested.".format(MAX_LIVE_CREATED_ACCOUNTS))

def cache_maybe_retrieve_configured_db_files(self, cache_addr, db_target_path):
db_target_path = py.path.local(db_target_path)
db_target_path = pathlib.Path(db_target_path)
assert not db_target_path.exists()

try:
Expand All @@ -190,7 +190,7 @@ def cache_maybe_retrieve_configured_db_files(self, cache_addr, db_target_path):
return False
else:
print("CACHE HIT for", cache_addr)
targetdir = db_target_path.dirpath()
targetdir = db_target_path.parent
write_dict_to_dir(filescache, targetdir)
return True

Expand All @@ -200,27 +200,26 @@ def cache_maybe_store_configured_db_files(self, acc):
# don't overwrite existing entries
if addr not in self._addr2files:
print("storing cache for", addr)
basedir = py.path.local(acc.get_blobdir()).dirpath()
basedir = pathlib.Path(acc.get_blobdir()).parent
self._addr2files[addr] = create_dict_from_files_in_path(basedir)
return True


def create_dict_from_files_in_path(path):
base = py.path.local(path)
def create_dict_from_files_in_path(base):
cachedict = {}
for path in base.visit(fil=py.path.local.isfile):
cachedict[path.relto(base)] = path.read_binary()
for path in base.glob("**/*"):
if path.is_file():
cachedict[path.relative_to(base)] = path.read_bytes()
return cachedict


def write_dict_to_dir(dic, target_dir):
assert dic
target_dir = py.path.local(target_dir)
for relpath, content in dic.items():
path = target_dir.join(relpath)
if not path.dirpath().exists():
path.dirpath().ensure(dir=1)
path.write_binary(content)
path = target_dir.joinpath(relpath)
if not path.parent.exists():
os.makedirs(path.parent)
path.write_bytes(content)


@pytest.fixture
Expand Down
18 changes: 17 additions & 1 deletion python/tests/test_4_lowlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,27 @@
from deltachat.hookspec import global_hookimpl
from deltachat.capi import ffi
from deltachat.capi import lib
from deltachat.testplugin import ACSetup
from deltachat.testplugin import ACSetup, create_dict_from_files_in_path, write_dict_to_dir
# from deltachat.account import EventLogger


class TestACSetup:

def test_cache_writing(self, tmp_path):
base = tmp_path.joinpath("hello")
base.mkdir()
d1 = base.joinpath("dir1")
d1.mkdir()
d1.joinpath("file1").write_bytes(b'content1')
d2 = d1.joinpath("dir2")
d2.mkdir()
d2.joinpath("file2").write_bytes(b"123")
d = create_dict_from_files_in_path(base)
newbase = tmp_path.joinpath("other")
write_dict_to_dir(d, newbase)
assert newbase.joinpath("dir1", "dir2", "file2").exists()
assert newbase.joinpath("dir1", "file1").exists()

def test_basic_states(self, acfactory, monkeypatch, testprocess):
pc = ACSetup(init_time=0.0, testprocess=testprocess)
acc = acfactory.get_unconfigured_account()
Expand Down

0 comments on commit cffa101

Please sign in to comment.