From 040dc97542cb150b75537f80d7f9f76455c1d2ff Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Thu, 27 Aug 2015 12:33:00 +0200 Subject: [PATCH] Import GitPython in a cleaner way * at the beginning of the code * without using 'global' * raise callbacks.Error like it should do, instead of base Exception. --- plugin.py | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/plugin.py b/plugin.py index 37dcf67..f850f79 100644 --- a/plugin.py +++ b/plugin.py @@ -41,8 +41,6 @@ import time import traceback -# 'import git' is performed during plugin initialization. -# # The GitPython library has different APIs depending on the version installed. # (0.1.x, 0.3.x supported) GIT_API_VERSION = -1 @@ -67,6 +65,18 @@ def plural(count, singular, plural=None): return singular[:-1] + 'ies' return singular + 's' +try: + import git +except ImportError: + raise callbacks.Error("GitPython is not installed.") +if not git.__version__.startswith('0.'): + raise callbacks.Error("Unsupported GitPython version.") +GIT_API_VERSION = int(git.__version__[2]) +if not GIT_API_VERSION in [1, 3]: + log_error('GitPython version %s unrecognized, using 0.3.x API.' + % git.__version__) + GIT_API_VERSION = 3 + def synchronized(tlockname): """ Decorates a class method (with self as the first parameter) to acquire the @@ -282,7 +292,6 @@ class Git(callbacks.PluginRegexp): unaddressedRegexps = [ '_snarf' ] def __init__(self, irc): - self.init_git_python() self.__parent = super(Git, self) self.__parent.__init__(irc) # Workaround the fact that self.log already exists in plugins @@ -299,20 +308,6 @@ def __init__(self, irc): log_warning(str(e)) self._schedule_next_event() - def init_git_python(self): - global GIT_API_VERSION, git - try: - import git - except ImportError: - raise Exception("GitPython is not installed.") - if not git.__version__.startswith('0.'): - raise Exception("Unsupported GitPython version.") - GIT_API_VERSION = int(git.__version__[2]) - if not GIT_API_VERSION in [1, 3]: - log_error('GitPython version %s unrecognized, using 0.3.x API.' - % git.__version__) - GIT_API_VERSION = 3 - def die(self): self._stop_polling() self.__parent.die()