Skip to content

Commit d59cd04

Browse files
authored
Merge pull request psychopy#7514 from TEParsons/dev-enh-prefs-json
ENH: Allow prefs to be set from JSON
2 parents 93ef550 + 8c09392 commit d59cd04

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

psychopy/preferences/preferences.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import errno
55
import os
66
import sys
7+
import argparse
78
import platform
89
from pathlib import Path
910
from psychopy import logging
@@ -354,6 +355,21 @@ def loadUserPrefs(self):
354355

355356
return cfg
356357

358+
def fromJSON(self, file):
359+
import json
360+
361+
# load params from JSON
362+
with open(file, "r") as f:
363+
spec = json.load(f)
364+
params = spec['params']
365+
# iterate through relevant sections
366+
for section in [self.general, self.hardware, self.piloting]:
367+
# iterate through keys
368+
for key in section:
369+
# if given in the JSON, set value
370+
if key in params:
371+
section[key] = params[key]['val']
372+
357373
@property
358374
def devices(self):
359375
if not hasattr(self, "_devices"):
@@ -461,3 +477,17 @@ def restoreBadPrefs(self, cfg, result):
461477
print(msg % (', '.join(sectionList), cfg.filename))
462478

463479
prefs = Preferences()
480+
# parse calling args for any which are prefs relevant
481+
parser = argparse.ArgumentParser(
482+
prog="PsychoPy Preferences",
483+
description="Parses arguments relevant to PsychoPy's preferences"
484+
)
485+
parser.add_argument(
486+
"--prefs-json",
487+
type=Path,
488+
default=None
489+
)
490+
args = parser.parse_known_args()[0]
491+
# load prefs from JSON if one was given
492+
if args.prefs_json:
493+
prefs.fromJSON(args.prefs_json)

0 commit comments

Comments
 (0)