import argparse
import getpass
import hashlib
import os
import sqlalchemy as sa
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

Base = declarative_base()
db = sa.create_engine('sqlite:///testdb.sqlite')


class DomainPassword(Base):
    __tablename__ = 'domainpassword'
    id = sa.Column(sa.Integer, primary_key=True)
    domain = sa.Column(sa.String(30))
    salt = sa.Column(sa.String(128))


    def __init__(self, **kwargs):
        super(DomainPassword, self).__init__(**kwargs)
        self.new_salt()


    def new_salt(self):
        self.salt = os.urandom(128).encode('base64')


    def derive_domain_key(self, master_password):
        key_bytes = hashlib.sha1('%s:%s' % (master_password, self.domain)).digest()
        return key_bytes.encode('hex')


    def __repr__(self):
        return 'DomainPassword(domain=%s, salt=%s)' % (self.domain, self.salt)


Base.metadata.create_all(db)

def main():
    args = get_args()
    domain_password = get_domain_password(args)
    master_password = getpass.getpass('Enter your master password: ')
    print domain_password.derive_domain_key(master_password)


def get_args():
    argparser = argparse.ArgumentParser(prog='pwm')
    argparser.add_argument('domain',
        help='The domain to retrieve the password for',
    )
    argparser.add_argument('-d', '--database', metavar='<database>',
        help='The database to store the salts in',
    )
    return argparser.parse_args()


def get_domain_password(args):
    session = get_db_session(args.database)
    dp = get_salt(session, args.domain)
    return dp


def get_salt(session, domain):
    domain_password = session.query(DomainPassword).filter(DomainPassword.domain == domain).first()
    if domain_password is None:
        domain_password = DomainPassword(domain=domain)
        session.add(domain_password)
        session.commit()
    return domain_password


def get_db_session(database):
    DBSession = sessionmaker(bind=db)
    session = DBSession()
    return session

@@ -49,16 +49,32 @@
 port os%0A
+import textwrap%0A
 import s
@@ -190,81 +190,148 @@
 er%0A%0A
-Base = declarative_base()%0Adb = sa.create_engine('sqlite:///testdb.sqlite'
+try:%0A    import configparser%0Aexcept:%0A    # python 2%0A    import ConfigParser as configparser%0A    input = raw_input%0A%0A%0ABase = declarative_base(
 )%0A%0A%0A
@@ -998,37 +998,8 @@
 t)%0A%0A
-%0ABase.metadata.create_all(db)
 %0A%0Ade
@@ -1386,40 +1386,246 @@
     
-argparser.add_argument('-d
+rc_dir = os.path.join(os.path.expanduser('~'), '.pwm')%0A    args = argparser.parse_args()%0A    if not os.path.exists(rc_dir):%0A        os.makedirs(rc_dir)%0A        run_setup()%0A    config = read_config()%0A    args.database = config.get('pwm
 ', '
---
 data
@@ -1633,122 +1633,1063 @@
 ase'
-, metavar='%3Cdatabase%3E',%0A        help='The database to store the salts in',%0A    )%0A    return argparser.parse_args()
+)%0A    return args%0A%0A%0Adef run_setup():%0A    print textwrap.dedent(%22%22%22%5C%0A        Hi, it looks like it's the first time you're using pwm on this machine. Let's take a little%0A        moment to set things up before we begin.%22%22%22)%0A    db_uri = input('Which database do you want to use? Default: local') or 'local'%0A    rc_dir = os.path.join(os.path.expanduser('~'), '.pwm')%0A%0A    if db_uri == 'local':%0A%0A        # normalize windows-style paths for sqlalchemy:%0A        rc_dir = rc_dir.replace('%5C%5C', '/')%0A%0A        # Create the local database%0A        db_uri = 'sqlite:///%25s/db.sqlite' %25 rc_dir%0A        db = sa.create_engine(db_uri)%0A        Base.metadata.create_all(db)%0A%0A    config = configparser.RawConfigParser()%0A    config.add_section('pwm')%0A    config.set('pwm', 'database', db_uri)%0A%0A    with open(os.path.join(rc_dir, 'config'), 'w') as configfile:%0A        config.write(configfile)%0A%0A%0Adef read_config():%0A    config_file = os.path.join(os.path.expanduser('~'), '.pwm', 'config')%0A    config = configparser.RawConfigParser()%0A    config.read(config_file)%0A    return config
 %0A%0A%0Ad
@@ -2767,17 +2767,30 @@
 e)%0A    d
-p
+omain_password
  = get_s
@@ -2827,17 +2827,30 @@
 return d
-p
+omain_password
 %0A%0A%0Adef g
@@ -3180,24 +3180,60 @@
 (database):%0A
+    db = sa.create_engine(database)%0A
     DBSessio

Add setup tutorial