Skip to content

Commit

Permalink
use json format for config file
Browse files Browse the repository at this point in the history
Signed-off-by: dt-rush <nickp@balena.io>
  • Loading branch information
dt-rush committed Jul 15, 2019
1 parent 05426f7 commit fe44840
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
22 changes: 17 additions & 5 deletions mps_youtube/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys
import copy
import pickle
import json
from urllib.request import urlopen
from urllib.error import HTTPError
from urllib.parse import urlencode
Expand Down Expand Up @@ -371,17 +372,28 @@ def save(self):
""" Save current config to file. """
config = {setting: self[setting].value for setting in self}

with open(g.CFFILE, "wb") as cf:
pickle.dump(config, cf, protocol=2)
with open(g.CFFILE, "w") as cf:
json.dump(config, cf, indent=2)

util.dbg(c.p + "Saved config: " + g.CFFILE + c.w)

def convert_old_cf_to_json(self):
"""
check if old-style config exists,
convert old-style pickled binary config to json and save to disk,
delete old-style config
"""
if os.path.exists(g.OLD_CFFILE):
with open(g.OLD_CFFILE, "rb") as cf:
with open(g.CFFILE, "w") as cfj:
json.dump(pickle.load(cf), cfj, indent=2)
os.remove(g.OLD_CFFILE)

def load(self):
""" Override config if config file exists. """
if os.path.exists(g.CFFILE):

with open(g.CFFILE, "rb") as cf:
saved_config = pickle.load(cf)
with open(g.CFFILE, "r") as cf:
saved_config = json.load(cf)

for k, v in saved_config.items():

Expand Down
3 changes: 2 additions & 1 deletion mps_youtube/g.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
pafy_pls = {} #
last_opened = message = content = ""
suffix = "3" # Python 3
CFFILE = os.path.join(paths.get_config_dir(), "config")
OLD_CFFILE = os.path.join(paths.get_config_dir(), "config")
CFFILE = os.path.join(paths.get_config_dir(), "config.json")
TCFILE = os.path.join(paths.get_config_dir(), "transcode")
OLD_PLFILE = os.path.join(paths.get_config_dir(), "playlist" + suffix)
PLFILE = os.path.join(paths.get_config_dir(), "playlist_v2")
Expand Down
3 changes: 3 additions & 0 deletions mps_youtube/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ def init():
suffix = ".exe" if mswin else ""
mplayer, mpv = "mplayer" + suffix, "mpv" + suffix

# check for old pickled binary config and convert to json if so
config.convert_old_cf_to_json()

if not os.path.exists(g.CFFILE):

if has_exefile(mpv):
Expand Down

0 comments on commit fe44840

Please sign in to comment.