gitstats: Removed the $GIT_STATS_PATH related code
[git-stats.git] / src / git_stats / stats.py
blob42e50c8136ede4c1208d3be057173ca1f5a31a0c
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 split = os.path.split(dir)
19 dir = split[0]
21 if not split[1] == "git_stats":
22 raise Exception("Please run this script inside the git_stats directory")
24 os.sys.path.insert(0, dir)
26 from git_python import Git
28 from git_stats import author
29 from git_stats import branch
30 from git_stats import commit
31 from git_stats import diff
32 from git_stats import index
34 class DispatchException(Exception):
35 """This exception is raised when something went wrong during dispatching
36 """
38 pass
40 class Dispatcher():
41 """This class provides basic dispatching functionality
42 """
44 def __init__(self, commands):
45 self.commands = commands
47 def showUsageMessage(self):
48 print("Available commands are:")
49 for key in self.commands.iterkeys():
50 print(key)
52 def dispatch(self, argv):
53 if not len(argv) > 1:
54 self.showUsageMessage()
55 return 1
57 command = argv[1]
59 for key, value in self.commands.iteritems():
60 if key.startswith(command):
61 func = value
62 break
63 else:
64 raise DispatchException("Unknown command '" + command + "'.")
66 return func(*argv[2:])
68 commands = {
69 "author" : author.dispatch,
70 "branch" : branch.dispatch,
71 "commit" : commit.dispatch,
72 "diff" : diff.dispatch,
73 "index" : index.dispatch,
77 if __name__ == '__main__':
78 import sys
79 result = Dispatcher(commands).dispatch(sys.argv)
80 sys.exit(result)