use argparse and configparser
This commit is contained in:
parent
eaedf03c33
commit
7e6fa0438b
@ -33,7 +33,8 @@ import locale
|
|||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
from os.path import dirname, join
|
from os.path import dirname, join
|
||||||
from optparse import OptionParser, make_option
|
from argparse import ArgumentParser
|
||||||
|
from ConfigParser import ConfigParser
|
||||||
|
|
||||||
import misaka
|
import misaka
|
||||||
from itsdangerous import URLSafeTimedSerializer
|
from itsdangerous import URLSafeTimedSerializer
|
||||||
@ -61,14 +62,14 @@ url_map = Map([
|
|||||||
|
|
||||||
class Isso(object):
|
class Isso(object):
|
||||||
|
|
||||||
BASE_URL = 'http://localhost:8080/'
|
|
||||||
MAX_AGE = 15 * 60
|
|
||||||
PRODUCTION = False
|
PRODUCTION = False
|
||||||
|
|
||||||
def __init__(self, dbpath, secret, base_url, max_age):
|
def __init__(self, dbpath, secret, base_url, max_age, passphrase):
|
||||||
|
|
||||||
self.DBPATH = dbpath
|
self.DBPATH = dbpath
|
||||||
self.BASE_URL = utils.normalize(base_url)
|
self.BASE_URL = utils.normalize(base_url)
|
||||||
|
self.PASSPHRASE = passphrase
|
||||||
|
self.MAX_AGE = max_age
|
||||||
|
|
||||||
self.db = db.SQLite(dbpath, moderation=False)
|
self.db = db.SQLite(dbpath, moderation=False)
|
||||||
self.signer = URLSafeTimedSerializer(secret)
|
self.signer = URLSafeTimedSerializer(secret)
|
||||||
@ -115,46 +116,48 @@ class Isso(object):
|
|||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
||||||
options = [
|
parser = ArgumentParser(description="a blog comment hosting service")
|
||||||
make_option("--version", action="store_true",
|
subparser = parser.add_subparsers(help="commands", dest="command")
|
||||||
help="print version info and exit"),
|
|
||||||
|
|
||||||
make_option("--dbpath", dest="dbpath", metavar='FILE', default=":memory:",
|
parser.add_argument('--version', action='version', version='%(prog)s ' + dist.version)
|
||||||
help="database location"),
|
parser.add_argument("-c", dest="conf", default="/etc/isso.conf",
|
||||||
make_option("--base-url", dest="base_url", default="http://localhost:8080/",
|
metavar="/etc/isso.conf", help="set configuration file")
|
||||||
help="set base url for comments"),
|
|
||||||
make_option("--secret-key", dest="secret", default=None,
|
|
||||||
help="fixed secret key (admin auth etc.)"),
|
|
||||||
make_option("--max-age", dest="max_age", default=15*60, type=int,
|
|
||||||
help="..."),
|
|
||||||
|
|
||||||
make_option("--host", dest="host", default="localhost",
|
imprt = subparser.add_parser('import', help="import Disqus XML export")
|
||||||
help="webserver address"),
|
imprt.add_argument("dump", metavar="FILE")
|
||||||
make_option("--port", dest="port", default=8080,
|
|
||||||
help="webserver port"),
|
serve = subparser.add_parser("run", help="run server")
|
||||||
|
|
||||||
|
defaultcfg = [
|
||||||
|
"[general]",
|
||||||
|
"dbpath = /tmp/isso.db", "secret = %r" % os.urandom(24),
|
||||||
|
"base_url = http://localhost:8080/", "passphrase = p@$$w0rd",
|
||||||
|
"max_age = 450",
|
||||||
|
"[server]",
|
||||||
|
"host = localhost", "port = 8080"
|
||||||
]
|
]
|
||||||
|
|
||||||
parser = OptionParser(option_list=options)
|
args = parser.parse_args()
|
||||||
options, args = parser.parse_args()
|
conf = ConfigParser(allow_no_value=True)
|
||||||
|
conf.readfp(io.StringIO(u'\n'.join(defaultcfg)))
|
||||||
|
conf.read(args.conf)
|
||||||
|
|
||||||
if options.version:
|
isso = Isso(
|
||||||
print('isso', dist.version)
|
dbpath=conf.get('general', 'dbpath'),
|
||||||
sys.exit(0)
|
secret=conf.get('general', 'secret'),
|
||||||
|
base_url=conf.get('general', 'base_url'),
|
||||||
|
max_age=conf.getint('general', 'max_age'),
|
||||||
|
passphrase=conf.get('general', 'passphrase')
|
||||||
|
)
|
||||||
|
|
||||||
isso = Isso(dbpath=options.dbpath, secret=options.secret or utils.mksecret(12),
|
if args.command == "import":
|
||||||
base_url=options.base_url, max_age=options.max_age)
|
migrate.disqus(isso.db, args.dump)
|
||||||
|
|
||||||
if len(args) > 0 and args[0] == 'import':
|
|
||||||
if len(args) < 2:
|
|
||||||
print('Usage: isso import FILE')
|
|
||||||
sys.exit(2)
|
|
||||||
|
|
||||||
migrate.disqus(isso.db, args[1])
|
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
app = SharedDataMiddleware(isso.wsgi_app, {
|
app = SharedDataMiddleware(isso.wsgi_app, {
|
||||||
'/static': join(dirname(__file__), 'static/')
|
'/static': join(dirname(__file__), 'static/')
|
||||||
})
|
})
|
||||||
|
|
||||||
print(' * Session Key:', isso.signer.secret_key)
|
run_simple(conf.get('server', 'host'), conf.getint('server', 'port'),
|
||||||
run_simple(options.host, options.port, app, threaded=True)
|
app, threaded=True)
|
||||||
|
|
||||||
|
@ -15,8 +15,7 @@ from itsdangerous import SignatureExpired, BadSignature
|
|||||||
def index(app, environ, request):
|
def index(app, environ, request):
|
||||||
|
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
print request.form.get('secret'), app.signer.secret_key
|
if request.form.get('password') == app.PASSPHRASE:
|
||||||
if request.form.get('secret') == app.signer.secret_key:
|
|
||||||
resp = redirect('/admin/', 301)
|
resp = redirect('/admin/', 301)
|
||||||
resp.set_cookie('admin', app.signer.dumps('*'), max_age=app.MAX_AGE)
|
resp.set_cookie('admin', app.signer.dumps('*'), max_age=app.MAX_AGE)
|
||||||
return resp
|
return resp
|
||||||
|
Loading…
Reference in New Issue
Block a user