gitstats: Licensed GitStats under the Apache License, 2.0
[git-stats.git] / src / stats.py
blobc1073f227c311a154daf08f76ea087cc43cc0bcb
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
29 from git_stats import tests
31 def version(*args):
32 """Shows a silly version message
33 """
35 print("Current version is 0.1-alpha_pre_dinner_build")
36 return 0
38 class DispatchException(Exception):
39 """This exception is raised when something went wrong during dispatching
40 """
42 pass
44 class Dispatcher():
45 """This class provides basic dispatching functionality
46 """
49 def __init__(self, commands):
50 self.commands = commands
52 def showUsageMessage(self):
53 """Shows a usage message, listing all available commands
54 """
56 print("Available commands are:")
58 for key in sorted(self.commands):
59 print(key)
61 def dispatch(self, argv):
62 """Dispatches a command with the specified arguments
64 Args:
65 argv: The arguments to parse and dispatch.
66 """
68 if not len(argv) > 0:
69 self.showUsageMessage()
70 return 1
72 command = argv[0]
74 for key, value in self.commands.iteritems():
75 if key.startswith(command):
76 func = value
77 break
78 else:
79 raise DispatchException("Unknown command '" + command + "'.")
81 # When not specifying a command, throw in the --help switch
82 if len(argv) == 1:
83 argv.append("--help")
85 return func(*argv[1:])
87 commands = {
88 "author" : author.dispatch,
89 "bug" : bug.dispatch,
90 "branch" : branch.dispatch,
91 "commit" : commit.dispatch,
92 "diff" : diff.dispatch,
93 "index" : index.dispatch,
94 "matcher":matcher.dispatch,
95 "tests" : tests.dispatch,
96 "-v" : version,
99 def main(args):
100 """Main routine, dispatches the command specified in args
102 If called with sys.argv, cut off the first element!
105 result = Dispatcher(commands).dispatch(args)
106 return result
108 if __name__ == '__main__':
109 import sys
110 result = main(sys.argv[1:])
111 sys.exit(result)