예제 #1
0
파일: test_config.py 프로젝트: zippyy/rtv
def test_config_get_args():
    "Ensure that command line arguments are parsed properly"

    args = [
        'rtv', 'https://reddit.com/permalink •', '-s', 'cfb', '--log',
        'logfile.log', '--config', 'configfile.cfg', '--ascii', '--monochrome',
        '--non-persistent', '--clear-auth', '--copy-config', '--enable-media',
        '--theme', 'molokai', '--list-themes', '--no-flash'
    ]

    with mock.patch('sys.argv', ['rtv']):
        config_dict = Config.get_args()
        config = Config(**config_dict)
        assert config.config == {}

    with mock.patch('sys.argv', args):
        config_dict = Config.get_args()

        config = Config(**config_dict)
        assert config['ascii'] is True
        assert config['monochrome'] is True
        assert config['subreddit'] == 'cfb'
        assert config['log'] == 'logfile.log'
        assert config['ascii'] is True
        assert config['persistent'] is False
        assert config['clear_auth'] is True
        assert config['link'] == 'https://reddit.com/permalink •'
        assert config['config'] == 'configfile.cfg'
        assert config['copy_config'] is True
        assert config['enable_media'] is True
        assert config['theme'] == 'molokai'
        assert config['list_themes'] is True
        assert config['flash'] is False
예제 #2
0
파일: test_config.py 프로젝트: rpesche/rtv
def test_config_get_args():
    "Ensure that command line arguments are parsed properly"

    args = ['rtv',
            '-s', 'cfb',
            '-l', 'https://reddit.com/permalink •',
            '--log', 'logfile.log',
            '--config', 'configfile.cfg',
            '--ascii',
            '--non-persistent',
            '--clear-auth',
            '--copy-config']

    with mock.patch('sys.argv', ['rtv']):
        config_dict = Config.get_args()
        config = Config(**config_dict)
        assert config.config == {}

    with mock.patch('sys.argv', args):
        config_dict = Config.get_args()

        config = Config(**config_dict)
        assert config['ascii'] is True
        assert config['subreddit'] == 'cfb'
        assert config['log'] == 'logfile.log'
        assert config['ascii'] is True
        assert config['persistent'] is False
        assert config['clear_auth'] is True
        assert config['link'] == 'https://reddit.com/permalink •'
        assert config['config'] == 'configfile.cfg'
        assert config['copy_config'] is True
예제 #3
0
파일: test_config.py 프로젝트: 5225225/rtv
def test_config_history():
    "Ensure that the history can be loaded and saved"

    # Should still be able to load if the file doesn't exist
    config = Config(history_file='/fake_path/fake_file')
    config.load_history()
    assert len(config.history) == 0

    with NamedTemporaryFile(delete=False) as fp:
        config = Config(history_file=fp.name, history_size=3)

        config.history.add('link1')
        config.history.add('link2')
        config.history.add('link3')
        config.history.add('link4')
        assert len(config.history) == 4

        # Saving should only write the 3 most recent links
        config.save_history()
        config.load_history()
        assert len(config.history) == 3
        assert 'link1' not in config.history
        assert 'link4' in config.history

        config.delete_history()
        assert len(config.history) == 0
        assert not os.path.exists(fp.name)
예제 #4
0
def test_config_interface():
    "Test setting and removing values"

    config = Config(ascii=True)
    assert config['ascii'] is True
    config['ascii'] = False
    assert config['ascii'] is False
    config['ascii'] = True
    del config['ascii']
    assert config['ascii'] is False
    config.update(subreddit='cfb', new_value=2.0)
    assert config['subreddit'] == 'cfb'
    assert config['new_value'] == 2.0
예제 #5
0
파일: test_config.py 프로젝트: zippyy/rtv
def test_config_link_deprecated():

    # Should still be able to specify the link using the old "-l"
    args = ['rtv', '-l', 'https://reddit.com/option']
    with mock.patch('sys.argv', args):
        config_dict = Config.get_args()
        config = Config(**config_dict)
        assert config['link'] == 'https://reddit.com/option'

    # But the positional argument should take preference
    args = ['rtv', 'https://reddit.com/arg', '-l', 'https://reddit.com/option']
    with mock.patch('sys.argv', args):
        config_dict = Config.get_args()
        config = Config(**config_dict)
        assert config['link'] == 'https://reddit.com/arg'
