def __init__(self, parent, flags=0): super(AccountsManager.__AccountsManager, self).__init__(parent, flags) self.ui = accountsmanager_ui.Ui_MainWindow() self.ui.setupUi(self) data = GithubAuthorizator.listAuthorizations() self.__authModel = QStringListModel([x['user'] for x in data], self) self.ui.authListView.setEditTriggers( QAbstractItemView.NoEditTriggers) self.ui.authListView.setModel(self.__authModel) data = GithubAuthorizator.listPublicCollections() self.__publicModel = QStringListModel([x['user'] for x in data], self) self.ui.publicListView.setEditTriggers( QAbstractItemView.NoEditTriggers) self.ui.publicListView.setModel(self.__publicModel) #slots-signals self.ui.addAuthPushButton.clicked.connect(self.newAuth) self.ui.removeAuthPushButton.clicked.connect(self.removeAuth) self.ui.addPublicPushButton.clicked.connect(self.newPublic) self.ui.removePublicPushButton.clicked.connect(self.removePublic) self.ui.reinitPushButton.clicked.connect(self.reinitCallback)
def _authCallback(self, callbackinfo): auth, public, action = callbackinfo if self.__insideAuthCallback: return # prevent looping self.__insideAuthCallback = True try: if action == 0 or (action == 2 and not auth['enabled']): good = self.removeCollection(auth['user']) if not good: # means something went wrong during removal attempt - probably async collection syncing problem. Try later if public: GithubAuthorizator.setPublicCollsctionEnabled( auth['user'], True) else: GithubAuthorizator.setAuthorizationEnabled( auth['user'], True) elif action == 1 or (action == 2 and auth['enabled']): if public: self.addCollection( GithubCollection(auth['user'], public=True), async=True ) # TODO: reuse some token for public access else: self.addCollection(GithubCollection(auth['token']), async=True) except CollectionSyncError as e: QMessageBox.critical( self, 'something went wrong!', 'could not add/remove collection: %s' % e.message) finally: self.__insideAuthCallback = False
def __init__(self, parent): if (HPasteCollectionWidget.__instance is None): HPasteCollectionWidget.__instance = HPasteCollectionWidget.__HPasteCollectionWidget( parent) try: auths = [] while len(auths) == 0: auths = list(GithubAuthorizator.listAuthorizations()) if (len(auths) == 0): if (GithubAuthorizator.newAuthorization()): continue else: raise RuntimeError("No collections") #test todel = [] for auth in auths: if (not GithubAuthorizator.testAuthorization(auth)): if (not GithubAuthorizator.newAuthorization(auth)): todel.append(auth) for d in todel: auths.remove(d) except Exception as e: hou.ui.displayMessage('Something went wrong.\n%s' % e.message) self.__instance = None raise for auth in auths: HPasteCollectionWidget.__instance.addCollection( GithubCollection(auth['token'])) #now public collections cols = GithubAuthorizator.listPublicCollections() for col in cols: try: #TODO: test if collection works ptkn = None if (len(auths) > 0): import random ptkn = random.sample(auths, 1)[0]['token'] HPasteCollectionWidget.__instance.addCollection( GithubCollection(col['user'], public=True, token_for_public_access=ptkn)) except Exception as e: msg = '' if (isinstance(e, urllib2.HTTPError)): msg = 'code %d. %s' % (e.code, e.reason) elif (isinstance(e, urllib2.URLError)): msg = e.reason else: msg = e.message hou.ui.displayMessage( 'unable to load public collection %s' % col['user']) elif (parent is not HPasteCollectionWidget.__instance.parent()): print("reparenting") HPasteCollectionWidget.__instance.setParent(parent)
def _killInstance( cls): #TODO: TO BE RETHOUGHT LATER !! THIS GUY SHOULD GO AWAY # remove callback, it holds a reference to us GithubAuthorizator.unregisterCollectionChangedCallback( (cls.__instance, HPasteCollectionWidget.__HPasteCollectionWidget._authCallback)) cls.__instance.deleteLater( ) # widget has parent, so it won't be deleted unless we explicitly tell it to cls.__instance = None
def authDataChanged(self, indextl, indexbr): # For now process only checked state isauthmodel = indextl.model() == self.__authModel for row in range(indextl.row(), indexbr.row() + 1): for col in range(indextl.column(), indexbr.column() + 1): index = indextl.sibling(row, col) checked = index.data(Qt.CheckStateRole) name = index.data(Qt.DisplayRole) if isauthmodel: GithubAuthorizator.setAuthorizationEnabled( name, checked) else: GithubAuthorizator.setPublicCollsctionEnabled( name, checked)
def updatePublicList(self): try: data = GithubAuthorizator.listPublicCollections() except IOError as e: QMessageBox.warning(self, 'could not read the account file!', 'Error: %d : %s' % e.args) self.__publicModel.setStringList([x['user'] for x in data])
def newPublic(self): good = False try: good = GithubAuthorizator.newPublicCollection() except IOError as e: QMessageBox.warning(self, 'could not write the account file!', 'Error: %d : %s' % e.args) if (good): self.updatePublicList()
def updatePublicList(self): try: data = GithubAuthorizator.listPublicCollections() except IOError as e: QMessageBox.warning(self, 'could not read the account file!', 'Error: %d : %s' % e.args) self.__publicModel.setItemList( [[Qt.Checked if x['enabled'] else Qt.Unchecked, x['user']] for x in data])
def removePublic(self): index = self.ui.publicListView.currentIndex() good = False try: good = GithubAuthorizator.removePublicCollection(index.data()) except IOError as e: QMessageBox.warning(self, 'could not write the account file!', 'Error: %d : %s' % e.args) if (good): self.updatePublicList()
def removeAuth(self): index = self.ui.authListView.currentIndex() good = GithubAuthorizator.removeAuthorization(index.data()) if (good): self.updateAuthList() msg = QMessageBox(self) msg.setWindowTitle('account removed from the list!') msg.setTextFormat(Qt.RichText) msg.setText( "But...<br>The access token should be deleted manually from your account.<br>Please visit <a href='https://github.com/settings/tokens'>https://github.com/settings/tokens</a> and delete access tokens you don't use anymore" ) msg.exec_()
def __init__(self, parent): if (HPasteCollectionWidget.__instance is None): HPasteCollectionWidget.__instance = HPasteCollectionWidget.__HPasteCollectionWidget( parent) try: auths = [] if (True): auths = list(GithubAuthorizator.listAuthorizations()) ## test #todel = [] #for auth in auths: # if (not GithubAuthorizator.testAuthorization(auth)): # if (not GithubAuthorizator.newAuthorization(auth)): # todel.append(auth) #for d in todel: # auths.remove(d) # For now don't force people to have their own collections while False and len(auths) == 0: auths = list(GithubAuthorizator.listAuthorizations()) if (len(auths) == 0): if (GithubAuthorizator.newAuthorization()): continue else: raise RuntimeError("No collections") #test todel = [] for auth in auths: if (not GithubAuthorizator.testAuthorization(auth)): if (not GithubAuthorizator.newAuthorization(auth)): todel.append(auth) for d in todel: auths.remove(d) except Exception as e: hou.ui.displayMessage('Something went wrong.\n%s' % e.message) HPasteCollectionWidget.__instance = None raise for auth in auths: if auth['enabled']: HPasteCollectionWidget.__instance.addCollection( GithubCollection(auth['token']), async=True) #now public collections cols = GithubAuthorizator.listPublicCollections() for col in cols: if not col['enabled']: continue try: #TODO: test if collection works ptkn = None if (len(auths) > 0): import random ptkn = random.sample(auths, 1)[0]['token'] HPasteCollectionWidget.__instance.addCollection( GithubCollection(col['user'], public=True, token_for_public_access=ptkn), async=True) except Exception as e: msg = '' if (isinstance(e, urllib2.HTTPError)): msg = 'code %d. %s' % (e.code, e.reason) elif (isinstance(e, urllib2.URLError)): msg = e.reason else: msg = e.message hou.ui.displayMessage( 'unable to load public collection %s: %s' % (col['user'], msg)) # set callback GithubAuthorizator.registerCollectionChangedCallback(( HPasteCollectionWidget.__instance, HPasteCollectionWidget.__HPasteCollectionWidget._authCallback)) elif (parent is not HPasteCollectionWidget.__instance.parent()): log("reparenting", 0) HPasteCollectionWidget.__instance.setParent(parent)
def updatePublicList(self): data = GithubAuthorizator.listPublicCollections() self.__publicModel.setStringList([x['user'] for x in data])
def updateAuthList(self): data = GithubAuthorizator.listAuthorizations() self.__authModel.setStringList([x['user'] for x in data])
def removePublic(self): index = self.ui.publicListView.currentIndex() good = GithubAuthorizator.removePublicCollection(index.data()) if (good): self.updatePublicList()
def newPublic(self): good = GithubAuthorizator.newPublicCollection() if (good): self.updatePublicList()
def newAuth(self): good = GithubAuthorizator.newAuthorization() if (good): self.updateAuthList()