def test_load_config_file_exception_msg_invalid_ext(): handle, f_name = tempfile.mkstemp(suffix=".invalid") f = os.fdopen(handle, "w") # necessary for windows f.close() try: exception_msg = re.compile( 'Configuration file ".*.invalid" has invalid or ' 'unsupported extension ".invalid".') with pytest.raises(ValueError) as excinfo: load_config_file(f_name) assert exception_msg.match(excinfo.value.args[0]) finally: os.remove(f_name)
def test_load_config_file_exception_msg_invalid_ext(): handle, f_name = tempfile.mkstemp(suffix='.invalid') f = os.fdopen(handle, "w") # necessary for windows f.close() try: exception_msg = re.compile( 'Configuration file ".*.invalid" has invalid or ' 'unsupported extension ".invalid".' ) with pytest.raises(ValueError) as excinfo: load_config_file(f_name) assert exception_msg.match(excinfo.value.args[0]) finally: os.remove(f_name)
def from_config(cls, filename): """ Create a SlackObserver from a given configuration file. The file can be in any format supported by Sacred (.json, .pickle, [.yaml]). It has to specify a ``webhook_url`` and can optionally set ``bot_name``, ``icon``, ``completed_text``, ``interrupted_text``, and ``failed_text``. """ d = load_config_file(filename) obs = None if "webhook_url" in d: obs = cls(d["webhook_url"]) else: raise ValueError("Slack configuration file must contain " "an entry for 'webhook_url'!") for k in [ "completed_text", "interrupted_text", "failed_text", "bot_name", "icon", ]: if k in d: setattr(obs, k, d[k]) return obs
def from_config(cls, filename): """ Create a TelegramObserver from a given configuration file. The file can be in any format supported by Sacred (.json, .pickle, [.yaml]). It has to specify a ``token`` and a ``chat_id`` and can optionally set ``silent_completion``, ``started_text``, ``completed_text``, ``interrupted_text``, and ``failed_text``. """ import telegram d = load_config_file(filename) request = cls.get_proxy_request(d) if 'proxy_url' in d else None if 'token' in d and 'chat_id' in d: bot = telegram.Bot(d['token'], request=request) obs = cls(bot, **d) else: raise ValueError("Telegram configuration file must contain " "entries for 'token' and 'chat_id'!") for k in [ 'started_text', 'completed_text', 'interrupted_text', 'failed_text' ]: if k in d: setattr(obs, k, d[k]) return obs
def test_load_config_file(ext, handler): handle, f_name = tempfile.mkstemp(suffix=ext) f = os.fdopen(handle, "w" + handler.mode) handler.dump(data, f) f.close() d = load_config_file(f_name) assert d == data os.remove(f_name)
def from_config(cls, filename): """ Create a TelegramObserver from a given configuration file. The file can be in any format supported by Sacred (.json, .pickle, [.yaml]). It has to specify a ``token`` and a ``chat_id`` and can optionally set ``silent_completion``,``completed_text``, ``interrupted_text``, and ``failed_text``. """ import telegram d = load_config_file(filename) obs = None if 'proxy_url' in d: from telegram.utils.request import Request if d['proxy_url'].startswith('socks5'): urllib3_proxy_kwargs = dict() for key in ['username', 'password']: if key in d: urllib3_proxy_kwargs[key] = d[key] request = Request(proxy_url=d['proxy_url'], urllib3_proxy_kwargs=urllib3_proxy_kwargs) elif d['proxy_url'].startswith('http'): cred_string = '' if 'username' in d: cred_string += d['username'] if 'password' in d: cred_string += ':' + d['password'] if len(cred_string) > 0: domain = d['proxy_url'].split('/')[-1].split('@')[-1] cred_string += '@' proxy_url = 'http://{}{}'.format(cred_string, domain) request = Request(proxy_url=proxy_url) else: request = Request(proxy_url=d['proxy_url']) else: raise Exception( "Proxy URL should be in format PROTOCOL://PROXY_HOST[:PROXY_PORT].\n" "HTTP and Socks5 are supported.") else: request = None if 'token' in d and 'chat_id' in d: bot = telegram.Bot(d['token'], request=request) obs = cls(bot, **d) else: raise ValueError("Telegram configuration file must contain " "entries for 'token' and 'chat_id'!") for k in ['completed_text', 'interrupted_text', 'failed_text']: if k in d: setattr(obs, k, d[k]) return obs
def from_config(cls, filename): """ Create a MattermostObserver from a given configuration file. The file can be in any format supported by Sacred (.json, .pickle, [.yaml]). It has to specify a ``webhook_url`` and can optionally set ``bot_name``, ``icon``, ``completed_text``, ``interrupted_text``, and ``failed_text``. """ return cls(**load_config_file(filename))
def from_config(cls, filename): """ Create a TelegramObserver from a given configuration file. The file can be in any format supported by Sacred (.json, .pickle, [.yaml]). It has to specify a ``token`` and a ``chat_id`` (both strings) and can optionally set these values: * ``silent_success``: bool. If True, send a "silent" message upon successful completion. * ``completed_text``: str. Format string to use upon completion. * ``use_reply``: bool. If True, completion, interruption and failure messages will be sent as replies to the start message. * ``send_image``: bool. If True, and if ``PIL.Image`` is available, experiment results that are deemed to be images by ``Image.isImageType(result)`` will be sent as images with the ``completed_text_image`` as a caption. * ``completed_text_image``: str. Format string for completion when the result is an image. Markdown will *not* work here! * ``interrupted_text``: str. Format string for interruption message. * ``failed_text``: str. Format string for failure message. """ import telegram d = load_config_file(filename) obs = None if 'token' in d and 'chat_id' in d: bot = telegram.Bot(d['token']) obs = cls(bot, **d) else: raise ValueError("Telegram configuration file must contain " "entries for 'token' and 'chat_id'!") for k in ['started_text', 'completed_text', 'completed_text_image', 'interrupted_text', 'failed_text', 'use_reply', 'send_image']: if k in d: setattr(obs, k, d[k]) return obs
def from_config(cls, filename): """ Create a SlackObserver from a given configuration file. The file can be in any format supported by Sacred (.json, .pickle, [.yaml]). It has to specify a ``webhook_url`` and can optionally set ``bot_name``, ``icon``, ``completed_text``, ``interrupted_text``, and ``failed_text``. """ d = load_config_file(filename) obs = None if 'webhook_url' in d: obs = cls(d['webhook_url']) else: raise ValueError("Slack configuration file must contain " "an entry for 'webhook_url'!") for k in ['completed_text', 'interrupted_text', 'failed_text', 'bot_name', 'icon']: if k in d: setattr(obs, k, d[k]) return obs
def from_config(cls, filename): """ Create a TelegramObserver from a given configuration file. The file can be in any format supported by Sacred (.json, .pickle, [.yaml]). It has to specify a ``token`` and a ``chat_id`` and can optionally set ``silent_completion``,``completed_text``, ``interrupted_text``, and ``failed_text``. """ import telegram d = load_config_file(filename) request = cls.get_proxy_request(d) if 'proxy_url' in d else None if 'token' in d and 'chat_id' in d: bot = telegram.Bot(d['token'], request=request) obs = cls(bot, **d) else: raise ValueError("Telegram configuration file must contain " "entries for 'token' and 'chat_id'!") for k in ['completed_text', 'interrupted_text', 'failed_text']: if k in d: setattr(obs, k, d[k]) return obs
def test_load_config_file(ext, handler): with tempfile.NamedTemporaryFile('w+' + handler.mode, suffix=ext) as f: handler.dump(data, f) f.seek(0) d = load_config_file(f.name) assert d == data