🐛 fix bug on empty configuration file
This commit is contained in:
parent
742a717b49
commit
9d2faec7a5
@ -1,6 +1,5 @@
|
|||||||
from gpuutil import GPUStat, loaddict
|
from gpuutil import GPUStat, loaddict, savedict
|
||||||
import sys
|
import sys
|
||||||
import json
|
|
||||||
import argparse
|
import argparse
|
||||||
import os
|
import os
|
||||||
|
|
||||||
@ -22,8 +21,7 @@ def load_config():
|
|||||||
def save_config(config):
|
def save_config(config):
|
||||||
home_dir = os.path.expanduser('~')
|
home_dir = os.path.expanduser('~')
|
||||||
configdir = os.path.join(home_dir, '.gpuutil.conf')
|
configdir = os.path.join(home_dir, '.gpuutil.conf')
|
||||||
with open(configdir, 'w+', encoding='utf-8') as f:
|
savedict(configdir, config)
|
||||||
json.dump(config, f, ensure_ascii=False, indent=4)
|
|
||||||
|
|
||||||
# style format: |c|l:15|r|c:14rl:13|
|
# style format: |c|l:15|r|c:14rl:13|
|
||||||
def parse_style(style):
|
def parse_style(style):
|
||||||
|
|||||||
@ -13,6 +13,9 @@ osname = platform.system()
|
|||||||
def loadfile(path):
|
def loadfile(path):
|
||||||
with open(path, 'r', encoding='utf-8') as f:
|
with open(path, 'r', encoding='utf-8') as f:
|
||||||
return f.read()
|
return f.read()
|
||||||
|
def savefile(path, content):
|
||||||
|
with open(path, 'w+', encoding='utf-8') as f:
|
||||||
|
return f.write(content)
|
||||||
|
|
||||||
def loaddict(path):
|
def loaddict(path):
|
||||||
content = loadfile(path)
|
content = loadfile(path)
|
||||||
@ -21,6 +24,9 @@ def loaddict(path):
|
|||||||
return json.loads(content)
|
return json.loads(content)
|
||||||
else:
|
else:
|
||||||
return {}
|
return {}
|
||||||
|
def savedict(path, dictionary):
|
||||||
|
content = json.dumps(dictionary, indent=4, ensure_ascii=False)
|
||||||
|
savefile(path, content)
|
||||||
|
|
||||||
def exe_cmd(command):
|
def exe_cmd(command):
|
||||||
pipe = os.popen(command)
|
pipe = os.popen(command)
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
import argparse
|
import argparse
|
||||||
import os
|
import os
|
||||||
from gpuutil import loaddict
|
from gpuutil import loaddict, savedict
|
||||||
|
|
||||||
availabel_name_trans = ['command', 'user', 'pid']
|
availabel_name_trans = ['command', 'user', 'pid']
|
||||||
|
|
||||||
@ -39,5 +39,4 @@ configuration['redirect'] = {
|
|||||||
"ps_name_trans": parsed_name_trans
|
"ps_name_trans": parsed_name_trans
|
||||||
}
|
}
|
||||||
|
|
||||||
with open(config_file, 'w+', encoding='utf-8') as f:
|
savedict(config_file, configuration)
|
||||||
f.write(json.dumps(configuration, ensure_ascii=False, indent=4))
|
|
||||||
Loading…
Reference in New Issue
Block a user