from bfs.bfs10135 import bfs10134
from bfs.bfs10135.errors import bfs10136
from bfs.bfs10135.bfs11054 import bfs11074
from bfs.bfs10077.bfs11157 import bfs11162
from helium.bfs11173 import bfs11174, bfs11172
from helium.bfs10146 import APIImpl
from helium.bfs10052 import bfs10056, bfs10055
from helium.bfs10135.errors import bfs10172
from helium.bfs10135.bfs11203 import bfs11213, bfs11207
from helium.bfs10135.bfs10202 import bfs10220
from helium.bfs10135.bfs11223 import bfs11246
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(bfs11255, value, traceback):
  if isinstance(value, bfs10136):
    print value.message
  else:
    sys.__excepthook__(bfs11255, value, traceback)


sys.excepthook = excepthook
bfs10107 = logging.getLogger(__name__)
bfs10107.setLevel(logging.INFO)
bfs11257 = None
def bfs11262():
  global bfs11257
  if (bfs11257 is None):
    bfs10107.info('Starting Helium')
    bfs11257 = ApplicationContext((bfs11261() if bfs10055() else bfs11260())
)

  return bfs11257

class bfs11263(PythonConfig):
  @Object(lazy_init=True)
  def APIImpl(self):
    return APIImpl(self.bfs10056(), self.License(), self.bfs11265())

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

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

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


class bfs11261(bfs11263):
  @Object
  def License(self):
    return bfs11207()

  @Object
  def bfs10056(self):
    bfs11264 = join(dirname(__file__), pardir, pardir, pardir, pardir, pardir)
    def bfs11267(bfs11266):
      return normpath(join(bfs11264, *bfs11266.split('/')))

    return bfs10056(bfs11267('../src/main/resources/base'), bfs11267('../src/main/resources'))

  @Object
  def bfs11265(self):
    return None


class bfs11260(bfs11263):
  @Object
  def bfs11162(self):
    return bfs11162()

  @Object
  def License(self):
    return bfs11213(self.bfs11074(), self.bfs11162(), self.bfs11246(), self.bfs11270())

  @Object
  def bfs11265(self):
    bfs11200 = self.bfs11174().bfs11200
    bfs11202 = self.bfs11174().bfs11202
    bfs11201 = self.bfs11174().bfs11201
    version = self.bfs11174().bfs11176
    bfs11271 = {'version': version, 'computer_name': self.bfs11162().bfs11164(), 'identifier': self.UUID()}
    return bfs10220(('http://%s:%s%slicenses/verify?' % (bfs11200, bfs11202, bfs11201)), bfs11271)

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

  @Object
  def bfs11270(self):
    return self.bfs11273()[1]

  @Object
  def bfs11273(self):
    bfs11272 = self.bfs10056().bfs10063('uuid')
    if exists(bfs11272):
      with open(bfs11272) as bfs11274:
        return [UUID(bfs11274.read()), False]

    else:
      bfs11275 = uuid1()
      with open(bfs11272, 'w') as bfs11274:
        bfs11274.write(str(bfs11275))

      return [bfs11275, True]


  @Object
  def bfs11174(self):
    return bfs11174(self.bfs10056().bfs10063(bfs11172))

  @Object
  def bfs11074(self):
    bfs11277 = 'No Helium license key found. Please contact us at contact@heliumhq.com or visit http://heliumhq.com to obtain a new one.'
    bfs11276 = self.bfs10056().bfs10063(bfs10134)
    if (not exists(bfs11276)):
      raise bfs10172(bfs11277)

    return bfs11074(bfs11276)

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

  @Object
  def bfs10056(self):
    bfs11300 = dirname(helium.__file__)
    bfs11301 = join(bfs11300, 'data')
    bfs11302 = (bfs11301 if exists(bfs11301) else bfs11300)

    bfs10107.info(('Helium home is %s.' % bfs11302))
    return bfs10056(bfs11302)


