def cast_info_from_path(cast_file=None): if cast_file: if os.path.exists(cast_file): cast = Cast.from_content(cast_file) else: cast = Cast() else: cast_files = glob('*.cast') if not cast_files: log.error( 'There is no cast file. To create a new one, specify the name with --file' ) sys.exit(1) if len(cast_files) > 1: log.error( 'There are more than one cast file. Please specify which to modify with --file' ) sys.exit(1) cast_file = cast_files[0] cast = Cast.from_content(cast_file) return cast, cast_file
def test_from_url(self): cast = Cast.from_content(CAST_URL) assert cast.messages from remoteconfig import RemoteConfig c = RemoteConfig(CAST_URL) assert str(c)
def test_add_msg(self): cast = Cast() cast.add_msg('Message 1') cast.add_msg('Message Alert', alert=True) cast.add_msg('Message 2') assert cast.alert == 'Message Alert' assert not cast.alert_exit assert [(m.key, m.message) for m in cast.messages] == [('1', 'Message 1'), ('2', 'Message 2')] cast.add_msg('Message Alert Exit', alert_exit=True) assert cast.alert == 'Message Alert Exit' assert cast.alert_exit
def test_filter(self): def msg_filter(msg, alert=False): if 'small bug' in msg: return msg cast = Cast.from_content(CAST_FILE, msg_filter) assert str( cast ) == '[Messages]\n3: There is a small bug over there, so watch out!\n_limit: 5'
def test_add_msg(self): cast = Cast() cast.add_msg('Message 1') cast.add_msg('Message Alert', alert=True) cast.add_msg('Message 2') assert cast.alert == 'Message Alert' assert not cast.alert_exit assert [(m.key, m.message) for m in cast.messages] == [ ('1', 'Message 1'), ('2', 'Message 2') ] cast.add_msg('Message Alert Exit', alert_exit=True) assert cast.alert == 'Message Alert Exit' assert cast.alert_exit
def test_save(self): from_content = open(CAST_FILE).read() cast = Cast.from_content(CAST_FILE) to_cast_file = os.path.join(tempfile.gettempdir(), 'clicast.to_file_test.cast') try: cast.save(to_cast_file) to_content = open(to_cast_file).read() assert from_content == to_content finally: if os.path.exists(to_cast_file): os.unlink(to_cast_file)
def test_set_msg_limit(self): cast = Cast() cast.set_msg_limit(2) for x in range(10): cast.add_msg('Message %d' % x) assert str( cast) == '[Messages]\n9: Message 8\n10: Message 9\n_limit: 2'
def cast_info_from_path(cast_file=None): if cast_file: if os.path.exists(cast_file): cast = Cast.from_file(cast_file) else: cast = Cast() else: cast_files = glob('*.cast') if not cast_files: log.error('There is no cast file. To create a new one, specify the name with --file') sys.exit(1) if len(cast_files) > 1: log.error('There are more than one cast file. Please specify which to modify with --file') sys.exit(1) cast_file = cast_files[0] cast = Cast.from_file(cast_file) return cast, cast_file
def check_message(url, msg_filter=None, cache_duration=None, allow_exit=False, raises=False, local_file=None, reset=False, **show_kwargs): """ Check remote url for new messages and display them. :param str url: Cast file URL to check :param callable msg_filter: Filter messages with callable that accepts message string and alert boolean (True for alert message). It should return the original or an updated message, or None if the message should be ignored. :param int cache_duration: Cache messages locally for number of seconds to avoid checking the URL too often. This is useful for response latency sensitive CLI to ensure user's experience isn't compromised. Alternatively, you may want to check messages in a seperate thread. :param bool allow_exit: Perform sys.exit(1) if cast requests it. :param bool raises: Raise exception for failed to download/parse cast file or such. Recommended to set this to False for production / in non-debug mode. :param str local_file: Local file path for the cast file URL. It will be used instead of 'url' if exists. This is primarily used for testing / in development. :param bool reset: Reset read messages. All messages will be displayed again like the user runs this for the first time :param dict show_kwargs: kwargs to be passed to :meth:`CastReader.show_messages` """ try: if local_file and os.path.exists(local_file): cast = Cast.from_file(local_file, msg_filter) else: cast = Cast.from_url(url, msg_filter, cache_duration) if reset: CastReader.reset() reader = CastReader(cast) reader.show_messages(**show_kwargs) if allow_exit and cast.alert_exit: sys.exit(1) except Exception: if raises: raise
def check_message(url, msg_filter=None, cache_duration=None, allow_exit=False, raises=False, local_file=None, reset=False, **show_kwargs): """ Check remote url for new messages and display them. :param str url: Cast file URL to check :param callable msg_filter: Filter messages with callable that accepts message string and alert boolean (True for alert message). It should return the original or an updated message, or None if the message should be ignored. :param int cache_duration: Cache messages locally for number of seconds to avoid checking the URL too often. This is useful for response latency sensitive CLI to ensure user's experience isn't compromised. Alternatively, you may want to check messages in a seperate thread. :param bool allow_exit: Perform sys.exit(1) if cast requests it. :param bool raises: Raise exception for failed to download/parse cast file or such. Recommended to set this to False for production / in non-debug mode. :param str local_file: Local file path for the cast file URL. It will be used instead of 'url' if exists. This is primarily used for testing / in development. :param bool reset: Reset read messages. All messages will be displayed again like the user runs this for the first time :param dict show_kwargs: kwargs to be passed to :meth:`clicast.CastReader.show_messages` """ try: if local_file and os.path.exists(local_file): cast = Cast.from_content(local_file, msg_filter) else: cast = Cast.from_content(url, msg_filter, cache_duration) if reset: CastReader.reset() reader = CastReader(cast) reader.show_messages(**show_kwargs) if allow_exit and cast.alert_exit: sys.exit(1) except Exception: if raises: raise
def test_new_messages(self): cast = Cast.from_content(CAST_FILE) reader = CastReader(cast) assert reader.new_messages() == [ 'We found a big bad bug. Please try not to step on it!! Icky...\nNo worries. It will be fixed soon! :)', 'Version 0.1 has been released! If you upgrade, you will get:\n' '1) Cool feature 1\n' '2) Cool feature 2\n' 'So what are you waiting for? :)', 'Version 0.2 has been released! Upgrade today to get cool features.', 'There is a small bug over there, so watch out!', '[-f\\b] A bug that affects the -f option. (applies only if `clicast.filters.match_cli_args` filter is used)' ]
def test_del_msg(self): cast = Cast() cast.add_msg('Message 1') cast.add_msg('Message 2') cast.add_msg('Message Alert', alert_exit=True) cast.del_msg() assert cast.alert == 'Message Alert' assert cast.alert_exit assert [(m.key, m.message) for m in cast.messages] == [('2', 'Message 2')] del_count = cast.del_msg(100) assert del_count == 1 cast.add_msg('Message 3') cast.add_msg('Message 4') cast.add_msg('Message 5') cast.del_msg(2) cast.del_msg(alert=True) assert not cast.alert assert not cast.alert_exit assert [(m.key, m.message) for m in cast.messages] == [('5', 'Message 5')] cast_file = os.path.join(tempfile.gettempdir(), 'clicast.to_file_test.cast') try: cast.save(cast_file) cast = Cast.from_content(cast_file) cast.del_msg(100) cast.save(cast_file) cast = Cast.from_content(cast_file) cast.add_msg('Message 6') assert str(cast) == '[Messages]\n6: Message 6' finally: if os.path.exists(cast_file): os.unlink(cast_file)
def test_from_url(self): cast = Cast.from_url(CAST_URL) assert cast.messages
def test_filter(self): def msg_filter(msg, alert=False): if 'small bug' in msg: return msg cast = Cast.from_content(CAST_FILE, msg_filter) assert str(cast) == '[Messages]\n3: There is a small bug over there, so watch out!\n_limit: 5'
def test_set_msg_limit(self): cast = Cast() cast.set_msg_limit(2) for x in range(10): cast.add_msg('Message %d' % x) assert str(cast) == '[Messages]\n9: Message 8\n10: Message 9\n_limit: 2'