diff --git a/setup.cfg b/setup.cfg index 779aea0..12f8e75 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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 @@ -22,3 +22,7 @@ python_requires = >=3.6 [options.packages.find] where = src + +[options.entry_points] +console_scripts = + shelve-tool = sqliteshelve.cli:cli diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..f13cf21 --- /dev/null +++ b/setup.py @@ -0,0 +1,3 @@ +from setuptools import setup +if __name__ == '__main__': + setup() diff --git a/src/sqliteshelve/__init__.py b/src/sqliteshelve/__init__.py index 8e879d5..5ef6f14 100644 --- a/src/sqliteshelve/__init__.py +++ b/src/sqliteshelve/__init__.py @@ -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): diff --git a/bin/shelve-tool b/src/sqliteshelve/cli.py similarity index 94% rename from bin/shelve-tool rename to src/sqliteshelve/cli.py index 11bceba..ea0d067 100755 --- a/bin/shelve-tool +++ b/src/sqliteshelve/cli.py @@ -12,7 +12,8 @@ # show _key_ - shows the contents of record indexed by _key_ # namespace(file='mydb', func=, 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 @@ -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: @@ -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) @@ -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') @@ -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] @@ -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] @@ -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 @@ -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) diff --git a/tests/test_sqliteshelve.py b/tests/test_sqliteshelve.py index 75bb6d9..5a3e633 100644 --- a/tests/test_sqliteshelve.py +++ b/tests/test_sqliteshelve.py @@ -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')