import locale
import clr
clr.AddReference("Ans.Utilities")
from Ansys.Utilities.CultureUtilities import *

class CustomUnits:

    def __init__(self, api, load, prop):
        self.api = api
        decimalSeparator = GetDecimalSeparator(False)
        digitSeparator = GetDigitGroupSeparator(False)
        locale._override_localeconv['decimal_point'] = decimalSeparator
        locale._override_localeconv['thousands_sep'] = digitSeparator

    def value2string(self,load,prop,value):
        try:
            result = locale.str(value) + ' [' + prop.GetAttributeValue('unit') + ']'
        except:
            return
        return result

    def string2value(self,load,prop,inputString):
        try:
            result = locale.atof(inputString)
        except:
            return prop.Value
        return result

    def oninit(self,load,prop):
        try:
            prop.Value = locale.atof(prop.Value)
        except:
            return
