import numpy_eigen
import inspect

from libsm_python import *
from plotCoordinateFrame import *
from Progress import Progress, Progress2
from saveFigTight import saveFigTight


def log(level, message, stackdepth=1):
    if getLoggingLevel() > level:
        return
    callerframerecord = inspect.stack()[stackdepth]
    frame = callerframerecord[0]
    info = inspect.getframeinfo(frame)
    rawLog(level, info.filename, info.lineno, info.function, message)
    
def logNamed(name, level, message, stackdepth=1):
    if getLoggingLevel() > level:
        return
    callerframerecord = inspect.stack()[stackdepth]
    frame = callerframerecord[0]
    info = inspect.getframeinfo(frame)
    rawLogNamed(name,level, info.filename, info.lineno, info.function, message)

def logFinest(message):
    log(LoggingLevel.Finest, message, stackdepth=2)
    
def logVerbose(message):
    log(LoggingLevel.FineVerbose, message, stackdepth=2)
    
def logFiner(message):
    log(LoggingLevel.Finer, message, stackdepth=2)
    
def logTrace(message):
    log(LoggingLevel.Trace, message, stackdepth=2)
    
def logFine(message):
    log(LoggingLevel.Fine, message, stackdepth=2)
    
def logInfo(message):
    log(LoggingLevel.Info, message, stackdepth=2)

def logDebug(message):
    log(LoggingLevel.Debug, message, stackdepth=2)

def logWarn(message):
    log(LoggingLevel.Warn, message, stackdepth=2)

def logFatal(message):
    log(LoggingLevel.Fatal, message, stackdepth=2)

def logError(message):
    log(LoggingLevel.Error, message, stackdepth=2)


def logFinestNamed(name, message):
    logNamed(name, LoggingLevel.Finest, message, stackdepth=2)
    
def logVerboseNamed(name, message):
    logNamed(name, LoggingLevel.Verbose, message, stackdepth=2)
    
def logFinerNamed(name, message):
    logNamed(name, LoggingLevel.Finer, message, stackdepth=2)
    
def logTraceNamed(name, message):
    logNamed(name, LoggingLevel.Trace, message, stackdepth=2)
    
def logFineNamed(name, message):
    logNamed(name, LoggingLevel.Fine, message, stackdepth=2)

def logInfoNamed(name,message):
    logNamed(name, LoggingLevel.Info, message, stackdepth=2)

def logDebugNamed(name,message):
    logNamed(name, LoggingLevel.Debug, message, stackdepth=2)

def logWarnNamed(name,message):
    logNamed(name, LoggingLevel.Warn, message, stackdepth=2)

def logFatalNamed(name,message):
    logNamed(name, LoggingLevel.Fatal, message, stackdepth=2)

def logErrorNamed(name,message):
    logNamed(name, LoggingLevel.Error, message, stackdepth=2)

def setExtendableValueStoreValue(evs, key, value, add_if_missing=True):
    def type2func_string(value):
        if isinstance(value, str):
            return "String"
        elif isinstance(value, bool):
            return "Bool"
        elif isinstance(value, int):
            return "Int"
        elif isinstance(value, float):
            return "Double"
        else:
            logFatal("Don't know how to add value of type " + str(value.__class__))

    if add_if_missing and not evs.hasKey(key):
        getattr(evs, "add" + type2func_string(value))(key, value)
    else:
        getattr(evs, "set" + type2func_string(value))(key, value)
        

from types import MethodType
ExtendibleValueStoreRef.set = MethodType(setExtendableValueStoreValue, None, ExtendibleValueStoreRef)
@@ -2890,26 +2890,105 @@
     
-else:%0A     
+return None%0A%0A    accessorSuffix = type2func_string(value)%0A    if accessorSuffix is None:%0A
        
+ 
 logF
@@ -3011,19 +3011,19 @@
  how to 
-add
+set
  value o
@@ -3054,16 +3054,31 @@
 lass__))
+%0A        return
 %0A%0A    if
@@ -3147,39 +3147,30 @@
 %22add%22 + 
-type2func_string(value)
+accessorSuffix
 )(key, v
@@ -3214,39 +3214,30 @@
 %22set%22 + 
-type2func_string(value)
+accessorSuffix
 )(key, v
@@ -3382,8 +3382,9 @@
 toreRef)
+%0A

Return after fatal error