def test_render_and_file(self): err_ini = utils.get_render('configs/config.ini', dict(config=dict(test=[]))) for line in err_ini.split('\n'): self.assertEqual(line[0], '#') self.assertTrue('# Invalid config.' in err_ini, err_ini) config_dict = dict(config=dict(main=dict(test_key="test_value"))) ini = utils.get_render('configs/config.ini', config_dict) with utils.tmp_file_context(ini, delete=False) as file: file_name = file.name file.write('\n') with open(file_name, 'r') as tmp_file: self.assertEqual(tmp_file.read(), test_config) try: self.assertFalse(utils.os.path.exists(file_name)) except AssertionError: # nocv utils.os.remove(file_name) try: with utils.tmp_file(ini) as file: file_name = file.name file.write('\n') with open(file_name, 'r') as tmp_file: self.assertEqual(tmp_file.read(), test_config) raise Exception('Normal') except AssertionError: # nocv raise except Exception: pass with utils.tmp_file_context() as file: with open(file.name, 'w') as output: with utils.redirect_stdany(output): print("Test") with open(file.name, 'r') as output: self.assertEqual(output.read(), "Test\n")
def get_inventory(self): hvars, key = self.get_generated_vars() keys = [key] if key else list() hosts_strings, keys = _get_strings(list(self.hosts_list), keys) groups_strings, keys = _get_strings(list(self.groups_list), keys) inv = get_render("models/inventory", dict(groups=groups_strings, hosts=hosts_strings, vars=self.vars_string(hvars, "\n"))) return inv, keys
def toString(self, var_sep="\n"): hvars, key = self.get_generated_vars() keys = [key] if key is not None else [] if self.children: groups = self.groups.values_list("name", flat=True).order_by("name") objects = "\n".join(groups) else: hosts = self.hosts.all().order_by("name") hosts_strings, keys = _get_strings(hosts, keys) objects = "\n".join(hosts_strings) data = dict(vars=self.vars_string(hvars, var_sep), objects=objects, group=self) return get_render("models/group", data), keys
def generate_file(self, name: Text): with open(os.path.join(self.path, name), 'w', encoding='utf-8') as fd: with raise_context(): fd.write(get_render('polemarch/{}'.format(name), settings.MANUAL_PROJECT_VARS))