Prechádzať zdrojové kódy

Support __eq__, __contains__, __getitem__, __len__ and __iter__ on FetchPackResult. Fixes #548

Jelmer Vernooij 7 rokov pred
rodič
commit
3e0315d5f5
1 zmenil súbory, kde vykonal 31 pridanie a 4 odobranie
  1. 31 4
      dulwich/client.py

+ 31 - 4
dulwich/client.py

@@ -227,12 +227,39 @@ class FetchPackResult(object):
         self.symrefs = symrefs
         self.agent = agent
 
+    def _warn_deprecated(self):
+        import warnings
+        warnings.warn(
+            "Use FetchPackResult.refs instead.",
+            DeprecationWarning, stacklevel=3)
+
+    def __eq__(self, other):
+        if isinstance(other, dict):
+            self._warn_deprecated()
+            return (self.refs == other)
+        return (self.refs == other.refs and
+                self.symrefs == other.symrefs and
+                self.agent == other.agent)
+
+    def __contains__(self, name):
+        self._warn_deprecated()
+        return name in self.refs
+
+    def __getitem__(self, name):
+        self._warn_deprecated()
+        return self.refs[name]
+
+    def __len__(self):
+        self._warn_deprecated()
+        return len(self.refs)
+
+    def __iter__(self):
+        self._warn_deprecated()
+        return iter(self.refs)
+
     def __getattribute__(self, name):
         if name in type(self)._FORWARDED_ATTRS:
-            import warnings
-            warnings.warn(
-                "Use FetchPackResult.refs instead.",
-                DeprecationWarning, stacklevel=2)
+            self._warn_deprecated()
             return getattr(self.refs, name)
         return super(FetchPackResult, self).__getattribute__(name)