Print the usage help sorted and added some documentation
[git-stats.git] / src / stats.py
blob284cd7b701797dce9653c37793521b99014419b6
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 def version(*args):
29 """
30 """
32 print("Current version is 0.1-alpha_pre_dinner_build")
33 return 0
35 class DispatchException(Exception):
36 """This exception is raised when something went wrong during dispatching
37 """
39 pass
41 class Dispatcher():
42 """This class provides basic dispatching functionality
43 """
45 def __init__(self, commands):
46 self.commands = commands
48 def showUsageMessage(self):
49 """Shows a usage message, listing all available commands
50 """
52 print("Available commands are:")
54 keys = self.commands.keys()
55 keys.sort()
57 for key in keys:
58 print(key)
60 def dispatch(self, argv):
61 """Dispatches a command with the specified arguments
63 Args:
64 argv: The arguments to parse and dispatch.
65 """
67 if not len(argv) > 1:
68 self.showUsageMessage()
69 return 1
71 command = argv[1]
73 for key, value in self.commands.iteritems():
74 if key.startswith(command):
75 func = value
76 break
77 else:
78 raise DispatchException("Unknown command '" + command + "'.")
80 # When not specifying a command, throw in the --help switch
81 if len(argv) == 2:
82 argv.append("--help")
84 return func(*argv[2:])
86 commands = {
87 "author" : author.dispatch,
88 "branch" : branch.dispatch,
89 "commit" : commit.dispatch,
90 "diff" : diff.dispatch,
91 "index" : index.dispatch,
92 "-v" : version,
96 if __name__ == '__main__':
97 import sys
98 result = Dispatcher(commands).dispatch(sys.argv)
99 sys.exit(result)