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 6551edb
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,13 @@ jobs:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "pypy-3.10"]
include:
- os: windows-latest
python-version: ["3.8", "3.12"]
python-version: "3.8"
- os: macos-latest
python-version: ["3.8", "3.12"]
python-version: "3.8"
- os: windows-latest
python-version: "3.12"
- os: macos-latest
python-version: "3.12"
runs-on: ${{ matrix.os }}

steps:
Expand Down
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 6551edb

Please sign in to comment.