🐛 fix bug on empty configuration file
This commit is contained in:
parent
9ec4d877eb
commit
742a717b49
@ -1,4 +1,4 @@
|
|||||||
from gpuutil import GPUStat
|
from gpuutil import GPUStat, loaddict
|
||||||
import sys
|
import sys
|
||||||
import json
|
import json
|
||||||
import argparse
|
import argparse
|
||||||
@ -18,8 +18,7 @@ def load_config():
|
|||||||
configpath = os.path.join(home_dir, '.gpuutil.conf')
|
configpath = os.path.join(home_dir, '.gpuutil.conf')
|
||||||
if not os.path.isfile(configpath):
|
if not os.path.isfile(configpath):
|
||||||
return {}
|
return {}
|
||||||
with open(configpath, 'r', encoding='utf-8') as f:
|
return loaddict(configpath)
|
||||||
return json.load(f)
|
|
||||||
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')
|
||||||
|
|||||||
@ -14,6 +14,14 @@ 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 loaddict(path):
|
||||||
|
content = loadfile(path)
|
||||||
|
content = content.strip()
|
||||||
|
if len(content) != 0:
|
||||||
|
return json.loads(content)
|
||||||
|
else:
|
||||||
|
return {}
|
||||||
|
|
||||||
def exe_cmd(command):
|
def exe_cmd(command):
|
||||||
pipe = os.popen(command)
|
pipe = os.popen(command)
|
||||||
return pipe.read()
|
return pipe.read()
|
||||||
@ -311,16 +319,15 @@ class GPUStat():
|
|||||||
self.load_configure()
|
self.load_configure()
|
||||||
def load_configure(self):
|
def load_configure(self):
|
||||||
configuration_path = os.path.expanduser('~/.gpuutil.conf')
|
configuration_path = os.path.expanduser('~/.gpuutil.conf')
|
||||||
if os.path.exists(configuration_path):
|
if os.path.isfile(configuration_path):
|
||||||
with open(configuration_path, 'r', encoding='utf-8') as f:
|
configuration = loaddict(configuration_path)
|
||||||
configuration = json.load(f)
|
if 'redirect' in configuration:
|
||||||
if 'redirect' in configuration:
|
if 'nvsmi_src' in configuration['redirect']:
|
||||||
if 'nvsmi_src' in configuration['redirect']:
|
self.nvsmi_source = configuration['redirect']['nvsmi_src']
|
||||||
self.nvsmi_source = configuration['redirect']['nvsmi_src']
|
if 'ps_src' in configuration['redirect']:
|
||||||
if 'ps_src' in configuration['redirect']:
|
self.ps_source = configuration['redirect']['ps_src']
|
||||||
self.ps_source = configuration['redirect']['ps_src']
|
if 'ps_name_trans' in configuration['redirect']:
|
||||||
if 'ps_name_trans' in configuration['redirect']:
|
self.ps_name_trans = configuration['redirect']['ps_name_trans']
|
||||||
self.ps_name_trans = configuration['redirect']['ps_name_trans']
|
|
||||||
|
|
||||||
|
|
||||||
def get_process_info(self):
|
def get_process_info(self):
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
import argparse
|
import argparse
|
||||||
import os
|
import os
|
||||||
import json
|
from gpuutil import loaddict
|
||||||
|
|
||||||
availabel_name_trans = ['command', 'user', 'pid']
|
availabel_name_trans = ['command', 'user', 'pid']
|
||||||
|
|
||||||
@ -32,8 +32,7 @@ if name_trans is not None:
|
|||||||
config_file = os.path.expanduser('~/.gpuutil.conf')
|
config_file = os.path.expanduser('~/.gpuutil.conf')
|
||||||
configuration = {}
|
configuration = {}
|
||||||
if os.path.isfile(config_file):
|
if os.path.isfile(config_file):
|
||||||
with open(config_file, 'r', encoding='utf-8') as f:
|
configuration = loaddict(config_file)
|
||||||
configuration = json.load(f)
|
|
||||||
configuration['redirect'] = {
|
configuration['redirect'] = {
|
||||||
"nvsmi_src": args.nvsmi,
|
"nvsmi_src": args.nvsmi,
|
||||||
"ps_src": args.ps,
|
"ps_src": args.ps,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user