# Path: python3.7libs/hpaste/nethelper.py
# def urlopen_nt(req: request.Request, fallback_cert: int = 0) -> (int, object):
#     """
#     wrapper around urllib2.urlopen that does not throw HTTPError
#     and does some additional things like falling back on SSL certs
#     :param req:
#     :param fallback_cert:  do not use
#     :return:
#     """
#     code = -1
#     rep = None
#     # print req.get_full_url(), req.get_data(), fallback_cert
#     try:
#         if fallback_cert == 0:
#             rep = request.urlopen(req)
#         elif fallback_cert == 1:
#             import certifi
#             rep = request.urlopen(req, cafile=certifi.where())
#         elif fallback_cert == 2:
#             import ssl
#             rep = request.urlopen(req, context=ssl._create_unverified_context())
#             log("connected with unverified context", 2)
#     except error.HTTPError as e:
#         code = e.code
#         rep = ErrorReply(code, e.headers, e.reason)
#     except error.URLError as e:
#         if fallback_cert < 2:
#             return urlopen_nt(req, fallback_cert+1)
#         else:
#             raise
# 
#     if code == -1:
#         code = rep.getcode()
#     return code, rep


import re
import json
import time
    import sys
    import string
    import random
from urllib import request
from .nethelper import urlopen_nt
    from PySide2.QtWidgets import QDialog, QVBoxLayout, QLabel, QSizePolicy, QPushButton, QMessageBox
    from PySide2.QtWebEngineWidgets import QWebEngineView, QWebEngineProfile, QWebEnginePage
    from PySide2.QtCore import Slot, Qt, QUrl
    from PySide2.QtWidgets import QApplication
                   'scope': 'gist'}
        req = request.Request('https://github.com/login/device/code', data=json.dumps(reqdata).encode('UTF-8'), headers=self.__headers)
        req.get_method = lambda: 'POST'
        code, ret = urlopen_nt(req)
        if code != 200:
            raise RuntimeError('code %d when trying to register device' % code)
        init_data = json.loads(ret.read().decode('UTF-8'))
        print(init_data)
        self.__device_code = init_data['device_code']
        self.__interval = init_data.get('interval', 5)
        url = init_data['verification_uri']

        self.__await_login_redirect = True
        self.__webprofile = QWebEngineProfile(parent=self.__webview)
        self.__webpage = QWebEnginePage(self.__webprofile, parent=self.__webview)  # just to be sure they are deleted in proper order
        self.__webview.setPage(self.__webpage)
        self.__webview.load(QUrl(url))
        self.__devidlabel.setText('code: %s' % (init_data['user_code'],))

    def get_result(self):
        return self.__result

    def closeEvent(self, event):
        # here we assume user has done his part, so lets get checking
        for attempt in range(5):
            reqdata = {'client_id': self.__client_id,
                       'device_code': self.__device_code,
                       'grant_type': 'urn:ietf:params:oauth:grant-type:device_code'}
            req = request.Request('https://github.com/login/oauth/access_token', data=json.dumps(reqdata).encode('UTF-8'), headers=self.__headers)
            req.get_method = lambda: 'POST'

# Path: python3.7libs/hpaste/nethelper.py
# def urlopen_nt(req: request.Request, fallback_cert: int = 0) -> (int, object):
#     """
#     wrapper around urllib2.urlopen that does not throw HTTPError
#     and does some additional things like falling back on SSL certs
#     :param req:
#     :param fallback_cert:  do not use
#     :return:
#     """
#     code = -1
#     rep = None
#     # print req.get_full_url(), req.get_data(), fallback_cert
#     try:
#         if fallback_cert == 0:
#             rep = request.urlopen(req)
#         elif fallback_cert == 1:
#             import certifi
#             rep = request.urlopen(req, cafile=certifi.where())
#         elif fallback_cert == 2:
#             import ssl
#             rep = request.urlopen(req, context=ssl._create_unverified_context())
#             log("connected with unverified context", 2)
#     except error.HTTPError as e:
#         code = e.code
#         rep = ErrorReply(code, e.headers, e.reason)
#     except error.URLError as e:
#         if fallback_cert < 2:
#             return urlopen_nt(req, fallback_cert+1)
#         else:
#             raise
# 
#     if code == -1:
#         code = rep.getcode()
#     return code, rep


# Path: python3.7libs/hpaste/QGithubDeviceAuthDialog.py
import re
import json
import time
    import sys
    import string
    import random
from urllib import request
from .nethelper import urlopen_nt
    from PySide2.QtWidgets import QDialog, QVBoxLayout, QLabel, QSizePolicy, QPushButton, QMessageBox
    from PySide2.QtWebEngineWidgets import QWebEngineView, QWebEngineProfile, QWebEnginePage
    from PySide2.QtCore import Slot, Qt, QUrl
    from PySide2.QtWidgets import QApplication

                   'scope': 'gist'}
        req = request.Request('https://github.com/login/device/code', data=json.dumps(reqdata).encode('UTF-8'), headers=self.__headers)
        req.get_method = lambda: 'POST'
        code, ret = urlopen_nt(req)
        if code != 200:
            raise RuntimeError('code %d when trying to register device' % code)
        init_data = json.loads(ret.read().decode('UTF-8'))
        print(init_data)
        self.__device_code = init_data['device_code']
        self.__interval = init_data.get('interval', 5)
        url = init_data['verification_uri']

        self.__await_login_redirect = True
        self.__webprofile = QWebEngineProfile(parent=self.__webview)
        self.__webpage = QWebEnginePage(self.__webprofile, parent=self.__webview)  # just to be sure they are deleted in proper order
        self.__webview.setPage(self.__webpage)
        self.__webview.load(QUrl(url))
        self.__devidlabel.setText('code: %s' % (init_data['user_code'],))

    def get_result(self):
        return self.__result

    def closeEvent(self, event):
        # here we assume user has done his part, so lets get checking
        for attempt in range(5):
            reqdata = {'client_id': self.__client_id,
                       'device_code': self.__device_code,
                       'grant_type': 'urn:ietf:params:oauth:grant-type:device_code'}
            req = request.Request('https://github.com/login/oauth/access_token', data=json.dumps(reqdata).encode('UTF-8'), headers=self.__headers)
            req.get_method = lambda: 'POST'

            code, ret = urlopen_nt(req)