from bfs.bfs10101 import bfs10103
from bfs.bfs10101.bfs10102 import bfs10104
from bfs.bfs10101.bfs10150 import bfs10671
from bfs.bfs10060.bfs10152 import bfs10747
from helium.bfs10762 import bfs10763, bfs10761
from helium.bfs10114 import APIImpl
from helium.bfs10772 import bfs10776, bfs10774
from helium.bfs10101.bfs10102 import NoLicenseKeyFound
from helium.bfs10101.bfs11004 import bfs11006, bfs11005
from helium.bfs10101.bfs10147 import bfs10167
from helium.bfs10101.bfs11020 import bfs11034
from logging import StreamHandler, Formatter, getLogger
from os.path import pardir, join, exists, dirname, normpath, expanduser
from uuid import uuid1, UUID
import helium
import logging.config
import sys
def excepthook(bfs11041, value, traceback):
  if isinstance(value, bfs10104):
    print value.message
  else:
    sys.__excepthook__(bfs11041, value, traceback)


sys.excepthook = excepthook
def bfs11047():
  global bfs11043
  if (bfs11043 is None):
    if bfs10774():
      bfs11043 = bfs11044()
    else:
      bfs11043 = bfs11045()


  return bfs11043

bfs11043 = None
class bfs11046(object):
  def __init__(self):
    self.bfs11050()
    self.bfs10114 = None

  def bfs11050(self):
    raise NotImplementedError()

  def APIImpl(self):
    if (self.bfs10114 is None):
      self.bfs10114 = APIImpl(self.bfs10776(), self.License(), self.bfs11051())

    return self.bfs10114

  def License(self):
    raise NotImplementedError()

  def bfs10776(self):
    raise NotImplementedError()

  def bfs11051(self):
    raise NotImplementedError()


class bfs11044(bfs11046):
  def __init__(self):
    super(bfs11044, self).__init__()
    self.license = self.bfs10335 = None

  def bfs11050(self):
    bfs11053 = StreamHandler()
    bfs11052 = Formatter('%(name)s %(levelname)s: %(message)s')
    bfs11053.setFormatter(bfs11052)
    getLogger('helium').addHandler(bfs11053)
    getLogger('bfs').addHandler(bfs11053)

  def License(self):
    if (self.license is None):
      self.license = bfs11005()

    return self.license

  def bfs10776(self):
    if (self.bfs10335 is None):
      bfs11054 = join(dirname(__file__), pardir, pardir, pardir, pardir, pardir)
      def bfs11055(bfs11056):
        return normpath(join(bfs11054, *bfs11056.split('/')))

      return bfs10776(bfs11055('../src/main/resources/base'), bfs11055('../src/main/resources'))

    return self.bfs10335

  def bfs11051(self):
    return None


class bfs11045(bfs11046):
  def __init__(self):
    self.bfs11017 = self.bfs10152 = self.license = self.bfs10336 = self.uuid = self.bfs11057 = self.bfs11016 = self.bfs11020 = self.bfs10335 = None
    super(bfs11045, self).__init__()

  def bfs11050(self):
    bfs11061 = self.bfs10776().bfs10350('logging.conf')
    if exists(bfs11061):
      logging.config.fileConfig(bfs11061)


  def bfs10747(self):
    if (self.bfs10152 is None):
      self.bfs10152 = bfs10747()

    return self.bfs10152

  def License(self):
    if (self.license is None):
      self.license = bfs11006(self.bfs10671(), self.bfs10747(), self.bfs11034(), self.bfs11060())

    return self.license

  def bfs11051(self):
    if (self.bfs10336 is None):
      bfs10770 = self.bfs10763().bfs10770
      bfs10767 = self.bfs10763().bfs10767
      bfs10771 = self.bfs10763().bfs10771
      version = self.bfs10763().bfs10766
      bfs11062 = {'version': version, 'computer_name': self.bfs10747().bfs10754(), 'identifier': self.UUID()}
      self.bfs10336 = bfs10167(('http://%s:%s%slicenses/verify?' % (bfs10770, bfs10767, bfs10771)), bfs11062)

    return self.bfs10336

  def UUID(self):
    self.bfs11064()
    return self.uuid

  def bfs11060(self):
    self.bfs11064()
    return self.bfs11017

  def bfs11064(self):
    bfs11063 = self.bfs10776().bfs10350('uuid')
    if exists(bfs11063):
      with open(bfs11063) as bfs11065:
        self.uuid = UUID(bfs11065.read())
        self.bfs11017 = False

    else:
      bfs10063 = uuid1()
      with open(bfs11063, 'w') as bfs11065:
        bfs11065.write(str(bfs10063))

      self.uuid = uuid1()
      self.bfs11017 = True


  def bfs10763(self):
    if (self.bfs11057 is None):
      self.bfs11057 = bfs10763(self.bfs10776().bfs10350(bfs10761))

    return self.bfs11057

  def bfs10671(self):
    if (self.bfs11016 is None):
      bfs11067 = 'No Helium license key found. Please contact us at contact@heliumhq.com or visit http://heliumhq.com to obtain a new one.'
      bfs11066 = join(expanduser('~'), '.helium', bfs10103)
      if (not exists(bfs11066)):
        bfs11066 = self.bfs10776().bfs10350(bfs10103)
        if (not exists(bfs11066)):
          raise NoLicenseKeyFound(bfs11067)


      self.bfs11016 = bfs10671(bfs11066)

    return self.bfs11016

  def bfs11034(self):
    if (self.bfs11020 is None):
      self.bfs11020 = bfs11034()

    return self.bfs11020

  def bfs10776(self):
    if (self.bfs10335 is None):
      bfs11070 = dirname(helium.__file__)
      bfs11071 = join(bfs11070, 'data')
      bfs11073 = (bfs11071 if exists(bfs11071) else bfs11070)

      self.bfs10335 = bfs10776(bfs11073)

    return self.bfs10335


