Skip to content

Instantly share code, notes, and snippets.

@junmakii
Created May 6, 2015 22:50
Show Gist options
  • Select an option

  • Save junmakii/baf85a544758dd1e4522 to your computer and use it in GitHub Desktop.

Select an option

Save junmakii/baf85a544758dd1e4522 to your computer and use it in GitHub Desktop.
#!/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)
text = Column(TEXT, nullable=False)
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('-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.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:
print(text)
print(text.id)
print(text.text)
print(text.created_at)
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.initialize:
Base.metadata.create_all(db)
text = (args.text if args.file is None else open(args.file, 'r').read())
text = Text(text=text)
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