[libsoup/pgriffis/python-overrides] Add pygobject overrides




commit 1fee49b1b0d5a5980995fc60796a51cd2b065d45
Author: Patrick Griffis <pgriffis igalia com>
Date:   Wed Jul 28 16:58:27 2021 -0500

    Add pygobject overrides

 libsoup/Soup.py         | 123 ++++++++++++++++++++++++++++++++++++++++++++++++
 libsoup/meson.build     |  31 ++++++++++++
 tests/overrides-test.py |  38 +++++++++++++++
 3 files changed, 192 insertions(+)
---
diff --git a/libsoup/Soup.py b/libsoup/Soup.py
new file mode 100644
index 00000000..d531303c
--- /dev/null
+++ b/libsoup/Soup.py
@@ -0,0 +1,123 @@
+#!/usr/bin/env python3
+
+# Copyright (C) 2021 Igalia S.L.
+#
+# 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
+
+import collections.abc
+
+from ..module import get_introspection_module
+from ..overrides import override
+
+Soup = get_introspection_module('Soup')
+
+__all__ = []
+
+if Soup._version == '3.0':
+
+    class MessageHeadersIter(Soup.MessageHeadersIter):
+
+        def __next__(self):
+            ret, key, value = self.next()
+            if ret is True:
+                return key, value
+            else:
+                raise StopIteration
+
+    @override
+    class MessageHeaders(Soup.MessageHeaders):
+
+        def __getitem__(self, key):
+            if not isinstance(key, str):
+                raise TypeError
+
+            value = self.get_one(key)
+            if value is None:
+                raise KeyError
+
+            return value
+
+        def __delitem__(self, key):
+            if not isinstance(key, str):
+                raise TypeError
+
+            self.remove(key)
+
+        def __setitem__(self, key, value):
+            if not isinstance(key, str) or not isinstance(value, str):
+                raise TypeError
+
+            self.replace(key, value)
+
+        def __len__(self):
+            i = 0
+            iter = Soup.MessageHeadersIter.init(self)
+            while True:
+                ret, *_ = iter.next()
+                if ret:
+                    i += 1
+                else:
+                    return i
+
+        def __contains__(self, item):
+            if isinstance(item, str):
+                for k, _ in self:
+                    if k == item:
+                        return True
+            elif isinstance(item, tuple) and len(tuple) == 2:
+                for k, v in self:
+                    if (k, v) == item:
+                        return True
+            else:
+                raise TypeError
+
+            return False
+
+        def __iter__(self):
+            return MessageHeadersIter.init(self)
+
+        def keys(self):
+            return [k for k, _ in self]
+
+        def values(self):
+            return [v for _, v in self]
+
+        def items(self):
+            return {k: v for k, v in self}
+
+        def get(self, default=None):
+            return collections.abc.Mapping.get(self, default)
+
+        def pop(self, key):
+            return collections.abc.MutableMapping.pop(self, key)
+
+        def update(self, key, value):
+            return collections.abc.MutableMapping.update(self, key, value)
+
+        def setdefault(self, key, default=None):
+            return collections.abc.MutableMapping.setdefault(self, key, default)
+
+        def popitem(self):
+            return collections.abc.MutableMapping.popitem(self)
+
+    __all__.append('MessageHeaders')
+    __all__.append('MessageHeadersIter')
+    collections.abc.Iterable.register(MessageHeaders)
+    collections.abc.Iterator.register(MessageHeadersIter)
+    collections.abc.Mapping.register(MessageHeaders)
+    collections.abc.MutableMapping.register(MessageHeaders)
+    collections.abc.Container.register(MessageHeaders)
+    collections.abc.Sized.register(MessageHeaders)
diff --git a/libsoup/meson.build b/libsoup/meson.build
index bb4417ca..2176ce8a 100644
--- a/libsoup/meson.build
+++ b/libsoup/meson.build
@@ -289,3 +289,34 @@ if enable_introspection or enable_vapi
     )
   endif
 endif
+
+
+# We want to find the subdirectory to install our override into:
+pymod = import('python')
+python3 = pymod.find_installation('python3')
+
+get_overridedir = '''
+import os
+import sysconfig
+libdir = sysconfig.get_config_var('LIBDIR')
+if not libdir:
+  libdir = '/usr/lib'
+try:
+  import gi
+  overridedir = gi._overridesdir
+except ImportError:
+  purelibdir = sysconfig.get_path('purelib')
+  overridedir = os.path.join(purelibdir, 'gi', 'overrides')
+if overridedir.startswith(libdir): # Should always be True..
+  overridedir = overridedir[len(libdir) + 1:]
+print(overridedir)
+'''
+
+ret = run_command([python3, '-c', get_overridedir])
+if ret.returncode() != 0
+  error('Failed to determine pygobject overridedir')
+else
+  pygobject_override_dir = join_paths(get_option('libdir'), ret.stdout().strip())
+endif
+
+install_data('Soup.py', install_dir: pygobject_override_dir)
diff --git a/tests/overrides-test.py b/tests/overrides-test.py
new file mode 100755
index 00000000..7e548e1d
--- /dev/null
+++ b/tests/overrides-test.py
@@ -0,0 +1,38 @@
+#!/usr/bin/env python3
+
+import collections.abc
+import gi
+
+gi.require_version('Soup', '3.0')
+
+from gi.repository import Soup
+
+# MessageHeaders overrides
+headers = Soup.MessageHeaders.new(Soup.MessageHeadersType.REQUEST)
+
+assert isinstance(headers, collections.abc.Mapping)
+assert isinstance(headers, collections.abc.MutableMapping)
+
+headers['one'] = 'one-value'
+headers['two'] = 'two-value'
+
+assert headers['one'] == 'one-value'
+assert headers['two'] == 'two-value'
+
+assert len(headers) == 2
+
+assert headers.keys() == ['one', 'two']
+assert headers.values() == ['one-value', 'two-value']
+assert headers.items() == {'one': 'one-value', 'two': 'two-value'}
+assert 'one' in headers
+assert headers.get('one') == 'one-value'
+
+del headers['one']
+assert 'one' not in headers
+
+assert headers.pop('two') == 'two-value'
+assert not headers
+headers['one'] = 'one-value'
+assert headers
+headers.clear()
+assert not headers


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