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
def test_config_set(self): """Test set""" _cfg = configparser.RawConfigParser() _cfg.add_section("TestApp") cfg = mock.create_autospec(Configuration) cfg._log = logging.getLogger("config_set") cfg._config = _cfg cfg.jobtype = "TestApp" Configuration.set(cfg, "key", "value") self.assertEqual(dict(cfg._config.items('TestApp')), {'key': 'value'}) cfg.jobtype = "TestApp2" with self.assertRaises(InvalidConfigException): Configuration.set(cfg, "key", "value")
def test_config_set(self): """Test set""" _cfg = configparser.RawConfigParser() _cfg.add_section("TestApp") cfg = mock.create_autospec(Configuration) cfg._log = logging.getLogger("config_set") cfg._config = _cfg cfg.jobtype = "TestApp" Configuration.set(cfg, "key", "value") self.assertEqual(dict(cfg._config.items('TestApp')), {'key':'value'}) cfg.jobtype = "TestApp2" with self.assertRaises(InvalidConfigException): Configuration.set(cfg, "key", "value")
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