Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = sqlite-shelve
version = 2.0.1
version = 2.2.1
author = Michael Mabin
author_email = d3vvnull@gmail.com
description = A SQLite implementation of the Python Shelf interface
Expand All @@ -22,3 +22,7 @@ python_requires = >=3.6

[options.packages.find]
where = src

[options.entry_points]
console_scripts =
shelve-tool = sqliteshelve.cli:cli
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from setuptools import setup
if __name__ == '__main__':
setup()
7 changes: 5 additions & 2 deletions src/sqliteshelve/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# sqliteshelve module
import pickle, sqlite3, os
import os
import pickle
import shelve
import sqlite3


class Shelf(object):
class Shelf(shelve.Shelf):
"""Hackified Shelf class with sqlite3"""

def __init__(self, dbpath):
Expand Down
29 changes: 17 additions & 12 deletions bin/shelve-tool → src/sqliteshelve/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
# show _key_ - shows the contents of record indexed by _key_
# namespace(file='mydb', func=<function add at 0x1013f4050>, key='obj1', keywords=['database=blah', 'count=4'], type='user-object')
##################################################################################################
import sqliteshelve as shelve
from . import open as shelve_open
from . import close as shelve_close
import argparse
import os
import sys
Expand Down Expand Up @@ -46,7 +47,7 @@ def add(args):
rec['type'] = args.type
rec['key'] = args.key

db = shelve.open(args.file)
db = shelve_open(args.file)

udt_key = 'UDT_%s' % args.type
if udt_key in db:
Expand Down Expand Up @@ -77,7 +78,7 @@ def add_type(args):
a which fields are required. It also enables more convenient searches for information based on type.
And queries on fields.
"""
db = shelve.open(args.file)
db = shelve_open(args.file)
user_type_name = 'UDT_' + args.type_name
if user_type_name in db:
print("User type %s already exists." % user_type_name)
Expand All @@ -102,7 +103,7 @@ def list_records(args):
"""
List the records in a database.
"""
db = shelve.open(args.file)
db = shelve_open(args.file)
count = 0
if args.long:
header = "%-15s %-60s" % ('Object Key','Data')
Expand Down Expand Up @@ -137,7 +138,7 @@ def show(args):
"""
Show contents of a record.
"""
db = shelve.open(args.file)
db = shelve_open(args.file)
if args.key in db:
print("Displaying contents of record: %s" % args.key)
rec = db[args.key]
Expand All @@ -150,7 +151,7 @@ def update(args):
"""
Update a records attributes.
"""
db = shelve.open(args.file)
db = shelve_open(args.file)
if args.key in db:
updates = parse_keywords(args.keywords)
rec_to_update = db[args.key]
Expand All @@ -169,7 +170,7 @@ def delete(args):
"""
Delete a record from the database.
"""
db = shelve.open(args.file)
db = shelve_open(args.file)
if args.key in db:
del db[args.key]
db.close() #commit or delete will not happen
Expand Down Expand Up @@ -219,11 +220,15 @@ def delete(args):
parser_show.add_argument('key',help='Key identifying name of record.')
parser_show.set_defaults(func=show)

def cli():
"""
Execute cli functions
"""

args = parser.parse_args()
args = parser.parse_args()

if len(sys.argv) < 2:
parser.print_help()
sys.exit(0)
if len(sys.argv) < 2:
parser.print_help()
sys.exit(0)

args.func(args)
args.func(args)
15 changes: 15 additions & 0 deletions tests/test_sqliteshelve.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,21 @@ def test__delitem__(self):
self.assertEqual(len(self.db), 3)
self.assertNotIn("AL", self.db)

def test_contextmanager(self):
"""
Ensures contextmanager works for sqliteshelve.Shelf
"""
with shelve.open("ctx_shelve") as ctx_shelve:
ctx_shelve["MN"] = "Minnesota"
ctx_shelve["OR"] = "Oregon"
ctx_shelve["CA"] = "California"

with shelve.open("ctx_shelve") as ctx:
keys = ctx.keys()
self.assertIn("MN", keys)
self.assertIn("OR", keys)
self.assertIn("CA", keys)

def tearDown(self):
if os.path.exists('test_shelf'):
os.remove('test_shelf')
Expand Down