Skip to content

Commit

Permalink
set configuration is ok
Browse files Browse the repository at this point in the history
  • Loading branch information
d0u9 committed Sep 7, 2017
1 parent ef99ff6 commit 69cc1ad
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
18 changes: 17 additions & 1 deletion youtube_dl_webui/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-

import logging
import json

from os.path import expanduser

Expand Down Expand Up @@ -91,8 +92,9 @@ class conf(object):
svr_conf = None
gen_conf = None

def __init__(self, conf_dict={}, cmd_args={}):
def __init__(self, conf_file, conf_dict={}, cmd_args={}):
self.logger = logging.getLogger('ydl_webui')
self.conf_file = conf_file
self.cmd_args = cmd_args
self.load(conf_dict)

Expand Down Expand Up @@ -122,6 +124,20 @@ def load(self, conf_dict):
# override configurations by cmdline arguments
self.cmd_args_override()

def save2file(self):
print(self.dict())
print(self.conf_file)
if self.conf_file is not None:
try:
with open(self.conf_file, 'w') as f:
json.dump(self.dict(), f, indent=4)
except PermissionError:
return (False, 'permission error')
except FileNotFoundError:
return (False, 'can not find file')
else:
return (True, None)

def dict(self):
d = {}
for f in self._valid_fields:
Expand Down
22 changes: 15 additions & 7 deletions youtube_dl_webui/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,17 @@ def event_config(cls, svr, event, data, arg):
act = data['act']

ret_val = cls.RequestErrorMsg
if act == 'get':
if act == 'get':
ret_val = {'status': 'success'}
ret_val['config'] = cls._conf.dict()
elif act == 'update':
conf_dict = data['param']
cls._conf.load(conf_dict)
suc, msg = cls._conf.save2file()
if suc:
ret_val = cls.SuccessMsg
else:
ret_val = {'status': 'error', 'errmsg': msg}

svr.put(ret_val)

Expand Down Expand Up @@ -201,11 +209,12 @@ def load_conf_from_file(cmd_args):
logger.info('load config file (%s)' %(conf_file))

if cmd_args is None or conf_file is None:
return ({}, {})
return (None, {}, {})

abs_file = os.path.abspath(conf_file)
try:
with open(expanduser(conf_file)) as f:
return (json.load(f), cmd_args)
with open(abs_file) as f:
return (abs_file, json.load(f), cmd_args)
except FileNotFoundError as e:
logger.critical("Config file (%s) doesn't exist", conf_file)
exit(1)
Expand All @@ -219,11 +228,10 @@ def __init__(self, cmd_args=None):

self.logger.debug('cmd_args = %s' %(cmd_args))

conf_dict, cmd_args = load_conf_from_file(cmd_args)
self.conf = conf(conf_dict=conf_dict, cmd_args=cmd_args)
conf_file, conf_dict, cmd_args = load_conf_from_file(cmd_args)
self.conf = conf(conf_file, conf_dict=conf_dict, cmd_args=cmd_args)
self.logger.debug("configuration: \n%s", json.dumps(self.conf.dict(), indent=4))


self.msg_mgr = MsgMgr()
web_cli = self.msg_mgr.new_cli('server')
task_cli = self.msg_mgr.new_cli()
Expand Down
2 changes: 1 addition & 1 deletion youtube_dl_webui/templates/test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@

data['general'] = general;
data['server'] = server;
data['ydl'] = ydl;
data['youtube_dl'] = ydl;

return data;
}
Expand Down

0 comments on commit 69cc1ad

Please sign in to comment.