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 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 test_config_add_jobtype(self): """Test add_jobtype""" _cfg = configparser.RawConfigParser() _cfg.add_section("TestApp") cfg = mock.create_autospec(Configuration) cfg._log = logging.getLogger("add_jobtype") cfg._config = _cfg Configuration.add_jobtype(cfg, "TestApp") self.assertEqual(cfg._config.sections(), ['TestApp']) self.assertEqual(dict(cfg._config.items('TestApp')), {}) Configuration.add_jobtype(cfg, "TestApp2", a="1", b=2, c=None) self.assertEqual(cfg._config.sections(), ['TestApp', 'TestApp2']) self.assertEqual(dict(cfg._config.items('TestApp2')), { 'a': '1', 'b': 2, 'c': None })
def test_config_add_jobtype(self): """Test add_jobtype""" _cfg = configparser.RawConfigParser() _cfg.add_section("TestApp") cfg = mock.create_autospec(Configuration) cfg._log = logging.getLogger("add_jobtype") cfg._config = _cfg Configuration.add_jobtype(cfg, "TestApp") self.assertEqual(cfg._config.sections(), ['TestApp']) self.assertEqual(dict(cfg._config.items('TestApp')), {}) Configuration.add_jobtype(cfg, "TestApp2", a="1", b=2, c=None) self.assertEqual(cfg._config.sections(), ['TestApp', 'TestApp2']) self.assertEqual(dict(cfg._config.items('TestApp2')), {'a':'1', 'b':2, 'c':None})