gitstats: Fix some spelling mistakes.
[git-stats.git] / src / stats.py
blob3ddd863ab1e271398cd75f285095633765916636
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 environname = "GIT_STATS_PATH"
12 # Try to retreive the path from the environment
13 # This is so that we can then import from git_python
14 if os.environ.has_key(environname):
15 dir = os.environ[environname]
16 dir = os.path.expanduser(dir)
17 dir = os.path.expandvars(dir)
18 else:
19 # Figure out what we are running from
20 # Then get the directory we are in and use that instead
21 # Ofcourse, this only works if git_python is in the dir we are run from
23 file = __file__
24 path = os.path.abspath(file)
25 dir = os.path.dirname(path)
27 if not os.path.isdir(os.path.join(dir, "git_stats")):
28 raise Exception("Cannot find git_stats directory, please set $GIT_STATS_PATH")
30 os.sys.path.insert(0, dir)
32 from git_python import Git
34 from git_stats import author
35 from git_stats import branch
36 from git_stats import commit
37 from git_stats import diff
38 from git_stats import index
40 class DispatchException(Exception):
41 """This exception is raised when something went wrong during dispatching
42 """
44 pass
46 class Dispatcher():
47 """This class provides basic dispatching functionality
48 """
50 def __init__(self, commands):
51 self.commands = commands
53 def showUsageMessage(self):
54 print("Available commands are:")
55 for key in self.commands.iterkeys():
56 print(key)
58 def dispatch(self, argv):
59 if not len(argv) > 1:
60 self.showUsageMessage()
61 return 1
63 command = argv[1]
65 for key, value in self.commands.iteritems():
66 if key.startswith(command):
67 func = value
68 break
69 else:
70 raise DispatchException("Unknown command '" + command + "'.")
72 return func(*argv[2:])
74 commands = {
75 "author" : author.dispatch,
76 "branch" : branch.dispatch,
77 "commit" : commit.dispatch,
78 "diff" : diff.dispatch,
79 "index" : index.dispatch,
83 if __name__ == '__main__':
84 import sys
85 result = Dispatcher(commands).dispatch(sys.argv)
86 sys.exit(result)