コード例 #1
0
    def write_json(self, params):
        """Write data into a json file."""
        self._check_required_params(["file", "data"], params, "write_json")

        # Get file
        filename = self._get_file(params["file"])

        # Create dir if necessary
        basedir = os.path.dirname(filename)
        if not os.path.exists(basedir):
            os.makedirs(basedir)

        merge = params.get("merge", True)

        if not os.path.exists(filename):
            # create an empty file
            with open(filename, "a+"):
                pass

        with open(filename, "r+" if merge else "w") as json_file:
            json_data = {}
            if merge:
                try:
                    json_data = json.load(json_file)
                except ValueError:
                    logger.error("Failed to parse JSON from file %s", filename)

            json_data = selective_merge(json_data, params.get("data", {}))
            json_file.seek(0)
            json_file.write(json.dumps(json_data, indent=2))
コード例 #2
0
ファイル: commands.py プロジェクト: Yilnis/lutris-1
    def write_json(self, params):
        """Write data into a json file."""
        self._check_required_params(['file', 'data'], params, 'write_json')

        # Get file
        file = self._get_file(params['file']) or self._substitute(
            params['file'])

        # Create dir if necessary
        basedir = os.path.dirname(file)
        if not os.path.exists(basedir):
            os.makedirs(basedir)

        merge = params.get('merge', True)

        with open(file, 'a+') as f:
            pass

        with open(file, 'r+' if merge else 'w') as f:
            data = {}
            if merge:
                try:
                    data = json.load(f)
                except ValueError:
                    pass

            data = selective_merge(data, params.get('data', {}))
            f.seek(0)
            f.write(json.dumps(data, indent=2))
コード例 #3
0
ファイル: commands.py プロジェクト: Ryochan7/lutris
    def write_json(self, params):
        """Write data into a json file."""
        self._check_required_params(['file', 'data'], params, 'write_json')

        # Get file
        file = self._get_file(params['file']) or self._substitute(params['file'])

        # Create dir if necessary
        basedir = os.path.dirname(file)
        if not os.path.exists(basedir):
            os.makedirs(basedir)

        merge = params.get('merge', True)

        with open(file, 'a+') as f:
            pass

        with open(file, 'r+' if merge else 'w') as f:
            data = {}
            if merge:
                try:
                    data = json.load(f)
                except ValueError:
                    pass

            data = selective_merge(data, params.get('data', {}))
            f.seek(0)
            f.write(json.dumps(data, indent=2))
コード例 #4
0
    def write_json(self, params):
        """Write data into a json file."""
        self._check_required_params(["file", "data"], params, "write_json")

        # Get file
        filename = self._get_file(params["file"])

        # Create dir if necessary
        basedir = os.path.dirname(filename)
        if not os.path.exists(basedir):
            os.makedirs(basedir)

        merge = params.get("merge", True)

        if not os.path.exists(filename):
            # create an empty file
            with open(filename, "a+"):
                pass

        with open(filename, "r+" if merge else "w") as json_file:
            json_data = {}
            if merge:
                try:
                    json_data = json.load(json_file)
                except ValueError:
                    logger.error("Failed to parse JSON from file %s", filename)

            json_data = selective_merge(json_data, params.get("data", {}))
            json_file.seek(0)
            json_file.write(json.dumps(json_data, indent=2))
コード例 #5
0
    def write_json(self, params):
        """Write data into a json file."""
        self._check_required_params(["file", "data"], params, "write_json")

        # Get file
        filename = self._get_file_path(params["file"])

        # Create dir if necessary
        basedir = os.path.dirname(filename)
        os.makedirs(basedir, exist_ok=True)

        merge = params.get("merge", True)

        # create an empty file if it doesn't exist
        Path(filename).touch(exist_ok=True)

        with open(filename, "r+" if merge else "w",
                  encoding='utf-8') as json_file:
            json_data = {}
            if merge:
                try:
                    json_data = json.load(json_file)
                except ValueError:
                    logger.error("Failed to parse JSON from file %s", filename)

            json_data = selective_merge(json_data, params.get("data", {}))
            json_file.seek(0)
            json_file.write(json.dumps(json_data, indent=2))
コード例 #6
0
    def write_json(self, params):
        """Write data into a json file."""
        self._check_required_params(['file', 'data'], params, 'write_json')

        # Get file
        filename = self._get_file(params['file'])

        # Create dir if necessary
        basedir = os.path.dirname(filename)
        if not os.path.exists(basedir):
            os.makedirs(basedir)

        merge = params.get('merge', True)

        with open(filename, 'r+' if merge else 'w') as json_file:
            json_data = {}
            if merge:
                try:
                    json_data = json.load(json_file)
                except ValueError:
                    logger.error("Failed to parse JSON from file %s", filename)

            json_data = selective_merge(json_data, params.get('data', {}))
            json_file.seek(0)
            json_file.write(json.dumps(json_data, indent=2))