io.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. logger.info("Dumping to JSON")
  55. func = lambda x, y: _json.json.dump(x, y, indent=indent)
  56. mode = 'w'
  57. logger.info("Writing to %s", filepath)
  58. with open(filepath, mode=mode) as stream:
  59. func(data, stream)
  60. def load(filepath, options):
  61. """Load the contents of the file path with the correct parser
  62. :param filepath: input file path
  63. :param options:
  64. :type options: dict
  65. """
  66. logger.debug("io.load(%s, %s)", filepath, options)
  67. compress = options.get(constants.COMPRESSION, constants.NONE)
  68. if compress == constants.MSGPACK:
  69. try:
  70. import msgpack
  71. except ImportError:
  72. logger.error("msgpack module not found")
  73. raise
  74. module = msgpack
  75. mode = 'rb'
  76. else:
  77. logger.info("Loading JSON")
  78. module = _json.json
  79. mode = 'r'
  80. with open(filepath, mode=mode) as stream:
  81. data = module.load(stream)
  82. return data