예제 #6
0
파일: test_config.py 프로젝트: zippyy/rtv
def test_config_interface():
    "Test setting and removing values"

    config = Config(ascii=True)
    assert config['ascii'] is True
    config['ascii'] = False
    assert config['ascii'] is False
    config['ascii'] = None
    assert config['ascii'] is None
    del config['ascii']
    assert config['ascii'] is False

    config.update(subreddit='cfb', new_value=2.0)
    assert config['subreddit'] == 'cfb'
    assert config['new_value'] == 2.0

    assert config['link'] is None
    assert config['log'] is None
예제 #7
0
파일: test_config.py 프로젝트: 5225225/rtv
def test_config_from_file():
    "Ensure that config file arguments are parsed properly"

    args = {
        'ascii': True,
        'monochrome': True,
        'persistent': False,
        'clear_auth': True,
        'log': 'logfile.log',
        'link': 'https://reddit.com/permalink •',
        'subreddit': 'cfb',
        'enable_media': True}

    bindings = {
        'REFRESH': 'r, <KEY_F5>',
        'UPVOTE': ''}

    with NamedTemporaryFile(suffix='.cfg') as fp:

        fargs, fbindings = Config.get_file(filename=fp.name)
        config = Config(**fargs)
        config.keymap.set_bindings(fbindings)
        assert config.config == {}
        assert config.keymap._keymap == {}

        # [rtv]
        rows = ['{0}={1}'.format(key, val) for key, val in args.items()]
        data = '\n'.join(['[rtv]'] + rows)
        fp.write(codecs.encode(data, 'utf-8'))

        # [bindings]
        rows = ['{0}={1}'.format(key, val) for key, val in bindings.items()]
        data = '\n'.join(['', '', '[bindings]'] + rows)
        fp.write(codecs.encode(data, 'utf-8'))

        fp.flush()
        fargs, fbindings = Config.get_file(filename=fp.name)
        config.update(**fargs)
        config.keymap.set_bindings(fbindings)
        assert config.config == args
        assert config.keymap.get('REFRESH') == ['r', '<KEY_F5>']
        assert config.keymap.get('UPVOTE') == ['']
예제 #8
0
def config():
    conf = Config()
    with mock.patch.object(conf, 'save_history'),          \
            mock.patch.object(conf, 'delete_history'),     \
            mock.patch.object(conf, 'save_refresh_token'), \
            mock.patch.object(conf, 'delete_refresh_token'):
 
        def delete_refresh_token():
            # Skip the os.remove
            conf.refresh_token = None
        conf.delete_refresh_token.side_effect = delete_refresh_token

        yield conf
예제 #9
0
def test_config_from_file():
    "Ensure that config file arguments are parsed properly"

    args = {
        'ascii': True,
        'persistent': False,
        'clear_auth': True,
        'log': 'logfile.log',
        'link': 'https://reddit.com/permalink •',
        'subreddit': 'cfb'}

    with NamedTemporaryFile(suffix='.cfg') as fp:
        config = Config(config_file=fp.name)
        config.from_file()
        assert config.config == {}

        rows = ['{0}={1}'.format(key, val) for key, val in args.items()]
        data = '\n'.join(['[rtv]'] + rows)
        fp.write(codecs.encode(data, 'utf-8'))
        fp.flush()
        config.from_file()
        assert config.config == args
예제 #10
0
def test_config_history():
    "Ensure that the history can be loaded and saved"

    with NamedTemporaryFile(delete=False) as fp:
        config = Config(history_file=fp.name, history_size=3)

        config.history.add('link1')
        config.history.add('link2')
        config.history.add('link3')
        config.history.add('link4')
        assert len(config.history) == 4

        # Saving should only write the 3 most recent links
        config.save_history()
        config.load_history()
        assert len(config.history) == 3
        assert 'link1' not in config.history
        assert 'link4' in config.history

        config.delete_history()
        assert len(config.history) == 0
        assert not os.path.exists(fp.name)
