io.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import os
  2. import shutil
  3. from .. import constants, logger
  4. from . import _json
  5. def copy_registered_textures(dest, registration):
  6. """Copy the registered textures to the destination (root) path
  7. :param dest: destination directory
  8. :param registration: registered textures
  9. :type dest: str
  10. :type registration: dict
  11. """
  12. logger.debug("io.copy_registered_textures(%s, %s)", dest, registration)
  13. os.makedirs(dest, exist_ok=True)
  14. for value in registration.values():
  15. copy(value['file_path'], dest)
  16. def copy(src, dst):
  17. """Copy a file to a destination
  18. :param src: source file
  19. :param dst: destination file/path
  20. """
  21. logger.debug("io.copy(%s, %s)" % (src, dst))
  22. if os.path.isdir(dst):
  23. file_name = os.path.basename(src)
  24. dst = os.path.join(dst, file_name)
  25. if src != dst:
  26. shutil.copy(src, dst)
  27. def dump(filepath, data, options=None):
  28. """Dump the output to disk (JSON, msgpack, etc)
  29. :param filepath: output file path
  30. :param data: serializable data to write to disk
  31. :param options: (Default value = None)
  32. :type options: dict
  33. """
  34. options = options or {}
  35. logger.debug("io.dump(%s, data, options=%s)", filepath, options)
  36. compress = options.get(constants.COMPRESSION, constants.NONE)
  37. if compress == constants.MSGPACK:
  38. try:
  39. import msgpack
  40. except ImportError:
  41. logger.error("msgpack module not found")
  42. raise
  43. logger.info("Dumping to msgpack")
  44. func = lambda x, y: msgpack.dump(x, y)
  45. mode = 'wb'
  46. else:
  47. round_off = options.get(constants.ENABLE_PRECISION)
  48. if round_off:
  49. _json.ROUND = options[constants.PRECISION]
  50. else:
  51. _json.ROUND = None
  52. indent = options.get(constants.INDENT, True)
  53. indent = 4 if indent else None
  54. compact_separators = (',', ':')
  55. logger.info("Dumping to JSON")
  56. func = lambda x, y: _json.json.dump(x, y, indent=indent, separators=compact_separators)
  57. mode = 'w'
  58. logger.info("Writing to %s", filepath)
  59. with open(filepath, mode=mode) as stream:
  60. func(data, stream)
  61. def load(filepath, options):
  62. """Load the contents of the file path with the correct parser
  63. :param filepath: input file path
  64. :param options:
  65. :type options: dict
  66. """
  67. logger.debug("io.load(%s, %s)", filepath, options)
  68. compress = options.get(constants.COMPRESSION, constants.NONE)
  69. if compress == constants.MSGPACK:
  70. try:
  71. import msgpack
  72. except ImportError:
  73. logger.error("msgpack module not found")
  74. raise
  75. module = msgpack
  76. mode = 'rb'
  77. else:
  78. logger.info("Loading JSON")
  79. module = _json.json
  80. mode = 'r'
  81. with open(filepath, mode=mode) as stream:
  82. data = module.load(stream)
  83. return data