from bfs.bfs10131 import bfs10133
from bfs.bfs10131.errors import bfs10135
from bfs.bfs10131.bfs11071 import bfs11112
from bfs.bfs10076.bfs11174 import bfs11206
from helium.bfs11216 import bfs11222, bfs11220
from helium.bfs10145 import APIImpl
from helium.bfs10050 import bfs10055, bfs10053
from helium.bfs10131.errors import bfs10167
from helium.bfs10131.bfs11231 import bfs11241, bfs11235
from helium.bfs10131.bfs10177 import bfs10225
from helium.bfs10131.bfs11250 import bfs11273
from os.path import pardir, join, exists, dirname, normpath
from springpython.config import Object, PythonConfig
from springpython.context import ApplicationContext
from uuid import uuid1, UUID
import helium
import logging
import sys
def excepthook(bfs11304, value, traceback):
  if isinstance(value, bfs10135):
    print(value.message)
  else:
    sys.__excepthook__(bfs11304, value, traceback)


sys.excepthook = excepthook
bfs10106 = logging.getLogger(__name__)
bfs10106.setLevel(logging.INFO)
bfs11303 = None
def bfs11310():
  global bfs11303
  if (bfs11303 is None):
    bfs10106.info('Starting Helium')
    bfs11303 = ApplicationContext((bfs11305() if bfs10053() else bfs11306())
)

  return bfs11303

class bfs11307(PythonConfig):
  @Object(lazy_init=True)
  def APIImpl(self):
    return APIImpl(self.bfs10055(), self.License(), self.bfs11311())

  @Object
  def License(self):
    raise NotImplementedError()

  @Object
  def bfs10055(self):
    raise NotImplementedError()

  @Object
  def bfs11311(self):
    raise NotImplementedError()


class bfs11305(bfs11307):
  @Object
  def License(self):
    return bfs11235()

  @Object
  def bfs10055(self):
    bfs11313 = join(dirname(__file__), pardir, pardir, pardir, pardir, pardir)
    def bfs11314(bfs11312):
      return normpath(join(bfs11313, *bfs11312.split('/')))

    return bfs10055(bfs11314('../src/main/resources/base'), bfs11314('../src/main/resources'))

  @Object
  def bfs11311(self):
    return None


class bfs11306(bfs11307):
  @Object
  def bfs11206(self):
    return bfs11206()

  @Object
  def License(self):
    return bfs11241(self.bfs11112(), self.bfs11206(), self.bfs11273(), self.bfs11316())

  @Object
  def bfs11311(self):
    bfs11226 = self.bfs11222().bfs11226
    bfs11225 = self.bfs11222().bfs11225
    bfs11227 = self.bfs11222().bfs11227
    version = self.bfs11222().bfs11224
    bfs11315 = {'version': version, 'computer_name': self.bfs11206().bfs11211(), 'identifier': self.UUID()}
    return bfs10225(('http://%s:%s%slicenses/verify?' % (bfs11226, bfs11225, bfs11227)), bfs11315)

  @Object
  def UUID(self):
    return self.bfs11317()[0]

  @Object
  def bfs11316(self):
    return self.bfs11317()[1]

  @Object
  def bfs11317(self):
    bfs11320 = self.bfs10055().bfs10062('uuid')
    if exists(bfs11320):
      with open(bfs11320) as bfs11321:
        return [UUID(bfs11321.read()), False]

    else:
      bfs11322 = uuid1()
      with open(bfs11320, 'w') as bfs11321:
        bfs11321.write(str(bfs11322))

      return [bfs11322, True]


  @Object
  def bfs11222(self):
    return bfs11222(self.bfs10055().bfs10062(bfs11220))

  @Object
  def bfs11112(self):
    bfs11323 = 'No Helium license key found. Please contact us at contact@heliumhq.com or visit http://heliumhq.com to obtain a new one.'
    bfs11325 = self.bfs10055().bfs10062(bfs10133)
    if (not exists(bfs11325)):
      raise bfs10167(bfs11323)

    return bfs11112(bfs11325)

  @Object
  def bfs11273(self):
    return bfs11273()

  @Object
  def bfs10055(self):
    bfs11324 = dirname(helium.__file__)
    bfs11326 = join(bfs11324, 'data')
    bfs11330 = (bfs11326 if exists(bfs11326) else bfs11324)

    bfs10106.info(('Helium home is %s.' % bfs11330))
    return bfs10055(bfs11330)