예제 #11
0
def test_config_from_file():
    """Ensure that config file arguments are parsed properly"""

    args = {
        'ascii': True,
        'monochrome': True,
        'persistent': False,
        'clear_auth': True,
        'log': 'logfile.log',
        'link': 'https://reddit.com/permalink •',
        'subreddit': 'cfb',
        'enable_media': True,
        'max_comment_cols': 150,
        'max_pager_cols': 120,
        'hide_username': True,
        'theme': 'molokai',
        'flash': True,
        'autologin': True,
    }

    bindings = {'REFRESH': 'r, <KEY_F5>', 'UPVOTE': ''}

    with NamedTemporaryFile(suffix='.cfg') as fp:

        fargs, fbindings = Config.get_file(filename=fp.name)
        config = Config(**fargs)
        default_keymap = config.keymap._keymap.copy()
        config.keymap.set_bindings(fbindings)
        assert config.config == {}
        assert config.keymap._keymap == default_keymap

        # [rtv]
        rows = ['{0}={1}'.format(key, val) for key, val in args.items()]
        data = '\n'.join(['[rtv]'] + rows)
        fp.write(codecs.encode(data, 'utf-8'))

        # [bindings]
        rows = ['{0}={1}'.format(key, val) for key, val in bindings.items()]
        data = '\n'.join(['', '', '[bindings]'] + rows)
        fp.write(codecs.encode(data, 'utf-8'))

        fp.flush()
        fargs, fbindings = Config.get_file(filename=fp.name)
        config.update(**fargs)
        config.keymap.set_bindings(fbindings)
        assert config.config == args
        assert config.keymap.get('REFRESH') == ['r', '<KEY_F5>']
        assert config.keymap.get('UPVOTE') == ['']
예제 #12
0
파일: test_config.py 프로젝트: Demorde/rtv
def test_config_from_file():
    "Ensure that config file arguments are parsed properly"

    args = {
        'ascii': True,
        'persistent': False,
        'clear_auth': True,
        'log': 'logfile.log',
        'link': 'https://reddit.com/permalink •',
        'subreddit': 'cfb'
    }

    bindings = {'REFRESH': 'r, <KEY_F5>', 'UPVOTE': ''}

    with NamedTemporaryFile(suffix='.cfg') as fp:

        fargs, fbindings = Config.get_file(filename=fp.name)
        config = Config(**fargs)
        config.keymap.set_bindings(fbindings)
        assert config.config == {}
        assert config.keymap._keymap == {}

        # [rtv]
        rows = ['{0}={1}'.format(key, val) for key, val in args.items()]
        data = '\n'.join(['[rtv]'] + rows)
        fp.write(codecs.encode(data, 'utf-8'))

        # [bindings]
        rows = ['{0}={1}'.format(key, val) for key, val in bindings.items()]
        data = '\n'.join(['', '', '[bindings]'] + rows)
        fp.write(codecs.encode(data, 'utf-8'))

        fp.flush()
        fargs, fbindings = Config.get_file(filename=fp.name)
        config.update(**fargs)
        config.keymap.set_bindings(fbindings)
        assert config.config == args
        assert config.keymap.get('REFRESH') == ['r', '<KEY_F5>']
        assert config.keymap.get('UPVOTE') == ['']
예제 #13
0
def main():

    locale.setlocale(locale.LC_ALL, '')

    if len(sys.argv) > 1:
        theme = Theme.from_name(sys.argv[1])
    else:
        theme = Theme()

    vcr = initialize_vcr()
    with vcr.use_cassette('demo_theme.yaml') as cassette, \
            curses_session() as stdscr:

        config = Config()
        if vcr.record_mode == 'once':
            config.load_refresh_token()
        else:
            config.refresh_token = 'mock_refresh_token'

        reddit = praw.Reddit(user_agent='RTV Theme Demo',
                             decode_html_entities=False,
                             disable_update_check=True)
        reddit.config.api_request_delay = 0

        config.history.add('https://api.reddit.com/comments/6llvsl/_/djutc3s')
        config.history.add('http://i.imgur.com/Z9iGKWv.gifv')
        config.history.add('https://www.reddit.com/r/Python/comments/6302cj/rpython_official_job_board/')

        term = Terminal(stdscr, config)
        term.set_theme()
        oauth = OAuthHelper(reddit, term, config)
        oauth.authorize()

        theme_list = ThemeList()

        while True:
            term = Terminal(stdscr, config)
            term.set_theme(theme)
            threads = draw_screen(stdscr, reddit, config, theme, oauth)

            try:
                ch = term.show_notification(theme.display_string)
            except KeyboardInterrupt:
                ch = Terminal.ESCAPE

            for thread, term in threads:
                term.pause_getch = False
                thread.join()

            if vcr.record_mode == 'once':
                break
            else:
                cassette.play_counts = Counter()

            theme_list.reload()

            if ch == curses.KEY_RIGHT:
                theme = theme_list.next(theme)
            elif ch == curses.KEY_LEFT:
                theme = theme_list.previous(theme)
            elif ch == Terminal.ESCAPE:
                break
            else:
                # Force the theme to reload
                theme = theme_list.next(theme)
                theme = theme_list.previous(theme)
