Merge branch 'Ram-Z-expandvars'

This commit is contained in:
Florian Bruhin 2015-02-18 16:17:01 +01:00
commit 1268cbecf3
5 changed files with 37 additions and 8 deletions

View File

@ -146,6 +146,7 @@ Contributors, sorted by the number of commits in descending order:
* Larry Hynes * Larry Hynes
* Thorsten Wißmann * Thorsten Wißmann
* Thiago Barroso Perrotta * Thiago Barroso Perrotta
* Samir Benmendil
* Matthias Lisin * Matthias Lisin
* Helen Sherwood-Taylor * Helen Sherwood-Taylor
* HalosGhost * HalosGhost

View File

@ -34,7 +34,7 @@
|<<ui-display-statusbar-messages,display-statusbar-messages>>|Whether to display javascript statusbar messages. |<<ui-display-statusbar-messages,display-statusbar-messages>>|Whether to display javascript statusbar messages.
|<<ui-zoom-text-only,zoom-text-only>>|Whether the zoom factor on a frame applies only to the text or to all content. |<<ui-zoom-text-only,zoom-text-only>>|Whether the zoom factor on a frame applies only to the text or to all content.
|<<ui-frame-flattening,frame-flattening>>|Whether to expand each subframe to its contents. |<<ui-frame-flattening,frame-flattening>>|Whether to expand each subframe to its contents.
|<<ui-user-stylesheet,user-stylesheet>>|User stylesheet to use (absolute filename or CSS string). |<<ui-user-stylesheet,user-stylesheet>>|User stylesheet to use (absolute filename or CSS string). Will expand environment variables.
|<<ui-css-media-type,css-media-type>>|Set the CSS media type. |<<ui-css-media-type,css-media-type>>|Set the CSS media type.
|<<ui-remove-finished-downloads,remove-finished-downloads>>|Whether to remove finished downloads automatically. |<<ui-remove-finished-downloads,remove-finished-downloads>>|Whether to remove finished downloads automatically.
|<<ui-hide-statusbar,hide-statusbar>>|Whether to hide the statusbar unless a message is shown. |<<ui-hide-statusbar,hide-statusbar>>|Whether to hide the statusbar unless a message is shown.
@ -105,7 +105,7 @@
[options="header",width="75%",cols="25%,75%"] [options="header",width="75%",cols="25%,75%"]
|============== |==============
|Setting|Description |Setting|Description
|<<storage-download-directory,download-directory>>|The directory to save downloads to. An empty value selects a sensible os-specific default. |<<storage-download-directory,download-directory>>|The directory to save downloads to. An empty value selects a sensible os-specific default. Will expand environment variables.
|<<storage-maximum-pages-in-cache,maximum-pages-in-cache>>|The maximum number of pages to hold in the memory page cache. |<<storage-maximum-pages-in-cache,maximum-pages-in-cache>>|The maximum number of pages to hold in the memory page cache.
|<<storage-object-cache-capacities,object-cache-capacities>>|The capacities for the memory cache for dead objects such as stylesheets or scripts. Syntax: cacheMinDeadCapacity, cacheMaxDead, totalCapacity. |<<storage-object-cache-capacities,object-cache-capacities>>|The capacities for the memory cache for dead objects such as stylesheets or scripts. Syntax: cacheMinDeadCapacity, cacheMaxDead, totalCapacity.
|<<storage-offline-storage-default-quota,offline-storage-default-quota>>|Default quota for new offline storage databases. |<<storage-offline-storage-default-quota,offline-storage-default-quota>>|Default quota for new offline storage databases.
@ -471,7 +471,7 @@ Default: +pass:[false]+
[[ui-user-stylesheet]] [[ui-user-stylesheet]]
=== user-stylesheet === user-stylesheet
User stylesheet to use (absolute filename or CSS string). User stylesheet to use (absolute filename or CSS string). Will expand environment variables.
Default: +pass:[::-webkit-scrollbar { width: 0px; height: 0px; }]+ Default: +pass:[::-webkit-scrollbar { width: 0px; height: 0px; }]+
@ -907,7 +907,7 @@ Settings related to cache and storage.
[[storage-download-directory]] [[storage-download-directory]]
=== download-directory === download-directory
The directory to save downloads to. An empty value selects a sensible os-specific default. The directory to save downloads to. An empty value selects a sensible os-specific default. Will expand environment variables.
Default: empty Default: empty

View File

