Skip to content

Commit

Permalink
Fix shelve2json for Win/Mac
Browse files Browse the repository at this point in the history
  • Loading branch information
brainsik committed Sep 11, 2024
1 parent 34977bb commit 3e898d3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/json_store/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .json_store import JSONStore

__version__ = "4.0"
__version__ = "4.1"

open = JSONStore
10 changes: 7 additions & 3 deletions src/json_store/shelve2json.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@
import json_store


def convert(oldfile):
def convert(oldfile: str):
if not os.path.isfile(oldfile):
raise ValueError("No such file: {}".format(oldfile))

data = shelve.open(oldfile)
name = oldfile
# remove extensions that are implicitly added by the underlying DBM module
name = name.rsplit(".dat")[0] # Windows
name = name.rsplit(".db")[0] # macOS

newfile = oldfile.rsplit(".db")[0] + ".json"
data = shelve.open(name)
newfile = name + ".json"
store = json_store.open(newfile)
store.update(data)
store.sync()
Expand Down
22 changes: 17 additions & 5 deletions tests/test_shelve2json.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
#!/bin/sh
#!/usr/bin/env bash
set -e -x

tmp=$(mktemp)
tmp=$(mktemp tmpXXXXX)
uname -s
rm -f "$tmp"
python -c 'import shelve; db=shelve.open("'"$tmp"'", flag="n"); db["eggs"] = "eggs"; db.close()'
shelve2json "$tmp"
rm -f "$tmp" "$tmp.json"
python3 -c 'import shelve; db=shelve.open("'"$tmp"'", flag="n"); db["eggs"] = "eggs"; db.sync(); db.close()'

if [[ -s "$tmp".dat ]]; then
# Windows
shelve2json "$tmp".dat
elif [[ -s "$tmp".db ]]; then
# macOS
shelve2json "$tmp".db
else
# Linux
shelve2json "$tmp"
fi

rm -f "$tmp" "$tmp".{dat,db,json}

0 comments on commit 3e898d3

Please sign in to comment.