from bfs.bfs10131 import bfs10133
from bfs.bfs10131.errors import bfs10135
from bfs.bfs10131.bfs11073 import bfs11111
from bfs.bfs10076.bfs11175 import bfs11210
from helium.bfs11220 import bfs11221, bfs11222
from helium.bfs10145 import APIImpl
from helium.bfs10050 import bfs10055, bfs10053
from helium.bfs10131.errors import bfs10167
from helium.bfs10131.bfs11230 import bfs11240, bfs11236
from helium.bfs10131.bfs10177 import bfs10225
from helium.bfs10131.bfs11251 import bfs11272
from os.path import pardir, join, exists, dirname, normpath, expanduser
from springpython.config import Object, PythonConfig
from springpython.context import ApplicationContext
from uuid import uuid1, UUID
import helium
import logging
import sys
def excepthook(bfs11303, value, traceback):
  if isinstance(value, bfs10135):
    print value.message
  else:
    sys.__excepthook__(bfs11303, value, traceback)


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

  return bfs11305

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

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

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

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


class bfs11306(bfs11311):
  @Object
  def License(self):
    return bfs11236()

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

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

  @Object
  def bfs11313(self):
    return None


class bfs11310(bfs11311):
  @Object
  def bfs11210(self):
    return bfs11210()

  @Object
  def License(self):
    return bfs11240(self.bfs11111(), self.bfs11210(), self.bfs11272(), self.bfs11315())

  @Object
  def bfs11313(self):
    bfs11225 = self.bfs11221().bfs11225
    bfs11227 = self.bfs11221().bfs11227
    bfs11231 = self.bfs11221().bfs11231
    version = self.bfs11221().bfs11226
    bfs11317 = {'version': version, 'computer_name': self.bfs11210().bfs11212(), 'identifier': self.UUID()}
    return bfs10225(('http://%s:%s%slicenses/verify?' % (bfs11225, bfs11227, bfs11231)), bfs11317)

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

  @Object
  def bfs11315(self):
    return self.bfs11320()[1]

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

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

      return [bfs11323, True]


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

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


    return bfs11111(bfs11324)

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

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

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


