# Path: python2.7libs/hpaste/nethelper.py
# def urlopen_nt(req, fallback_cert=0):
# 	# type: (urllib2.Request, int) -> (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 = urllib2.urlopen(req)
# 		elif fallback_cert == 1:
# 			import certifi
# 			rep = urllib2.urlopen(req, cafile=certifi.where())
# 		elif fallback_cert == 2:
# 			import ssl
# 			rep = urllib2.urlopen(req, context=ssl._create_unverified_context())
# 			log("connected with unverified context", 2)
# 	except urllib2.HTTPError as e:
# 		code = e.code
# 		rep = ErrorReply(code, e.headers, e.msg)
# 	except urllib2.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: python2.7libs/hpaste/logger.py
# def simpleLogger(s,level=1):
# def passLogger(s,level=1):


import urllib2
import json
import copy
import re
import base64
from ..nethelper import urlopen_nt
from collectionbase import CollectionBase,CollectionItem,CollectionInconsistentError,CollectionSyncError,CollectionItemInvalidError, \
	CollectionReadonlyError
from ..logger import defaultLogger as log
	from PySide2.QtGui import QPixmap
	from PySide2.QtCore import QBuffer, QByteArray
	from PySide.QtGui import QPixmap
	from PySide.QtCore import QBuffer, QByteArray
	from os import path
					log("unsupported collection item version, id:%s" % gist['id'], 2)
					continue

				# now get the item
				itemfiles = [x for x in files if x.startswith("item:")]
				if len(itemfiles) != 1: raise CollectionInconsistentError('could not find unique item data in gist')
				filedata = files[itemfiles[0]]
				itemname = itemfiles[0].split(':', 1)[1]
				rawurl = filedata['raw_url']
				retaccess = CollectionItem.AccessType.public if gist['public'] else CollectionItem.AccessType.private
				newitem = CollectionItem(self, itemname, desc, '%s@%s'%(gist['id'], itemfiles[0]), retaccess, self.__readonly, metadata={'raw_url':rawurl, 'nettype':nettype, 'ver':ver})

				for typefilename in files:
					if ':' not in typefilename: continue
					filetype, filename = typefilename.split(':', 1)
					if filetype == 'item': continue  # Skip item file, it was already processed
					if filetype == 'icon':
						if not qtAvailable: continue  # If we were loaded from non-interactive session - just skip icons

						if ':' not in filename: continue
						icontype, iconname =  filename.split(':', 1)
						if icontype not in ['XPM-simple', 'PNG-base64']: continue  # supported icon format list

						filedata = files[typefilename]
						url = filedata['raw_url']
						if url in globalIconCacher:
							data = globalIconCacher[url]
						else:
							req = urllib2.Request(url, headers=self.__headers)
							try:

# Path: python2.7libs/hpaste/nethelper.py
# def urlopen_nt(req, fallback_cert=0):
# 	# type: (urllib2.Request, int) -> (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 = urllib2.urlopen(req)
# 		elif fallback_cert == 1:
# 			import certifi
# 			rep = urllib2.urlopen(req, cafile=certifi.where())
# 		elif fallback_cert == 2:
# 			import ssl
# 			rep = urllib2.urlopen(req, context=ssl._create_unverified_context())
# 			log("connected with unverified context", 2)
# 	except urllib2.HTTPError as e:
# 		code = e.code
# 		rep = ErrorReply(code, e.headers, e.msg)
# 	except urllib2.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: python2.7libs/hpaste/logger.py
# def simpleLogger(s,level=1):
# def passLogger(s,level=1):


# Path: python2.7libs/hpaste/hcollections/githubcollection.py
import urllib2
import json
import copy
import re
import base64
from ..nethelper import urlopen_nt
from collectionbase import CollectionBase,CollectionItem,CollectionInconsistentError,CollectionSyncError,CollectionItemInvalidError, \
	CollectionReadonlyError
from ..logger import defaultLogger as log
	from PySide2.QtGui import QPixmap
	from PySide2.QtCore import QBuffer, QByteArray
	from PySide.QtGui import QPixmap
	from PySide.QtCore import QBuffer, QByteArray
	from os import path

					log("unsupported collection item version, id:%s" % gist['id'], 2)
					continue

				# now get the item
				itemfiles = [x for x in files if x.startswith("item:")]
				if len(itemfiles) != 1: raise CollectionInconsistentError('could not find unique item data in gist')
				filedata = files[itemfiles[0]]
				itemname = itemfiles[0].split(':', 1)[1]
				rawurl = filedata['raw_url']
				retaccess = CollectionItem.AccessType.public if gist['public'] else CollectionItem.AccessType.private
				newitem = CollectionItem(self, itemname, desc, '%s@%s'%(gist['id'], itemfiles[0]), retaccess, self.__readonly, metadata={'raw_url':rawurl, 'nettype':nettype, 'ver':ver})

				for typefilename in files:
					if ':' not in typefilename: continue
					filetype, filename = typefilename.split(':', 1)
					if filetype == 'item': continue  # Skip item file, it was already processed
					if filetype == 'icon':
						if not qtAvailable: continue  # If we were loaded from non-interactive session - just skip icons

						if ':' not in filename: continue
						icontype, iconname =  filename.split(':', 1)
						if icontype not in ['XPM-simple', 'PNG-base64']: continue  # supported icon format list

						filedata = files[typefilename]
						url = filedata['raw_url']
						if url in globalIconCacher:
							data = globalIconCacher[url]
						else:
							req = urllib2.Request(url, headers=self.__headers)
							try:

								code, rep = urlopen_nt(req)