[libgit2-glib] Update and fixup the overrides



commit 790f55e252213dc4586eee1e84894eee5b3984de
Author: Garrett Regier <garrettregier gmail com>
Date:   Sat Aug 23 02:52:41 2014 -0700

    Update and fixup the overrides
    
    Support more iterable types, don't have str() act like repr(),
    correctly override classes, use pygobject's new
    get_introspection_module(), add license and more.

 libgit2-glib/Ggit.py |  110 +++++++++++++++++++++++++++++++++----------------
 1 files changed, 74 insertions(+), 36 deletions(-)
---
diff --git a/libgit2-glib/Ggit.py b/libgit2-glib/Ggit.py
index 6ce9410..675b45c 100644
--- a/libgit2-glib/Ggit.py
+++ b/libgit2-glib/Ggit.py
@@ -1,65 +1,103 @@
-from gi.repository import GObject, Gio
+# -*- Mode: Python; py-indent-offset: 4 -*-
+# vim: tabstop=4 shiftwidth=4 expandtab
+#
+# Copyright (C) 2012 - Jesse van den Kieboom
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+from gi.repository import Gio
 from ..overrides import override
-from ..importer import modules
-from gi import types
+from ..module import get_introspection_module
 
-Ggit = modules['Ggit']._introspection_module
-__all__ = []
 
-def _wrap_to_string(self):
-    b = type(self).__base__
-    mod = b.__module__
+Ggit = get_introspection_module('Ggit')
+__all__ = []
 
-    prefix = "gi.repository."
 
-    if mod.find(prefix) == 0:
-        mod = mod[len(prefix):]
+def _wrap_iter_size(self):
+    for i in range(0, self.size()):
+        yield self.get(i)
 
-    return "<%s.%s at %s: %s>" % (mod, b.__name__, hex(id(self)), self.to_string())
+def _wrap_iter_get(self):
+    while self.next():
+        yield self.get()
 
-def _wrap_iter(self):
+def _wrap_iter_get_by_index(self):
     for i in range(0, self.size()):
-        yield self.get(i)
+        yield self.get_by_index(i)
+
+def _wrap_iter_next(self):
+    while True:
+        value = self.next()
+        if value is None:
+            break
+
+        yield value
 
 def _wrap_initable_init(self, *args, **kwargs):
-    super(GObject.Object, self).__init__(*args, **kwargs)
+    super(self.__class__, self).__init__(*args, **kwargs)
     Gio.Initable.init(self, None)
 
 def _override_dyn(base, **kwargs):
-    nm = base.__name__
+    name = base.__name__
+
+    try:
+        cls = globals()[name]
+
+    except KeyError:
+        cls = override(type(name, (base,), {}))
+        globals()[name] = cls
+        __all__.append(name)
+
+    for method, wrapper in kwargs.items():
+        setattr(cls, method, wrapper)
 
-    if nm in globals():
-        for k in kwargs:
-            setattr(globals()[nm], k, kwargs[k])
-    else:
-        cls = type(nm, (base,), kwargs)
-        globals()[nm] = cls
-        __all__.append(nm)
 
 for c in dir(Ggit):
     try:
         o = getattr(Ggit, c)
-    except:
-        pass
 
-    if not isinstance(o, types.GObjectMeta) and not isinstance(o, types.StructMeta):
+    except AttributeError:
+        continue
+
+    if not hasattr(o, '__gtype__'):
         continue
 
-    # add __str__ mapping to to_string
+    # Add __str__ mapping using to_string
     if hasattr(o, 'to_string'):
-        _override_dyn(o, __str__=_wrap_to_string)
+        _override_dyn(o, __str__=o.to_string)
 
-    # add iterator pattern for objects having _get and _size
+    # Add iterator pattern
+    # GgitCommitParents, GgitIndexEntriesResolveUndo, GgitTree
     if hasattr(o, 'get') and hasattr(o, 'size'):
-        _override_dyn(o, __iter__=_wrap_iter)
+        _override_dyn(o, __iter__=_wrap_iter_size)
 
-    if o.__gtype__.is_a(Gio.Initable):
-        _override_dyn(o, __init__=_wrap_initable_init)
+    # GgitBranchEnumerator
+    elif hasattr(o, 'get') and hasattr(o, 'next'):
+        _override_dyn(o, __iter__=_wrap_iter_get)
 
-def _revision_walker_init(self, repository):
-    Ggit.RevisionWalker.__init__(self, repository=repository)
-    Gio.Initable.init(self, None)
+    # GgitIndexEntries
+    elif hasattr(o, 'get_by_index') and hasattr(o, 'size'):
+        _override_dyn(o, __iter__=_wrap_iter_get_by_index)
+
+    # GgitRevisionWalker
+    elif hasattr(o, 'next'):
+        _override_dyn(o, __iter__=_wrap_iter_next)
 
-_override_dyn(Ggit.RevisionWalker, __init__=_revision_walker_init)
+    # GgitIndex, GgitPush, GgitRepository, GgitRevisionWalker, ...
+    if o.__gtype__.is_a(Gio.Initable):
+        _override_dyn(o, __init__=_wrap_initable_init)
 
 # vi:ex:ts=4:et


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]