Created
May 27, 2015 23:09
-
-
Save junmakii/4ae7bcd6bc642628bcb5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| #-*- coding: utf-8 -*- | |
| import os, sys, json | |
| import argparse | |
| from sqlalchemy.ext.declarative import declarative_base | |
| from sqlalchemy import create_engine | |
| from sqlalchemy.orm import sessionmaker, scoped_session | |
| from sqlalchemy.schema import Table | |
| from sqlalchemy.schema import MetaData | |
| Base = declarative_base() | |
| CONFIG_FILES = [os.path.join(os.environ.get('HOME'), '.config/junmakii/database.json')] | |
| DATABASE_ENCODING = 'utf-8' | |
| def parse_config_files(files): | |
| obj = {} | |
| for file in files: | |
| file_name = os.path.splitext(os.path.basename(file))[0] | |
| with open(file, 'r') as fp: | |
| json_obj = json.load(fp) | |
| obj.update({file_name: json_obj}) | |
| return obj | |
| def main(argv): | |
| parser = argparse.ArgumentParser() | |
| #parser.add_mutually_exclusive_group | |
| #parser.add_argument_group() | |
| #parser.add_mutually_exclusive_group() | |
| parser.add_argument('-c', '--config', default=CONFIG_FILES, action='append') | |
| args = parser.parse_args(argv) | |
| print(args) | |
| database_config = parse_config_files(args.config)['database'] | |
| database_url = 'mysql://%(user)s:%(password)s@%(host)s/%(database)s?unix_socket=%(socket)s' % database_config | |
| engine = create_engine(database_url, encoding=DATABASE_ENCODING) | |
| print(engine.execute('select * from bookmarks')) | |
| if __name__ == '__main__': | |
| sys.exit(main(sys.argv[1:])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment