Exemplo n.º 1
0
 def test_load2(self):
     """Test load method"""
     config = Configuration()
     try:
         config.load("__missing__")
         self.fail("expected error")
     except ConfigurationError:
         pass
Exemplo n.º 2
0
 def test_load4(self):
     """Test load method"""
     config = Configuration()
     try:
         config.load(currdir + "config3.ini")
         self.fail("expected error")
     except ConfigurationError:
         pass
Exemplo n.º 3
0
 def test_load4(self):
     """Test load method"""
     config = Configuration()
     try:
         config.load(currdir + "config3.ini")
         self.fail("expected error")
     except ConfigurationError:
         pass
Exemplo n.º 4
0
 def test_save2(self):
     """Test save method"""
     config = Configuration()
     try:
         config.save(None)
         self.fail("expected error")
     except ConfigurationError:
         pass
Exemplo n.º 5
0
 def test_load1(self):
     """Test load method"""
     config = Configuration()
     try:
         config.load(None)
         self.fail("expected error")
     except ConfigurationError:
         pass
Exemplo n.º 6
0
 def test_getitem(self):
     """Test getitem method"""
     config = Configuration()
     try:
         config["globals"]
         self.fail("expected error")
     except ConfigurationError:
         pass
     config.load(currdir + "config1.ini")
     keys = list(config["globals"].keys())
     keys.sort()
     self.assertTrue(keys == ["a", "b", "c"])
Exemplo n.º 7
0
 def test_getitem(self):
     """Test getitem method"""
     config = Configuration()
     try:
         config["globals"]
         self.fail("expected error")
     except ConfigurationError:
         pass
     config.load(currdir + "config1.ini")
     keys = list(config["globals"].keys())
     keys.sort()
     self.assertTrue(keys == ["a", "b", "c"])
Exemplo n.º 8
0
 def test_save1(self):
     """Test save method"""
     config = Configuration()
     config.load(currdir + "config1.ini")
     #
     # A hack, to ensure cross-platform portability of this test
     #
     if False and sys.platform == "win32":
         e = ExtensionPoint(IFileOption)
         for ep in e.extensions():
             if ep.enabled():
                 ep.set_value("/dev/null", raw=True)
     config.save(currdir + "config1.out")
     #PluginGlobals.pprint()
     self.assertFileEqualsBaseline(
         currdir + "config1.out", currdir + "config1.txt", filter=filter)
Exemplo n.º 9
0
    def test_load5(self):
        """Test load method"""
        PluginGlobals.add_env("testing.config_loading")

        class TMP2(Plugin):

            def __init__(self):
                declare_option("a")
                declare_option("b", cls=FileOption)
                declare_option("c")
                declare_option("xx", section_re='globals.*')

        config = Configuration()
        tmp2 = TMP2()
        config.load(currdir + "config4.ini")
        #config.pprint()
        if False and sys.platform == "win32":
            #
            # A hack, to ensure cross-platform portability of this test
            #
            e = ExtensionPoint(IFileOption)
            for ep in e.extensions():
                ep.set_value("/dev/null", raw=True)
        #PluginGlobals.pprint()
        config.save(currdir + "config4.out")
        #print config
        self.assertFileEqualsBaseline(
            currdir + "config4.out", currdir + "config4.txt", filter=filter)
        pyutilib.misc.setup_redirect(currdir + "log2.out")
        config.pprint()
        pyutilib.misc.reset_redirect()
        self.assertFileEqualsBaseline(
            currdir + "log2.out", currdir + "log2.txt", filter=filter)
        PluginGlobals.remove_env(
            "testing.config_loading", cleanup=True, singleton=False)
Exemplo n.º 10
0
    def test_load5(self):
        """Test load method"""
        PluginGlobals.add_env("testing.config_loading")

        class TMP2(Plugin):
            def __init__(self):
                declare_option("a")
                declare_option("b", cls=FileOption)
                declare_option("c")
                declare_option("xx", section_re='globals.*')

        config = Configuration()
        tmp2 = TMP2()
        config.load(currdir + "config4.ini")
        #config.pprint()
        if False and sys.platform == "win32":
            #
            # A hack, to ensure cross-platform portability of this test
            #
            e = ExtensionPoint(IFileOption)
            for ep in e.extensions():
                ep.set_value("/dev/null", raw=True)
        #PluginGlobals.pprint()
        config.save(currdir + "config4.out")
        #print config
        self.assertFileEqualsBaseline(currdir + "config4.out",
                                      currdir + "config4.txt",
                                      filter=filter)
        pyutilib.misc.setup_redirect(currdir + "log2.out")
        config.pprint()
        pyutilib.misc.reset_redirect()
        self.assertFileEqualsBaseline(currdir + "log2.out",
                                      currdir + "log2.txt",
                                      filter=filter)
        PluginGlobals.remove_env("testing.config_loading",
                                 cleanup=True,
                                 singleton=False)
Exemplo n.º 11
0
 def test_save1(self):
     """Test save method"""
     config = Configuration()
     config.load(currdir + "config1.ini")
     #
     # A hack, to ensure cross-platform portability of this test
     #
     if False and sys.platform == "win32":
         e = ExtensionPoint(IFileOption)
         for ep in e.extensions():
             if ep.enabled():
                 ep.set_value("/dev/null", raw=True)
     config.save(currdir + "config1.out")
     #PluginGlobals.pprint()
     self.assertFileEqualsBaseline(currdir + "config1.out",
                                   currdir + "config1.txt",
                                   filter=filter)
Exemplo n.º 12
0
 def load_default(self):
     """ Loads a built-in, default configuration. """
     log.debug("Loading default config.")
     self._create_environment()
     self._config = Configuration()
Exemplo n.º 13
0
class Config(object):
    """
        Main Bumblebee configuration class.

    """

    _environment = None

    def __init__(self):
        self._found_config_path = None
        self._load_time = None

    def get_config_path(self):
        """ Returns absolute path to configuration file. """
        # If a config file was already found earlier, return its path.
        if self._found_config_path:
            return self._found_config_path

        # If no config file found yet, look for one.
        # First, look for a config file in the local directory.
        local_directory = os.path.realpath(os.path.dirname(sys.argv[0]))
        local_path = os.path.join(local_directory, "Bumblebee.ini")
        if os.path.isfile(local_path):
            self._found_config_path = local_path
            return local_path

        # Second, look for a config file in the system local
        #  app data directory.
        folder_id = shellcon.CSIDL_LOCAL_APPDATA
        system_directory = shell.SHGetFolderPath(0, folder_id, 0, 0)
        system_path = os.path.join(system_directory, "Bumblebee",
                                   "Bumblebee.ini")
        self._found_config_path = system_path
        return system_path

    def load_default(self):
        """ Loads a built-in, default configuration. """
        log.debug("Loading default config.")
        self._create_environment()
        self._config = Configuration()

    def _create_environment(self):
        if Config._environment == None:
            env_name = "Bumblebee"
            Config._environment = PluginEnvironment(env_name)
            PluginGlobals.push_env(Config._environment)

    def load(self):
        """
            Loads configuration from file, if the file is present.

            :returns:   True if configuration loaded;
                        False if configuration not loaded.

        """

        path = self.get_config_path()
        log.info("Loading config from {0}.".format(path))

        try:
            config_file = open(path)
        except IOError, e:
            if e.errno == 2:  # No such file or directory.
                log.warning("Config file ({0}) not found." "".format(path))
                return False
            else:  # Other IOError types.
                log.exception("Failed to open config file ({0}):"
                              " {1}".format(path, e))
                raise

        self._create_environment()
        self._config = Configuration()
        self._config.load(path)
        self._load_time = os.path.getmtime(path)

        return True
Exemplo n.º 14
0
 def test_sections(self):
     """Test getitem method"""
     config = Configuration()
     config.load(currdir + "config1.ini")
     keys = list(config.sections())
     keys.sort()
Exemplo n.º 15
0
 def test_contains(self):
     """Test contains method"""
     config = Configuration()
     self.assertFalse("globals" in config)
     config.load(currdir + "config1.ini")
     self.assertTrue("globals" in config)
Exemplo n.º 16
0
 def test_init(self):
     """Test Configuration construction"""
     config = Configuration()
Exemplo n.º 17
0
 def test_sections(self):
     """Test getitem method"""
     config = Configuration()
     config.load(currdir + "config1.ini")
     keys = list(config.sections())
     keys.sort()
Exemplo n.º 18
0
 def test_contains(self):
     """Test contains method"""
     config = Configuration()
     self.assertFalse("globals" in config)
     config.load(currdir + "config1.ini")
     self.assertTrue("globals" in config)