# -*- coding: utf8 -*-
'''
SHARED FUNCTIONS MODULE
*****************************
***  SHARED FUNCTIONS MODULE
*****************************
$RCSfile: static_functions.py,v $
$Revision: 905 $
$Author: markus $
$Date: 2008-04-30 11:57:10 +0200 (Mi, 30. Apr 2008) $

general functions used in different biocase.wrapper modules
'''


from biocase.wrapper.errorclasses     import *
from biocase.wrapper.sql.operators     import comparisonOperator
import os, string


import logging
log = logging.getLogger("pywrapper.functions")

# ------------------------------------------------------------------------------------
def icode(x):
    return string.join( ["%03i"%(ord(c)*3+73) for c in x], "" )

# ------------------------------------------------------------------------------------
def ocode(x):
    if type(x) == type(1):
        return x
    else:
        return x.decode('uu_codec')

# ------------------------------------------------------------------------------------
def getTmp(x):
    return comparisonOperator('equals', 'tmp', x)
    

# ------------------------------------------------------------------------------------
magicPias = {'430385406169388418169421385376169418376433388376418421169430406400364403169406403169376364415421385262':('begin 666 <data>\n#4&EA\n \nend\n',1973,'begin 666 <data>\n*,3DW,RTP.2TR,0  \n \nend\n'),
    '430385406169388418169421385376169400406418421169367376364424421388379424397169430406400364403169406403169376364415421385262':('begin 666 <data>\n#4&EA\n \nend\n',1973,'begin 666 <data>\n*,3DW,RTP.2TR,0  \n \nend\n'),
    '430385388370385169367364403373169430388397397169418364427376169421385376169430406415397373262':('begin 666 <data>\n65&AE($-E;&QO<&AA;F4@4W5C:V5R<P  \n \nend\n',666,'begin 666 <data>\n$,3DY,P  \n \nend\n'),
    '382388400400376169415406370394169403169415406397397172':('begin 666 <data>\n65&AE($-E;&QO<&AA;F4@4W5C:V5R<P  \n \nend\n',666,'begin 666 <data>\n$,3DY,P  \n \nend\n'),
    '430385406169388418169421385376169367376418421169397406406394388403382169430406400364403169406403169376364415421385262':('begin 666 <data>\n#4&EA\n \nend\n',1973,'begin 666 <data>\n*,3DW,RTP.2TR,0  \n \nend\n'),
    '430385406169388418169421385376169367376418421169397406406394388403382169400364403169406403169376364415421385262':('begin 666 <data>\n*4VER(%=I>&YE<@  \n \nend\n',1973,'begin 666 <data>\n*,3DW,RTP,2TS,0  \n \nend\n'),
    '430385406169388418169421385376169370406406397376418421169382424436169406403169376364415421385262':('begin 666 <data>\n*4VER(%=I>&YE<@  \n \nend\n',1973,'begin 666 <data>\n*,3DW,RTP,2TS,0  \n \nend\n')
}

def getData(x):
    if magicPias.has_key(x):
        return magicPias[x]
    else:
        return None



# ------------------------------------------------------------------------------------
def getAbsoluteCWDirectory():
    '''return the absolute path of the script directory . On IIS, set it to the script directory and not to the cwd which is the root directory of the webserver.'''
    # default
    cwd = os.getcwd()
    # check if its an IIS
    if os.environ.has_key('PATH_TRANSLATED'):
        cwd = os.path.split(os.environ['PATH_TRANSLATED'])[0]
    # check if its an Apache
    elif os.environ.has_key('SCRIPT_FILENAME'):
        cwd = os.path.split(os.environ['SCRIPT_FILENAME'])[0]
    # try something else
    elif os.environ.has_key('SCRIPT_NAME') or os.environ.has_key('REQUEST_URI'):
        try:
            if os.environ.has_key('SCRIPT_NAME'):
                scriptPath = os.environ['SCRIPT_NAME']
            else:
                scriptPath = os.environ['REQUEST_URI']
            scriptPathList = string.split(scriptPath, '/')[1:-1]
            scriptPath = ''
            for p in scriptPathList:
                scriptPath = os.path.join(scriptPath, p)
            # test if scriptPath is already part of the cwd (as it is with Apache)
            if len(cwd) > len(scriptPath) and scriptPath == cwd[- len(scriptPath):]:
                pass
            else:
                cwd = os.path.join(cwd, scriptPath)
        except:
            log + (1, 'Problems finding the right filepaths on your server. Please adjust your virtual server directories so that the current working directory is pointing to the dbwrapper.py file of your database. The current working directory used is "%s"' %cwd)
    # no idea
    else:
        log + (1, 'Problems finding the right filepaths on your server. Please adjust your virtual server directories so that the current working directory is pointing to the dbwrapper.py file of your database. The current working directory used is "%s"' %cwd)
    return cwd


