Skip to content

Commit

Permalink
new: Add --defaultconfig so you can choose a default config to load.
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-ong committed Jul 13, 2020
1 parent 289f07a commit f312a47
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
18 changes: 16 additions & 2 deletions nestris_ocr/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from nestris_ocr.utils.sub_image import spawn_subimage
from nestris_ocr.utils.program_args import args


# fmt: off
CONFIG_DEFAULTS = {
"player.name": "",
Expand Down Expand Up @@ -91,10 +92,23 @@ def __init__(self, path, auto_save=True):
with open(path, "r") as file:
self.data = json.load(file, object_pairs_hook=OrderedDict)
except Exception:
# override with default on non-existent file or parsing error
self.data = OrderedDict(CONFIG_DEFAULTS)
self.data = self.load_defaultconfig()
self.save()

def load_defaultconfig(self):
result = None
if args.defaultconfig is not None:
try:
with open(args.defaultconfig, "r") as file:
result = json.load(file, object_pairs_hook=OrderedDict)
except Exception:
result = None

if result is None:
result = OrderedDict(CONFIG_DEFAULTS)

return result

def __getitem__(self, key):
if key not in CONFIG_DEFAULTS:
raise KeyError("Invalid key %s" % (key,))
Expand Down
1 change: 1 addition & 0 deletions nestris_ocr/utils/program_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def init_args():
parser.add_argument(
"--calibrate", help="Run calibrator rather than scanner", action="store_true"
)
parser.add_argument("--defaultconfig", default=None)
args = parser.parse_args()


Expand Down

0 comments on commit f312a47

Please sign in to comment.