git @ Cat's Eye Technologies klaus / fab7f84
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
1 changed file(s) with 18 addition(s) and 10 deletion(s). Raw diff Collapse all Expand all
33 import sys
44 import os
55 import argparse
6 from webbrowser import open as open_url
6 import webbrowser
77
88 from dulwich.errors import NotGitRepository
99 from dulwich.repo import Repo
2828 parser.add_argument('--host', help="default: 127.0.0.1", default='127.0.0.1')
2929 parser.add_argument('--port', help="default: 8080", default=8080, type=int)
3030 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='?')
3333
3434 parser.add_argument('repos', help='repositories to serve',
3535 metavar='DIR', nargs='*', type=git_repository)
6363 )
6464
6565 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
7368 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))
7482
7583
7684 if __name__ == '__main__':