diff --git a/src/json_store/__init__.py b/src/json_store/__init__.py index bc9bef4..82e2e1b 100644 --- a/src/json_store/__init__.py +++ b/src/json_store/__init__.py @@ -1,5 +1,5 @@ from .json_store import JSONStore -__version__ = "4.0" +__version__ = "4.1" open = JSONStore diff --git a/src/json_store/shelve2json.py b/src/json_store/shelve2json.py index 99b9796..797d237 100644 --- a/src/json_store/shelve2json.py +++ b/src/json_store/shelve2json.py @@ -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() diff --git a/tests/test_shelve2json.sh b/tests/test_shelve2json.sh index 36abfae..44e20d8 100644 --- a/tests/test_shelve2json.sh +++ b/tests/test_shelve2json.sh @@ -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}