Teach config.py to read multiple verses
[git-stats.git] / src / stats.py
blob35ac77911fc171d7cfa7a6714eb5da49f4c698d7
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 bug
24 from git_stats import branch
25 from git_stats import commit
26 from git_stats import diff
27 from git_stats import index
28 from git_stats import matcher
30 def version(*args):
31 """
32 """
34 print("Current version is 0.1-alpha_pre_dinner_build")
35 return 0
37 class DispatchException(Exception):
38 """This exception is raised when something went wrong during dispatching
39 """
41 pass
43 class Dispatcher():
44 """This class provides basic dispatching functionality
45 """
48 def __init__(self, commands):
49 self.commands = commands
51 def showUsageMessage(self):
52 """Shows a usage message, listing all available commands
53 """
55 print("Available commands are:")
57 keys = self.commands.keys()
58 keys.sort()
60 for key in keys:
61 print(key)
63 def dispatch(self, argv):
64 """Dispatches a command with the specified arguments
66 Args:
67 argv: The arguments to parse and dispatch.
68 """
70 if not len(argv) > 0:
71 self.showUsageMessage()
72 return 1
74 command = argv[0]
76 for key, value in self.commands.iteritems():
77 if key.startswith(command):
78 func = value
79 break
80 else:
81 raise DispatchException("Unknown command '" + command + "'.")
83 # When not specifying a command, throw in the --help switch
84 if len(argv) == 1:
85 argv.append("--help")
87 return func(*argv[1:])
89 commands = {
90 "author" : author.dispatch,
91 "bug" : bug.dispatch,
92 "branch" : branch.dispatch,
93 "commit" : commit.dispatch,
94 "diff" : diff.dispatch,
95 "index" : index.dispatch,
96 "matcher":matcher.dispatch,
97 "-v" : version,
100 def main(args):
101 """Main routine, dispatches the command specified in args
103 If called with sys.argv, cut off the first element!
106 result = Dispatcher(commands).dispatch(args)
107 return result
109 if __name__ == '__main__':
110 import sys
111 result = main(sys.argv[1:])
112 sys.exit(result)