Skip to content

Commit 9ac7dd0

Browse files
committed
Revert "alternative approach using DI and FakeFilesystem" [chapter_03_abstractions_ends]
This reverts commit 832bab7.
1 parent f19f8d2 commit 9ac7dd0

2 files changed

Lines changed: 40 additions & 63 deletions

File tree

‎sync.py‎

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,23 @@
33
import shutil
44
from pathlib import Path
55

6-
class FileSystem:
76

8-
def read(self, path):
9-
return read_paths_and_hashes(path)
7+
def sync(source, dest):
8+
# imperative shell step 1, gather inputs
9+
source_hashes = read_paths_and_hashes(source)
10+
dest_hashes = read_paths_and_hashes(dest)
1011

11-
def copy(self, source, dest):
12-
shutil.copyfile(source, dest)
13-
14-
def move(self, source, dest):
15-
shutil.move(source, dest)
16-
17-
def delete(self, dest):
18-
os.remove(dest)
19-
20-
21-
def sync(source, dest, filesystem=FileSystem()):
22-
source_hashes = filesystem.read(source)
23-
dest_hashes = filesystem.read(dest)
24-
25-
for sha, filename in source_hashes.items():
26-
if sha not in dest_hashes:
27-
sourcepath = Path(source) / filename
28-
destpath = Path(dest) / filename
29-
filesystem.copy(sourcepath, destpath)
30-
31-
elif dest_hashes[sha] != filename:
32-
olddestpath = Path(dest) / dest_hashes[sha]
33-
newdestpath = Path(dest) / filename
34-
filesystem.move(olddestpath, newdestpath)
35-
36-
for sha, filename in dest_hashes.items():
37-
if sha not in source_hashes:
38-
filesystem.delete(dest / filename)
12+
# step 2: call functional core
13+
actions = determine_actions(source_hashes, dest_hashes, source, dest)
3914

15+
# imperative shell step 3, apply outputs
16+
for action, *paths in actions:
17+
if action == "COPY":
18+
shutil.copyfile(*paths)
19+
if action == "MOVE":
20+
shutil.move(*paths)
21+
if action == "DELETE":
22+
os.remove(paths[0])
4023

4124

4225
BLOCKSIZE = 65536
@@ -58,3 +41,20 @@ def read_paths_and_hashes(root):
5841
for fn in files:
5942
hashes[hash_file(Path(folder) / fn)] = fn
6043
return hashes
44+
45+
46+
def determine_actions(source_hashes, dest_hashes, source_folder, dest_folder):
47+
for sha, filename in source_hashes.items():
48+
if sha not in dest_hashes:
49+
sourcepath = Path(source_folder) / filename
50+
destpath = Path(dest_folder) / filename
51+
yield "COPY", sourcepath, destpath
52+
53+
elif dest_hashes[sha] != filename:
54+
olddestpath = Path(dest_folder) / dest_hashes[sha]
55+
newdestpath = Path(dest_folder) / filename
56+
yield "MOVE", olddestpath, newdestpath
57+
58+
for sha, filename in dest_hashes.items():
59+
if sha not in source_hashes:
60+
yield "DELETE", dest_folder / filename

‎test_sync.py‎

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import tempfile
22
from pathlib import Path
33
import shutil
4-
from sync import FileSystem, sync
4+
from sync import sync, determine_actions
55

66

77
class TestE2E:
@@ -47,38 +47,15 @@ def test_when_a_file_has_been_renamed_in_the_source():
4747
shutil.rmtree(dest)
4848

4949

50-
class FakeFilesystem:
51-
def __init__(self, path_hashes):
52-
self.path_hashes = path_hashes
53-
self.actions = []
54-
55-
def read(self, path):
56-
return self.path_hashes[path]
57-
58-
def copy(self, src, dest):
59-
self.actions.append(('COPY', src, dest))
60-
61-
def move(self, src, dest):
62-
self.actions.append(('MOVE', src, dest))
63-
64-
def delete(self, dest):
65-
self.actions.append(('DELETE', dest))
66-
67-
68-
6950
def test_when_a_file_exists_in_the_source_but_not_the_destination():
70-
fakefs = FakeFilesystem({
71-
'/src': {"hash1": "fn1"},
72-
'/dst': {},
73-
})
74-
sync('/src', '/dst', filesystem=fakefs)
75-
assert fakefs.actions == [("COPY", Path("/src/fn1"), Path("/dst/fn1"))]
51+
source_hashes = {"hash1": "fn1"}
52+
dest_hashes = {}
53+
actions = determine_actions(source_hashes, dest_hashes, Path("/src"), Path("/dst"))
54+
assert list(actions) == [("COPY", Path("/src/fn1"), Path("/dst/fn1"))]
7655

7756

7857
def test_when_a_file_has_been_renamed_in_the_source():
79-
fakefs = FakeFilesystem({
80-
'/src': {"hash1": "fn1"},
81-
'/dst': {"hash1": "fn2"},
82-
})
83-
sync('/src', '/dst', filesystem=fakefs)
84-
assert fakefs.actions == [("MOVE", Path("/dst/fn2"), Path("/dst/fn1"))]
58+
source_hashes = {"hash1": "fn1"}
59+
dest_hashes = {"hash1": "fn2"}
60+
actions = determine_actions(source_hashes, dest_hashes, Path("/src"), Path("/dst"))
61+
assert list(actions) == [("MOVE", Path("/dst/fn2"), Path("/dst/fn1"))]

0 commit comments

Comments
 (0)