def main(): arg_parser = argparse.ArgumentParser() arg_parser.add_argument( "path", help="Chemin du fichier de config à visualiser") arg_parser.add_argument( "module", help="Précise le module dans lequel se trouve le fichier de config") args = arg_parser.parse_args() parser = ExtendedConfigParser( config=environ.get("MFCONFIG", "GENERIC"), strict=False, inheritance='im') data = codecs.open(args.path, "r", "utf-8") module = args.module home_env = str(module) + "_HOME" home = environ.get(home_env) parser.read_file(data) res_dict = read_config_file(parser, parser.config_name, home) for s in res_dict: for o in res_dict[s]: print("export " + module.upper() + "_" + s.upper() + "_" + o.upper() + "=\"" + str(res_dict[s][o]) + "\"") print("export %s_CONF_GENERATION_TIME=%i" % (module.upper(), time.time()))
def main(): arg_parser = argparse.ArgumentParser() arg_parser.add_argument("path", help="Chemin du fichier de config à visualiser") arg_parser.add_argument( "module", help="Précise le module dans lequel se trouve le fichier de config") args = arg_parser.parse_args() parser = ExtendedConfigParser(config=environ.get("MFCONFIG", "GENERIC"), strict=False, inheritance='im') data = codecs.open(args.path, "r", "utf-8") module = args.module home_env = str(module) + "_HOME" home = environ.get(home_env) parser.read_file(data) res_dict = read_config_file(parser, parser.config_name, home) for s in res_dict: for o in res_dict[s]: print("export " + module.upper() + "_" + s.upper() + "_" + o.upper() + "=\"" + str(res_dict[s][o]) + "\"") print("export %s_CONF_GENERATION_TIME=%i" % (module.upper(), time.time()))
def main(): arg_parser = argparse.ArgumentParser( description="Lit le contenu d'un fichier .ini") arg_parser.add_argument( "path", help="Chemin du fichier de config a visualiser") arg_parser.add_argument( "section", help="Precise la section a afficher", nargs='?', default=None) arg_parser.add_argument( "option", help="Precise l'option a afficher", nargs='?', default=None) arg_parser.add_argument( "config", help="Precise la config a utiliser pour l'affichage", nargs='?', default=None) args = arg_parser.parse_args() if(args.config): parser = ExtendedConfigParser(config="NULL", strict=False, inheritance='im') else: parser = ExtendedConfigParser( config=environ.get("SYNCONFIG", "GENERIC"), strict=False, inheritance='im') data = codecs.open(str(args.path), "r", "utf-8") parser.read_file(data) if(args.section): # Affichage de la valeur d'une option if(args.option): print_option(parser, args.section, args.option) # Affichage du contenu d'une section else: print_section(parser, args.section) # Affichage de l'ensemble des sections du fichier else: print_file(parser)
def main(): arg_parser = argparse.ArgumentParser( description="Lit le contenu d'un fichier .ini") arg_parser.add_argument("path", help="Chemin du fichier de config a visualiser") arg_parser.add_argument("section", help="Precise la section a afficher", nargs='?', default=None) arg_parser.add_argument("option", help="Precise l'option a afficher", nargs='?', default=None) arg_parser.add_argument( "config", help="Precise la config a utiliser pour l'affichage", nargs='?', default=None) args = arg_parser.parse_args() if (args.config): parser = ExtendedConfigParser(config="NULL", strict=False, inheritance='im') else: parser = ExtendedConfigParser(config=environ.get( "SYNCONFIG", "GENERIC"), strict=False, inheritance='im') data = codecs.open(str(args.path), "r", "utf-8") parser.read_file(data) if (args.section): # Affichage de la valeur d'une option if (args.option): print_option(parser, args.section, args.option) # Affichage du contenu d'une section else: print_section(parser, args.section) # Affichage de l'ensemble des sections du fichier else: print_file(parser)
def read_config_file(parser, config, home): res = OrderedDict() for s in parser._sections: # On inclut les sections des fichiers de config à inclure if(s.startswith("INCLUDE_")): parser_included = ExtendedConfigParser(config=config, inheritance='im') included_file = s[8:] data = codecs.open(home + "/config/" + included_file, "r", "utf-8") parser_included.read_file(data) res.update(read_config_file(parser_included, config, home)) # On ajoute la section si elle n'y est pas ou on la met à jour # si elle a été incluse auparavant else: try: res[s].update(read_section(parser, s, home)) except KeyError: res[s] = read_section(parser, s, home) return res
def read_config_file(parser, config, home): res = OrderedDict() for s in parser._sections: # On inclut les sections des fichiers de config à inclure if (s.startswith("INCLUDE_")): parser_included = ExtendedConfigParser(config=config, inheritance='im') included_file = s[8:] data = codecs.open(home + "/config/" + included_file, "r", "utf-8") parser_included.read_file(data) res.update(read_config_file(parser_included, config, home)) # On ajoute la section si elle n'y est pas ou on la met à jour # si elle a été incluse auparavant else: try: res[s].update(read_section(parser, s, home)) except KeyError: res[s] = read_section(parser, s, home) return res
def init(): global monitors, inactivityTimeout, logger if monitors: # Init already performed return True arg_parser = ArgumentParser() arg_parser.add_argument('-c', '--config', help='Configuration filename ' '( absolute or relative path to a directory under ' 'module home directory )') args = arg_parser.parse_args() homedir = None try: if args.config is None: logger.critical("you must provide a configuration file") sys.exit(1) if args.config.startswith('~'): args.config = os.path.abspath(args.config) if args.config.startswith('/'): ficconfig = args.config if not os.path.exists(ficconfig): logger.critical("File %s does not exist or is not accessible" % ficconfig) ficconfig = None else: ficconfig = None homedir = os.environ[os.environ['MFMODULE'] + '_HOME'] for directory in DEFAULT_HOME_CONFIG_DIRS: ficconfig = os.path.abspath(homedir) ficconfig = os.path.join(ficconfig, directory) ficconfig = os.path.join(ficconfig, args.config) if os.path.exists(ficconfig): break ficconfig = None if not ficconfig: logger.info("File %s not found under standard configuration" " directories") pass except KeyError: logger.critical("Can't obtain Module home directory") ficconfig = None if ficconfig and os.path.isdir(ficconfig): logger.critical("%s is a directory !" % ficconfig) ficconfig = None if not ficconfig: return False parser = ExtendedConfigParser(config=environ.get("MFCONFIG", "GENERIC"), strict=False, inheritance='im') data = codecs.open(ficconfig, "r", "utf-8") try: parser.read_file(data) except Exception: logger.critical('Error Loading config: %s' % ficconfig) return False Monitor.server = None Monitor.port = None Monitor.redis = None Monitor.unix_socket = None Monitor.wm = None monitor_inactivitytimeout = None monitors = [] for s in parser._sections: mon = Monitor() active = False key = 'active' if parser.has_option(s, key): active = parser.getboolean(s, key) if not active: continue key = 'redis_server' if not parser.has_option(s, key): continue server = parser[s][key] if Monitor.server is None: Monitor.server = server elif Monitor.server != server: logger.critical('Only 1 redis server Please') return False key = 'redis_unix_socket' if not parser.has_option(s, key): continue unix_socket = parser[s][key] if len(unix_socket) == 0 or unix_socket == "null": unix_socket = None Monitor.unix_socket = unix_socket key = 'redis_port' if not parser.has_option(s, key): continue port = parser.getint(s, key) if Monitor.port is None: Monitor.port = port elif Monitor.port != port: logger.critical('Only 1 redis port Please') return False key = 'monitor_inactivitytimeout' if parser.has_option(s, key): timeout = parser.getint(s, key) if timeout <= 0: # Standardization timeout = -1 if monitor_inactivitytimeout is None: monitor_inactivitytimeout = timeout inactivityTimeout = timeout elif monitor_inactivitytimeout != timeout: logger.critical('Only 1 monitor_inactivitytimeout Please') return False key = 'directory' if not parser.has_option(s, key): continue mon.dir = parser[s][key] key = 'queue' if not parser.has_option(s, key): continue mon.redisqueue = parser[s][key] if not mon.redisqueue: logger.critical('Invalid redis queue name') continue q = mon.redisqueue.replace('queue:', '').replace('out_', 'out.') q = re.sub(r'stage(\d+)_', r'stage\1.', q) key = 'timeout' if parser.has_option(s, key): mon.redistimeout = parser.getint(s, key) key = 'forced_poll' if parser.has_option(s, key): mon.tpolling = parser.getint(s, key) key = 'poll_queue_limit' if parser.has_option(s, key): mon.pollinglimit = parser.getint(s, key) key = 'only' if parser.has_option(s, key): mon.only = getMask(parser[s][key]) key = 'ignore' cnfgignorestr = None if parser.has_option(s, key): cnfgignorestr = parser[s][key] mon.ignore = getMask(cnfgignorestr, (r'^.*\.working$', )) monitors.append(mon) if not monitors or len(monitors) < 1: logger.critical('No directory to scan') return False return True
class BasicTestCase(unittest.TestCase): def setUp(self): self.x = ExtendedConfigParser() self.x.read('./test_cfg.ini') def test_basic(self): self.assertTrue(self.x is not None) def test_get_basic(self): self.assertEqual(self.x.get('sect2', 'key2'), 'val2') def test_get_basic_fail(self): self.assertRaises(NoOptionError, self.x.get, 'sect2', 'key4') def test_get_basic_fail2(self): self.assertRaises(NoSectionError, self.x.get, 'sect4', 'key3') def test_get_int(self): self.assertEqual(self.x.getint('sect1', 'key_int'), 1) def test_get_float(self): self.assertEqual(self.x.getfloat('sect1', 'key_float'), 1.24) def test_get_boolean_true(self): self.assertTrue(self.x.getboolean('sect1', 'key_bool1')) def test_get_boolean_on(self): self.assertTrue(self.x.getboolean('sect1', 'key_bool2')) def test_get_boolean_1(self): self.assertTrue(self.x.getboolean('sect1', 'key_bool3')) def test_get_boolean_yes(self): self.assertTrue(self.x.getboolean('sect1', 'key_bool4')) def test_get_boolean_false(self): self.assertFalse(self.x.getboolean('sect1', 'key_bool5')) def test_get_boolean_random(self): self.assertRaises(ValueError, self.x.getboolean, 'sect1', 'key_bool6') def test_default(self): self.assertEqual(self.x.get('sect3', 'key2'), 'default2') def test_default_section(self): default = OrderedDict([('key1[dev_plop_toto_stuff]', 'dev_plop_toto1_default'), ('key2', 'default2'), ('key3', 'default3'), ('key049', 'DEFAULT'), ('key049[dev]', 'DEFAULT_dev')]) self.assertEqual(self.x.default_section, default) def test_get_vars(self): self.assertEqual(self.x.get('sect1', 'key1', vars={'key1': 'deez'}), 'deez') def test_get_fallback(self): self.assertEqual(self.x.get('sect1', 'key173', fallback='deez'), 'deez') def test_has_section(self): self.assertTrue(self.x.has_section('sect2')) def test_has_section_advanced(self): self.assertTrue(self.x.has_section('sect1')) def test_has_section_fail(self): self.assertFalse(self.x.has_section('sect42')) def test_has_option(self): self.assertTrue(self.x.has_option('sect3', 'key3')) def test_has_option_config(self): self.assertTrue(self.x.has_option('sect3', 'key2', 'dev')) def test_has_option_config_fail(self): self.assertFalse(self.x.has_option('sect3', 'key2', 'scp')) def test_has_option_inheritance(self): self.assertTrue(self.x.has_option('sect1', 'key3')) def test_has_option_specificaction_fail(self): # Because config dependant self.assertFalse(self.x.has_option('sect3', 'key2')) def test_has_option_fail(self): self.assertFalse(self.x.has_option('sect3', 'key4')) def test_has_option_strict(self): self.assertTrue(self.x.has_option('sect3', 'key3', strict=True)) def test_has_option_strict_config(self): self.assertTrue(self.x.has_option('sect3', 'key2', 'dev', strict=True)) def test_has_option_strict_config_fail(self): self.assertFalse(self.x.has_option('sect3', 'key2', 'scp', strict=True)) def test_has_option_strict_inheritance(self): # Because strict self.assertFalse(self.x.has_option('sect1', 'key3', strict=True)) def test_has_option_strict_specificaction_fail(self): # Because config dependant self.assertFalse(self.x.has_option('sect3', 'key2', strict=True)) def test_has_option_strict_fail(self): self.assertFalse(self.x.has_option('sect3', 'key4', strict=True)) def test_has_option_config_ind(self): self.assertTrue(self.x.has_option('sect3', 'key3', cfg_ind=True)) def test_has_option_config_ind_inheritance(self): self.assertTrue(self.x.has_option('sect1', 'key3', cfg_ind=True)) def test_has_option_config_ind_specificaction(self): self.assertTrue(self.x.has_option('sect3', 'key2', cfg_ind=True)) def test_has_option_config_ind_fail(self): self.assertFalse(self.x.has_option('sect3', 'key4', cfg_ind=True)) def test_has_option_strict_config_ind(self): self.assertTrue(self.x.has_option('sect3', 'key3', cfg_ind=True, strict=True)) def test_has_option_strict_config_ind_inheritance(self): self.assertFalse(self.x.has_option('sect1', 'key3', cfg_ind=True, strict=True)) def test_has_option_strict_config_ind_specificaction(self): self.assertTrue(self.x.has_option('sect3', 'key2', cfg_ind=True, strict=True)) def test_has_option_strict_config_ind_fail(self): self.assertFalse(self.x.has_option('sect3', 'key4', cfg_ind=True, strict=True)) def test_options_basic(self): res = ['key1', 'key2', 'key2[dev]', 'key2[dev_plop]', 'key1[dev_plop_toto_stuff]', 'key2', 'key3', 'key049', 'key049[dev]'] self.assertEquals(self.x.options('sect2'), res) def test_options_inheritance(self): res = ['key1', 'key_int', 'key_bool1', 'key_bool2', 'key_bool3', 'key_bool4', 'key_bool5', 'key_bool6', 'key_float', 'key_list', 'key_list_int', 'key_list_bool', 'key_list_float', 'key1[dev]', 'key1', 'key2', 'key2[dev]', 'key2[dev_plop]', 'key1[dev_plop_toto]', 'key2[dev]', 'key3', 'key3[dev]', 'key3[toto]', 'key3[dev_plop]', 'key3[dev_plop_toto]', 'key1[dev_plop_toto_stuff]', 'key2', 'key3', 'key049', 'key049[dev]'] self.assertEquals(self.x.options('sect1'), res) def test_options_strict(self): res = ['key1', 'key_int', 'key_bool1', 'key_bool2', 'key_bool3', 'key_bool4', 'key_bool5', 'key_bool6', 'key_float', 'key_list', 'key_list_int', 'key_list_bool', 'key_list_float', 'key1[dev]'] self.assertEquals(self.x.options('sect1', strict=True), res) def test_options_strict_defaults(self): res = ['key1', 'key_int', 'key_bool1', 'key_bool2', 'key_bool3', 'key_bool4', 'key_bool5', 'key_bool6', 'key_float', 'key_list', 'key_list_int', 'key_list_bool', 'key_list_float', 'key1[dev]', 'key1[dev_plop_toto_stuff]', 'key2', 'key3', 'key049', 'key049[dev]'] self.assertEquals(self.x.options('sect1', strict=True, defaults=True), res) def test_options_strict_config_ind(self): res = ['key1', 'key_int', 'key_bool1', 'key_bool2', 'key_bool3', 'key_bool4', 'key_bool5', 'key_bool6', 'key_float', 'key_list', 'key_list_int', 'key_list_bool', 'key_list_float'] self.assertEquals(self.x.options('sect1', strict=True, cfg_ind=True), res) def test_options_strict_config_ind_defaults(self): res = ['key1', 'key_int', 'key_bool1', 'key_bool2', 'key_bool3', 'key_bool4', 'key_bool5', 'key_bool6', 'key_float', 'key_list', 'key_list_int', 'key_list_bool', 'key_list_float', 'key2', 'key3', 'key049'] self.assertEquals(self.x.options('sect1', strict=True, defaults=True, cfg_ind=True), res) def test_options_config_ind(self): res = ['key1', 'key_int', 'key_bool1', 'key_bool2', 'key_bool3', 'key_bool4', 'key_bool5', 'key_bool6', 'key_float', 'key_list', 'key_list_int', 'key_list_bool', 'key_list_float', 'key2', 'key3', 'key049'] self.assertEquals(self.x.options('sect1', cfg_ind=True).sort(), res.sort()) def test_items_basic(self): res = [('key1', 'val1_sect2'), ('key2', 'val2'), ('key2[dev]', 'dev2'), ('key2[dev_plop]', 'dev_plop2'), ('key1[dev_plop_toto_stuff]', 'dev_plop_toto1_default'), ('key2', 'default2'), ('key3', 'default3'), ('key049', 'DEFAULT'), ('key049[dev]', 'DEFAULT_dev')] res.sort() test = self.x.items('sect2') test.sort() self.assertEquals(test, res) def test_items_inheritance(self): res = [('key1', 'val1'), ('key_int', '1'), ('key_bool1', 'true'), ('key_bool2', 'on'), ('key_bool3', '1'), ('key_bool4', 'yes'), ('key_bool5', 'false'), ('key_bool6', 'random'), ('key_float', '1.24'), ('key_list', 'damn;dang;nabbit'), ('key_list_int', '1;7;3'), ('key_list_bool', 'true;false;true'), ('key_list_float', '0.96;1.73;6.82'), ('key1[dev]', 'dev1'), ('key1', 'val1_sect2'), ('key2', 'val2'), ('key2[dev]', 'dev2'), ('key2[dev_plop]', 'dev_plop2'), ('key1[dev_plop_toto]', 'dev_plop_toto1_sect3'), ('key2[dev]', 'dev2_sect3'), ('key3', 'val3'), ('key3[dev]', 'dev3'), ('key3[toto]', 'toto3'), ('key3[dev_plop]', 'dev_plop3'), ('key3[dev_plop_toto]', 'dev_plop_toto3'), ('key1[dev_plop_toto_stuff]', 'dev_plop_toto1_default'), ('key2', 'default2'), ('key3', 'default3'), ('key049', 'DEFAULT'), ('key049[dev]', 'DEFAULT_dev')] res.sort() test = self.x.items('sect1') test.sort() self.assertEquals(test, res) def test_items_strict_basic(self): res = [('key1', 'val1'), ('key_int', '1'), ('key_bool1', 'true'), ('key_bool2', 'on'), ('key_bool3', '1'), ('key_bool4', 'yes'), ('key_bool5', 'false'), ('key_bool6', 'random'), ('key_float', '1.24'), ('key_list', 'damn;dang;nabbit'), ('key_list_int', '1;7;3'), ('key_list_bool', 'true;false;true'), ('key_list_float', '0.96;1.73;6.82'), ('key1[dev]', 'dev1')] res.sort() test = self.x.items('sect1', strict=True) test.sort() self.assertEquals(test, res) def test_items_strict_defaults(self): res = [('key1', 'val1'), ('key_int', '1'), ('key_bool1', 'true'), ('key_bool2', 'on'), ('key_bool3', '1'), ('key_bool4', 'yes'), ('key_bool5', 'false'), ('key_bool6', 'random'), ('key_float', '1.24'), ('key_list', 'damn;dang;nabbit'), ('key_list_int', '1;7;3'), ('key_list_bool', 'true;false;true'), ('key_list_float', '0.96;1.73;6.82'), ('key1[dev]', 'dev1'), ('key1[dev_plop_toto_stuff]', 'dev_plop_toto1_default'), ('key2', 'default2'), ('key3', 'default3'), ('key049', 'DEFAULT'), ('key049[dev]', 'DEFAULT_dev')] res.sort() test = self.x.items('sect1', defaults=True, strict=True) test.sort() self.assertEquals(test, res) def test_items_all(self): res = [] for key in self.x._sections: res.append((key, SectionProxyExtended(self.x, key))) res.sort() test = self.x.items() test.sort() self.assertEquals(test, res) def test_items_strict_all(self): res = [] for key in self.x._sections: res.append((key, SectionProxyExtended(self.x, key))) res.sort() test = self.x.items(strict=True) test.sort() self.assertEquals(test, res) def test_items_vars(self): res = [('william', 'Overbeck'), ('key1', 'val1_sect2'), ('key2', 'val2'), ('key2[dev]', 'dev2'), ('key2[dev_plop]', 'dev_plop2'), ('key1[dev_plop_toto_stuff]', 'dev_plop_toto1_default'), ('key2', 'default2'), ('key3', 'default3'), ('key049', 'DEFAULT'), ('key049[dev]', 'DEFAULT_dev')] res.sort() test = self.x.items('sect2', vars={'William': 'Overbeck'}) test.sort() self.assertEquals(test, res) def test_items_vars_strict(self): res = [('william', 'Overbeck'), ('key1', 'val1_sect2'), ('key2', 'val2'), ('key2[dev]', 'dev2'), ('key2[dev_plop]', 'dev_plop2')] res.sort() test = self.x.items('sect2', vars={'William': 'Overbeck'}, strict=True) test.sort() self.assertEquals(test, res) def test_get_key_dict(self): self.assertEqual(self.x['sect2']['key2'], 'val2') def test_get_sect_dict_fail(self): self.assertRaises(KeyError, lambda: self.x['sect4']) def test_get_key_dict_fail(self): self.assertRaises(KeyError, lambda: self.x['sect2']['key42']) def test_has_section_strict(self): self.assertTrue(self.x.has_section('sect2', strict=True)) def test_has_section_strict_fail(self): self.assertFalse(self.x.has_section('sect1', strict=True)) def test_defaults(self): self.x = ExtendedConfigParser(defaults={'william': 'Overbeck'}) self.x.read('./test_cfg.ini') res = OrderedDict([('william', 'Overbeck')]) self.assertEquals(self.x.defaults(), res) def test_add_section(self): self.x.add_section('Jim Hoxworth') self.assertTrue(self.x.has_section('Jim Hoxworth')) self.assertEqual(self.x.get('Jim Hoxworth', 'key2'), 'default2') self.assertEqual(self.x['Jim Hoxworth']['key2'], 'default2') def test_read_file(self): self.x = ExtendedConfigParser() data = open('./test_cfg.ini', 'r') self.x.read_file(data) self.assertEqual(self.x.get('sect2', 'key2'), 'val2')