File: wikkid-serve

package info (click to toggle)
python-wikkid 0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 728 kB
  • sloc: python: 3,051; makefile: 12
file content (113 lines) | stat: -rwxr-xr-x 3,622 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#! /usr/bin/python3
# -*- coding: utf-8 -*-
#
# Copyright (C) 2010 Wikkid Developers.
#
# This software is licensed under the GNU Affero General Public License
# version 3 (see the file LICENSE).

"""The server class for the wiki."""

try:
    import _wikkid_path
except ImportError:
    # Not running from a branch.
    pass

import logging
import optparse
import sys

from wikkid import version
from wikkid.app import WikkidApp
from wikkid.context import (
    DEFAULT_FORMAT,
    DEFAULT_HOST,
    DEFAULT_PORT,
    ExecutionContext,
    )


def setup_logging():
    """Set up a logger sending to stderr."""
    handler = logging.StreamHandler(sys.stderr)
    fmt = '%(asctime)s %(levelname)-7s %(message)s'
    formatter = logging.Formatter(
        fmt=fmt, datefmt="%Y-%m-%d %H:%M:%S")
    handler.setFormatter(formatter)
    root = logging.getLogger()
    root.addHandler(handler)


def main(args):

    usage = "Usage: %prog [options] <wiki-branch>"
    parser = optparse.OptionParser(
        usage=usage, description="Run a Wikkid Wiki server.", version=version)
    parser.add_option('--format', type='choice', default='bzr',
        choices=['bzr', 'git'], help=("Default repository format to use."))
    parser.add_option(
        '--host', type='string', default=DEFAULT_HOST,
        help=('The interface to listen on. Defaults to %r' % DEFAULT_HOST))
    parser.add_option(
        '--port', type='int', default=DEFAULT_PORT,
        help=('The port to listen on.  Defaults to %s.' % DEFAULT_PORT))
    parser.add_option(
        '--default-format', type='string', default=DEFAULT_FORMAT,
        help=("Specify the default wiki format to use. Defaults to %r"
              % DEFAULT_FORMAT))
    parser.add_option(
        '--script-name',
        help=('The SCRIPT_NAME for the environment.  This is the prefix for the URLs'))
    options, args = parser.parse_args(sys.argv[1:])

    execution_context = ExecutionContext(
        host=options.host,
        port=options.port,
        default_format=options.default_format,
        script_name=options.script_name)

    if len(args) == 0:
        print("No branch location specified.")
        parser.print_usage()
        sys.exit(1)
    if len(args) > 1:
        print("Unexpected positional args: %s" % args[1:])
        parser.print_usage()
        sys.exit(1)
    branch = args[0]
    setup_logging()
    logger = logging.getLogger('wikkid')
    logger.setLevel(logging.INFO)

    if options.format == 'bzr':
        from breezy.workingtree import WorkingTree
        from wikkid.filestore.bzr import FileStore
        from wikkid.user.bzr import LocalBazaarUserMiddleware
        import breezy.bzr
        import breezy.git

        working_tree = WorkingTree.open(branch)
        logger.info('Using: %s', working_tree)
        filestore = FileStore(working_tree)
    elif options.format == 'git':
        from wikkid.filestore.git import FileStore
        from wikkid.user.git import LocalGitUserMiddleware
        filestore = FileStore.from_path(branch)

    app = WikkidApp(filestore=filestore, execution_context=execution_context)
    if options.format == 'bzr':
        app = LocalBazaarUserMiddleware(app, working_tree.branch)
    elif options.format == 'git':
        app = LocalGitUserMiddleware(app, filestore.repo)
    from wsgiref.simple_server import make_server
    httpd = make_server(options.host, options.port, app)
    logger.info('Serving on http://%s:%s', options.host, options.port)
    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        logger.info('Done.')


if __name__ == "__main__":
    main(sys.argv)