@ -51,6 +51,10 @@ FIRST_COMMENT = r"""
# Interpolation looks like ${value} or ${section:value} and will be # Interpolation looks like ${value} or ${section:value} and will be
# replaced by the respective value. # replaced by the respective value.
# #
# Some settings will expand environment variables. Note that, since
# interpolation is run first, you will need to escape the $ char as
# described below.
#
# This is the default config, so if you want to remove anything from # This is the default config, so if you want to remove anything from
# here (as opposed to change/add), for example a keybinding, set it to # here (as opposed to change/add), for example a keybinding, set it to
# an empty value. # an empty value.
@ -235,7 +239,8 @@ DATA = collections.OrderedDict([
('user-stylesheet', ('user-stylesheet',
SettingValue(typ.UserStyleSheet(), SettingValue(typ.UserStyleSheet(),
'::-webkit-scrollbar { width: 0px; height: 0px; }'), '::-webkit-scrollbar { width: 0px; height: 0px; }'),
"User stylesheet to use (absolute filename or CSS string)."), "User stylesheet to use (absolute filename or CSS string). Will "
"expand environment variables."),
('css-media-type', ('css-media-type',
SettingValue(typ.String(none_ok=True), ''), SettingValue(typ.String(none_ok=True), ''),
@ -444,7 +449,7 @@ DATA = collections.OrderedDict([
('download-directory', ('download-directory',
SettingValue(typ.Directory(none_ok=True), ''), SettingValue(typ.Directory(none_ok=True), ''),
"The directory to save downloads to. An empty value selects a " "The directory to save downloads to. An empty value selects a "
"sensible os-specific default."), "sensible os-specific default. Will expand environment variables."),
('maximum-pages-in-cache', ('maximum-pages-in-cache',
SettingValue( SettingValue(

View File

@ -837,6 +837,7 @@ class Directory(BaseType):
return return
else: else:
raise configexc.ValidationError(value, "may not be empty!") raise configexc.ValidationError(value, "may not be empty!")
value = os.path.expandvars(value)
value = os.path.expanduser(value) value = os.path.expanduser(value)
if not os.path.isdir(value): if not os.path.isdir(value):
raise configexc.ValidationError(value, "must be a valid " raise configexc.ValidationError(value, "must be a valid "
@ -847,6 +848,7 @@ class Directory(BaseType):
def transform(self, value): def transform(self, value):
if not value: if not value:
return None return None
value = os.path.expandvars(value)
return os.path.expanduser(value) return os.path.expanduser(value)
@ -1132,6 +1134,7 @@ class UserStyleSheet(File):
return return
else: else:
raise configexc.ValidationError(value, "may not be empty!") raise configexc.ValidationError(value, "may not be empty!")
value = os.path.expandvars(value)
value = os.path.expanduser(value) value = os.path.expanduser(value)
if not os.path.isabs(value): if not os.path.isabs(value):
# probably a CSS, so we don't handle it as filename. # probably a CSS, so we don't handle it as filename.
@ -1147,7 +1150,8 @@ class UserStyleSheet(File):
raise configexc.ValidationError(value, "must be a valid file!") raise configexc.ValidationError(value, "must be a valid file!")
def transform(self, value): def transform(self, value):
path = os.path.expanduser(value) path = os.path.expandvars(value)
path = os.path.expanduser(path)
if not value: if not value:
return None return None
elif os.path.isabs(path): elif os.path.isabs(path):

View File

@ -26,7 +26,7 @@ import base64
from unittest import mock from unittest import mock
from qutebrowser.config import configtypes, configexc from qutebrowser.config import configtypes, configexc
from qutebrowser.test import stubs from qutebrowser.test import stubs, helpers
from qutebrowser.utils import debug, utils from qutebrowser.utils import debug, utils
from PyQt5.QtCore import QUrl from PyQt5.QtCore import QUrl
@ -1377,14 +1377,27 @@ class DirectoryTests(unittest.TestCase):
def test_validate_expanduser(self, os_path): def test_validate_expanduser(self, os_path):
"""Test if validate expands the user correctly.""" """Test if validate expands the user correctly."""
os_path.expandvars.side_effect = lambda x: x
os_path.expanduser.side_effect = lambda x: x.replace('~', '/home/foo') os_path.expanduser.side_effect = lambda x: x.replace('~', '/home/foo')
os_path.isdir.side_effect = lambda path: path == '/home/foo/foobar' os_path.isdir.side_effect = lambda path: path == '/home/foo/foobar'
os_path.isabs.return_value = True os_path.isabs.return_value = True
self.t.validate('~/foobar') self.t.validate('~/foobar')
os_path.expanduser.assert_called_once_with('~/foobar') os_path.expanduser.assert_called_once_with('~/foobar')
def test_validate_expandvars(self, os_path):
"""Test if validate expands the user correctly."""
os_path.expandvars.side_effect = lambda x: x.replace('$BAR',
'/home/foo/bar')
os_path.expanduser.side_effect = lambda x: x
os_path.isdir.side_effect = lambda path: path == '/home/foo/bar/foobar'
os_path.isabs.return_value = True
with helpers.environ_set_temp('bar', '/home/foo/bar'):
self.t.validate('$BAR/foobar')
os_path.expandvars.assert_called_once_with('$BAR/foobar')
def test_transform(self, os_path): def test_transform(self, os_path):
"""Test transform.""" """Test transform."""
os_path.expandvars.side_effect = lambda x: x
os_path.expanduser.side_effect = lambda x: x.replace('~', '/home/foo') os_path.expanduser.side_effect = lambda x: x.replace('~', '/home/foo')
self.assertEqual(self.t.transform('~/foobar'), '/home/foo/foobar') self.assertEqual(self.t.transform('~/foobar'), '/home/foo/foobar')
os_path.expanduser.assert_called_once_with('~/foobar') os_path.expanduser.assert_called_once_with('~/foobar')
@ -1780,6 +1793,12 @@ class UserStyleSheetTests(unittest.TestCase):
path = os.path.join(os.path.sep, 'foo', 'bar') path = os.path.join(os.path.sep, 'foo', 'bar')
self.assertEqual(self.t.transform(path), QUrl("file:///foo/bar")) self.assertEqual(self.t.transform(path), QUrl("file:///foo/bar"))
def test_transform_file_expandvars(self):
"""Test transform with a filename (expandvars)."""
with helpers.environ_set_temp('FOO', 'foo'):
path = os.path.join(os.path.sep, '$FOO', 'bar')
self.assertEqual(self.t.transform(path), QUrl("file:///foo/bar"))
def test_transform_base64(self): def test_transform_base64(self):
"""Test transform with a data string.""" """Test transform with a data string."""
b64 = base64.b64encode(b"test").decode('ascii') b64 = base64.b64encode(b"test").decode('ascii')