#!/usr/bin/env python
# -*- coding: utf-8 -*-

# -------------------------------------------------------------------
# Copyright (c) 2010-2019 Denis Machard
# This file is part of the extensive automation project
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA
# -------------------------------------------------------------------

import base64
import zlib
import os

from ea.libs import Settings, Logger

class HelperManager(Logger.ClassLogger):
    def __init__ (self):
        """
        Documentation is generated with the start of the server in save in a file
        """
        pass

    def trace(self, txt):
        """
        Trace message
        """
        Logger.ClassLogger.trace(self, txt="HMG - %s" % txt)

    def getHelps(self):
        """
        Returns the documentation cache

        @return:
        @rtype:
        """
        self.trace("Helper manager - get helps" )
        ret = ''
        try:
            complete_path = '%s/%s/documentations.dat' % ( Settings.getDirExec(),
                                                           Settings.get( 'Paths', 'var' ))
            if os.path.exists( complete_path ):
                fd = open( complete_path , "rb")
                data = fd.read()
                fd.close()

                ret = base64.b64encode( zlib.compress(data) )
            else:
                self.error( 'documentation cache does not exist' )
        except Exception as e:
            self.error( "unable to get helps: %s" % e )
        return ret.decode('utf8')

HM = None # singleton
def instance ():
    """
    Returns the singleton

    @return:
    @rtype:
    """
    return HM

def initialize ():
    """
    Instance creation
    """
    global HM
    HM = HelperManager()

def finalize ():
    """
    Destruction of the singleton
    """
    global HM
    if HM:
        HM = None