def readConfFile(self): conf = ast.literal_eval(sampleConfStr)['默认配置'] confPath = self.ConfPath() if os.path.exists(confPath): try: with open(confPath, 'rb') as f: cusConf = ast.literal_eval(BYTES2STR(f.read())) if type(cusConf) is not dict: raise ConfError('文件内容必须是一个dict') elif self.user is None: pass elif self.user not in cusConf: raise ConfError('用户 %s 不存在' % self.user) elif type(cusConf[self.user]) is not dict: raise ConfError('用户 %s 的配置必须是一个 dict' % self.user) else: for k, v in list(cusConf[self.user].items()): if k not in conf: raise ConfError('不存在的配置选项 %s.%s ' % (self.user, k)) elif type(v) is not type(conf[k]): raise ConfError( '%s.%s 必须是一个 %s' % (self.user, k, type(conf[k]).__name__)) else: conf[k] = v except (IOError, SyntaxError, ValueError, ConfError) as e: PRINT('配置文件 %s 错误: %s\n' % (confPath, e), end='') sys.exit(1) else: try: with open(confPath, 'wb') as f: f.write(STR2BYTES(sampleConfStr)) except IOError: pass if self.user is not None: PRINT('用户 %s 不存在\n' % self.user, end='') sys.exit(1) for k, v in list(conf.items()): if getattr(self, k, None) is None: setattr(self, k, v) if self.mailAccount and not self.mailAuthCode: msg = '请输入 %s 的 IMAP/SMTP 服务授权码: ' % self.mailAccount self.mailAuthCode = RAWINPUT(msg)
def readConfFile(self): confPath = self.ConfPath() strConfPath = SYSTEMSTR2STR(confPath) conf = rootConf.copy() if os.path.exists(confPath): try: with open(confPath, 'rb') as f: cusConf = ast.literal_eval(BYTES2STR(f.read())) if type(cusConf) is not dict: raise ConfError('文件内容必须是一个 dict') if type(cusConf.get('默认配置', {})) is not dict: raise ConfError('默认配置必须是一个 dict') if self.user is not None: if self.user not in cusConf: raise ConfError('用户 %s 不存在' % self.user) elif type(cusConf[self.user]) is not dict: raise ConfError('用户 %s 的配置必须是一个 dict' % self.user) else: names = ['默认配置', self.user] else: names = ['默认配置'] for name in names: for k, v in list(cusConf.get(name, {}).items()): if k in deprecatedConfKeys: PRINT('被废弃的配置选项 %s ,将忽略此选项' % k) elif k not in conf: raise ConfError('不存在的配置选项 %s.%s ' % (name, k)) elif type(v) is not type(conf[k]): t = type(conf[k]).__name__ raise ConfError('%s.%s 必须是一个 %s' % (name, k, t)) else: conf[k] = v except (IOError, SyntaxError, ValueError, ConfError) as e: PRINT('配置文件 %s 错误: %s\n' % (strConfPath, e), end='') sys.exit(1) else: PRINT('未找到配置文件“%s”,将使用默认配置' % strConfPath) try: with open(confPath, 'wb') as f: f.write(STR2BYTES(sampleConfStr)) except IOError: pass else: PRINT('已创建一个默认配置文件“%s”' % strConfPath) if self.user is not None: PRINT('用户 %s 不存在\n' % self.user, end='') sys.exit(1) for k, v in list(conf.items()): if getattr(self, k, None) is None: setattr(self, k, v) if self.pluginPath and not os.path.isdir(STR2SYSTEMSTR( self.pluginPath)): PRINT('配置文件 %s 错误: 插件目录 “%s” 不存在\n' % \ (strConfPath, self.pluginPath), end='') sys.exit(1) if self.mailAccount and not self.mailAuthCode: msg = '请输入 %s 的 IMAP/SMTP 服务授权码: ' % self.mailAccount self.mailAuthCode = RAWINPUT(msg) if self.cmdQrcode: try: import PIL import wcwidth except ImportError: PRINT('您已选择以文本模式显示二维码,请先安装 pillow, wcwidth 库') sys.exit(1)