From 34977bb987694b4a770cbb69bbe249aba18ea5cf Mon Sep 17 00:00:00 2001 From: jeremy avnet Date: Wed, 11 Sep 2024 12:17:10 -0700 Subject: [PATCH 1/2] Migrate to `uv` and modernize things --- .github/workflows/main.yml | 74 +++++-------------- pyproject.toml | 51 +++++++++++++ setup.cfg | 2 - setup.py | 40 ---------- {json_store => src/json_store}/__init__.py | 0 {json_store => src/json_store}/json_store.py | 0 {json_store => src/json_store}/shelve2json.py | 0 7 files changed, 68 insertions(+), 99 deletions(-) create mode 100644 pyproject.toml delete mode 100644 setup.cfg delete mode 100644 setup.py rename {json_store => src/json_store}/__init__.py (100%) rename {json_store => src/json_store}/json_store.py (100%) rename {json_store => src/json_store}/shelve2json.py (100%) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index db39929..3dc298d 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,9 +1,6 @@ name: json-store on: [push, pull_request] -env: - pip-cache-key: 2023.02.03 - jobs: lint: runs-on: ubuntu-latest @@ -11,13 +8,8 @@ jobs: steps: - uses: actions/checkout@v4 - - name: pip cache - uses: actions/cache@v4 - with: - path: ~/.cache/pip - key: ${{ runner.os }}-pip-${{ github.job }}-${{ env.pip-cache-key }} - restore-keys: | - ${{ runner.os }}-pip- + - name: Install uv + uses: astral-sh/setup-uv@v2 - name: pre-commit cache uses: actions/cache@v4 @@ -27,14 +19,8 @@ jobs: restore-keys: | ${{ runner.os }}-pre-commit- - - name: Display Python version - run: python -c "import sys; print(sys.version)" - - - name: Install ${{ github.job }} dependencies - run: pip install --disable-pip-version-check pre-commit - - name: Run pre-commit tests - run: pre-commit run -a + run: uv tool run pre-commit run -a build: runs-on: ubuntu-latest @@ -42,25 +28,11 @@ jobs: steps: - uses: actions/checkout@v4 - - name: pip cache - uses: actions/cache@v4 - with: - path: ~/.cache/pip - key: ${{ runner.os }}-pip-${{ github.job }}-${{ env.pip-cache-key }} - restore-keys: | - ${{ runner.os }}-pip- - - - name: Display Python version - run: python -c "import sys; print(sys.version)" - - - name: Upgrade pip and setuptools - run: python -m pip install --upgrade pip setuptools - - - name: Install ${{ github.job }} dependencies - run: pip --disable-pip-version-check install wheel build + - name: Install uv + uses: astral-sh/setup-uv@v2 - name: Build package - run: python -m build + run: uv build - name: Store build artifacts uses: actions/upload-artifact@v4 @@ -72,44 +44,32 @@ jobs: needs: [lint, build] strategy: matrix: - os: [ubuntu-latest, windows-latest] + os: [ubuntu-latest] 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"] + - os: macos-latest + python-version: ["3.8", "3.12"] runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v4 - - name: pip cache - uses: actions/cache@v4 - with: - path: ~/.cache/pip - key: ${{ runner.os }}-pip-${{ github.job }}-${{ env.pip-cache-key }} - restore-keys: | - ${{ runner.os }}-pip- + - name: Install uv + uses: astral-sh/setup-uv@v2 - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} - - name: Download build artifacts - uses: actions/download-artifact@v4 - with: - name: dist - path: dist - - name: Install ${{ github.job }} dependencies run: | - pip install --disable-pip-version-check pytest - - - name: Install json-store package - shell: bash # so this works on both Linux and Windows - run: | - pip install --disable-pip-version-check dist/json_store-*.whl + uv sync --dev - name: Test with pytest - run: pytest + run: uv run pytest - name: Test shelve2json - if: ${{ matrix.os != 'windows-latest' }} # shelve.open has an internal traceback on Windows - run: sh tests/test_shelve2json.sh + run: uv run sh tests/test_shelve2json.sh diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..8d8ecfa --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,51 @@ +[project] +dynamic = ["version"] +name = "json-store" +requires-python = ">=3.8" +dependencies = [] + +authors = [{name = "jeremy avnet", email = "json-store@theory.org"}] +description = "A shelve-like store using JSON serialization." +readme = "README.md" +license = {file = "LICENSE.txt"} +keywords = ["json", "shelve"] + +classifiers = [ + "Development Status :: 5 - Production/Stable", + + "Intended Audience :: Developers", + "Topic :: Software Development :: Libraries", + + "License :: OSI Approved :: MIT License", + + "Operating System :: OS Independent", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: Implementation :: CPython", + "Programming Language :: Python :: Implementation :: PyPy", +] + +[project.urls] +Homepage = "https://github.com/brainsik/json-store" +Repository = "https://github.com/brainsik/json-store.git" +Issues = "https://github.com/brainsik/json-store/issues" +Changelog = "https://github.com/brainsik/json-store/releases" + +[project.scripts] +shelve2json = "json_store.shelve2json:main" + +[build-system] +requires = ["hatchling"] +build-backend = "hatchling.build" + +[tool.hatch.version] +path = "src/json_store/__init__.py" + +[tool.uv] +dev-dependencies = [ + "pytest >=8.3" +] diff --git a/setup.cfg b/setup.cfg deleted file mode 100644 index 3c6e79c..0000000 --- a/setup.cfg +++ /dev/null @@ -1,2 +0,0 @@ -[bdist_wheel] -universal=1 diff --git a/setup.py b/setup.py deleted file mode 100644 index 140f10b..0000000 --- a/setup.py +++ /dev/null @@ -1,40 +0,0 @@ -# encoding: utf-8 -import setuptools - -import json_store - -setuptools.setup( - name="json-store", - version=json_store.__version__, - packages=["json_store"], - entry_points={ - "console_scripts": [ - "shelve2json=json_store.shelve2json:main", - ], - }, - description="A shelve-like store using JSON serialization.", - long_description=( - "JSON store is a simple replacement for shelve. It writes" - " JSON serialized files, accepts unicode keys, and tracks" - " whether the store has been changed since last sync." - " It has no dependencies." - ), - author="jeremy avnet", - author_email="json-store@theory.org", - license="MIT License", - url="https://github.com/brainsik/json-store", - classifiers=[ - "Development Status :: 5 - Production/Stable", - "Intended Audience :: Developers", - "License :: OSI Approved :: MIT License", - "Operating System :: OS Independent", - "Programming Language :: Python :: 3", - "Programming Language :: Python :: 3.8", - "Programming Language :: Python :: 3.9", - "Programming Language :: Python :: 3.10", - "Programming Language :: Python :: 3.11", - "Programming Language :: Python :: 3.12", - "Programming Language :: Python :: Implementation :: CPython", - "Programming Language :: Python :: Implementation :: PyPy", - ], -) diff --git a/json_store/__init__.py b/src/json_store/__init__.py similarity index 100% rename from json_store/__init__.py rename to src/json_store/__init__.py diff --git a/json_store/json_store.py b/src/json_store/json_store.py similarity index 100% rename from json_store/json_store.py rename to src/json_store/json_store.py diff --git a/json_store/shelve2json.py b/src/json_store/shelve2json.py similarity index 100% rename from json_store/shelve2json.py rename to src/json_store/shelve2json.py From 6551edbd7540103211fd2a95b3792f8c6b9421a0 Mon Sep 17 00:00:00 2001 From: jeremy avnet Date: Wed, 11 Sep 2024 12:22:11 -0700 Subject: [PATCH 2/2] Fix shelve2json for Win/Mac --- .github/workflows/main.yml | 8 ++++++-- src/json_store/__init__.py | 2 +- src/json_store/shelve2json.py | 10 +++++++--- tests/test_shelve2json.sh | 22 +++++++++++++++++----- 4 files changed, 31 insertions(+), 11 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 3dc298d..1f1d22d 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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: 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}