예제 #1
0
    def load(self,
             abspath,
             object_hook=None,
             ignore_comments=False,
             verbose=True,
             **kwargs):
        """load object from json file.

        :param abspath: if ``*.json, *.js** then do regular dump. if ``*.gz``,
          then perform decompression.
        :type abspath: str

        :param ignore_comments: default ``False. If True, then ignore comments.
        :type ignore_comments: bool

        :param verbose: default True, help-message-display trigger.
        :type verbose: boolean
        """
        prt_console("\nLoad from '%s' ..." % abspath, verbose)

        is_compressed = is_compressed_json_file(abspath)

        if not os.path.exists(abspath):
            raise ValueError("'%s' doesn't exist." % abspath)
            raise

        st = time.clock()

        with open(abspath, "rb") as f:
            if is_compressed:
                s = compresslib.decompress(f.read(), return_type="str")
            else:
                s = f.read().decode("utf-8")

        obj = self.loads(
            s,
            object_hook=object_hook,
            decompress=False,
            ignore_comments=ignore_comments,
        )

        prt_console("    Complete! Elapse %.6f sec." % (time.clock() - st),
                    verbose)

        return obj
    def dump(self,
             obj,
             abspath,
             indent=None,
             sort_keys=None,
             pretty=False,
             float_precision=None,
             ensure_ascii=True,
             overwrite=False,
             verbose=True,
             **kwargs):
        """Dump any object into file.

        :param abspath: if ``*.json, *.js** then do regular dump. if ``*.gz``,
          then perform compression.
        :type abspath: str

        :param pretty: if True, dump json into pretty indent and sorted key
          format.
        :type pretty: bool

        :param float_precision: default ``None``, limit floats to
          N-decimal points.
        :type float_precision: integer

        :param overwrite: default ``False``, If ``True``, when you dump to
          existing file, it silently overwrite it. If ``False``, an alert
          message is shown. Default setting ``False`` is to prevent overwrite
          file by mistake.
        :type overwrite: boolean

        :param verbose: default True, help-message-display trigger.
        :type verbose: boolean
        """
        prt_console("\nDump to '%s' ..." % abspath, verbose)

        is_compressed = is_compressed_json_file(abspath)

        if not overwrite:
            if os.path.exists(abspath):  # pragma: no cover
                prt_console(
                    "    Stop! File exists and overwrite is not allowed",
                    verbose,
                )
                return

        st = time.clock()

        s = self.dumps(
            obj,
            indent=indent,
            sort_keys=sort_keys,
            pretty=pretty,
            float_precision=float_precision,
            ensure_ascii=ensure_ascii,
            compress=
            False,  # use uncompressed string, and directly write to file
            **kwargs)

        with atomic_write(abspath, mode="wb", overwrite=True) as f:
            if is_compressed:
                f.write(compresslib.compress(s, return_type="bytes"))
            else:
                f.write(s.encode("utf-8"))

        prt_console(
            "    Complete! Elapse %.6f sec." % (time.clock() - st),
            verbose,
        )
        return s