__init__.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. from __res import find as __find, count, key_by_index, resfs_files as __resfs_files
  2. from __res import resfs_read, resfs_resolve, resfs_src # noqa
  3. import six
  4. def iterkeys(prefix="", strip_prefix=False):
  5. decode = lambda s: s # noqa: E731
  6. if isinstance(prefix, six.text_type):
  7. prefix = prefix.encode("utf-8")
  8. decode = lambda s: s.decode("utf-8") # noqa: E731
  9. for i in six.moves.range(count()):
  10. key = key_by_index(i)
  11. if key.startswith(prefix):
  12. if strip_prefix:
  13. key = key[len(prefix) :]
  14. yield decode(key)
  15. def itervalues(prefix=b""):
  16. for key in iterkeys(prefix=prefix):
  17. value = find(key)
  18. yield value
  19. def iteritems(prefix="", strip_prefix=False):
  20. for key in iterkeys(prefix=prefix):
  21. value = find(key)
  22. if strip_prefix:
  23. key = key[len(prefix) :]
  24. yield key, value
  25. def resfs_file_exists(path):
  26. return resfs_src(path, resfs_file=True) is not None
  27. def resfs_files(prefix=""):
  28. decode = lambda s: s # noqa: E731
  29. if isinstance(prefix, six.text_type):
  30. decode = lambda s: s.decode("utf-8") # noqa: E731
  31. return [decode(s) for s in __resfs_files(prefix=prefix)]
  32. def find(path):
  33. if isinstance(path, six.text_type):
  34. path = path.encode("utf-8")
  35. return __find(path)