aboutsummaryrefslogtreecommitdiffstats
path: root/library/python/resource/__init__.py
blob: 26503ef7fce023d5c93ac329176f97ec113b8a70 (plain) (blame)
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
from __res import find as __find, count, key_by_index, resfs_files as __resfs_files
from __res import resfs_read, resfs_resolve, resfs_src  # noqa

import six


def iterkeys(prefix='', strip_prefix=False):
    decode = lambda s: s
    if isinstance(prefix, six.text_type):
        prefix = prefix.encode('utf-8')
        decode = lambda s: s.decode('utf-8')

    for i in six.moves.range(count()):
        key = key_by_index(i)
        if key.startswith(prefix):
            if strip_prefix:
                key = key[len(prefix):]
            yield decode(key)


def itervalues(prefix=b''):
    for key in iterkeys(prefix=prefix):
        value = find(key)
        yield value


def iteritems(prefix='', strip_prefix=False):
    for key in iterkeys(prefix=prefix):
        value = find(key)
        if strip_prefix:
            key = key[len(prefix):]
        yield key, value


def resfs_file_exists(path):
    return resfs_src(path, resfs_file=True) is not None


def resfs_files(prefix=''):
    decode = lambda s: s
    if isinstance(prefix, six.text_type):
        decode = lambda s: s.decode('utf-8')
    return [decode(s) for s in __resfs_files(prefix=prefix)]


def find(path):
    if isinstance(path, six.text_type):
        path = path.encode('utf-8')
    return __find(path)