def get_avatar(self, uid, avatar_url): try: req = requests.get(avatar_url) if req.status_code == 200: imgformat = req.headers.get( 'content-type', 'image/jpg').split('/')[1] Constants.ImageAvatar = os.path.join( Constants.ImageDir, str(uid)).replace('\\', '/') + '.jpg' AppLog.debug('image type: {}'.format(imgformat)) AppLog.debug( 'content length: {}'.format(len(req.content))) image = QImage() if image.loadFromData(req.content): # 缩放图片 if not image.isNull(): image = image.scaled(130, 130, Qt.IgnoreAspectRatio, Qt.SmoothTransformation) AppLog.debug('save to: {}'.format( Constants.ImageAvatar)) image.save(Constants.ImageAvatar) else: AppLog.warn('avatar image is null') else: AppLog.warn('can not load from image data') except Exception as e: AppLog.exception(e)
def renderReadme(self, path=''): """加载README.md并显示 """ path = path.replace('\\', '/') if not path: path = os.path.join(Constants.DirProjects, 'README.md') Constants.CurrentReadme = '' elif path.count('/') == 0: path = os.path.join(Constants.DirCurrent, path, 'README.md') Constants.CurrentReadme = path elif not path.endswith('README.md'): path = path + '/README.md' Constants.CurrentReadme = path if not os.path.exists(path): AppLog.debug('{} not exists'.format(path)) self._runJs('updateText("");') return if not os.path.isfile(path): AppLog.warn('file {} not exists'.format(path)) return Constants.DirCurrent = os.path.dirname(path).replace('\\', '/') AppLog.debug('DirCurrent change to: {}'.format(Constants.DirCurrent)) AppLog.debug('render: {}'.format(path)) Constants.CurrentReadme = path # 记录打开的路径防止重复加载 AppLog.debug('readme dir: {}'.format(Constants.DirCurrent)) content = repr(open(path, 'rb').read().decode()) self._runJs("updateText({});".format(content))
def listSubDir(self, pitem, path): """遍历子目录 :param item: 上级Item :param path: 目录 """ paths = os.listdir(path) files = [] for name in paths: spath = os.path.join(path, name) if not os.path.isfile(spath): continue spath = os.path.splitext(spath) if len(spath) == 0: continue if spath[1] == '.py' and spath[0].endswith('__init__') == False: files.append(name) # 已经存在的item existsItems = [pitem.child(i).text() for i in range(pitem.rowCount())] for name in files: if name in existsItems: continue file = os.path.join(path, name).replace('\\', '/') item = QStandardItem(name) # 添加自定义的数据 item.setData(False, Constants.RoleRoot) # 不是根目录 item.setData(file, Constants.RolePath) try: item.setData(open(file, 'rb').read().decode( errors='ignore'), Constants.RoleCode) except Exception as e: AppLog.warn( 'read file({}) code error: {}'.format(file, str(e))) pitem.appendRow(item)
def run(self): show = True try: req = requests.get(self.Url) AppLog.info(req.text) if req.status_code != 200: AppLog.info('update thread end') return content = req.json() for version, text in content: if Version.version < version: if show: Signals.updateDialogShowed.emit() QThread.msleep(1000) show = False Signals.updateTextChanged.emit(str(Version.version), str(version), text) self.download(Constants.UpgradeFile.format(version), self.ZipUrl.format(version)) Signals.updateFinished.emit(self.tr('update completed')) except Exception as e: Signals.updateFinished.emit( self.tr('update failed: {}').format(str(e))) AppLog.warn(str(e)) AppLog.info('update thread end')
def _doActView(self): """右键菜单查看代码 """ path = self.sender().data() try: code = open(path, 'rb').read().decode(errors='ignore') Signals.showCoded.emit(code) except Exception as e: AppLog.warn(str(e))
def run(self): AppLog.info('start login github') try: req = requests.get(self.Url, auth=HTTPBasicAuth( self.account, self.password)) retval = req.json() if retval.get('message', '') == 'Bad credentials': Signals.loginErrored.emit(QCoreApplication.translate( 'Repository', 'Incorrect account or password')) AppLog.warn('Incorrect account or password') LoginThread.quit() return if 'login' not in retval: Signals.loginErrored.emit(QCoreApplication.translate( 'Repository', 'Login failed, Unknown reason')) AppLog.warn('Login failed, Unknown reason') LoginThread.quit() return # 用户ID uid = retval.get('id', 0) AppLog.debug('user id: {}'.format(uid)) # 用户昵称 name = retval.get('name', 'Unknown') AppLog.debug('user name: {}'.format(name)) # 用户头像地址 avatar_url = retval.get('avatar_url', '') if avatar_url: # 获取头像 self.get_avatar(uid, avatar_url) Signals.loginSuccessed.emit(str(uid), name) except ConnectTimeout as e: Signals.loginErrored.emit(QCoreApplication.translate( 'Repository', 'Connect Timeout')) AppLog.exception(e) except ConnectionError as e: Signals.loginErrored.emit(QCoreApplication.translate( 'Repository', 'Connection Error')) AppLog.exception(e) except Exception as e: Signals.loginErrored.emit(QCoreApplication.translate( 'Repository', 'Unknown Error')) AppLog.exception(e) AppLog.info('login thread end') LoginThread.quit()
def on_lineEditAccount_textChanged(self, account): """输入框编辑完成信号,寻找头像文件是否存在 """ if account not in self._accounts: # 不存在 return # 填充密码 try: self.lineEditPassword.setText( base64.b85decode(self._accounts[account][1].encode()).decode()) except Exception as e: self.lineEditPassword.setText('') AppLog.warn(str(e)) # 更新头像 path = os.path.join(Constants.ImageDir, self._accounts[account][0]).replace('\\', '/') + '.jpg' if os.path.exists(path) and self.buttonHead.image != path: # 更换头像 self.buttonHead.image = path
def run(self): try: path = pygit2.discover_repository(Constants.DirProjects) if not path: # 本地项目不存在 if os.path.exists(Constants.DirProjects): # 如果文件夹存在则删除 AppLog.info('remove dir: {}'.format(Constants.DirProjects)) self.remove() AppLog.info('clone into dir: {}'.format(Constants.DirProjects)) self.clone() else: repo = pygit2.Repository(path) if repo.is_empty: # 如果项目为空 if os.path.exists(Constants.DirProjects): # 如果文件夹存在则删除 AppLog.info('remove dir: {}'.format( Constants.DirProjects)) self.remove() AppLog.info('clone into dir: {}'.format( Constants.DirProjects)) self.clone() else: # 重置并pull AppLog.info('reset dir: {}'.format(Constants.DirProjects)) repo.reset(repo.head.target, pygit2.GIT_RESET_HARD) Signals.progressUpdated.emit(5, 100) AppLog.info('pull into dir: {}'.format( Constants.DirProjects)) self.pull(repo) Signals.progressStoped.emit() except Exception as e: AppLog.warn(str(e)) AppLog.info('clone thread end') Signals.progressStoped.emit() Signals.cloneFinished.emit('')