11import os
2- from user_exception import UserException
2+ from .user_exception import UserException
3+ import json
34
45
56class Config ():
7+ PARAM_SMMS_TOKEN = 'smms_token'
8+ PARAM_RRUU_TOKEN = 'rruu_token'
9+ PARAM_IMG_SERVICE = 'img_service'
10+ IMG_SERVICE_SMMS = 'smms'
11+ IMG_SERVICE_RRUU = 'rruu'
12+ IMG_SERVICE_ALI = 'ali'
13+ IMG_SERVICE_VIMCN = 'vimcn'
614 smmsTokenFile = ""
15+ configFile = ""
16+ mainConfig = {}
717
818 def __init__ (self ):
919 pass
@@ -27,16 +37,60 @@ def getSmmsTokenFile(self):
2737 Config .smmsTokenFile = self .getCurrentDirPath ()+ '\\ smms_token.config'
2838 return Config .smmsTokenFile
2939
40+ def __getConfigFile (self ):
41+ if Config .configFile == "" :
42+ Config .configFile = self .getCurrentDirPath ()+ '\\ main.config'
43+ return Config .configFile
44+
45+ def __getMainConfig (self , real = False ):
46+ if len (Config .mainConfig ) == 0 or real == True :
47+ if os .path .exists (self .__getConfigFile ()):
48+ with open (file = self .__getConfigFile (), mode = 'r' , encoding = 'UTF-8' ) as fopen :
49+ Config .mainConfig = json .loads (fopen .read ())
50+ else :
51+ # 不存在配置文件的,给予默认配置
52+ Config .mainConfig ['img_service' ] = 'smms'
53+ return Config .mainConfig
54+
55+ def getConfigParam (self , param ):
56+ '''获取配置参数'''
57+ mainConfig = self .__getMainConfig ()
58+ if param in mainConfig :
59+ return mainConfig [param ]
60+ else :
61+ return ''
62+
63+ def setConfigParam (self , param , value ):
64+ '''设置配置参数'''
65+ mainConfig = self .__getMainConfig ()
66+ mainConfig [param ] = value
67+
68+ def writeMainConfig (self ):
69+ fopen = open (file = self .__getConfigFile (), mode = 'w' , encoding = 'UTF-8' )
70+ mainConfig = self .__getMainConfig ()
71+ print (json .dumps (mainConfig ), file = fopen )
72+ fopen .close ()
73+
74+ def getRruuToken (self ):
75+ token = self .getConfigParam (Config .PARAM_RRUU_TOKEN )
76+ if token == '' :
77+ raise UserException (UserException .CODE_NO_RRUU_TOKEN )
78+ return token
79+
3080 def getSmmsToken (self ):
31- if not os .path .exists (self .getSmmsTokenFile ()):
32- raise UserException (UserException .CODE_NO_CONFIG )
33- configFileOpen = open (file = self .getSmmsTokenFile (), mode = 'r' )
34- token = configFileOpen .readline ()
35- configFileOpen .close ()
36- if len (token ) <= 4 :
37- raise UserException (UserException .CODE_NO_CONFIG )
38- token = token [0 :len (token )- 1 ]
81+ token = self .getConfigParam (Config .PARAM_SMMS_TOKEN )
82+ if token == '' :
83+ raise UserException (UserException .CODE_NO_SMMS_TOKEN )
3984 return token
85+ # if not os.path.exists(self.getSmmsTokenFile()):
86+ # raise UserException(UserException.CODE_NO_CONFIG)
87+ # configFileOpen = open(file=self.getSmmsTokenFile(), mode='r')
88+ # token = configFileOpen.readline()
89+ # configFileOpen.close()
90+ # if len(token) <= 4:
91+ # raise UserException(UserException.CODE_NO_CONFIG)
92+ # token = token[0:len(token)-1]
93+ # return token
4094
4195 def writeSmmsToken (self , token : str ):
4296 with open (file = self .getSmmsTokenFile (), mode = 'w' ) as configFileOpen :
0 commit comments