Added --copy-mailcap command
This commit is contained in:
@@ -4,5 +4,4 @@ include CONTRIBUTORS.rst
|
|||||||
include README.rst
|
include README.rst
|
||||||
include LICENSE
|
include LICENSE
|
||||||
include rtv.1
|
include rtv.1
|
||||||
include rtv/rtv.cfg
|
|
||||||
include rtv/templates/*
|
include rtv/templates/*
|
||||||
|
|||||||
@@ -100,7 +100,7 @@ Auto-generate the config file by running
|
|||||||
|
|
||||||
$ rtv --copy-config
|
$ rtv --copy-config
|
||||||
|
|
||||||
See the `default config <https://github.com/michael-lazar/rtv/blob/master/rtv/rtv.cfg>`_ for the full list of settings.
|
See the `default config <https://github.com/michael-lazar/rtv/blob/master/rtv/templates/rtv.cfg>`_ for the full list of settings.
|
||||||
|
|
||||||
------
|
------
|
||||||
Editor
|
Editor
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import praw
|
|||||||
import tornado
|
import tornado
|
||||||
|
|
||||||
from . import docs
|
from . import docs
|
||||||
from .config import Config, copy_default_config
|
from .config import Config, copy_default_config, copy_default_mailcap
|
||||||
from .oauth import OAuthHelper
|
from .oauth import OAuthHelper
|
||||||
from .terminal import Terminal
|
from .terminal import Terminal
|
||||||
from .objects import curses_session, Color
|
from .objects import curses_session, Color
|
||||||
@@ -59,6 +59,10 @@ def main():
|
|||||||
copy_default_config()
|
copy_default_config()
|
||||||
return
|
return
|
||||||
|
|
||||||
|
if config['copy_mailcap']:
|
||||||
|
copy_default_mailcap()
|
||||||
|
return
|
||||||
|
|
||||||
# Load the browsing history from previous sessions
|
# Load the browsing history from previous sessions
|
||||||
config.load_history()
|
config.load_history()
|
||||||
|
|
||||||
|
|||||||
@@ -16,9 +16,11 @@ from .objects import KeyMap
|
|||||||
PACKAGE = os.path.dirname(__file__)
|
PACKAGE = os.path.dirname(__file__)
|
||||||
HOME = os.path.expanduser('~')
|
HOME = os.path.expanduser('~')
|
||||||
TEMPLATE = os.path.join(PACKAGE, 'templates')
|
TEMPLATE = os.path.join(PACKAGE, 'templates')
|
||||||
DEFAULT_CONFIG = os.path.join(PACKAGE, 'rtv.cfg')
|
DEFAULT_CONFIG = os.path.join(TEMPLATE, 'rtv.cfg')
|
||||||
|
DEFAULT_MAILCAP = os.path.join(TEMPLATE 'mailcap')
|
||||||
XDG_HOME = os.getenv('XDG_CONFIG_HOME', os.path.join(HOME, '.config'))
|
XDG_HOME = os.getenv('XDG_CONFIG_HOME', os.path.join(HOME, '.config'))
|
||||||
CONFIG = os.path.join(XDG_HOME, 'rtv', 'rtv.cfg')
|
CONFIG = os.path.join(XDG_HOME, 'rtv', 'rtv.cfg')
|
||||||
|
MAILCAP = os.path.join(XDG_HOME, '.mailcap')
|
||||||
TOKEN = os.path.join(XDG_HOME, 'rtv', 'refresh-token')
|
TOKEN = os.path.join(XDG_HOME, 'rtv', 'refresh-token')
|
||||||
HISTORY = os.path.join(XDG_HOME, 'rtv', 'history.log')
|
HISTORY = os.path.join(XDG_HOME, 'rtv', 'history.log')
|
||||||
|
|
||||||
@@ -59,33 +61,50 @@ def build_parser():
|
|||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--copy-config', dest='copy_config', action='store_const', const=True,
|
'--copy-config', dest='copy_config', action='store_const', const=True,
|
||||||
help='Copy the default configuration to {HOME}/.config/rtv/rtv.cfg')
|
help='Copy the default configuration to {HOME}/.config/rtv/rtv.cfg')
|
||||||
|
parser.add_argument(
|
||||||
|
'--copy-mailcap', dest='copy_mailcap', action='store_const', const=True,
|
||||||
|
help='Copy an example mailcap configuration to {HOME}/.mailcap')
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--enable-media', dest='enable_media', action='store_const', const=True,
|
'--enable-media', dest='enable_media', action='store_const', const=True,
|
||||||
help='Open external links using programs defined in the mailcap config')
|
help='Open external links using programs defined in the mailcap config')
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
|
|
||||||
|
def copy_default_mailcap(filename=MAILCAP):
|
||||||
|
"""
|
||||||
|
Copy the example mailcap configuration to the specified file.
|
||||||
|
"""
|
||||||
|
return _copy_settings_file(DEFAULT_MAILCAP, filename, 'mailcap')
|
||||||
|
|
||||||
|
|
||||||
def copy_default_config(filename=CONFIG):
|
def copy_default_config(filename=CONFIG):
|
||||||
"""
|
"""
|
||||||
Copy the default configuration file to the user's {HOME}/.config/rtv
|
Copy the default rtv user configuration to the specified file.
|
||||||
|
"""
|
||||||
|
return _copy_settings_file(DEFAULT_CONFIG, filename, 'config')
|
||||||
|
|
||||||
|
|
||||||
|
def _copy_settings_file(source, destination, name):
|
||||||
|
"""
|
||||||
|
Copy a file from the repo to the user's home directory.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if os.path.exists(filename):
|
if os.path.exists(destination):
|
||||||
try:
|
try:
|
||||||
ch = six.moves.input(
|
ch = six.moves.input(
|
||||||
'File %s already exists, overwrite? y/[n]):' % filename)
|
'File %s already exists, overwrite? y/[n]):' % destination)
|
||||||
if ch not in ('Y', 'y'):
|
if ch not in ('Y', 'y'):
|
||||||
return
|
return
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
return
|
return
|
||||||
|
|
||||||
filepath = os.path.dirname(filename)
|
filepath = os.path.dirname(destination)
|
||||||
if not os.path.exists(filepath):
|
if not os.path.exists(filepath):
|
||||||
os.makedirs(filepath)
|
os.makedirs(filepath)
|
||||||
|
|
||||||
print('Copying default settings to %s' % filename)
|
print('Copying default %s to %s' % (name, destination))
|
||||||
shutil.copy(DEFAULT_CONFIG, filename)
|
shutil.copy(source, destination)
|
||||||
os.chmod(filename, 0o664)
|
os.chmod(destination, 0o664)
|
||||||
|
|
||||||
|
|
||||||
class OrderedSet(object):
|
class OrderedSet(object):
|
||||||
@@ -243,4 +262,4 @@ class Config(object):
|
|||||||
|
|
||||||
filepath = os.path.dirname(filename)
|
filepath = os.path.dirname(filename)
|
||||||
if not os.path.exists(filepath):
|
if not os.path.exists(filepath):
|
||||||
os.makedirs(filepath)
|
os.makedirs(filepath)
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
# Example mailcap file for Reddit Terminal Viewer
|
# Example mailcap file for Reddit Terminal Viewer
|
||||||
#
|
|
||||||
# Copy the contents of this file to {HOME}/.mailcap, or point to using $MAILCAPS
|
# Copy the contents of this file to {HOME}/.mailcap, or point to using $MAILCAPS
|
||||||
# Then launch RTV using the --enable-media flag. All shell commands defined in
|
# Then launch RTV using the --enable-media flag. All shell commands defined in
|
||||||
# this file depend on external programs that must be installed on your system.
|
# this file depend on external programs that must be installed on your system.
|
||||||
@@ -23,12 +23,12 @@ image/*; feh -g 640x480 '%s'; test=test -n "$DISPLAY"
|
|||||||
|
|
||||||
# Youtube videos are assigned a custom mime-type, which can be streamed with
|
# Youtube videos are assigned a custom mime-type, which can be streamed with
|
||||||
# vlc or youtube-dl.
|
# vlc or youtube-dl.
|
||||||
video/x-youtube; vlc '%s'; test=test -n "$DISPLAY"
|
video/x-youtube; vlc '%s' --width 640 --height 480; test=test -n "$DISPLAY"
|
||||||
video/x-youtube; youtube-dl -q -o - '%s' | mpv -; test=test -n "$DISPLAY"
|
video/x-youtube; youtube-dl -q -o - '%s' | mpv - --autofit 640x480; test=test -n "$DISPLAY"
|
||||||
|
|
||||||
# Mpv is a simple and effective video streamer
|
# Mpv is a simple and effective video streamer
|
||||||
video/webm; mpv '%s'; test=test -n "$DISPLAY"
|
video/webm; mpv '%s' --autofit 640x480; test=test -n "$DISPLAY"
|
||||||
video/*; mpv '%s'; test=test -n "$DISPLAY"
|
video/*; mpv '%s' --autofit 640x480; test=test -n "$DISPLAY"
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
# Commands below this point will attempt to display media directly in the
|
# Commands below this point will attempt to display media directly in the
|
||||||
@@ -36,14 +36,20 @@ video/*; mpv '%s'; test=test -n "$DISPLAY"
|
|||||||
###############################################################################
|
###############################################################################
|
||||||
|
|
||||||
# View true images in the terminal, supported by rxvt-unicode, xterm and st
|
# View true images in the terminal, supported by rxvt-unicode, xterm and st
|
||||||
|
# Requires the w3m-img package
|
||||||
# image/*; w3m -o 'ext_image_viewer=off' '%s'; needsterminal
|
# image/*; w3m -o 'ext_image_viewer=off' '%s'; needsterminal
|
||||||
|
|
||||||
# Ascii images: download, convert to jpg, and print with img2txt
|
|
||||||
# Don't have a solution for albums yet
|
# Don't have a solution for albums yet
|
||||||
image/x-imgur-album; echo
|
image/x-imgur-album; echo
|
||||||
|
|
||||||
|
# 256 color images using half-width unicode characters
|
||||||
|
# Much higher quality that img2txt, but must be built from source
|
||||||
|
# https://github.com/rossy/img2xterm
|
||||||
|
image/*; curl -s '%s' | convert -resize 80x80 - jpg:/tmp/rtv.jpg && img2xterm /tmp/rtv.jpg; needsterminal; copiousoutput
|
||||||
|
|
||||||
|
# Display images in classic ascii using img2txt and lib-caca
|
||||||
image/*; curl -s '%s' | convert - jpg:/tmp/rtv.jpg && img2txt -f utf8 /tmp/rtv.jpg; needsterminal; copiousoutput
|
image/*; curl -s '%s' | convert - jpg:/tmp/rtv.jpg && img2txt -f utf8 /tmp/rtv.jpg; needsterminal; copiousoutput
|
||||||
|
|
||||||
# Ascii videos
|
# Ascii videos
|
||||||
video/x-youtube; youtube-dl -q -o - '%s' | mplayer -cache 8192 -vo caca -quiet -; needsterminal
|
video/x-youtube; youtube-dl -q -o - '%s' | mplayer -cache 8192 -vo caca -quiet -; needsterminal
|
||||||
video/*; wget '%s' -O - | mplayer -cache 8192 -vo caca -quiet -; needsterminal
|
video/*; wget '%s' -O - | mplayer -cache 8192 -vo caca -quiet -; needsterminal
|
||||||
|
|
||||||
2
setup.py
2
setup.py
@@ -21,7 +21,7 @@ setuptools.setup(
|
|||||||
license='MIT',
|
license='MIT',
|
||||||
keywords='reddit terminal praw curses',
|
keywords='reddit terminal praw curses',
|
||||||
packages=['rtv'],
|
packages=['rtv'],
|
||||||
package_data={'rtv': ['templates/*', 'rtv.cfg']},
|
package_data={'rtv': ['templates/*']},
|
||||||
data_files=[("share/man/man1", ["rtv.1"])],
|
data_files=[("share/man/man1", ["rtv.1"])],
|
||||||
extras_require={
|
extras_require={
|
||||||
':python_version=="2.6" or python_version=="2.7"': ['futures']},
|
':python_version=="2.6" or python_version=="2.7"': ['futures']},
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import os
|
|||||||
import codecs
|
import codecs
|
||||||
from tempfile import NamedTemporaryFile
|
from tempfile import NamedTemporaryFile
|
||||||
|
|
||||||
from rtv.config import Config, copy_default_config, DEFAULT_CONFIG
|
from rtv.config import Config, copy_default_config, copy_default_mailcap
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
@@ -20,6 +20,19 @@ def test_copy_default_config():
|
|||||||
with mock.patch('rtv.config.six.moves.input', return_value='y'):
|
with mock.patch('rtv.config.six.moves.input', return_value='y'):
|
||||||
copy_default_config(fp.name)
|
copy_default_config(fp.name)
|
||||||
assert fp.read()
|
assert fp.read()
|
||||||
|
# Check that the permissions were changed
|
||||||
|
permissions = os.stat(fp.name).st_mode & 0o777
|
||||||
|
assert permissions == 0o664
|
||||||
|
|
||||||
|
|
||||||
|
def test_copy_default_mailcap():
|
||||||
|
"Make sure the example mailcap file was included in the package"
|
||||||
|
|
||||||
|
with NamedTemporaryFile() as fp:
|
||||||
|
with mock.patch('rtv.config.six.moves.input', return_value='y'):
|
||||||
|
copy_default_mailcap(fp.name)
|
||||||
|
assert fp.read()
|
||||||
|
# Check that the permissions were changed
|
||||||
permissions = os.stat(fp.name).st_mode & 0o777
|
permissions = os.stat(fp.name).st_mode & 0o777
|
||||||
assert permissions == 0o664
|
assert permissions == 0o664
|
||||||
|
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ URLS = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(ARGS, URLS.values(), ids=URLS.keys())
|
@pytest.mark.parametrize(ARGS, URLS.values(), ids=list(URLS))
|
||||||
def test_parser(url, modified_url, mime_type, reddit):
|
def test_parser(url, modified_url, mime_type, reddit):
|
||||||
# Include the reddit fixture so the cassettes get generated
|
# Include the reddit fixture so the cassettes get generated
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user