aboutsummaryrefslogtreecommitdiffstats
path: root/RepSys/command.py
diff options
context:
space:
mode:
authorFrederic Lepied <flepied@mandriva.com>2005-12-07 10:06:33 +0000
committerFrederic Lepied <flepied@mandriva.com>2005-12-07 10:06:33 +0000
commit2c78c40c9bfae22d1501022b2e52cf938b5a957e (patch)
tree33a030236dd735f665e7b1ff1a9fba203742ecc0 /RepSys/command.py
parentaa9174bf0cce8d6a3c6e4d9e795be717da184406 (diff)
downloadmgarepo-2c78c40c9bfae22d1501022b2e52cf938b5a957e.tar
mgarepo-2c78c40c9bfae22d1501022b2e52cf938b5a957e.tar.gz
mgarepo-2c78c40c9bfae22d1501022b2e52cf938b5a957e.tar.bz2
mgarepo-2c78c40c9bfae22d1501022b2e52cf938b5a957e.tar.xz
mgarepo-2c78c40c9bfae22d1501022b2e52cf938b5a957e.zip
Diffstat (limited to 'RepSys/command.py')
-rw-r--r--RepSys/command.py53
1 files changed, 53 insertions, 0 deletions
diff --git a/RepSys/command.py b/RepSys/command.py
new file mode 100644
index 0000000..8029e08
--- /dev/null
+++ b/RepSys/command.py
@@ -0,0 +1,53 @@
+#!/usr/bin/python
+from RepSys import Error, config
+import sys, os, urllib
+import optparse
+
+__all__ = ["OptionParser", "do_command", "default_parent"]
+
+class CapitalizeHelpFormatter(optparse.IndentedHelpFormatter):
+
+ def format_usage(self, usage):
+ return optparse.IndentedHelpFormatter \
+ .format_usage(self, usage).capitalize()
+
+ def format_heading(self, heading):
+ return optparse.IndentedHelpFormatter \
+ .format_heading(self, heading).capitalize()
+
+class OptionParser(optparse.OptionParser):
+
+ def __init__(self, usage=None, help=None, **kwargs):
+ if not "formatter" in kwargs:
+ kwargs["formatter"] = CapitalizeHelpFormatter()
+ optparse.OptionParser.__init__(self, usage, **kwargs)
+ self._overload_help = help
+
+ def format_help(self, formatter=None):
+ if self._overload_help:
+ return self._overload_help
+ else:
+ return optparse.OptionParser.format_help(self, formatter)
+
+ def error(self, msg):
+ raise Error, msg
+
+def do_command(parse_options_func, main_func):
+ try:
+ opt = parse_options_func()
+ main_func(**opt.__dict__)
+ except Error, e:
+ sys.stderr.write("error: %s\n" % str(e))
+ sys.exit(1)
+
+def default_parent(url):
+ if url.find("://") == -1:
+ default_parent = config.get("global", "default_parent")
+ if not default_parent:
+ raise Error, "received a relative url, " \
+ "but default_parent was not setup"
+ type, rest = urllib.splittype(default_parent)
+ url = type+':'+os.path.normpath(rest+'/'+url)
+ return url
+
+# vim:et:ts=4:sw=4