Created
May 6, 2015 23:04
-
-
Save junmakii/3ca492bc7caa01326fa6 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/python | |
| #-*- coding: utf-8 -*- | |
| import os, sys, datetime | |
| import argparse | |
| import json | |
| from sqlalchemy.types import (VARCHAR, TEXT, BOOLEAN, INTEGER, FLOAT, TIME, | |
| DATE, DATETIME, TIMESTAMP) | |
| from sqlalchemy.schema import (ForeignKey) | |
| from sqlalchemy.sql.expression import (asc, desc) | |
| from sqlalchemy.schema import Column | |
| from sqlalchemy.ext.declarative import declarative_base | |
| from sqlalchemy import create_engine | |
| from sqlalchemy.orm import sessionmaker, scoped_session | |
| Base = declarative_base() | |
| class Text(Base): | |
| __tablename__ = 'texts' | |
| id = Column(INTEGER, primary_key=True, autoincrement=True) | |
| name = Column(VARCHAR(255), nullable=True) | |
| text = Column(TEXT, nullable=False) | |
| keyword = Column(VARCHAR(255), nullable=True) | |
| created_at = Column(DATETIME, default=datetime.datetime.now()) | |
| db_path = os.path.join(os.environ.get('XDG_HOME') or os.environ.get('HOME'), | |
| '.' + os.path.basename(__file__) + '.db.sqlite') | |
| db_url = 'sqlite:///' + db_path | |
| print(Text.__table__.columns.keys()) | |
| def parse_args(argv): | |
| parser = argparse.ArgumentParser(description='') | |
| parser.add_argument('-d', '--database', action='store') | |
| parser.add_argument('--initialize', action='store_true') | |
| parser.add_argument('-a', '--all', action='store_true') | |
| parser.add_argument('-l', '--limit', type=int, default=10, action='store') | |
| parser.add_argument('-s', '--search', default='%', action='store') | |
| parser.add_argument('-f', '--file', action='store') | |
| parser.add_argument('-r', '--remove', type=int, action='store') | |
| parser.add_argument('-e', '--export', action='store') | |
| parser.add_argument('-n', '--name', action='store') | |
| parser.add_argument('-k', '--keyword', action='store') | |
| parser.add_argument('-t', '--text', action='store') | |
| args = parser.parse_args(argv) | |
| return args | |
| def datetime_json_format(obj): | |
| return obj.isoformat() if hasattr(obj, 'isoformat') else obj | |
| def main(argv): | |
| args = parse_args(argv) | |
| print(db_url) | |
| db = create_engine(db_url) | |
| session = scoped_session(sessionmaker(bind=db)) | |
| if args.initialize: | |
| Base.metadata.create_all(db) | |
| return 0 | |
| if args.remove: | |
| text = session.query(Text).filter(Text.id == args.remove)[0] | |
| session.delete(text) | |
| session.commit() | |
| return 0 | |
| if args.all: | |
| texts = session.query(Text).filter(Text.text.like( | |
| args.search)).order_by(desc(Text.created_at))[0:args.limit] | |
| for text in texts: | |
| for key in Text.__table__.columns.keys(): | |
| print('key: ' + key) | |
| print(getattr(text, key)) | |
| print('---') | |
| text_dicts = dict([(key, getattr(text, key)) | |
| for key in Text.__table__.columns.keys()]) | |
| if args.export: | |
| json_str = json.dumps(text_dicts, default=datetime_json_format) | |
| with open(args.export, 'w') as fp: | |
| fp.write(json_str.encode('utf8')) | |
| return 0 | |
| if args.file: | |
| text = open(args.file, 'r').read() | |
| name = args.file | |
| else: | |
| text = args.text | |
| name = args.name | |
| keyword = args.keyword | |
| text = Text(text=text, name=name, keyword=keyword) | |
| print(session.add(text)) | |
| print(session.commit()) | |
| if __name__ == '__main__': | |
| main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment