1 | #!/usr/bin/python |
---|
2 | from RepSys import Error, config |
---|
3 | import sys, os, urllib |
---|
4 | import optparse |
---|
5 | |
---|
6 | __all__ = ["OptionParser", "do_command", "default_parent"] |
---|
7 | |
---|
8 | class CapitalizeHelpFormatter(optparse.IndentedHelpFormatter): |
---|
9 | |
---|
10 | def format_usage(self, usage): |
---|
11 | return optparse.IndentedHelpFormatter \ |
---|
12 | .format_usage(self, usage).capitalize() |
---|
13 | |
---|
14 | def format_heading(self, heading): |
---|
15 | return optparse.IndentedHelpFormatter \ |
---|
16 | .format_heading(self, heading).capitalize() |
---|
17 | |
---|
18 | class OptionParser(optparse.OptionParser): |
---|
19 | |
---|
20 | def __init__(self, usage=None, help=None, **kwargs): |
---|
21 | if not "formatter" in kwargs: |
---|
22 | kwargs["formatter"] = CapitalizeHelpFormatter() |
---|
23 | optparse.OptionParser.__init__(self, usage, **kwargs) |
---|
24 | self._overload_help = help |
---|
25 | |
---|
26 | def format_help(self, formatter=None): |
---|
27 | if self._overload_help: |
---|
28 | return self._overload_help |
---|
29 | else: |
---|
30 | return optparse.OptionParser.format_help(self, formatter) |
---|
31 | |
---|
32 | def error(self, msg): |
---|
33 | raise Error, msg |
---|
34 | |
---|
35 | def do_command(parse_options_func, main_func): |
---|
36 | try: |
---|
37 | opt = parse_options_func() |
---|
38 | main_func(**opt.__dict__) |
---|
39 | except Error, e: |
---|
40 | sys.stderr.write("error: %s\n" % str(e)) |
---|
41 | sys.exit(1) |
---|
42 | |
---|
43 | def default_parent(url): |
---|
44 | if url.find("://") == -1: |
---|
45 | default_parent = config.get("global", "default_parent") |
---|
46 | if not default_parent: |
---|
47 | raise Error, "received a relative url, " \ |
---|
48 | "but default_parent was not setup" |
---|
49 | type, rest = urllib.splittype(default_parent) |
---|
50 | url = type+':'+os.path.normpath(rest+'/'+url) |
---|
51 | return url |
---|
52 | |
---|
53 | # vim:et:ts=4:sw=4 |
---|