示例#1
0
    def save_json(fp=None, **kwargs):

        # filename and file path handling
        if fp is not None:

            # json extension handling
            if splitext(fp)[-1] != '.json':
                fn = basename(splitext(fp)[0])+'.json'
                fp = join(dirname(splitext(fp)[0]), fn)

            # create folder
            mkdir_p(dirname(fp), False)

        # save calibration data as json file
        json_dict = kwargs['json_dict'] if 'json_dict' in kwargs else kwargs
        try:
            # amend write privileges of (potentially existing) config file
            if exists(fp):
                st = stat(fp)
                chmod(fp, st.st_mode | 0o111)
            # write file
            with open(fp, 'wt') as f:
                json.dump(json_dict, f, sort_keys=True, indent=4)
        except TypeError:
            return False

        return True
示例#2
0
    def save_params(self, fp=None):

        if not fp:
            fp = join(self._dir_path, 'cfg.json')

        try:
            # create config folder (if not already present)
            mkdir_p(self._dir_path)
            # amend write privileges of (potentially existing) config file
            if exists(fp):
                st = stat(fp)
                chmod(fp, st.st_mode | 0o111)
            # write config file
            with open(fp, 'w+') as f:
                json.dump(self.params,
                          f,
                          sort_keys=True,
                          indent=4,
                          cls=NumpyTypeEncoder)
        except PermissionError:
            warnings.warn(
                '\n\nGrant permission to write to the config file ' + fp,
                UserWarning)

        return True