Commit 77fc0b3a authored by Romain Bignon's avatar Romain Bignon

parsing of arguments and connection to database

parent 62c3bdd1
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, scoped_session
class Database(object):
def __init__(self, url):
self.url = url
self.engine = create_engine(url, pool_recycle=3600)
self.Session = scoped_session(sessionmaker(bind=self.engine))
import builtins
import argparse
import sys
from prettytable import PrettyTable
from sqlalchemy.engine.url import URL
from xonsh.shell import Shell
from clitodb import __version__
from .database import Database
class CLItoDB(Shell):
def cmdloop(self):
def build_parser(self):
p = argparse.ArgumentParser(description='clitodb', add_help=False)
p.add_argument('--help',
dest='help',
action='store_true',
default=False,
help='show help and exit')
p.add_argument('--version',
dest='version',
action='store_true',
default=False,
help='show version information and exit')
p.add_argument('--driver',
help='What driver to use to connect to database',
dest='driver',
required=False,
default='mysql')
p.add_argument('-h', '--host',
help="Connect to the server on the given host",
dest='host_name',
required=False,
default='localhost')
p.add_argument('-P', '--port',
help='Connect to the server on the given port',
dest='port_num',
required=False,
default=None)
p.add_argument('-u', '--user',
help='The user name to use when connecting to the server',
dest='user',
required=False,
default=None)
p.add_argument('-p', '--password',
help='The password to use when connecting to the server',
dest='password',
required=False,
default=None)
p.add_argument('database',
help='Database name',
nargs='?',
default=None)
return p
def cmdloop(self, argv=None):
if argv is None:
argv = sys.argv[1:]
parser = self.build_parser()
args = parser.parse_args(argv)
if args.help:
parser.print_help()
parser.exit()
if args.version:
version = '/'.join(('clitodb', __version__))
print(version)
parser.exit()
self.db = Database(self.build_url(args))
builtins.__xonsh_shell__ = self
builtins.__xonsh_subproc_uncaptured__ = self.sql_cmd
builtins.__xonsh_subproc_captured_stdout__ = self.sql_cmd
builtins.__xonsh_subproc_captured_inject__ = self.sql_cmd
builtins.__xonsh_subproc_captured_object__ = self.sql_cmd
builtins.__xonsh_subproc_captured_hiddenobject__ = self.sql_cmd
builtins.__xonsh_env__['PROMPT'] = '{BOLD_CYAN}{user}{BOLD_WHITE}@{BOLD_GREEN}{hostname}{BOLD_BLUE}:budgea>{NO_COLOR} '
builtins.__xonsh_env__['FORMATTER_DICT']['database'] = args.database
builtins.__xonsh_env__['FORMATTER_DICT']['user'] = args.user
builtins.__xonsh_env__['FORMATTER_DICT']['hostname'] = args.host_name
builtins.__xonsh_env__['PROMPT'] = '{BOLD_CYAN}{user}{BOLD_WHITE}@{BOLD_GREEN}{hostname}{BOLD_BLUE}:{database}>{NO_COLOR} '
self.shell.cmdloop()
def sql_cmd(self, *args, **kwargs):
print(args, kwargs)
def sql_cmd(self, *cmds):
session = self.db.Session()
for cmd in cmds:
table = PrettyTable()
for line in session.execute(' '.join(cmd)):
table.add_row(line)
print(table.get_string())
self.db.Session.remove()
def build_url(self, args):
url = URL(args.driver,
username=args.user,
password=args.password,
host=args.host_name,
port=args.port_num,
database=args.database,
query={"charset": "utf8"})
return url
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment