-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed backup database to be conformant JSON, #56
- Loading branch information
Showing
5 changed files
with
52 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
pytest==6.2.4 | ||
pyinstaller==4.3 | ||
pytest==7.1.2 | ||
pyinstaller==5.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
""" osxmetadata version """ | ||
|
||
__version__ = "0.99.37" | ||
__version__ = "0.99.38" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,63 @@ | ||
""" Functions for writing and loading backup files """ | ||
|
||
import json | ||
import logging | ||
import os | ||
from enum import Enum | ||
|
||
""" Functions for writing and loading backup files """ | ||
|
||
class BackupDatabaseType(Enum): | ||
SINGLE_RECORD_JSON = 1 | ||
JSON = 2 | ||
|
||
|
||
def backup_database_type(path: str) -> BackupDatabaseType: | ||
"""Return BackupDatabaseType enum indicating type of backup file for path""" | ||
with open(path) as fp: | ||
line = fp.readline() | ||
if not line: | ||
raise ValueError("Unknown backup file type") | ||
ch = line[0] | ||
if ch == "{": | ||
return BackupDatabaseType.SINGLE_RECORD_JSON | ||
elif ch == "[": | ||
return BackupDatabaseType.JSON | ||
else: | ||
raise ValueError("Unknown backup file type") | ||
|
||
|
||
def write_backup_file(backup_file, backup_data): | ||
""" Write backup_data to backup_file as JSON | ||
backup_data: dict where key is filename and value is dict of the attributes | ||
as returned by json.loads(OSXMetaData.to_json()) """ | ||
"""Write backup_data to backup_file as JSON | ||
backup_data: dict where key is filename and value is dict of the attributes | ||
as returned by json.loads(OSXMetaData.to_json())""" | ||
|
||
with open(backup_file, mode="w") as fp: | ||
for record in backup_data.values(): | ||
json_str = json.dumps(record) | ||
fp.write(json_str) | ||
fp.write("\n") | ||
json.dump(list(backup_data.values()), fp) | ||
|
||
|
||
def load_backup_file(backup_file): | ||
""" Load attribute data from JSON in backup_file | ||
Returns: backup_data dict """ | ||
"""Load attribute data from JSON in backup_file | ||
Returns: backup_data dict""" | ||
|
||
if not os.path.isfile(backup_file): | ||
raise FileNotFoundError(f"Could not find backup file: {backup_file}") | ||
|
||
backup_data = {} | ||
with open(backup_file, mode="r") as fp: | ||
for line in fp: | ||
data = json.loads(line) | ||
fname = data["_filename"] | ||
if fname in backup_data: | ||
logging.warning( | ||
f"WARNING: duplicate filename {fname} found in {backup_file}" | ||
) | ||
|
||
backup_data[fname] = data | ||
if backup_database_type(backup_file) == BackupDatabaseType.SINGLE_RECORD_JSON: | ||
# old style single record of json per file | ||
backup_data = {} | ||
with open(backup_file, mode="r") as fp: | ||
for line in fp: | ||
data = json.loads(line) | ||
fname = data["_filename"] | ||
if fname in backup_data: | ||
logging.warning( | ||
f"WARNING: duplicate filename {fname} found in {backup_file}" | ||
) | ||
|
||
backup_data[fname] = data | ||
else: | ||
with open(backup_file, mode="r") as fp: | ||
backup_records = json.load(fp) | ||
backup_data = {data["_filename"]: data for data in backup_records} | ||
|
||
return backup_data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters