def _configure_addon(self):
        """
        Configures the addon based on the current User Preferences
        and the supplied Batch Apps ini file configuration.

        :Returns:
            - A :class:`.batchapps.Configuration` object.
        """
        cfg = None
        try:
            data_dir = os.path.split(self.props.data_dir)

            cfg = Configuration(jobtype='Blender',
                                data_path=data_dir[0],
                                log_level=int(self.props.log_level),
                                name=self.props.ini_file,
                                datadir=data_dir[1])

        except (InvalidConfigException, IndexError) as exp:
            self.log.warning("Warning failed to load config file, "
                             "creating new default config.")
            self.log.warning(str(exp))

        finally:

            if not os.path.isdir(self.props.data_dir):
                raise EnvironmentError(
                    "Data directory not created - "
                    "please ensure you have adequate permissions.")

            if not cfg:
                cfg = Configuration(jobtype='Blender', log_level='warning')

            if self.props.endpoint:
                cfg = override_config(cfg, endpoint=self.props.endpoint)
            if self.props.account:
                cfg = override_config(cfg, account=self.props.account)
            if self.props.key:
                cfg = override_config(cfg, key=self.props.key)
            if self.props.client_id:
                cfg = override_config(cfg, client_id=self.props.client_id)
            if self.props.tenant:
                cfg = override_config(cfg, tenant=self.props.tenant)
            if self.props.redirect:
                cfg = override_config(cfg, redirect=self.props.redirect)

            cfg.save_config()
            return cfg
def create_config():
    """
    Looks for configuration settings for specified application, otherwise
    creates new configuration, sets chosen log_level.
    
    :Returns:
        - a :class:`.Configuration` instance object
    """

    global LOG_LEVEL

    if input("Run in debug mode? (yes/no)")[0].lower() == 'n':
        LOG_LEVEL = "info"

    try:
        # Look for application in existing config file
        config = Configuration(log_level=LOG_LEVEL, jobtype="MyApp")
        print("Config found.")
        return config

    except InvalidConfigException:
        print("Valid config not found. Attempting to create new config.")

    try:
        config = Configuration(log_level=LOG_LEVEL)
        config.aad_config(endpoint=ENDPOINT,
                          account=ACCOUNT_ID,
                          key=ACCOUNT_KEY,
                          unattended=True)

        config.add_jobtype("MyApp")
        config.current_jobtype("MyApp")

        # Examples of default config settings for your job
        config.set("width", "500")
        config.set("height", "500")

        # Set MyApp to be the default job type
        config.set_default_jobtype()

    except InvalidConfigException as e:
        raise RuntimeError("Invalid Configuration: {0}".format(e))

    finally:
        config.save_config()
        return config
def generate_config():
    """
    Uses a current configuration if possible otherwise creates a new
    configuration for the specified job type.

    :Returns:
        - An instance of the :class:`batchapps.Configuration`.
    """

    try:
        cfg = Configuration(log_level="info", job_type="ImageMagick")
        print("Existing config found.")
        return cfg

    except InvalidConfigException as exp:
        print("Invalid Configuration: {0}\n"
              "Attempting to create new config.".format(exp))

    try:
        cfg = Configuration(log_level="info")

        cfg.aad_config(account=ACCOUNT_ID,
                       key=ACCOUNT_KEY,
                       ENDPOINT=endpoint,
                       unattended=True)

        cfg.add_jobtype("ImageMagick")
        cfg.current_jobtype("ImageMagick")
        cfg.set("width", "500")
        cfg.set("height", "500")
        cfg.set_default_jobtype()

    except InvalidConfigException as exp:
        raise RuntimeError("Invalid Configuration: {0}".format(exp))

    finally:
        cfg.save_config()
        return cfg
Beispiel #4
0
    def test_config_read_defaults(self, mock_file, mock_save, mock_level,
                                  mock_logging, mock_dir):
        """Test read"""
        if not self.use_test_files:
            self.skipTest("No test files present")
        mock_dir.return_value = True
        mock_logging.return_value = logging.getLogger("read_defaults")
        mock_file.return_value = True

        cfg = Configuration(data_path=self.test_dir, datadir="")
        self.assertFalse(mock_save.called)
        mock_dir.assert_called_with(self.test_dir)
        mock_file.assert_called_with(
            os.path.join(self.test_dir, "batch_apps.ini"))

        self.assertEqual(cfg.jobtype, "Blender")
def logging_mode():
    """
    Sets configuration to chosen log_level using existing
    congifuration setup.
    
    :Returns:
        - a :class:`.Configuration` instance object
    """
    global LOG_LEVEL

    if input("Run in debug mode? (yes/no)")[0].lower() == 'n':
        LOG_LEVEL = "info"

    try:
        return Configuration(log_level=LOG_LEVEL)

    except InvalidConfigException as e:
        print("Invalid Configuration: {0}".format(e))
Beispiel #6
0
    def test_config_set_defaults(self, mock_read, mock_file, mock_save,
                                 mock_level, mock_logging, mock_dir):
        """Test _set_defaults"""

        mock_dir.return_value = False
        mock_logging.return_value = logging.getLogger("defaults")
        mock_file.return_value = False

        cfg = Configuration(default=True)
        self.assertTrue(mock_save.called)
        self.assertFalse(mock_read.called)
        self.assertFalse(mock_file.called)
        mock_logging.assert_called_with(
            os.path.join(self.userdir, "BatchAppsData"))

        mock_level.assert_called_with(30)
        self.assertEqual(
            sorted(cfg._config.sections()),
            sorted(["Authentication", "Blender", "Logging", "Test"]))

        cfg = Configuration()
        self.assertTrue(mock_save.called)
        self.assertFalse(mock_read.called)
        self.assertTrue(mock_file.called)
        mock_logging.assert_called_with(
            os.path.join(self.userdir, "BatchAppsData"))

        self.assertEqual(
            sorted(cfg._config.sections()),
            sorted(["Authentication", "Blender", "Logging", "Test"]))

        cfg = Configuration(data_path="c:\\mypath",
                            log_level=10,
                            datadir="data")

        self.assertFalse(mock_read.called)
        mock_dir.assert_any_call("c:\\mypath")
        mock_dir.assert_any_call(self.userdir)
        mock_logging.assert_called_with(os.path.join(self.userdir, "data"))
        mock_level.assert_called_with(10)

        mock_file.return_value = True
        cfg = Configuration(default=True)
        self.assertTrue(mock_save.called)
        self.assertFalse(mock_read.called)

        mock_save.reset()
        mock_read.side_effect = OSError("test")
        cfg = Configuration(data_path=self.test_dir, application='Blender')
        self.assertTrue(mock_save.called)
        self.assertTrue(mock_read.called)
        self.assertEqual(cfg.jobtype, "Blender")
        self.assertEqual(cfg.job_type, "Blender")

        cfg = Configuration(data_path=self.test_dir, jobtype=None)
        self.assertEqual(cfg.jobtype, "Blender")
        self.assertEqual(cfg.job_type, "Blender")

        with self.assertRaises(InvalidConfigException):
            Configuration(application='TestApp', default=True)
        with self.assertRaises(InvalidConfigException):
            Configuration(jobtype=42, default=True)