Make --browser option a bit more flexible
You may now optionally pass a browser name or executable to the
--browser option, which is then preferred over the default browser.
See #107.
Jonas Haag
10 years ago
3 | 3 | import sys |
4 | 4 | import os |
5 | 5 | import argparse |
6 | from webbrowser import open as open_url | |
6 | import webbrowser | |
7 | 7 | |
8 | 8 | from dulwich.errors import NotGitRepository |
9 | 9 | from dulwich.repo import Repo |
28 | 28 | parser.add_argument('--host', help="default: 127.0.0.1", default='127.0.0.1') |
29 | 29 | parser.add_argument('--port', help="default: 8080", default=8080, type=int) |
30 | 30 | parser.add_argument('--site-name', help="site name showed in header. default: your hostname") |
31 | parser.add_argument('-b', '--browser', help="launch a browser on server start", | |
32 | action='store_true', default=False) | |
31 | parser.add_argument('-b', '--browser', help="open klaus in default browser on server start. Optional argument: browser executable to use", | |
32 | default=None, const='__default_browser__', nargs='?') | |
33 | 33 | |
34 | 34 | parser.add_argument('repos', help='repositories to serve', |
35 | 35 | metavar='DIR', nargs='*', type=git_repository) |
63 | 63 | ) |
64 | 64 | |
65 | 65 | if args.browser: |
66 | # Open a web browser onto the server URL. Technically we're jumping the | |
67 | # gun a little here since the server is not running but there's not a | |
68 | # clean way to run a function after the server has started without | |
69 | # losing the simplicity of the code. In the Real World (TM) it'll take | |
70 | # longer for the browser to start than it will for use to start | |
71 | # serving. | |
72 | open_url('http://%s:%s' % (args.host, args.port)) | |
66 | _open_browser(args) | |
67 | ||
73 | 68 | app.run(args.host, args.port, args.debug) |
69 | ||
70 | def _open_browser(args): | |
71 | # Open a web browser onto the server URL. Technically we're jumping the | |
72 | # gun a little here since the server is not yet running, but there's no | |
73 | # clean way to run a function after the server has started without | |
74 | # losing the simplicity of the code. In the Real World (TM) it'll take | |
75 | # longer for the browser to start than it will for us to start | |
76 | # serving, so we'll be OK. | |
77 | if args.browser == '__default_browser__': | |
78 | opener = webbrowser.open | |
79 | else: | |
80 | opener = webbrowser.get(args.browser).open | |
81 | opener('http://%s:%s' % (args.host, args.port)) | |
74 | 82 | |
75 | 83 | |
76 | 84 | if __name__ == '__main__': |