io.py 2.8 KB
Newer Older
1
import os
R
repsac 已提交
2
import shutil
3 4
from .. import constants, logger
from . import _json
R
repsac 已提交
5 6 7


def copy_registered_textures(dest, registration):
8 9 10 11 12 13 14 15 16
    """Copy the registered textures to the destination (root) path

    :param dest: destination directory
    :param registration: registered textures
    :type dest: str
    :type registration: dict

    """
    logger.debug("io.copy_registered_textures(%s, %s)", dest, registration)
M
mese79 已提交
17
    os.makedirs(dest, exist_ok=True)
R
repsac 已提交
18 19 20 21 22
    for value in registration.values():
        copy(value['file_path'], dest)


def copy(src, dst):
23 24 25 26 27 28 29
    """Copy a file to a destination

    :param src: source file
    :param dst: destination file/path

    """
    logger.debug("io.copy(%s, %s)" % (src, dst))
30
    if os.path.isdir(dst):
31 32 33
        file_name = os.path.basename(src)
        dst = os.path.join(dst, file_name)

34 35
    if src != dst:
        shutil.copy(src, dst)
R
repsac 已提交
36 37 38


def dump(filepath, data, options=None):
39 40 41 42
    """Dump the output to disk (JSON, msgpack, etc)

    :param filepath: output file path
    :param data: serializable data to write to disk
43
    :param options: (Default value = None)
44 45 46
    :type options: dict

    """
R
repsac 已提交
47
    options = options or {}
48
    logger.debug("io.dump(%s, data, options=%s)", filepath, options)
R
repsac 已提交
49 50 51 52 53 54

    compress = options.get(constants.COMPRESSION, constants.NONE)
    if compress == constants.MSGPACK:
        try:
            import msgpack
        except ImportError:
55
            logger.error("msgpack module not found")
R
repsac 已提交
56 57
            raise

58 59
        logger.info("Dumping to msgpack")
        func = lambda x, y: msgpack.dump(x, y)
R
repsac 已提交
60 61
        mode = 'wb'
    else:
R
repsac 已提交
62
        round_off = options.get(constants.ENABLE_PRECISION)
R
repsac 已提交
63
        if round_off:
R
repsac 已提交
64
            _json.ROUND = options[constants.PRECISION]
R
repsac 已提交
65 66 67
        else:
            _json.ROUND = None

68 69
        indent = options.get(constants.INDENT, True)
        indent = 4 if indent else None
70 71
        logger.info("Dumping to JSON")
        func = lambda x, y: _json.json.dump(x, y, indent=indent)
R
repsac 已提交
72 73
        mode = 'w'

74
    logger.info("Writing to %s", filepath)
R
repsac 已提交
75 76 77 78 79
    with open(filepath, mode=mode) as stream:
        func(data, stream)


def load(filepath, options):
80 81 82 83 84 85 86 87
    """Load the contents of the file path with the correct parser

    :param filepath: input file path
    :param options:
    :type options: dict

    """
    logger.debug("io.load(%s, %s)", filepath, options)
R
repsac 已提交
88 89 90 91 92
    compress = options.get(constants.COMPRESSION, constants.NONE)
    if compress == constants.MSGPACK:
        try:
            import msgpack
        except ImportError:
93
            logger.error("msgpack module not found")
R
repsac 已提交
94 95 96 97
            raise
        module = msgpack
        mode = 'rb'
    else:
98
        logger.info("Loading JSON")
R
repsac 已提交
99 100 101 102 103 104 105
        module = _json.json
        mode = 'r'

    with open(filepath, mode=mode) as stream:
        data = module.load(stream)

    return data