prefpicker 2.20.0__tar.gz → 2.22.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. {prefpicker-2.20.0 → prefpicker-2.22.0}/PKG-INFO +1 -1
  2. {prefpicker-2.20.0 → prefpicker-2.22.0}/src/prefpicker/main.py +27 -1
  3. {prefpicker-2.20.0 → prefpicker-2.22.0}/src/prefpicker/prefpicker.py +38 -10
  4. {prefpicker-2.20.0 → prefpicker-2.22.0}/src/prefpicker/templates/browser-fuzzing.yml +0 -5
  5. {prefpicker-2.20.0 → prefpicker-2.22.0}/src/prefpicker/test_main.py +74 -0
  6. {prefpicker-2.20.0 → prefpicker-2.22.0}/src/prefpicker/test_prefpicker.py +78 -0
  7. {prefpicker-2.20.0 → prefpicker-2.22.0}/src/prefpicker.egg-info/PKG-INFO +1 -1
  8. {prefpicker-2.20.0 → prefpicker-2.22.0}/.github/CODEOWNERS +0 -0
  9. {prefpicker-2.20.0 → prefpicker-2.22.0}/.github/workflows/ci.yml +0 -0
  10. {prefpicker-2.20.0 → prefpicker-2.22.0}/.github/workflows/prefmonitor.yml +0 -0
  11. {prefpicker-2.20.0 → prefpicker-2.22.0}/.gitignore +0 -0
  12. {prefpicker-2.20.0 → prefpicker-2.22.0}/.pre-commit-config.yaml +0 -0
  13. {prefpicker-2.20.0 → prefpicker-2.22.0}/CODE_OF_CONDUCT.md +0 -0
  14. {prefpicker-2.20.0 → prefpicker-2.22.0}/LICENSE +0 -0
  15. {prefpicker-2.20.0 → prefpicker-2.22.0}/MANIFEST.in +0 -0
  16. {prefpicker-2.20.0 → prefpicker-2.22.0}/README.md +0 -0
  17. {prefpicker-2.20.0 → prefpicker-2.22.0}/pyproject.toml +0 -0
  18. {prefpicker-2.20.0 → prefpicker-2.22.0}/setup.cfg +0 -0
  19. {prefpicker-2.20.0 → prefpicker-2.22.0}/src/prefpicker/__init__.py +0 -0
  20. {prefpicker-2.20.0 → prefpicker-2.22.0}/src/prefpicker/__main__.py +0 -0
  21. {prefpicker-2.20.0 → prefpicker-2.22.0}/src/prefpicker/py.typed +0 -0
  22. {prefpicker-2.20.0 → prefpicker-2.22.0}/src/prefpicker/templates/schema.json +0 -0
  23. {prefpicker-2.20.0 → prefpicker-2.22.0}/src/prefpicker/test_templates.py +0 -0
  24. {prefpicker-2.20.0 → prefpicker-2.22.0}/src/prefpicker.egg-info/SOURCES.txt +0 -0
  25. {prefpicker-2.20.0 → prefpicker-2.22.0}/src/prefpicker.egg-info/dependency_links.txt +0 -0
  26. {prefpicker-2.20.0 → prefpicker-2.22.0}/src/prefpicker.egg-info/entry_points.txt +0 -0
  27. {prefpicker-2.20.0 → prefpicker-2.22.0}/src/prefpicker.egg-info/not-zip-safe +0 -0
  28. {prefpicker-2.20.0 → prefpicker-2.22.0}/src/prefpicker.egg-info/requires.txt +0 -0
  29. {prefpicker-2.20.0 → prefpicker-2.22.0}/src/prefpicker.egg-info/top_level.txt +0 -0
  30. {prefpicker-2.20.0 → prefpicker-2.22.0}/tox.ini +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: prefpicker
3
- Version: 2.20.0
3
+ Version: 2.22.0
4
4
  Summary: PrefPicker - Manage & generate prefs.js files
5
5
  Home-page: https://github.com/MozillaSecurity/prefpicker
6
6
  Author: Tyson Smith
@@ -6,6 +6,8 @@
6
6
  from __future__ import annotations
7
7
 
8
8
  from argparse import ArgumentParser, Namespace
9
+ from json import JSONDecodeError
10
+ from json import load as json_load
9
11
  from logging import DEBUG, INFO, basicConfig, getLogger
10
12
  from os import getenv
11
13
  from pathlib import Path
