From c5be3490ea063d8460571d571e935616d9a27e0a Mon Sep 17 00:00:00 2001 From: "bnmcgn@gmail.com" Date: Wed, 28 Feb 2024 07:29:13 -0800 Subject: [PATCH] Release version 0.1.0 - Added SQLite support - Adjusted command line options --- README.md | 16 +++++++++++++--- setup.py | 4 ++-- src/vibase.py | 32 +++++++++++++++++++++----------- 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 4faacbc..cca48d7 100644 --- a/README.md +++ b/README.md @@ -20,9 +20,17 @@ Or perhaps: # Usage - > vibase --module [module] [table] +## SQLite -At this time vibase only supports loading database connections through a python module. The first DBI connection found in the top level of the supplied module will be used. The module should be loadable by python 3. + > vibase [sqlite file] [table] + +Vibase can directly load a SQLite database file. + +## All others + + > vibase [module] [table] + +Vibase supports loading database connections through a python module. The first DBI connection found in the top level of the supplied module will be used. The module should be loadable by python 3. To connect to a postgresql database named cookiejar, you might create a file cjar.py: @@ -35,7 +43,7 @@ To connect to a postgresql database named cookiejar, you might create a file cja You may then edit the ingredients table: - > vibase --module cjar ingredients + > vibase cjar.py ingredients If you don't wish to store your totally unguessable password in the python file, use the getpass module: @@ -47,6 +55,8 @@ If you don't wish to store your totally unguessable password in the python file, user="me", \ password=getpass()) +Tested on PostgreSQL and SQLite. + # Author Ben McGunigle bnmcgn (at) gmail.com diff --git a/setup.py b/setup.py index 2f6ef2d..d69c783 100755 --- a/setup.py +++ b/setup.py @@ -7,12 +7,12 @@ sys.exit("Requires python 3 or greater") setup(name='vibase', - version='0.0.2', + version='0.1.0', description='Edit a database table using the VIM editor', author='Ben McGunigle', author_email='bnmcgn@gmail.com', url='https://github.com/BnMcGn/vibase', - download_url='https://github.com/BnMcGn/vibase/archive/0.0.2.zip', + download_url='https://github.com/BnMcGn/vibase/archive/0.1.0.tar.gzip', packages=['src'], entry_points = { 'console_scripts': [ diff --git a/src/vibase.py b/src/vibase.py index 33d758a..192d129 100644 --- a/src/vibase.py +++ b/src/vibase.py @@ -5,13 +5,20 @@ import subprocess import shutil import csv +import sqlite3 import argparse from importlib import import_module from src.util import query_yes_no, query_options +def is_file_sqlite(fname): + idstring = "SQLite format 3" + with open(fname, 'r', errors='ignore') as fh: + line = fh.readline() + return line.startswith(idstring) + def extract_conn_from_module(module): - for x in module.__dict__.values(): + for x in module.__dict__.values() if hasattr(x, "cursor"): return x raise (RuntimeError, "Couldn't find connection") @@ -33,15 +40,18 @@ def call_vim(target, vim=None): subprocess.run([vim, target]) def get_connection(args): - if "module" in args: - module = args.module - if module.endswith(".py"): - module = module[:-3] - elif module.endswith(".pyc"): - module = module[:-4] - conn = extract_conn_from_module(import_module(module)) - configure_for_connection(conn) - return conn + if "connection" in args and os.path.exists(args.connection): + if is_file_sqlite(args.connection): + return sqlite3.connect(args.connection) + else: + module = args.connection + if module.endswith(".py"): + module = module[:-3] + elif module.endswith(".pyc"): + module = module[:-4] + conn = extract_conn_from_module(import_module(module)) + configure_for_connection(conn) + return conn else: raise (RuntimeError, "No connection source supplied") @@ -182,8 +192,8 @@ def process_changes(reffile, editfile, conn, table, headers): def arguments(): args = argparse.ArgumentParser(description="Edit the contents of a database table using the VIM editor") + args.add_argument("connection", help="Supply a database. This can be a sqlite database file, or it can be a python module containing a connection object.") args.add_argument("table", help="The table to edit") - args.add_argument("-m", "--module", help="Supply a python module containing a connection object") return args def main():