Skip to content
This repository has been archived by the owner on Aug 20, 2018. It is now read-only.

Commit

Permalink
Bug 838273 - mozprofile should have the ability to read (at least) pr…
Browse files Browse the repository at this point in the history
…eferences from http://, https://;r=ahal
  • Loading branch information
Jeff Hammel committed May 13, 2013
1 parent b1e7520 commit 4a38d2c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
9 changes: 5 additions & 4 deletions mozprofile/mozprofile/prefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

__all__ = ('PreferencesReadError', 'Preferences')

import mozfile
import os
import re
import tokenize
Expand Down Expand Up @@ -91,7 +92,7 @@ def read(cls, path):
# section of INI file
path, section = path.rsplit(':', 1)

if not os.path.exists(path):
if not os.path.exists(path) and not mozfile.is_url(path):
raise PreferencesReadError("'%s' does not exist" % path)

if section:
Expand Down Expand Up @@ -120,7 +121,7 @@ def read_ini(cls, path, section=None):
"""read preferences from an .ini file"""

parser = ConfigParser()
parser.read(path)
parser.readfp(mozfile.load(path))

if section:
if section not in parser.sections():
Expand All @@ -136,7 +137,7 @@ def read_ini(cls, path, section=None):
def read_json(cls, path):
"""read preferences from a JSON blob"""

prefs = json.loads(file(path).read())
prefs = json.loads(mozfile.load(path).read())

if type(prefs) not in [list, dict]:
raise PreferencesReadError("Malformed preferences: %s" % path)
Expand All @@ -161,7 +162,7 @@ def read_prefs(cls, path, pref_setter='user_pref'):
comment = re.compile('/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/', re.MULTILINE)

marker = '##//' # magical marker
lines = [i.strip() for i in file(path).readlines() if i.strip()]
lines = [i.strip() for i in mozfile.load(path).readlines() if i.strip()]
_lines = []
for line in lines:
if line.startswith(('#', '//')):
Expand Down
6 changes: 4 additions & 2 deletions mozprofile/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
# we only support python 2 right now
assert sys.version_info[0] == 2

deps = ["ManifestDestiny >= 0.5.4"]
deps = ["ManifestDestiny >= 0.5.4",
"mozfile >= 0.6"]
# version-dependent dependencies
try:
import json
Expand All @@ -37,12 +38,13 @@
keywords='mozilla',
author='Mozilla Automation and Tools team',
author_email='tools@lists.mozilla.org',
url='https://wiki.mozilla.org/Auto-tools/Projects/MozBase',
url='https://wiki.mozilla.org/Auto-tools/Projects/Mozbase',
license='MPL 2.0',
packages=['mozprofile'],
include_package_data=True,
zip_safe=False,
install_requires=deps,
tests_require=['mozhttpd'],
entry_points="""
# -*- Entry points: -*-
[console_scripts]
Expand Down
34 changes: 29 additions & 5 deletions mozprofile/tests/test_preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.

import mozhttpd
import os
import shutil
import tempfile
Expand All @@ -17,6 +18,12 @@
class PreferencesTest(unittest.TestCase):
"""test mozprofile preference handling"""

# preferences from files/prefs_with_comments.js
_prefs_with_comments = {'browser.startup.homepage': 'http://planet.mozilla.org',
'zoom.minPercent': 30,
'zoom.maxPercent': 300,
'webgl.verbose': 'false'}

def run_command(self, *args):
"""
invokes mozprofile command line via the CLI factory
Expand Down Expand Up @@ -246,13 +253,30 @@ def test_prefs_write(self):
def test_read_prefs_with_comments(self):
"""test reading preferences from a prefs.js file that contains comments"""

_prefs = {'browser.startup.homepage': 'http://planet.mozilla.org',
'zoom.minPercent': 30,
'zoom.maxPercent': 300,
'webgl.verbose': 'false'}
path = os.path.join(here, 'files', 'prefs_with_comments.js')
self.assertEqual(dict(Preferences.read_prefs(path)), _prefs)
self.assertEqual(dict(Preferences.read_prefs(path)), self._prefs_with_comments)

def test_read_prefs_ttw(self):
"""test reading preferences through the web via mozhttpd"""

# create a MozHttpd instance
docroot = os.path.join(here, 'files')
host = '127.0.0.1'
port = 8888
httpd = mozhttpd.MozHttpd(host=host, port=port, docroot=docroot)

# create a preferences instance
prefs = Preferences()

try:
# start server
httpd.start(block=False)

# read preferences through the web
read = prefs.read_prefs('http://%s:%d/prefs_with_comments.js' % (host, port))
self.assertEqual(dict(read), self._prefs_with_comments)
finally:
httpd.stop()

if __name__ == '__main__':
unittest.main()

0 comments on commit 4a38d2c

Please sign in to comment.