def env_write_config(obj=None, filename='settings', unique_id='', sub_id=None, update_file=False, long_abs_path=False): """ Converts python objects to json, then writes it to disk. Supported writing formats: 'json', 'ini'. Format can be set via global var CFG_FORMAT. If ini used, there will be less sub folders created. :param obj: any dict, list etc... :param filename: name of the file to be written, without ext. 'settings' :param unique_id: if format set to json will create sub dirs to ensure uniqueness. 'a/b/c' :param sub_id: unique id inside dump. 'abc' :param update_file: updates json instead of rewriting :param long_abs_path: if set to true path for saving will match current environment paths """ if long_abs_path: abs_path = u'{0}/settings/{1}/{2}/{3}'.format( env_mode.get_current_path(), env_mode.get_node(), env_server.get_cur_srv_preset(), env_mode.get_mode()) else: abs_path = u'{0}/settings'.format(env_mode.get_current_path()) if CFG_FORMAT == u'json': full_abs_path = u'{0}/{1}'.format(abs_path, unique_id) if not os.path.exists(full_abs_path): os.makedirs(full_abs_path) full_path = u'{0}/{1}.json'.format(full_abs_path, filename) obj_from_file = None if update_file and sub_id: if os.path.exists(full_path): with open(full_path, 'r') as json_file: obj_from_file = json.load(json_file) if sub_id: if obj_from_file: obj_from_file[sub_id] = obj obj = obj_from_file else: obj = {sub_id: obj} with open(full_path, 'w') as json_file: json.dump(obj, json_file, indent=2, separators=(',', ': ')) elif CFG_FORMAT == u'ini': full_path = u'{0}/{1}.ini'.format(abs_path, filename) settings = QtCore.QSettings(full_path, QtCore.QSettings.IniFormat) settings.beginGroup(filename) if sub_id: settings.beginGroup(sub_id) settings.setValue(unique_id, json.dumps(obj, separators=(',', ':'))) settings.endGroup()
def env_read_config(filename='settings', unique_id='', sub_id=None, long_abs_path=False): filename = filename.replace('/', '_').replace('\\', '_') if long_abs_path: abs_path = u'{0}/settings/{1}/{2}/{3}'.format( env_mode.get_current_path(), env_mode.get_node(), env_server.get_cur_srv_preset(), env_mode.get_mode()) else: abs_path = u'{0}/settings'.format(env_mode.get_current_path()) if CFG_FORMAT == u'json': if unique_id: full_path = u'{0}/{1}/{2}.json'.format(abs_path, unique_id, filename) else: full_path = u'{0}/{1}.json'.format(abs_path, filename) if os.path.exists(full_path): with open(full_path, 'r') as json_file: try: obj = json.load(json_file) except Exception as expected: dl.exception(expected, group_id='configs') obj = {} json_file.close() if sub_id: return obj.get(sub_id) else: return obj elif CFG_FORMAT == u'ini': full_path = u'{0}/{1}.ini'.format(abs_path, filename) settings = QtCore.QSettings(full_path, QtCore.QSettings.IniFormat) settings.beginGroup(filename) if sub_id: settings.beginGroup(sub_id) value = settings.value(unique_id, None) settings.endGroup() if value: obj = json.loads(value) return obj
def env_read_config(filename='settings', unique_id='', sub_id=None, long_abs_path=False): if long_abs_path: abs_path = '{0}/settings/{1}/{2}/{3}'.format( env_mode.get_current_path(), env_mode.get_node(), env_server.get_cur_srv_preset(), env_mode.get_mode()) else: abs_path = '{0}/settings'.format(env_mode.get_current_path()) if CFG_FORMAT == 'json': if unique_id: full_path = '{0}/{1}/{2}.json'.format(abs_path, unique_id, filename) else: full_path = '{0}/{1}.json'.format(abs_path, filename) if os.path.exists(full_path): with open(full_path, 'r') as json_file: obj = json.load(json_file) if sub_id: return obj.get(sub_id) else: return obj elif CFG_FORMAT == 'ini': full_path = '{0}/{1}.ini'.format(abs_path, filename) settings = QtCore.QSettings(full_path, QtCore.QSettings.IniFormat) settings.beginGroup(filename) if sub_id: settings.beginGroup(sub_id) value = settings.value(unique_id, None) settings.endGroup() if value: obj = json.loads(value) return obj