aboutsummaryrefslogtreecommitdiff
path: root/cli.py
diff options
context:
space:
mode:
Diffstat (limited to 'cli.py')
-rwxr-xr-xcli.py114
1 files changed, 114 insertions, 0 deletions
diff --git a/cli.py b/cli.py
new file mode 100755
index 0000000..bcb4c94
--- /dev/null
+++ b/cli.py
@@ -0,0 +1,114 @@
+#!/usr/bin/env python3
+import click
+
+from orator import DatabaseManager, Model
+import orator
+
+from pyble import *
+
+import re
+import time
+
+def setup_database(filename):
+ """Setup a connection with an SQLite database in filename"""
+ config = {
+ 'default': 'sqlite',
+ 'sqlite': {
+ 'driver': 'sqlite',
+ 'database': filename,
+ }
+ }
+ db = DatabaseManager(config)
+ Model.set_connection_resolver(db)
+ return db
+
+@click.group()
+def cli():
+ """Wrapper for CLI commands"""
+ pass
+
+@cli.command()
+@click.argument('filename', type=click.Path())
+def init(filename):
+ """Initialise an SQLite database in filename, and create tables"""
+ click.echo('Initialising pyble database...')
+ db = setup_database(filename)
+ setup_tables(db)
+ click.echo('Done.')
+
+def load_biblehub_line(columns, line):
+ """Split a line from a Bible Hub dump into a usable dictionary
+
+ Arguments:
+ columns -- the columns from the first line of the dump
+ line -- the line to read
+ """
+ pieces = line.split('\t')
+ ref = pieces[0]
+ refm = re.search('([\w ]+?)\s*(\d+):(\d+)', ref)
+ try:
+ parsed = { 'book' : refm.group(1),
+ 'chapter' : int(refm.group(2)),
+ 'verse' : int(refm.group(3)),
+ 'translations' : {} }
+ for i, col in enumerate(columns[1:]):
+ parsed['translations'][col] = pieces[i + 1].strip()
+ return parsed
+ except AttributeError:
+ print('Something wrong with your regex.\n', str(pieces))
+
+def load_biblehub(db, f):
+ """Load a Bible Hub dump into the database"""
+ content = f.read().splitlines()
+ columns = content[0]
+ columns, verses = columns.split('\t'), content[1:]
+
+ for translation in columns[1:]:
+ Translation.first_or_create(name=translation)
+
+ db.begin_transaction()
+ conn = db.connection().get_connection()
+
+ cur_book = None
+ for v in verses:
+ v = load_biblehub_line(columns, v)
+
+ if cur_book != v['book']:
+ if cur_book != None:
+ click.echo(str(round(time.time() - timer, 3)) + 's')
+ timer = time.time()
+
+ click.echo(v['book'] + '...', nl=False)
+ cur_book = v['book']
+
+ book = Book.first_or_create(canonical_name=cur_book)
+
+ # We don't use Orator here to make things ~4x faster
+ conn.execute('INSERT OR IGNORE INTO verses (book, chapter, nr) '
+ 'VALUES (?, ?, ?)', (v['book'], v['chapter'], v['verse']))
+
+ for translation, text in v['translations'].items():
+ conn.execute('INSERT OR IGNORE INTO translated_verses '
+ '(translation, book, chapter, nr, text) '
+ 'VALUES (?, ?, ?, ?, ?)',
+ (translation, cur_book, v['chapter'], v['verse'], text))
+
+ click.echo(str(round(time.time() - timer, 3)) + 's')
+ db.commit()
+
+@cli.command()
+@click.option('--type', required=True)
+@click.argument('filename', type=click.Path())
+@click.argument('dbname', type=click.Path())
+def load(type, filename, dbname):
+ """Load external data into the database"""
+ db = setup_database(dbname)
+ if type == 'biblehub':
+ with open(filename) as f:
+ load_biblehub(db, f)
+ else:
+ click.secho('I don\'t know a type ' + type, fg='red')
+
+if __name__ == '__main__':
+ cli()
+