예제 #14
0
def test_config_refresh_token():
    "Ensure that the refresh token can be loaded, saved, and removed"

    with NamedTemporaryFile(delete=False) as fp:
        config = Config(token_file=fp.name)

        # Write a new token to the file
        config.refresh_token = 'secret_value'
        config.save_refresh_token()

        # Load a valid token from the file
        config.refresh_token = None
        config.load_refresh_token()
        assert config.refresh_token == 'secret_value'

        # Discard the token and delete the file
        config.delete_refresh_token()
        assert config.refresh_token is None
        assert not os.path.exists(fp.name)

        # Saving should create a new file
        config.refresh_token = 'new_value'
        config.save_refresh_token()

        # Which we can read back to verify
        config.refresh_token = None
        config.load_refresh_token()
        assert config.refresh_token == 'new_value'

        # And delete again to clean up
        config.delete_refresh_token()
        assert not os.path.exists(fp.name)

        # Loading from the non-existent file should return None
        config.refresh_token = 'secret_value'
        config.load_refresh_token()
        assert config.refresh_token is None
예제 #15
0
파일: conftest.py 프로젝트: methos2016/rtv
def config():
    with patch('rtv.config.Config.save_refresh_token'), \
            patch('rtv.config.Config.save_history'):
        yield Config()
"""
Initialize an authenticated instance of PRAW to interact with.

$ python -i initialize_session.py
"""
from rtv.docs import AGENT
from rtv.packages import praw
from rtv.content import RequestHeaderRateLimiter
from rtv.config import Config

config = Config()
config.load_refresh_token()

reddit = praw.Reddit(
    user_agent=AGENT.format(version='test_session'),
    decode_html_entities=False,
    disable_update_check=True,
    timeout=10,  # 10 second request timeout
    handler=RequestHeaderRateLimiter())

reddit.set_oauth_app_info(config['oauth_client_id'],
                          config['oauth_client_secret'],
                          config['oauth_redirect_uri'])
reddit.refresh_access_information(config.refresh_token)

inbox = reddit.get_inbox()
items = [next(inbox) for _ in range(20)]
pass
예제 #17
0
파일: test_config.py 프로젝트: zippyy/rtv
def test_config_refresh_token():
    "Ensure that the refresh token can be loaded, saved, and removed"

    with NamedTemporaryFile(delete=False) as fp:
        config = Config(token_file=fp.name)

        # Write a new token to the file
        config.refresh_token = 'secret_value'
        config.save_refresh_token()

        # Load a valid token from the file
        config.refresh_token = None
        config.load_refresh_token()
        assert config.refresh_token == 'secret_value'

        # Discard the token and delete the file
        config.delete_refresh_token()
        assert config.refresh_token is None
        assert not os.path.exists(fp.name)

        # Saving should create a new file
        config.refresh_token = 'new_value'
        config.save_refresh_token()

        # Which we can read back to verify
        config.refresh_token = None
        config.load_refresh_token()
        assert config.refresh_token == 'new_value'

        # And delete again to clean up
        config.delete_refresh_token()
        assert not os.path.exists(fp.name)

        # Loading from the non-existent file should return None
        config.refresh_token = 'secret_value'
        config.load_refresh_token()
        assert config.refresh_token is None