Debian Codegraph @

av/dictionary.py

raw
1 import cython
2 from cython.cimports.av.error import err_check
3
4
5 @cython.final
6 @cython.cclass
7 class Dictionary:
8 def __cinit__(self, *args, **kwargs):
9 for arg in args:
10 self.update(arg)
11 if kwargs:
12 self.update(kwargs)
13
14 def __dealloc__(self):
15 if self.ptr != cython.NULL:
16 lib.av_dict_free(cython.address(self.ptr))
17
18 def __getitem__(self, key: cython.str):
19 element: cython.pointer[lib.AVDictionaryEntry] = lib.av_dict_get(
20 self.ptr, key, cython.NULL, 0
21 )
22 if element == cython.NULL:
23 raise KeyError(key)
24 return element.value
25
26 def __setitem__(self, key: cython.str, value: cython.str):
27 err_check(lib.av_dict_set(cython.address(self.ptr), key, value, 0))
28
29 def __delitem__(self, key: cython.str):
30 err_check(lib.av_dict_set(cython.address(self.ptr), key, cython.NULL, 0))
31
32 def __len__(self):
33 return err_check(lib.av_dict_count(self.ptr))
34
35 def __iter__(self):
36 element: cython.pointer[lib.AVDictionaryEntry] = cython.NULL
37 while True:
38 element = lib.av_dict_get(self.ptr, "", element, lib.AV_DICT_IGNORE_SUFFIX)
39 if element == cython.NULL:
40 break
41 yield element.key
42
43 def __repr__(self):
44 return f"av.Dictionary({dict(self)!r})"
45
46 def keys(self):
47 return list(self)
48
49 def copy(self):
50 other: Dictionary = Dictionary()
51 lib.av_dict_copy(cython.address(other.ptr), self.ptr, 0)
52 return other
53
54 def pop(self, key: str):
55 value = self[key]
56 del self[key]
57 return value
58
59 def update(self, other=(), /, **kwds):
60 if isinstance(other, Dictionary):
61 lib.av_dict_copy(
62 cython.address(self.ptr), cython.cast(Dictionary, other).ptr, 0
63 )
64 elif hasattr(other, "keys"):
65 for key in other.keys():
66 self[key] = other[key]
67 else:
68 for key, value in other:
69 self[key] = value
70 for key, value in kwds.items():
71 self[key] = value
72
73
74 @cython.cfunc
75 def wrap_dictionary(input_: cython.pointer[lib.AVDictionary]) -> Dictionary:
76 output: Dictionary = Dictionary()
77 output.ptr = input_
78 return output