| 1 | #!/usr/bin/env python |
|---|
| 2 | # -*- coding: utf-8 -*- |
|---|
| 3 | """ |
|---|
| 4 | Open a Zine Shell |
|---|
| 5 | ~~~~~~~~~~~~~~~~~ |
|---|
| 6 | |
|---|
| 7 | This script opens a shell for Zine. |
|---|
| 8 | |
|---|
| 9 | :copyright: (c) 2010 by the Zine Team, see AUTHORS for more details. |
|---|
| 10 | :license: BSD, see LICENSE for more details. |
|---|
| 11 | """ |
|---|
| 12 | import sys |
|---|
| 13 | from os.path import abspath, join, pardir, isfile, dirname |
|---|
| 14 | from optparse import OptionParser |
|---|
| 15 | from werkzeug.script import make_shell |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | sys.path.append(dirname(__file__)) |
|---|
| 19 | from _init_zine import find_instance |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | def init_func(instance): |
|---|
| 23 | from zine import setup |
|---|
| 24 | app = setup(instance) |
|---|
| 25 | del setup |
|---|
| 26 | from zine import models |
|---|
| 27 | from zine.database import db |
|---|
| 28 | return locals() |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | def main(): |
|---|
| 32 | parser = OptionParser(usage='%prog [options]') |
|---|
| 33 | parser.add_option('--no-ipython', dest='no_ipython', action='store_true', |
|---|
| 34 | help='Do not launch ipython, even if present.') |
|---|
| 35 | parser.add_option('--instance', '-I', dest='instance', |
|---|
| 36 | help='Use the path provided as Zine instance.') |
|---|
| 37 | options, args = parser.parse_args() |
|---|
| 38 | if args: |
|---|
| 39 | parser.error('incorrect number of arguments') |
|---|
| 40 | instance = options.instance or find_instance() |
|---|
| 41 | if instance is None: |
|---|
| 42 | parser.error('instance not found. Specify path to instance') |
|---|
| 43 | if options.instance: |
|---|
| 44 | # Consume -I/--instance |
|---|
| 45 | for option in ('-I', '--instance'): |
|---|
| 46 | if option in sys.argv: |
|---|
| 47 | pop_index = sys.argv.index(option) |
|---|
| 48 | sys.argv.pop(pop_index) # pop option flag |
|---|
| 49 | sys.argv.pop(pop_index) # pop option value |
|---|
| 50 | |
|---|
| 51 | make_shell(lambda: init_func(instance), banner='Zine Shell [%s]' % |
|---|
| 52 | abspath(instance))(ipython=not options.no_ipython) |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | if __name__ == '__main__': |
|---|
| 56 | main() |
|---|