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

Commit

Permalink
Bug 841061 - Handle comments properly in Preferences.read_prefs, r=jh…
Browse files Browse the repository at this point in the history
…ammel
  • Loading branch information
ahal-test committed Feb 27, 2013
1 parent 41ba9f5 commit 4f7436e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
23 changes: 17 additions & 6 deletions mozprofile/mozprofile/prefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

import os
import re
import tokenize
from ConfigParser import SafeConfigParser as ConfigParser
from StringIO import StringIO

try:
import json
Expand Down Expand Up @@ -156,18 +158,27 @@ def read_prefs(cls, path, pref_setter='user_pref'):

comment = re.compile('/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/', re.MULTILINE)

token = '##//' # magical token
marker = '##//' # magical marker
lines = [i.strip() for i in file(path).readlines() if i.strip()]
_lines = []
for line in lines:
if line.startswith('#'):
if line.startswith(('#', '//')):
continue
if '//' in line:
line = line.replace('//', token)
line = line.replace('//', marker)
_lines.append(line)
string = '\n'.join(_lines)
string = re.sub(comment, '', string)

# skip trailing comments
processed_tokens = []
f_obj = StringIO(string)
for token in tokenize.generate_tokens(f_obj.readline):
if token[0] == tokenize.COMMENT:
continue
processed_tokens.append(token[:2]) # [:2] gets around http://bugs.python.org/issue9974
string = tokenize.untokenize(processed_tokens)

retval = []
def pref(a, b):
retval.append((a, b))
Expand All @@ -182,10 +193,10 @@ def pref(a, b):
print line
raise

# de-magic the token
# de-magic the marker
for index, (key, value) in enumerate(retval):
if isinstance(value, basestring) and token in value:
retval[index] = (key, value.replace(token, '//'))
if isinstance(value, basestring) and marker in value:
retval[index] = (key, value.replace(marker, '//'))

return retval

Expand Down
6 changes: 6 additions & 0 deletions mozprofile/tests/files/prefs_with_comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# A leading comment
user_pref("browser.startup.homepage", "http://planet.mozilla.org"); # A trailing comment
user_pref("zoom.minPercent", 30);
// Another leading comment
user_pref("zoom.maxPercent", 300); // Another trailing comment
user_pref("webgl.verbose", "false");
12 changes: 12 additions & 0 deletions mozprofile/tests/test_preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from mozprofile.prefs import Preferences
from mozprofile.profile import Profile

here = os.path.dirname(os.path.abspath(__file__))

class PreferencesTest(unittest.TestCase):
"""test mozprofile preference handling"""

Expand Down Expand Up @@ -241,6 +243,16 @@ def test_prefs_write(self):
# cleanup
os.remove(path)

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)


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

0 comments on commit 4f7436e

Please sign in to comment.