Skip to content

Commit

Permalink
Release version 0.1.0
Browse files Browse the repository at this point in the history
- Added SQLite support
- Adjusted command line options
  • Loading branch information
BnMcGn committed Feb 28, 2024
1 parent 30a62f9 commit c5be349
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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:

Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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='[email protected]',
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': [
Expand Down
32 changes: 21 additions & 11 deletions src/vibase.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")

Expand Down Expand Up @@ -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():
Expand Down

0 comments on commit c5be349

Please sign in to comment.