gitstats: Usability enhancement: print help msg on 'stats.py subcmd'
[git-stats.git] / src / stats.py
blob1dbef00683c7d4f2df97c8008c8d889a97751d29
1 #!/usr/bin/env python
3 """This module contains commands related to statistics on git repositories.
5 Each command here is merely a wrapper around other, more complicated, commands.
6 """
8 import os
10 # Figure out what we are running from
11 # Then get the directory we are in and use that instead
12 # Ofcourse, this only works if git_python is in the dir we are run from
14 file = __file__
15 path = os.path.abspath(file)
16 dir = os.path.dirname(path)
18 os.sys.path.insert(0, dir)
20 from git import Git
22 from git_stats import author
23 from git_stats import branch
24 from git_stats import commit
25 from git_stats import diff
26 from git_stats import index
28 class DispatchException(Exception):
29 """This exception is raised when something went wrong during dispatching
30 """
32 pass
34 class Dispatcher():
35 """This class provides basic dispatching functionality
36 """
38 def __init__(self, commands):
39 self.commands = commands
41 def showUsageMessage(self):
42 print("Available commands are:")
43 for key in self.commands.iterkeys():
44 print(key)
46 def dispatch(self, argv):
47 if not len(argv) > 1:
48 self.showUsageMessage()
49 return 1
51 command = argv[1]
53 for key, value in self.commands.iteritems():
54 if key.startswith(command):
55 func = value
56 break
57 else:
58 raise DispatchException("Unknown command '" + command + "'.")
60 # When not specifying a command, throw in the --help switch
61 if len(argv) == 2:
62 argv.append("--help")
64 return func(*argv[2:])
66 commands = {
67 "author" : author.dispatch,
68 "branch" : branch.dispatch,
69 "commit" : commit.dispatch,
70 "diff" : diff.dispatch,
71 "index" : index.dispatch,
75 if __name__ == '__main__':
76 import sys
77 result = Dispatcher(commands).dispatch(sys.argv)
78 sys.exit(result)