def unfeed_json(directory, name, **kwargs): """ Remove all json objects under a json file that satify kwargs logic ============================================================== """ if not kwargs: raise LockedOption("Unauthorized Move") set_dir(directory) with open(name, 'r') as f: data = json.load(f) if isinstance(data, list): i = 0 res = [] for obj in data: i += 1 for k in list(kwargs.keys()): if not obj[k] == kwargs[k]: res.append(obj) remove_file(directory, name) with open(name, 'w') as f: json.dump(res, f) elif isinstance(data, list): #TODO: add this part raise NotImplementedAlgo(name)
def test_gconfig_loader(): # make class copy_file(GREWW_CONFIG, 'gconfig.json', GREWW_CACHE, CFILE) CL = _generate_config_loader('KAPPA', GREWW_CACHE, CFILE) # test class assert CL.__name__ == 'KAPPA' # test main source mainsource = tuple(CL.mainsource()) assert (GREWW_CACHE, CFILE) == mainsource # test actual config assert CL.actual_config() == 'greww_config' # test configure CL.configure('machine_config') actual_config = CL.actual_config() assert CL.actual_config() == 'machine_config' #rebase CL.configure('greww_config') # add configs CL.add_config('kappa_config', 'kappapath', 'kappatype', True) data = CL.config_of(-1) assert data['path'] == 'kappapath' assert data['type'] == 'kappatype' assert CL.actual_config() == 'kappa_config' # rebase CL.configure('greww_config') # remove configure CL.remove_config('kappa_config') assert not ('kappa_config' in CL.all_configs()) # clean up remove_file(GREWW_CACHE, CFILE)
def _replace_json(d, f, nc): """ Replace json file content with another ============================================================== """ remove_file(d, f) make_json(d, f, from_data=nc, pretty=True)
def remove_option(full_path, c, o): """ Remove option o from configuration c """ config = configparser.ConfigParser() config.read(full_path) del config[c][o] # recreate remove_file(*_dispatch_path(full_path)) f = open(full_path, 'w') config.write(f) f.close()
def remove_configuration(full_path, c): """ Remove all configuration c with it content """ config = configparser.ConfigParser() config.read(full_path) del config[c] # recreate remove_file(*_dispatch_path(full_path)) f = open(full_path, 'w') config.write(f) f.close()
def set_option(full_path, c, o, value): """ Set option o with value v at configuration c """ config = configparser.ConfigParser() config.read(full_path) config[c][o] = value # recreate remove_file(*_dispatch_path(full_path)) f = open(full_path, 'w') config.write(f) f.close()
def set_configuration(full_path, c, **kwargs): """ Set configuration c with kwargs """ config = configparser.ConfigParser() config.read(full_path) config[c] = kwargs # recreate remove_file(*_dispatch_path(full_path)) f = open(full_path, 'w') config.write(f) f.close()
def tests_makers(): _cfs = ['alo', 'hello'] _alo_d = {'ki': 'da', 're': 'er'} _hello_d = {'ki': 'ka', 'ze': '2', 'zer': '3'} fp = "{0}/{1}".format(GREWW_CACHE, __ini_file) make_configuration_file(fp, _cfs, [_alo_d, _hello_d]) cfs = get_configurations(fp) assert len(cfs) == 2 assert 'alo' in cfs and 'hello' in cfs opts = get_options(fp, 'alo') opts2 = get_options(fp, 'hello') assert len(opts) == 2 and len(opts2) == 3 # remove test file remove_file(GREWW_CACHE, __ini_file)
def tests_removers(): # make test file mkfile_with_content(GREWW_CACHE, __ini_file, content=__ini_sample) fp = "{0}/{1}".format(GREWW_CACHE, __ini_file) # remove option remove_option(fp, 'test1', 'del') opts = get_options(fp, 'test1') assert len(opts) == 2 assert not ('del' in opts) # remove configuration remove_configuration(fp, 'test1') cfgs = get_configurations(fp) assert len(cfgs) == 2 assert not 'test1' in cfgs # remove test file remove_file(GREWW_CACHE, __ini_file)
def tests_parsers(): # make test file mkfile_with_content(GREWW_CACHE, __ini_file, content=__ini_sample) fp = "{0}/{1}".format(GREWW_CACHE, __ini_file) # get configs c = get_configurations(fp) assert 'test1' in c assert len(c) == 3 # config content cd = configuration_data(fp, 'test1') assert len(cd.keys()) == 3 assert 'cc' in cd.values() # get options o = get_options(fp, 'test1') assert len(o) == 3 assert 'kikou' in o # get option value od = option_data(fp, 'test1', 'kika') assert od == 'jajaja' # remove test file remove_file(GREWW_CACHE, __ini_file)
def tests_setters(): # make test file mkfile_with_content(GREWW_CACHE, __ini_file, content=__ini_sample) fp = "{0}/{1}".format(GREWW_CACHE, __ini_file) # set config nc = {'az': '5', 'ez': '2'} set_configuration(fp, 'toutou', **nc) d = get_configurations(fp) assert 'toutou' in d assert len(d) == 3 _c = configuration_data(fp, 'toutou') assert _c == nc # set option set_option(fp, 'toutou', 'ez', '5') set_option(fp, 'toutou', 'kek', '6') o = get_options(fp, 'toutou') assert 'ez' in o assert 'kek' in o assert len(o) == 3 assert option_data(fp, 'toutou', 'kek') == '6' assert option_data(fp, 'toutou', 'ez') == '5' # remove test file remove_file(GREWW_CACHE, __ini_file)