from helium.bfs10101.bfs10102 import bfs10140
from base64 import b64encode
from logging import getLogger
from socket import timeout
from urllib import urlencode
from urllib2 import urlopen, URLError
from helium.bfs10060.bfs10142 import bfs10143
class bfs10146(object):
  def bfs10151(self, bfs10150, bfs10152):
    raise NotImplementedError()


class bfs10153(bfs10146):
  def __init__(self, config):
    self.config = config

  def bfs10151(self, bfs10150, bfs10152):
    key = bfs10150.code
    if bfs10150.bfs10154:
      key += bfs10152.bfs10155()

    bfs10157 = {'email': bfs10150.bfs10156, 'key': key, 'p1': b64encode(str(bfs10152.bfs10160())), 'p2': b64encode(str(bfs10152.bfs10161())), 'p3': b64encode(str(bfs10152.bfs10163())), 'p4': b64encode(str(bfs10152.bfs10162())), 'p5': b64encode(str(bfs10152.bfs10164())), 'p6': b64encode(str(bfs10152.bfs10165())), 'p7': b64encode(str(bfs10152.bfs10166()))}
    return self.get(self.config.bfs10170(bfs10157))

  def get(self, url):
    raise NotImplementedError()


class bfs10167(object):
  def __init__(self, url, bfs10171=None):
    self.url = url
    self.bfs10171 = (bfs10171 or {})

  def bfs10170(self, bfs10157):
    bfs10172 = self.bfs10171.copy()
    bfs10172.update(bfs10157)
    return (self.url + urlencode(bfs10172))


class bfs10174(bfs10153):
  def get(self, url):
    try:
      bfs10173 = urlopen(url, timeout=10)
    except URLError:
      getLogger(__name__).debug('Got URLError while trying to validate license online.', exc_info=True)
      raise bfs10140()
    except timeout:
      getLogger(__name__).warning('Connection to licensing server timed out.', exc_info=True)
      raise bfs10140()

    return bfs10143(bfs10173.read())


class bfs10175(bfs10153):
  def __init__(self, config, bfs10176):
    super(bfs10175, self).__init__(config)
    self.bfs10176 = bfs10176

  def get(self, url):
    self.bfs10176.get(url)
    return bfs10143(self.bfs10176.page_source)


