Skip to content

Commit 3309bb3

Browse files
reworking my check for deleted logic as it was functionally correct but could be simplified
1 parent 8157289 commit 3309bb3

3 files changed

Lines changed: 9 additions & 8 deletions

File tree

snbackup/backup.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def save_file(local_pth: Path, file: bytes) -> None:
9898
local_pth.parent.mkdir(exist_ok=True, parents=True)
9999

100100
logger.info(f'Saving {local_pth.stem!r} to {local_pth}')
101-
with open(local_pth, 'wb') as file_output:
101+
with local_pth.open('wb', encoding='utf-8') as file_output:
102102
file_output.write(file)
103103
file_output.flush()
104104
os.fsync(file_output.fileno())
@@ -107,7 +107,7 @@ def save_file(local_pth: Path, file: bytes) -> None:
107107
def save_records(file_records: list[dict], json_md: Path) -> None:
108108
"""Persist today's file metadata to json file."""
109109
logger.info('Saving file records to metadata json file')
110-
with open(json_md, 'wt') as json_out:
110+
with json_md.open('wt', encoding='utf-8') as json_out:
111111
print(json.dumps(file_records), file=json_out)
112112

113113

@@ -116,7 +116,7 @@ def previous_record_gen(json_md: Path, *, previous=None) -> Iterator[tuple[str,
116116
relevant info needed to instantiate file objects.
117117
"""
118118
try:
119-
with open(json_md) as json_in:
119+
with json_md.open(encoding='utf-8') as json_in:
120120
previous = json.loads(json_in.read())
121121
except FileNotFoundError:
122122
logger.warning('Unable to locate metadata file. Creating new file')
@@ -136,9 +136,7 @@ def previous_record_gen(json_md: Path, *, previous=None) -> Iterator[tuple[str,
136136

137137
def check_for_deleted(current: set, previous: set) -> list[SnFiles]:
138138
"""Look for files no longer on device from last backup."""
139-
symmetric = current.symmetric_difference(previous)
140-
return [file for file in symmetric if file not in current]
141-
139+
return list(previous - current)
142140

143141
def prepare_upload(ufile: list) -> Iterator[dict[str, tuple[str, bytes, str]]]:
144142
"""Prepare file upload to send to device."""

snbackup/files.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def last_modified(self, mod_date: str) -> None:
4040
raise BadDateError(e) from None
4141

4242
@property
43-
def file_size(self) -> str:
43+
def file_size(self) -> int:
4444
return self._file_size
4545

4646
@property

snbackup/helpers.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,10 @@ def count_backups(directory: Path, pattern=FOLDER_PATTERN) -> tuple[int, Path, P
128128
"""Counts number of backup folders and returns
129129
oldest and newest found on local disk.
130130
"""
131-
previous = sorted(d for d in directory.iterdir() if d.is_dir() and pattern.fullmatch(d.name))
131+
previous = sorted(
132+
d for d in directory.iterdir()
133+
if d.is_dir() and pattern.fullmatch(d.name)
134+
)
132135
if not previous:
133136
return 0, directory, directory
134137
return len(previous), previous[0], previous[-1]

0 commit comments

Comments
 (0)