File: dictionary.pyx

package info (click to toggle)
python-av 14.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,664 kB
  • sloc: python: 4,712; sh: 175; ansic: 174; makefile: 123
file content (57 lines) | stat: -rw-r--r-- 1,558 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from collections.abc import MutableMapping

from av.error cimport err_check


cdef class _Dictionary:
    def __cinit__(self, *args, **kwargs):
        for arg in args:
            self.update(arg)
        if kwargs:
            self.update(kwargs)

    def __dealloc__(self):
        if self.ptr != NULL:
            lib.av_dict_free(&self.ptr)

    def __getitem__(self, str key):
        cdef lib.AVDictionaryEntry *element = lib.av_dict_get(self.ptr, key, NULL, 0)
        if element != NULL:
            return element.value
        else:
            raise KeyError(key)

    def __setitem__(self, str key, str value):
        err_check(lib.av_dict_set(&self.ptr, key, value, 0))

    def __delitem__(self, str key):
        err_check(lib.av_dict_set(&self.ptr, key, NULL, 0))

    def __len__(self):
        return err_check(lib.av_dict_count(self.ptr))

    def __iter__(self):
        cdef lib.AVDictionaryEntry *element = NULL
        while True:
            element = lib.av_dict_get(self.ptr, "", element, lib.AV_DICT_IGNORE_SUFFIX)
            if element == NULL:
                break
            yield element.key

    def __repr__(self):
        return f"av.Dictionary({dict(self)!r})"

    cpdef _Dictionary copy(self):
        cdef _Dictionary other = Dictionary()
        lib.av_dict_copy(&other.ptr, self.ptr, 0)
        return other


class Dictionary(_Dictionary, MutableMapping):
    pass


cdef _Dictionary wrap_dictionary(lib.AVDictionary *input_):
    cdef _Dictionary output = Dictionary()
    output.ptr = input_
    return output