@@ -43,6 +45,12 @@ def parse_args(argv: list[str] | None = None) -> Namespace:
43
45
  "--check", action="store_true", help="Display output of sanity checks."
44
46
  )
45
47
  parser.add_argument("--variant", default="default", help="Specify variant to use.")
48
+ parser.add_argument(
49
+ "--json",
50
+ "-j",
51
+ type=Path,
52
+ help="Path to JSON file containing additional prefs to include in the output.",
53
+ )
46
54
  parser.add_argument(
47
55
  "--version",
48
56
  "-V",
@@ -62,6 +70,9 @@ def parse_args(argv: list[str] | None = None) -> Namespace:
62
70
  parser.error(f"Output '{args.output}' is a directory.")
63
71
  if not args.output.parent.is_dir():
64
72
  parser.error(f"Output '{args.output.parent}' directory does not exist.")
73
+ # sanity check JSON file if provided
74
+ if args.json and not args.json.is_file():
75
+ parser.error(f"Cannot find JSON file '{args.json}'")
65
76
  return args
66
77
 
67
78
 
@@ -108,6 +119,21 @@ def main(argv: list[str] | None = None) -> int:
108
119
  LOG.error("Error: Variant %r does not exist", args.variant)
109
120
  return 1
110
121
  LOG.info("Generating %r using variant %r...", args.output.name, args.variant)
111
- pick.create_prefsjs(args.output, args.variant)
122
+
123
+ # Load additional preferences from JSON file if provided
124
+ additional_prefs = {}
125
+ if args.json:
126
+ try:
127
+ with args.json.open() as json_fp:
128
+ additional_prefs = json_load(json_fp)
129
+ except (JSONDecodeError, UnicodeDecodeError) as exc:
130
+ LOG.error("Failed to load JSON file '%s': %s", args.json, exc)
131
+ return 1
132
+ if not isinstance(additional_prefs, dict):
133
+ LOG.error("Failed to load JSON file '%s': expected object", args.json)
134
+ return 1
135
+ LOG.info("Overriding %d prefs from JSON input", len(additional_prefs))
136
+
137
+ pick.create_prefsjs(args.output, args.variant, additional_prefs)
112
138
  LOG.info("Done.")
113
139
  return 0
@@ -98,13 +98,19 @@ class PrefPicker: # pylint: disable=missing-docstring
98
98
  if value in variants["default"]:
99
99
  yield (pref, variant, value)
100
100
 
101
- def create_prefsjs(self, dest: Path, variant: str = "default") -> None:
101
+ def create_prefsjs(
102
+ self,
103
+ dest: Path,
104
+ variant: str = "default",
105
+ additional_prefs: dict[str, Any] | None = None,
106
+ ) -> None:
102
107
  """Write a `prefs.js` file based on the specified variant. The output file
103
108
  will also include comments containing the variant and a timestamp.
104
109
 
105
110
  Args:
106
111
  dest: Path of file to create.
107
112
  variant: Used to pick the values to output.
113
+ additional_prefs: Additional preferences to include in the output.
108
114
 
109
115
  Returns:
110
116
  None
@@ -113,16 +119,34 @@ class PrefPicker: # pylint: disable=missing-docstring
113
119
  prefs_fp.write(f"// Generated with PrefPicker ({__version__}) @ ")
114
120
  prefs_fp.write(datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S %Z"))
115
121
  prefs_fp.write(f"\n// Variant {variant!r}\n")
116
- for pref, keys in sorted(self.prefs.items()):
117
- variants = keys["variants"]
122
+
123
+ all_prefs = set(self.prefs)
124
+ if additional_prefs:
125
+ all_prefs |= set(additional_prefs)
126
+ for pref in sorted(all_prefs):
118
127
  # choose values
119
- if variant not in variants or variant == "default":
120
- options = variants["default"]
121
- default_variant = True
122
- else:
123
- options = variants[variant]
128
+ if additional_prefs and pref in additional_prefs:
129
+ value = additional_prefs[pref]
130
+ if pref not in self.prefs:
131
+ # pref is only from JSON, not in template
132
+ if value is None:
133
+ continue
134
+ json_only = True
135
+ else:
136
+ json_only = False
137
+ options = [value]
124
138
  default_variant = False
125
- value = choice(options)
139
+ else:
140
+ json_only = False
141
+ keys = self.prefs[pref]
142
+ variants = keys["variants"]
143
+ if variant not in variants or variant == "default":
144
+ options = variants["default"]
145
+ default_variant = True
146
+ else:
147
+ options = variants[variant]
148
+ default_variant = False
149
+ value = choice(options)
126
150
  if value is None:
127
151
  if len(options) > 1:
128
152
  prefs_fp.write(
@@ -145,7 +169,11 @@ class PrefPicker: # pylint: disable=missing-docstring
145
169
  f"Unsupported datatype {type(value).__name__!r}"
146
170
  )
147
171
  # write to prefs.js file
148
- if not default_variant:
172
+ if json_only:
173
+ prefs_fp.write(f"// {pref!r} defined by --json (not in template)\n")
174
+ elif additional_prefs and pref in additional_prefs:
175
+ prefs_fp.write(f"// {pref!r} defined by --json override\n")
176
+ elif not default_variant:
149
177
  prefs_fp.write(f"// {pref!r} defined by variant {variant!r}\n")
150
178
  prefs_fp.write(f'user_pref("{pref}", {sanitized});\n')
151
179
 
@@ -591,11 +591,6 @@ pref:
591
591
  variants:
592
592
  default:
593
593
  - false
594
- # Always disable the blocklist
595
- gfx.blocklist.all:
596
- variants:
597
- default:
598
- - -1
599
594
  gfx.color_management.enablev4:
600
595
  variants:
601
596
  default:
@@ -93,3 +93,77 @@ def test_main_07(tmp_path):
93
93
  yml = tmp_path / "test.yml"
94
94
  yml.write_text("{test{")
95
95
  assert main([str(yml), str(tmp_path / "prefs.js")]) == 1
96
+
97
+
98
+ def test_main_08(tmp_path):
99
+ """test main() with --json override and prefs not in template"""
100
+ prefs_js = tmp_path / "prefs.js"
101
+ yml = tmp_path / "test.yml"
102
+ yml.write_text(
103
+ """
104
+ variant: []
105
+ pref:
106
+ test.a:
107
+ variants:
108
+ default: [1]
109
+ test.b:
110
+ variants:
111
+ default: [true]"""
112
+ )
113
+ json_file = tmp_path / "overrides.json"
114
+ json_file.write_text('{"test.a": 99, "test.new": false}')
115
+ assert main([str(yml), str(prefs_js), "--json", str(json_file)]) == 0
116
+ assert prefs_js.is_file()
117
+ prefs_data = prefs_js.read_text()
118
+ assert 'user_pref("test.a", 99);' in prefs_data
119
+ assert "defined by --json override" in prefs_data
120
+ assert 'user_pref("test.b", true);' in prefs_data
121
+ assert 'user_pref("test.new", false);' in prefs_data
122
+ assert "// 'test.new' defined by --json (not in template)" in prefs_data
123
+
124
+
125
+ def test_main_09(capsys, tmp_path):
126
+ """test main() with missing --json file"""
127
+ yml = tmp_path / "test.yml"
128
+ yml.touch()
129
+ with raises(SystemExit):
130
+ main([str(yml), str(tmp_path / "prefs.js"), "--json", "missing.json"])
131
+ assert "Cannot find JSON file 'missing.json'" in capsys.readouterr()[1]
132
+
133
+
134
+ def test_main_10(tmp_path):
135
+ """test main() with invalid JSON content"""
136
+ prefs_js = tmp_path / "prefs.js"
137
+ yml = tmp_path / "test.yml"
138
+ yml.write_text(
139
+ """
140
+ variant: []
141
+ pref:
142
+ test.a:
143
+ variants:
144
+ default: [1]"""
145
+ )
146
+ # invalid JSON syntax
147
+ json_file = tmp_path / "bad.json"
148
+ json_file.write_text("{bad json")
149
+ assert main([str(yml), str(prefs_js), "--json", str(json_file)]) == 1
150
+ # non-UTF-8 bytes
151
+ json_file.write_bytes(b"\x80\x81\x82")
152
+ assert main([str(yml), str(prefs_js), "--json", str(json_file)]) == 1
153
+
154
+
155
+ def test_main_11(tmp_path):
156
+ """test main() with non-dict JSON content"""
157
+ prefs_js = tmp_path / "prefs.js"
158
+ yml = tmp_path / "test.yml"
159
+ yml.write_text(
160
+ """
161
+ variant: []
162
+ pref:
163
+ test.a:
164
+ variants:
165
+ default: [1]"""
166
+ )
167
+ json_file = tmp_path / "list.json"
168
+ json_file.write_text("[1, 2, 3]")
169
+ assert main([str(yml), str(prefs_js), "--json", str(json_file)]) == 1
@@ -279,3 +279,81 @@ def test_prefpicker_10():
279
279
  template = next(PrefPicker.templates(), None)
280
280
  assert template is not None
281
281
  assert PrefPicker.lookup_template(template.name)
282
+
283
+
284
+ def test_prefpicker_11(tmp_path):
285
+ """test PrefPicker.create_prefsjs() with additional_prefs override"""
286
+ raw_data = {
287
+ "variant": [],
288
+ "pref": {
289
+ "test.a": {"variants": {"default": [1]}},
290
+ "test.b": {"variants": {"default": [False]}},
291
+ },
292
+ }
293
+ PrefPicker.verify_data(raw_data)
294
+ ppick = PrefPicker()
295
+ ppick.variants = set(raw_data["variant"] + ["default"])
296
+ ppick.prefs = raw_data["pref"]
297
+ prefs = tmp_path / "prefs.js"
298
+ ppick.create_prefsjs(prefs, additional_prefs={"test.a": 99})
299
+ prefs_data = prefs.read_text()
300
+ assert 'user_pref("test.a", 99);' in prefs_data
301
+ assert "// 'test.a' defined by --json override" in prefs_data
302
+ # test.b is not overridden
303
+ assert 'user_pref("test.b", false);' in prefs_data
304
+ assert "// 'test.b' defined by --json override" not in prefs_data
305
+
306
+
307
+ def test_prefpicker_12(tmp_path):
308
+ """test PrefPicker.create_prefsjs() additional_prefs takes precedence"""
309
+ raw_data = {
310
+ "variant": ["v1"],
311
+ "pref": {
312
+ "test.a": {"variants": {"default": [1], "v1": [2]}},
313
+ },
314
+ }
315
+ PrefPicker.verify_data(raw_data)
316
+ ppick = PrefPicker()
317
+ ppick.variants = set(raw_data["variant"] + ["default"])
318
+ ppick.prefs = raw_data["pref"]
319
+ prefs = tmp_path / "prefs.js"
320
+ ppick.create_prefsjs(prefs, variant="v1", additional_prefs={"test.a": 42})
321
+ prefs_data = prefs.read_text()
322
+ assert 'user_pref("test.a", 42);' in prefs_data
323
+ assert "// 'test.a' defined by --json override" in prefs_data
324
+ assert "defined by variant 'v1'" not in prefs_data
325
+
326
+
327
+ def test_prefpicker_13(tmp_path):
328
+ """test PrefPicker.create_prefsjs() with additional_prefs not in template"""
329
+ raw_data = {
330
+ "variant": [],
331
+ "pref": {
332
+ "test.b": {"variants": {"default": [1]}},
333
+ },
334
+ }
335
+ PrefPicker.verify_data(raw_data)
336
+ ppick = PrefPicker()
337
+ ppick.variants = set(raw_data["variant"] + ["default"])
338
+ ppick.prefs = raw_data["pref"]
339
+ prefs = tmp_path / "prefs.js"
340
+ ppick.create_prefsjs(
341
+ prefs,
342
+ additional_prefs={
343
+ "test.a": 99,
344
+ "test.c": "hello",
345
+ "test.b": 42,
346
+ "test.skip": None,
347
+ },
348
+ )
349
+ prefs_data = prefs.read_text()
350
+ # test.a and test.c are not in template, should be included
351
+ assert 'user_pref("test.a", 99);' in prefs_data
352
+ assert "// 'test.a' defined by --json (not in template)" in prefs_data
353
+ assert "user_pref(\"test.c\", 'hello');" in prefs_data
354
+ assert "// 'test.c' defined by --json (not in template)" in prefs_data
355
+ # test.b is in template and overridden by JSON
356
+ assert 'user_pref("test.b", 42);' in prefs_data
357
+ assert "// 'test.b' defined by --json override" in prefs_data
358
+ # None values from JSON-only prefs should be skipped
359
+ assert "test.skip" not in prefs_data
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: prefpicker
3
- Version: 2.20.0
3
+ Version: 2.22.0
4
4
  Summary: PrefPicker - Manage & generate prefs.js files
5
5
  Home-page: https://github.com/MozillaSecurity/prefpicker
6
6
  Author: Tyson Smith
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes