text:
## Import Modules
#
from __future__ import print_function
from __future__ import absolute_import
from Common.GlobalData import *
from CommonDataClass.Exceptions import BadExpression
from CommonDataClass.Exceptions import WrnExpression
from .Misc import GuidStringToGuidStructureString, ParseFieldValue,CopyDict
import Common.EdkLogger as EdkLogger
import copy
from Common.DataType import *
import sys
from random import sample
import string

ERR_STRING_EXPR         = 'This operator cannot be used in string expression: [%s].'
ERR_SNYTAX              = 'Syntax error, the rest of expression cannot be evaluated: [%s].'
ERR_MATCH               = 'No matching right parenthesis.'
ERR_STRING_TOKEN        = 'Bad string token: [%s].'
ERR_MACRO_TOKEN         = 'Bad macro token: [%s].'
ERR_EMPTY_TOKEN         = 'Empty token is not allowed.'
ERR_PCD_RESOLVE         = 'The PCD should be FeatureFlag type or FixedAtBuild type: [%s].'
ERR_VALID_TOKEN         = 'No more valid token found from rest of string: [%s].'
ERR_EXPR_TYPE           = 'Different types found in expression.'
ERR_OPERATOR_UNSUPPORT  = 'Unsupported operator: [%s]'
ERR_REL_NOT_IN          = 'Expect "IN" after "not" operator.'
WRN_BOOL_EXPR           = 'Operand of boolean type cannot be used in arithmetic expression.'
WRN_EQCMP_STR_OTHERS    = '== Comparison between Operand of string type and Boolean/Number Type always return False.'
WRN_NECMP_STR_OTHERS    = '!= Comparison between Operand of string type and Boolean/Number Type always return True.'
ERR_RELCMP_STR_OTHERS   = 'Operator taking Operand of string type and Boolean/Number Type is not allowed: [%s].'
ERR_STRING_CMP          = 'Unicode string and general string cannot be compared: [%s %s %s]'
ERR_ARRAY_TOKEN         = 'Bad C array or C format GUID token: [%s].'
ERR_ARRAY_ELE           = 'This must be HEX value for NList or Array: [%s].'
ERR_EMPTY_EXPR          = 'Empty expression is not allowed.'
ERR_IN_OPERAND          = 'Macro after IN operator can only be: $(FAMILY), $(ARCH), $(TOOL_CHAIN_TAG) and $(TARGET).'

__ValidString = re.compile(r'[_a-zA-Z][_0-9a-zA-Z]*$')
_ReLabel = re.compile('LABEL\((\w+)\)')
_ReOffset = re.compile('OFFSET_OF\((\w+)\)')
PcdPattern = re.compile(r'[_a-zA-Z][0-9A-Za-z_]*\.[_a-zA-Z][0-9A-Za-z_]*$')

## SplitString
#  Split string to list according double quote
#  For example: abc"de\"f"ghi"jkl"mn will be: ['abc', '"de\"f"', 'ghi', '"jkl"', 'mn']
#
def SplitString(String):
    # There might be escaped quote: "abc\"def\\\"ghi", 'abc\'def\\\'ghi'
    RanStr = ''.join(sample(string.ascii_letters + string.digits, 8))
    String = String.replace('\\\\', RanStr).strip()
    RetList = []
    InSingleQuote = False
    InDoubleQuote = False
    Item = ''
    for i, ch in enumerate(String):
        if ch == '"' and not InSingleQuote:
            if String[i - 1] != '\\':
                InDoubleQuote = not InDoubleQuote
            if not InDoubleQuote:
                Item += String[i]
                RetList.append(Item)
                Item = ''
                continue
            if Item:
                RetList.append(Item)
                Item = ''
        elif ch == "'" and not InDoubleQuote:
            if String[i - 1] != '\\':
                InSingleQuote = not InSingleQuote
            if not InSingleQuote:
                Item += String[i]
                RetList.append(Item)
                Item = ''
                continue
            if Item:
                RetList.append(Item)
                Item = ''
        Item += String[i]
    if InSingleQuote or InDoubleQuote:
        raise BadExpression(ERR_STRING_TOKEN % Item)
    if Item:
        RetList.append(Item)
    for i, ch in enumerate(RetList):
        if RanStr in ch:
            RetList[i] = ch.replace(RanStr,'\\\\')
    return RetList

def SplitPcdValueString(String):
    # There might be escaped comma in GUID() or DEVICE_PATH() or " "
    # or ' ' or L' ' or L" "
    RanStr = ''.join(sample(string.ascii_letters + string.digits, 8))
    String = String.replace('\\\\', RanStr).strip()
    RetList = []
    InParenthesis = 0
    InSingleQuote = False
    InDoubleQuote = False
    Item = ''
    for i, ch in enumerate(String):
        if ch == '(':
            InParenthesis += 1
        elif ch == ')':
            if InParenthesis:
                InParenthesis -= 1
            else:
                raise BadExpression(ERR_STRING_TOKEN % Item)
        elif ch == '"' and not InSingleQuote:
            if String[i-1] != '\\':
                InDoubleQuote = not InDoubleQuote
        elif ch == "'" and not InDoubleQuote:
            if String[i-1] != '\\':
                InSingleQuote = not InSingleQuote
        elif ch == ',':
            if InParenthesis or InSingleQuote or InDoubleQuote:
                Item += String[i]
                continue
            elif Item:
                RetList.append(Item)
                Item = ''
            continue
        Item += String[i]
    if InSingleQuote or InDoubleQuote or InParenthesis:
        raise BadExpression(ERR_STRING_TOKEN % Item)
    if Item:
        RetList.append(Item)
    for i, ch in enumerate(RetList):
        if RanStr in ch:
            RetList[i] = ch.replace(RanStr,'\\\\')
    return RetList

def IsValidCName(Str):
    return True if __ValidString.match(Str) else False

def BuildOptionValue(PcdValue, GuidDict):
    if PcdValue.startswith('H'):
        InputValue = PcdValue[1:]
    elif PcdValue.startswith("L'") or PcdValue.startswith("'"):
        InputValue = PcdValue
    elif PcdValue.startswith('L'):
        InputValue = 'L"' + PcdValue[1:] + '"'
    else:
        InputValue = PcdValue
    try:
        PcdValue = ValueExpressionEx(InputValue, TAB_VOID, GuidDict)(True)
    except:
        pass

    return PcdValue

## ReplaceExprMacro
#
def ReplaceExprMacro(String, Macros, ExceptionList = None):
    StrList = SplitString(String)
    for i, String in enumerate(StrList):
        InQuote = False
        if String.startswith('"'):
            InQuote = True
        MacroStartPos = String.find('$(')
        if MacroStartPos < 0:
            for Pcd in gPlatformPcds:
                if Pcd in String:
                    if Pcd not in gConditionalPcds:
                        gConditionalPcds.append(Pcd)
            continue
        RetStr = ''
        while MacroStartPos >= 0:
            RetStr = String[0:MacroStartPos]
            MacroEndPos = String.find(')', MacroStartPos)
            if MacroEndPos < 0:
                raise BadExpression(ERR_MACRO_TOKEN % String[MacroStartPos:])
            Macro = String[MacroStartPos+2:MacroEndPos]
            if Macro not in Macros:
                # From C reference manual:
                # If an undefined macro name appears in the constant-expression of
                # !if or !elif, it is replaced by the integer constant 0.
                RetStr += '0'
            elif not InQuote:
                Tklst = RetStr.split()
                if Tklst and Tklst[-1] in {'IN', 'in'} and ExceptionList and Macro not in ExceptionList:
                    raise BadExpression(ERR_IN_OPERAND)
                # Make sure the macro in exception list is encapsulated by double quote
                # For example: DEFINE ARCH = IA32 X64
                # $(ARCH) is replaced with "IA32 X64"
                if ExceptionList and Macro in ExceptionList:
                    RetStr += '"' + Macros[Macro] + '"'
                elif Macros[Macro].strip():
                    RetStr += Macros[Macro]
                else:
                    RetStr += '""'
            else:
                RetStr += Macros[Macro]
            RetStr += String[MacroEndPos+1:]
            String = RetStr
            MacroStartPos = String.find('$(')
        StrList[i] = RetStr
    return ''.join(StrList)

# transfer int to string for in/not in expression
def IntToStr(Value):
    StrList = []
    while Value > 0:
        StrList.append(chr(Value & 0xff))
        Value = Value >> 8
    Value = '"' + ''.join(StrList) + '"'
    return Value

SupportedInMacroList = ['TARGET', 'TOOL_CHAIN_TAG', 'ARCH', 'FAMILY']

class BaseExpression(object):
    def __init__(self, *args, **kwargs):
        super(BaseExpression, self).__init__()

    # Check if current token matches the operators given from parameter
    def _IsOperator(self, OpSet):
        Idx = self._Idx
        self._GetOperator()
        if self._Token in OpSet:
            if self._Token in self.LogicalOperators:
                self._Token = self.LogicalOperators[self._Token]
            return True
        self._Idx = Idx
        return False

class ValueExpression(BaseExpression):
    # Logical operator mapping
    LogicalOperators = {
        '&&' : 'and', '||' : 'or',
        '!'  : 'not', 'AND': 'and',
        'OR' : 'or' , 'NOT': 'not',
        'XOR': '^'  , 'xor': '^',
        'EQ' : '==' , 'NE' : '!=',
        'GT' : '>'  , 'LT' : '<',
        'GE' : '>=' , 'LE' : '<=',
        'IN' : 'in'
    }

    NonLetterOpLst = ['+', '-', TAB_STAR, '/', '%', '&', '|', '^', '~', '<<', '>>', '!', '=', '>', '<', '?', ':']

    SymbolPattern = re.compile("("
                                 "\$\([A-Z][A-Z0-9_]*\)|\$\(\w+\.\w+\)|\w+\.\w+|"
                                 "&&|\|\||!(?!=)|"
                                 "(?<=\W)AND(?=\W)|(?<=\W)OR(?=\W)|(?<=\W)NOT(?=\W)|(?<=\W)XOR(?=\W)|"
                                 "(?<=\W)EQ(?=\W)|(?<=\W)NE(?=\W)|(?<=\W)GT(?=\W)|(?<=\W)LT(?=\W)|(?<=\W)GE(?=\W)|(?<=\W)LE(?=\W)"
                               ")")

    @staticmethod
    def Eval(Operator, Oprand1, Oprand2 = None):
        WrnExp = None

        if Operator not in {"==", "!=", ">=", "<=", ">", "<", "in", "not in"} and \
            (isinstance(Oprand1, type('')) or isinstance(Oprand2, type(''))):
            raise BadExpression(ERR_STRING_EXPR % Operator)
        if Operator in {'in', 'not in'}:
            if not isinstance(Oprand1, type('')):
                Oprand1 = IntToStr(Oprand1)
            if not isinstance(Oprand2, type('')):
                Oprand2 = IntToStr(Oprand2)
        TypeDict = {
            type(0)  : 0,
            # For python2 long type
            type(sys.maxsize + 1) : 0,
            type('') : 1,
            type(True) : 2
        }

        EvalStr = ''
        if Operator in {"!", "NOT", "not"}:
            if isinstance(Oprand1, type('')):
                raise BadExpression(ERR_STRING_EXPR % Operator)
            EvalStr = 'not Oprand1'
        elif Operator in {"~"}:
            if isinstance(Oprand1, type('')):
                raise BadExpression(ERR_STRING_EXPR % Operator)
            EvalStr = '~ Oprand1'
        else:
            if Operator in {"+", "-"} and (type(True) in {type(Oprand1), type(Oprand2)}):
                # Boolean in '+'/'-' will be evaluated but raise warning
                WrnExp = WrnExpression(WRN_BOOL_EXPR)
            elif type('') in {type(Oprand1), type(Oprand2)} and not isinstance(Oprand1, type(Oprand2)):
                # == between string and number/boolean will always return False, != return True
                if Operator == "==":
                    WrnExp = WrnExpression(WRN_EQCMP_STR_OTHERS)
                    WrnExp.result = False
                    raise WrnExp
                elif Operator == "!=":
                    WrnExp = WrnExpression(WRN_NECMP_STR_OTHERS)
                    WrnExp.result = True
                    raise WrnExp
                else:
                    raise BadExpression(ERR_RELCMP_STR_OTHERS % Operator)
            elif TypeDict[type(Oprand1)] != TypeDict[type(Oprand2)]:
                if Operator in {"==", "!=", ">=", "<=", ">", "<"} and set((TypeDict[type(Oprand1)], TypeDict[type(Oprand2)])) == set((TypeDict[type(True)], TypeDict[type(0)])):
                    # comparison between number and boolean is allowed
                    pass
                elif Operator in {'&', '|', '^', "and", "or"} and set((TypeDict[type(Oprand1)], TypeDict[type(Oprand2)])) == set((TypeDict[type(True)], TypeDict[type(0)])):
                    # bitwise and logical operation between number and boolean is allowed
                    pass
                else:
                    raise BadExpression(ERR_EXPR_TYPE)
            if isinstance(Oprand1, type('')) and isinstance(Oprand2, type('')):
                if ((Oprand1.startswith('L"') or Oprand1.startswith("L'")) and (not Oprand2.startswith('L"')) and (not Oprand2.startswith("L'"))) or \
                        (((not Oprand1.startswith('L"')) and (not Oprand1.startswith("L'"))) and (Oprand2.startswith('L"') or Oprand2.startswith("L'"))):
                    raise BadExpression(ERR_STRING_CMP % (Oprand1, Operator, Oprand2))
            if 'in' in Operator and isinstance(Oprand2, type('')):
                Oprand2 = Oprand2.split()
            EvalStr = 'Oprand1 ' + Operator + ' Oprand2'

        # Local symbols used by built in eval function
        Dict = {
            'Oprand1' : Oprand1,
            'Oprand2' : Oprand2
        }
        try:
            Val = eval(EvalStr, {}, Dict)
        except Exception as Excpt:
            raise BadExpression(str(Excpt))

        if Operator in {'and', 'or'}:
            if Val:
                Val = True
            else:
                Val = False

        if WrnExp:
            WrnExp.result = Val
            raise WrnExp
        return Val

    def __init__(self, Expression, SymbolTable={}):
        super(ValueExpression, self).__init__(self, Expression, SymbolTable)
        self._NoProcess = False
        if not isinstance(Expression, type('')):
            self._Expr = Expression
            self._NoProcess = True
            return

        self._Expr = ReplaceExprMacro(Expression.strip(),
                                  SymbolTable,
                                  SupportedInMacroList)

        if not self._Expr.strip():
            raise BadExpression(ERR_EMPTY_EXPR)

        #
        # The symbol table including PCD and macro mapping
        #
        self._Symb = CopyDict(SymbolTable)
        self._Symb.update(self.LogicalOperators)
        self._Idx = 0
        self._Len = len(self._Expr)
        self._Token = ''
        self._WarnExcept = None

        # Literal token without any conversion
        self._LiteralToken = ''

    # Public entry for this class
    #   @param RealValue: False: only evaluate if the expression is true or false, used for conditional expression
    #                     True : return the evaluated str(value), used for PCD value
    #
    #   @return: True or False if RealValue is False
    #            Evaluated value of string format if RealValue is True
    #
    def __call__(self, RealValue=False, Depth=0):
        if self._NoProcess:
            return self._Expr

        self._Depth = Depth

        self._Expr = self._Expr.strip()
        if RealValue and Depth == 0:
            self._Token = self._Expr
            if self.__IsNumberToken():
                return self._Expr
            Token = ''
            try:
                Token = self._GetToken()
            except BadExpression:
                pass
            if isinstance(Token, type('')) and Token.startswith('{') and Token.endswith('}') and self._Idx >= self._Len:
                return self._Expr

            self._Idx = 0
            self._Token = ''

        Val = self._ConExpr()
        RealVal = Val
        if isinstance(Val, type('')):
            if Val == 'L""':
                Val = False
            elif not Val:
                Val = False
                RealVal = '""'
            elif not Val.startswith('L"') and not Val.startswith('{') and not Val.startswith("L'") and not Val.startswith("'"):
                Val = True
                RealVal = '"' + RealVal + '"'

        # The expression has been parsed, but the end of expression is not reached
        # It means the rest does not comply EBNF of <Expression>
        if self._Idx != self._Len:
            raise BadExpression(ERR_SNYTAX % self._Expr[self._Idx:])

        if RealValue:
            RetVal = str(RealVal)
        elif Val:
            RetVal = True
        else:
            RetVal = False

        if self._WarnExcept:
            self._WarnExcept.result = RetVal
            raise self._WarnExcept
        else:
            return RetVal

    # Template function to parse binary operators which have same precedence
    # Expr [Operator Expr]*
    def _ExprFuncTemplate(self, EvalFunc, OpSet):
        Val = EvalFunc()
        while self._IsOperator(OpSet):
            Op = self._Token
            if Op == '?':
                Val2 = EvalFunc()
                if self._IsOperator({':'}):
                    Val3 = EvalFunc()
                if Val:
                    Val = Val2
                else:
                    Val = Val3
                continue
            try:
                Val = self.Eval(Op, Val, EvalFunc())
            except WrnExpression as Warn:
                self._WarnExcept = Warn
                Val = Warn.result
        return Val
    # A [? B]*
    def _ConExpr(self):
        return self._ExprFuncTemplate(self._OrExpr, {'?', ':'})

    # A [|| B]*
    def _OrExpr(self):
        return self._ExprFuncTemplate(self._AndExpr, {"OR", "or", "||"})

    # A [&& B]*
    def _AndExpr(self):
        return self._ExprFuncTemplate(self._BitOr, {"AND", "and", "&&"})

    # A [ | B]*
    def _BitOr(self):
        return self._ExprFuncTemplate(self._BitXor, {"|"})

    # A [ ^ B]*
    def _BitXor(self):
        return self._ExprFuncTemplate(self._BitAnd, {"XOR", "xor", "^"})

    # A [ & B]*
    def _BitAnd(self):
        return self._ExprFuncTemplate(self._EqExpr, {"&"})

    # A [ == B]*
    def _EqExpr(self):
        Val = self._RelExpr()
        while self._IsOperator({"==", "!=", "EQ", "NE", "IN", "in", "!", "NOT", "not"}):
            Op = self._Token
            if Op in {"!", "NOT", "not"}:
                if not self._IsOperator({"IN", "in"}):
                    raise BadExpression(ERR_REL_NOT_IN)
                Op += ' ' + self._Token
            try:
                Val = self.Eval(Op, Val, self._RelExpr())
            except WrnExpression as Warn:
                self._WarnExcept = Warn
                Val = Warn.result
        return Val

    # A [ > B]*
    def _RelExpr(self):
        return self._ExprFuncTemplate(self._ShiftExpr, {"<=", ">=", "<", ">", "LE", "GE", "LT", "GT"})

    def _ShiftExpr(self):
        return self._ExprFuncTemplate(self._AddExpr, {"<<", ">>"})

    # A [ + B]*
    def _AddExpr(self):
        return self._ExprFuncTemplate(self._MulExpr, {"+", "-"})

    # A [ * B]*
    def _MulExpr(self):
        return self._ExprFuncTemplate(self._UnaryExpr, {TAB_STAR, "/", "%"})

    # [!]*A
    def _UnaryExpr(self):
        if self._IsOperator({"!", "NOT", "not"}):
            Val = self._UnaryExpr()
            try:
                return self.Eval('not', Val)
            except WrnExpression as Warn:
                self._WarnExcept = Warn
                return Warn.result
        if self._IsOperator({"~"}):
            Val = self._UnaryExpr()
            try:
                return self.Eval('~', Val)
            except WrnExpression as Warn:
                self._WarnExcept = Warn
                return Warn.result
        return self._IdenExpr()

    # Parse identifier or encapsulated expression
    def _IdenExpr(self):
        Tk = self._GetToken()
        if Tk == '(':
            Val = self._ConExpr()
            try:
                # _GetToken may also raise BadExpression
                if self._GetToken() != ')':
                    raise BadExpression(ERR_MATCH)
            except BadExpression:
                raise BadExpression(ERR_MATCH)
            return Val
        return Tk

    # Skip whitespace or tab
    def __SkipWS(self):
        for Char in self._Expr[self._Idx:]:
            if Char not in ' \t':
                break
            self._Idx += 1

    # Try to convert string to number
    def __IsNumberToken(self):
        Radix = 10
        if self._Token.lower()[0:2] == '0x' and len(self._Token) > 2:
            Radix = 16
        if self._Token.startswith('"') or self._Token.startswith('L"'):
            Flag = 0
            for Index in range(len(self._Token)):
                if self._Token[Index] in {'"'}:
                    if self._Token[Index - 1] == '\\':
                        continue
                    Flag += 1
            if Flag == 2 and self._Token.endswith('"'):
                return True
        if self._Token.startswith("'") or self._Token.startswith("L'"):
            Flag = 0
            for Index in range(len(self._Token)):
                if self._Token[Index] in {"'"}:
                    if self._Token[Index - 1] == '\\':
                        continue
                    Flag += 1
            if Flag == 2 and self._Token.endswith("'"):
                return True
        try:
            self._Token = int(self._Token, Radix)
            return True
        except ValueError:
            return False
        except TypeError:
            return False

    # Parse array: {...}
    def __GetArray(self):
        Token = '{'
        self._Idx += 1
        self.__GetNList(True)
        Token += self._LiteralToken
        if self._Idx >= self._Len or self._Expr[self._Idx] != '}':
            raise BadExpression(ERR_ARRAY_TOKEN % Token)
        Token += '}'

        # All whitespace and tabs in array are already stripped.
        IsArray = IsGuid = False
        if len(Token.split(',')) == 11 and len(Token.split(',{')) == 2 \
            and len(Token.split('},')) == 1:
            HexLen = [11, 6, 6, 5, 4, 4, 4, 4, 4, 4, 6]
            HexList= Token.split(',')
            if HexList[3].startswith('{') and \
                not [Index for Index, Hex in enumerate(HexList) if len(Hex) > HexLen[Index]]:
                IsGuid = True
        if Token.lstrip('{').rstrip('}').find('{') == -1:
            if not [Hex for Hex in Token.lstrip('{').rstrip('}').split(',') if len(Hex) > 4]:
                IsArray = True
        if not IsArray and not IsGuid:
            raise BadExpression(ERR_ARRAY_TOKEN % Token)
        self._Idx += 1
        self._Token = self._LiteralToken = Token
        return self._Token

    # Parse string, the format must be: "..."
    def __GetString(self):
        Idx = self._Idx

        # Skip left quote
        self._Idx += 1

        # Replace escape \\\", \"
        if self._Expr[Idx] == '"':
            Expr = self._Expr[self._Idx:].replace('\\\\', '//').replace('\\\"', '\\\'')
            for Ch in Expr:
                self._Idx += 1
                if Ch == '"':
                    break
            self._Token = self._LiteralToken = self._Expr[Idx:self._Idx]
            if not self._Token.endswith('"'):
                raise BadExpression(ERR_STRING_TOKEN % self._Token)
        #Replace escape \\\', \'
        elif self._Expr[Idx] == "'":
            Expr = self._Expr[self._Idx:].replace('\\\\', '//').replace("\\\'", "\\\"")
            for Ch in Expr:
                self._Idx += 1
                if Ch == "'":
                    break
            self._Token = self._LiteralToken = self._Expr[Idx:self._Idx]
            if not self._Token.endswith("'"):
                raise BadExpression(ERR_STRING_TOKEN % self._Token)
        self._Token = self._Token[1:-1]
        return self._Token

    # Get token that is comprised by alphanumeric, underscore or dot(used by PCD)
    # @param IsAlphaOp: Indicate if parsing general token or script operator(EQ, NE...)
    def __GetIdToken(self, IsAlphaOp = False):
        IdToken = ''
        for Ch in self._Expr[self._Idx:]:
            if not self.__IsIdChar(Ch) or ('?' in self._Expr and Ch == ':'):
                break
            self._Idx += 1
            IdToken += Ch

        self._Token = self._LiteralToken = IdToken
        if not IsAlphaOp:
            self.__ResolveToken()
        return self._Token

    # Try to resolve token
    def __ResolveToken(self):
        if not self._Token:
            raise BadExpression(ERR_EMPTY_TOKEN)

        # PCD token
        if PcdPattern.match(self._Token):
            if self._Token not in self._Symb:
                Ex = BadExpression(ERR_PCD_RESOLVE % self._Token)
                Ex.Pcd = self._Token
                raise Ex
            self._Token = ValueExpression(self._Symb[self._Token], self._Symb)(True, self._Depth+1)
            if not isinstance(self._Token, type('')):
                self._LiteralToken = hex(self._Token)
                return

        if self._Token.startswith('"'):
            self._Token = self._Token[1:-1]
        elif self._Token in {"FALSE", "false", "False"}:
            self._Token = False
        elif self._Token in {"TRUE", "true", "True"}:
            self._Token = True
        else:
            self.__IsNumberToken()

    def __GetNList(self, InArray=False):
        self._GetSingleToken()
        if not self.__IsHexLiteral():
            if InArray:
                raise BadExpression(ERR_ARRAY_ELE % self._Token)
            return self._Token

        self.__SkipWS()
        Expr = self._Expr[self._Idx:]
        if not Expr.startswith(','):
            return self._Token

        NList = self._LiteralToken
        while Expr.startswith(','):
            NList += ','
            self._Idx += 1
            self.__SkipWS()
            self._GetSingleToken()
            if not self.__IsHexLiteral():
                raise BadExpression(ERR_ARRAY_ELE % self._Token)
            NList += self._LiteralToken
            self.__SkipWS()
            Expr = self._Expr[self._Idx:]
        self._Token = self._LiteralToken = NList
        return self._Token

    def __IsHexLiteral(self):
        if self._LiteralToken.startswith('{') and \
            self._LiteralToken.endswith('}'):
            return True

        if gHexPattern.match(self._LiteralToken):
            Token = self._LiteralToken[2:]
            if not Token:
                self._LiteralToken = '0x0'
            else:
                self._LiteralToken = '0x' + Token
            return True
        return False

    def _GetToken(self):
        return self.__GetNList()

    @staticmethod
    def __IsIdChar(Ch):
        return Ch in '._:' or Ch.isalnum()

    # Parse operand
    def _GetSingleToken(self):
        self.__SkipWS()
        Expr = self._Expr[self._Idx:]
        if Expr.startswith('L"'):
            # Skip L
            self._Idx += 1
            UStr = self.__GetString()
            self._Token = 'L"' + UStr + '"'
            return self._Token
        elif Expr.startswith("L'"):
            # Skip L
            self._Idx += 1
            UStr = self.__GetString()
            self._Token = "L'" + UStr + "'"
            return self._Token
        elif Expr.startswith("'"):
            UStr = self.__GetString()
            self._Token = "'" + UStr + "'"
            return self._Token
        elif Expr.startswith('UINT'):
            Re = re.compile('(?:UINT8|UINT16|UINT32|UINT64)\((.+)\)')
            try:
                RetValue = Re.search(Expr).group(1)
            except:
                 raise BadExpression('Invalid Expression %s' % Expr)
            Idx = self._Idx
            for Ch in Expr:
                self._Idx += 1
                if Ch == '(':
                    Prefix = self._Expr[Idx:self._Idx - 1]
                    Idx = self._Idx
                if Ch == ')':
                    TmpValue = self._Expr[Idx :self._Idx - 1]
                    TmpValue = ValueExpression(TmpValue)(True)
                    TmpValue = '0x%x' % int(TmpValue) if not isinstance(TmpValue, type('')) else TmpValue
                    break
            self._Token, Size = ParseFieldValue(Prefix + '(' + TmpValue + ')')
            return  self._Token

        self._Token = ''
        if Expr:
            Ch = Expr[0]
            Match = gGuidPattern.match(Expr)
            if Match and not Expr[Match.end():Match.end()+1].isalnum() \
                and Expr[Match.end():Match.end()+1] != '_':
                self._Idx += Match.end()
                self._Token = ValueExpression(GuidStringToGuidStructureString(Expr[0:Match.end()]))(True, self._Depth+1)
                return self._Token
            elif self.__IsIdChar(Ch):
                return self.__GetIdToken()
            elif Ch == '"':
                return self.__GetString()
            elif Ch == '{':
                return self.__GetArray()
            elif Ch == '(' or Ch == ')':
                self._Idx += 1
                self._Token = Ch
                return self._Token

        raise BadExpression(ERR_VALID_TOKEN % Expr)

    # Parse operator
    def _GetOperator(self):
        self.__SkipWS()
        LegalOpLst = ['&&', '||', '!=', '==', '>=', '<='] + self.NonLetterOpLst + ['?', ':']

        self._Token = ''
        Expr = self._Expr[self._Idx:]

        # Reach end of expression
        if not Expr:
            return ''

        # Script operator: LT, GT, LE, GE, EQ, NE, and, or, xor, not
        if Expr[0].isalpha():
            return self.__GetIdToken(True)

        # Start to get regular operator: +, -, <, > ...
        if Expr[0] not in self.NonLetterOpLst:
            return ''

        OpToken = ''
        for Ch in Expr:
            if Ch in self.NonLetterOpLst:
                if Ch in ['!', '~'] and OpToken:
                    break
                self._Idx += 1
                OpToken += Ch
            else:
                break

        if OpToken not in LegalOpLst:
            raise BadExpression(ERR_OPERATOR_UNSUPPORT % OpToken)
        self._Token = OpToken
        return OpToken

class ValueExpressionEx(ValueExpression):
    def __init__(self, PcdValue, PcdType, SymbolTable={}):
        ValueExpression.__init__(self, PcdValue, SymbolTable)
        self.PcdValue = PcdValue
        self.PcdType = PcdType

    def __call__(self, RealValue=False, Depth=0):
        PcdValue = self.PcdValue
        if "{CODE(" not in PcdValue:
            try:
                PcdValue = ValueExpression.__call__(self, RealValue, Depth)
                if self.PcdType == TAB_VOID and (PcdValue.startswith("'") or PcdValue.startswith("L'")):
                    PcdValue, Size = ParseFieldValue(PcdValue)
                    PcdValueList = []
                    for I in range(Size):
                        PcdValueList.append('0x%02X'%(PcdValue & 0xff))
                        PcdValue = PcdValue >> 8
                    PcdValue = '{' + ','.join(PcdValueList) + '}'
                elif self.PcdType in TAB_PCD_NUMERIC_TYPES and (PcdValue.startswith("'") or \
                          PcdValue.startswith('"') or PcdValue.startswith("L'") or PcdValue.startswith('L"') or PcdValue.startswith('{')):
                    raise BadExpression
            except WrnExpression as Value:
                PcdValue = Value.result
            except BadExpression as Value:
                if self.PcdType in TAB_PCD_NUMERIC_TYPES:
                    PcdValue = PcdValue.strip()
                    if PcdValue.startswith('{') and PcdValue.endswith('}'):
                        PcdValue = SplitPcdValueString(PcdValue[1:-1])
                    if isinstance(PcdValue, type([])):
                        TmpValue = 0
                        Size = 0
                        ValueType = ''
                        for Item in PcdValue:
                            Item = Item.strip()
                            if Item.startswith(TAB_UINT8):
                                ItemSize = 1
                                ValueType = TAB_UINT8
                            elif Item.startswith(TAB_UINT16):
                                ItemSize = 2
                                ValueType = TAB_UINT16
                            elif Item.startswith(TAB_UINT32):
                                ItemSize = 4
                                ValueType = TAB_UINT32
                            elif Item.startswith(TAB_UINT64):
                                ItemSize = 8
                                ValueType = TAB_UINT64
                            elif Item[0] in {'"', "'", 'L'}:
                                ItemSize = 0
                                ValueType = TAB_VOID
                            else:
                                ItemSize = 0
                                ValueType = TAB_UINT8
                            Item = ValueExpressionEx(Item, ValueType, self._Symb)(True)
                            if ItemSize == 0:
                                try:
                                    tmpValue = int(Item, 0)
                                    if tmpValue > 255:
                                        raise BadExpression("Byte  array number %s should less than 0xFF." % Item)
                                except BadExpression as Value:
                                    raise BadExpression(Value)
                                except ValueError:
                                    pass
                                ItemValue, ItemSize = ParseFieldValue(Item)
                            else:
                                ItemValue = ParseFieldValue(Item)[0]

                            if isinstance(ItemValue, type('')):
                                ItemValue = int(ItemValue, 0)

                            TmpValue = (ItemValue << (Size * 8)) | TmpValue
                            Size = Size + ItemSize
                    else:
                        try:
                            TmpValue, Size = ParseFieldValue(PcdValue)
                        except BadExpression as Value:
                            raise BadExpression("Type: %s, Value: %s, %s" % (self.PcdType, PcdValue, Value))
                    if isinstance(TmpValue, type('')):
                        try:
                            TmpValue = int(TmpValue)
                        except:
                            raise  BadExpression(Value)
                    else:
                        PcdValue = '0x%0{}X'.format(Size) % (TmpValue)
                    if TmpValue < 0:
                        raise  BadExpression('Type %s PCD Value is negative' % self.PcdType)
                    if self.PcdType == TAB_UINT8 and Size > 1:
                        raise BadExpression('Type %s PCD Value Size is Larger than 1 byte' % self.PcdType)
                    if self.PcdType == TAB_UINT16 and Size > 2:
                        raise BadExpression('Type %s PCD Value Size is Larger than 2 byte' % self.PcdType)
                    if self.PcdType == TAB_UINT32 and Size > 4:
                        raise BadExpression('Type %s PCD Value Size is Larger than 4 byte' % self.PcdType)
                    if self.PcdType == TAB_UINT64 and Size > 8:
                        raise BadExpression('Type %s PCD Value Size is Larger than 8 byte' % self.PcdType)
                else:
                    try:
                        TmpValue = int(PcdValue)
                        TmpList = []
                        if TmpValue.bit_length() == 0:
                            PcdValue = '{0x00}'
                        else:
                            for I in range((TmpValue.bit_length() + 7) / 8):
                                TmpList.append('0x%02x' % ((TmpValue >> I * 8) & 0xff))
                            PcdValue = '{' + ', '.join(TmpList) + '}'
                    except:
                        if PcdValue.strip().startswith('{'):
                            PcdValueList = SplitPcdValueString(PcdValue.strip()[1:-1])
                            LabelDict = {}
                            NewPcdValueList = []
                            LabelOffset = 0
                            for Item in PcdValueList:
                                # compute byte offset of every LABEL
                                LabelList = _ReLabel.findall(Item)
                                Item = _ReLabel.sub('', Item)
                                Item = Item.strip()
                                if LabelList:
                                    for Label in LabelList:
                                        if not IsValidCName(Label):
                                            raise BadExpression('%s is not a valid c variable name' % Label)
                                        if Label not in LabelDict:
                                            LabelDict[Label] = str(LabelOffset)
                                if Item.startswith(TAB_UINT8):
                                    LabelOffset = LabelOffset + 1
                                elif Item.startswith(TAB_UINT16):
                                    LabelOffset = LabelOffset + 2
                                elif Item.startswith(TAB_UINT32):
                                    LabelOffset = LabelOffset + 4
                                elif Item.startswith(TAB_UINT64):
                                    LabelOffset = LabelOffset + 8
                                else:
                                    try:
                                        ItemValue, ItemSize = ParseFieldValue(Item)
                                        LabelOffset = LabelOffset + ItemSize
                                    except:
                                        LabelOffset = LabelOffset + 1

                            for Item in PcdValueList:
                                # for LABEL parse
                                Item = Item.strip()
                                try:
                                    Item = _ReLabel.sub('', Item)
                                except:
                                    pass
                                try:
                                    OffsetList = _ReOffset.findall(Item)
                                except:
                                    pass
                                # replace each offset, except errors
                                for Offset in OffsetList:
                                    try:
                                        Item = Item.replace('OFFSET_OF({})'.format(Offset), LabelDict[Offset])
                                    except:
                                        raise BadExpression('%s not defined' % Offset)

                                NewPcdValueList.append(Item)

                            AllPcdValueList = []
                            for Item in NewPcdValueList:
                                Size = 0
                                ValueStr = ''
                                TokenSpaceGuidName = ''
                                if Item.startswith(TAB_GUID) and Item.endswith(')'):
                                    try:
                                        TokenSpaceGuidName = re.search('GUID\((\w+)\)', Item).group(1)
                                    except:
                                        pass
                                    if TokenSpaceGuidName and TokenSpaceGuidName in self._Symb:
                                        Item = 'GUID(' + self._Symb[TokenSpaceGuidName] + ')'
                                    elif TokenSpaceGuidName:
                                        raise BadExpression('%s not found in DEC file' % TokenSpaceGuidName)
                                    Item, Size = ParseFieldValue(Item)
                                    for Index in range(0, Size):
                                        ValueStr = '0x%02X' % (int(Item) & 255)
                                        Item >>= 8
                                        AllPcdValueList.append(ValueStr)
                                    continue
                                elif Item.startswith('DEVICE_PATH') and Item.endswith(')'):
                                    Item, Size = ParseFieldValue(Item)
                                    AllPcdValueList.append(Item[1:-1])
                                    continue
                                else:
                                    ValueType = ""
                                    if Item.startswith(TAB_UINT8):
                                        ItemSize = 1
                                        ValueType = TAB_UINT8
                                    elif Item.startswith(TAB_UINT16):
                                        ItemSize = 2
                                        ValueType = TAB_UINT16
                                    elif Item.startswith(TAB_UINT32):
                                        ItemSize = 4
                                        ValueType = TAB_UINT32
                                    elif Item.startswith(TAB_UINT64):
                                        ItemSize = 8
                                        ValueType = TAB_UINT64
                                    else:
                                        ItemSize = 0
                                    if ValueType:
                                        TmpValue = ValueExpressionEx(Item, ValueType, self._Symb)(True)
                                    else:
                                        TmpValue = ValueExpressionEx(Item, self.PcdType, self._Symb)(True)
                                    Item = '0x%x' % TmpValue if not isinstance(TmpValue, type('')) else TmpValue
                                    if ItemSize == 0:
                                        ItemValue, ItemSize = ParseFieldValue(Item)
                                        if Item[0] not in {'"', 'L', '{'} and ItemSize > 1:
                                            raise BadExpression("Byte  array number %s should less than 0xFF." % Item)
                                    else:
                                        ItemValue = ParseFieldValue(Item)[0]
                                    for I in range(0, ItemSize):
                                        ValueStr = '0x%02X' % (int(ItemValue) & 255)
                                        ItemValue >>= 8
                                        AllPcdValueList.append(ValueStr)
                                    Size += ItemSize

                            if Size > 0:
                                PcdValue = '{' + ','.join(AllPcdValueList) + '}'
                        else:
                            raise  BadExpression("Type: %s, Value: %s, %s"%(self.PcdType, PcdValue, Value))

            if PcdValue == 'True':
                PcdValue = '1'
            if PcdValue == 'False':
                PcdValue = '0'

        if RealValue:
            return PcdValue

if __name__ == '__main__':
    pass
    while True:
        input = raw_input('Input expr: ')
        if input in 'qQ':
            break
        try:
            print(ValueExpression(input)(True))
            print(ValueExpression(input)(False))
        except WrnExpression as Ex:
            print(Ex.result)
            print(str(Ex))
        except Exception as Ex:
            print(str(Ex))


text:
package org.http4s
package scalaxml

import cats.effect._
import cats.implicits._
import fs2.Stream
import fs2.text.utf8Encode
import org.http4s.Status.Ok
import scala.xml.Elem

class ScalaXmlSpec extends Http4sSpec {

  def getBody(body: EntityBody[IO]): Array[Byte] = body.compile.toVector.unsafeRunSync.toArray

  def strBody(body: String): EntityBody[IO] = Stream(body).through(utf8Encode)

  "xml" should {
    val server: Request[IO] => IO[Response[IO]] = { req =>
      req.decode { elem: Elem =>
        IO.pure(Response[IO](Ok).withEntity(elem.label))
      }
    }

    "parse the XML" in {
      val resp = server(Request[IO](body = strBody("
h1
"))).unsafeRunSync()
      resp.status must_== Ok
      getBody(resp.body) must_== "html".getBytes
    }

    "parse XML in parallel" in {
      // https://github.com/http4s/http4s/issues/1209
      val resp = ((0 to 5).toList)
        .parTraverse(_ =>
          server(Request(body = strBody(
            """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
h1
"""))))
        .unsafeRunSync
      resp.forall(_.status must_== Ok)
      resp.forall(x => getBody(x.body) must_== "html".getBytes)
    }

    "return 400 on parse error" in {
      val body = strBody("This is not XML.")
      val tresp = server(Request[IO](body = body))
      tresp.unsafeRunSync.status must_== Status.BadRequest
    }
  }

  "htmlEncoder" should {
    "render html" in {
      val html = Hello
      writeToString(html) must_== "Hello"
    }
  }
}

redpajama_set_name: RedPajamaGithub


text:
.class Landroid/speech/SpeechRecognizer$Connection;
.super Ljava/lang/Object;
.source "SpeechRecognizer.java"

# interfaces
.implements Landroid/content/ServiceConnection;

# annotations
.annotation system Ldalvik/annotation/EnclosingClass;
    value = Landroid/speech/SpeechRecognizer;
.end annotation

.annotation system Ldalvik/annotation/InnerClass;
    accessFlags = 0x2
    name = "Connection"
.end annotation

# instance fields
.field final synthetic this$0:Landroid/speech/SpeechRecognizer;

# direct methods
.method private constructor <init>(Landroid/speech/SpeechRecognizer;)V
    .locals 0
    .parameter

    .prologue
    .line 162
    iput-object p1, p0, Landroid/speech/SpeechRecognizer$Connection;->this$0:Landroid/speech/SpeechRecognizer;

    invoke-direct {p0}, Ljava/lang/Object;-><init>()V

    return-void
.end method

.method synthetic constructor <init>(Landroid/speech/SpeechRecognizer;Landroid/speech/SpeechRecognizer$1;)V
    .locals 0
    .parameter "x0"
    .parameter "x1"

    .prologue
    .line 162
    invoke-direct {p0, p1}, Landroid/speech/SpeechRecognizer$Connection;-><init>(Landroid/speech/SpeechRecognizer;)V

    return-void
.end method

# virtual methods
.method public onServiceConnected(Landroid/content/ComponentName;Landroid/os/IBinder;)V
    .locals 2
    .parameter "name"
    .parameter "service"

    .prologue
    .line 166
    iget-object v0, p0, Landroid/speech/SpeechRecognizer$Connection;->this$0:Landroid/speech/SpeechRecognizer;

    invoke-static {p2}, Landroid/speech/IRecognitionService$Stub;->asInterface(Landroid/os/IBinder;)Landroid/speech/IRecognitionService;

    move-result-object v1

    #setter for: Landroid/speech/SpeechRecognizer;->mService:Landroid/speech/IRecognitionService;
    invoke-static {v0, v1}, Landroid/speech/SpeechRecognizer;->access$502(Landroid/speech/SpeechRecognizer;Landroid/speech/IRecognitionService;)Landroid/speech/IRecognitionService;

    .line 168
    :goto_0
    iget-object v0, p0, Landroid/speech/SpeechRecognizer$Connection;->this$0:Landroid/speech/SpeechRecognizer;

    #getter for: Landroid/speech/SpeechRecognizer;->mPendingTasks:Ljava/util/Queue;
    invoke-static {v0}, Landroid/speech/SpeechRecognizer;->access$600(Landroid/speech/SpeechRecognizer;)Ljava/util/Queue;

    move-result-object v0

    invoke-interface {v0}, Ljava/util/Queue;->isEmpty()Z

    move-result v0

    if-nez v0, :cond_0

    .line 169
    iget-object v0, p0, Landroid/speech/SpeechRecognizer$Connection;->this$0:Landroid/speech/SpeechRecognizer;

    #getter for: Landroid/speech/SpeechRecognizer;->mHandler:Landroid/os/Handler;
    invoke-static {v0}, Landroid/speech/SpeechRecognizer;->access$700(Landroid/speech/SpeechRecognizer;)Landroid/os/Handler;

    move-result-object v1

    iget-object v0, p0, Landroid/speech/SpeechRecognizer$Connection;->this$0:Landroid/speech/SpeechRecognizer;

    #getter for: Landroid/speech/SpeechRecognizer;->mPendingTasks:Ljava/util/Queue;
    invoke-static {v0}, Landroid/speech/SpeechRecognizer;->access$600(Landroid/speech/SpeechRecognizer;)Ljava/util/Queue;

    move-result-object v0

    invoke-interface {v0}, Ljava/util/Queue;->poll()Ljava/lang/Object;

    move-result-object v0

    check-cast v0, Landroid/os/Message;

    invoke-virtual {v1, v0}, Landroid/os/Handler;->sendMessage(Landroid/os/Message;)Z

    goto :goto_0

    .line 171
    :cond_0
    return-void
.end method

.method public onServiceDisconnected(Landroid/content/ComponentName;)V
    .locals 2
    .parameter "name"

    .prologue
    const/4 v1, 0x0

    .line 175
    iget-object v0, p0, Landroid/speech/SpeechRecognizer$Connection;->this$0:Landroid/speech/SpeechRecognizer;

    #setter for: Landroid/speech/SpeechRecognizer;->mService:Landroid/speech/IRecognitionService;
    invoke-static {v0, v1}, Landroid/speech/SpeechRecognizer;->access$502(Landroid/speech/SpeechRecognizer;Landroid/speech/IRecognitionService;)Landroid/speech/IRecognitionService;

    .line 176
    iget-object v0, p0, Landroid/speech/SpeechRecognizer$Connection;->this$0:Landroid/speech/SpeechRecognizer;

    #setter for: Landroid/speech/SpeechRecognizer;->mConnection:Landroid/speech/SpeechRecognizer$Connection;
    invoke-static {v0, v1}, Landroid/speech/SpeechRecognizer;->access$802(Landroid/speech/SpeechRecognizer;Landroid/speech/SpeechRecognizer$Connection;)Landroid/speech/SpeechRecognizer$Connection;

    .line 177
    iget-object v0, p0, Landroid/speech/SpeechRecognizer$Connection;->this$0:Landroid/speech/SpeechRecognizer;

    #getter for: Landroid/speech/SpeechRecognizer;->mPendingTasks:Ljava/util/Queue;
    invoke-static {v0}, Landroid/speech/SpeechRecognizer;->access$600(Landroid/speech/SpeechRecognizer;)Ljava/util/Queue;

    move-result-object v0

    invoke-interface {v0}, Ljava/util/Queue;->clear()V

    .line 179
    return-void
.end method


text:
package de.twenty11.skysail.server.ext.mail;

import java.util.*;

import javax.mail.*;
import javax.mail.Message;
import javax.mail.internet.*;

import org.osgi.service.component.annotations.*;
import org.osgi.service.event.EventAdmin;

import de.twenty11.skysail.server.app.ApplicationProvider;
import de.twenty11.skysail.server.core.restlet.RouteBuilder;
import de.twenty11.skysail.server.ext.mail.accounts.impl.*;
import de.twenty11.skysail.server.ext.mail.folders.*;
import de.twenty11.skysail.server.ext.mail.mails.*;
import io.skysail.api.repos.DbRepository;
import io.skysail.server.app.SkysailApplication;
import io.skysail.server.menus.*;
import lombok.Getter;

@Component(immediate = true)
public class MailApplication extends SkysailApplication implements ApplicationProvider, MenuItemProvider {

	private static final String APP_NAME = "mail";
    private MailRepository repo;

    @Reference(cardinality = ReferenceCardinality.OPTIONAL)
    @Getter
    private volatile EventAdmin eventAdmin;

	public MailApplication() {
		super(APP_NAME);
	}

	@Reference(policy = ReferencePolicy.DYNAMIC, cardinality = ReferenceCardinality.MANDATORY, target = "(name=MailRepository)")
    public void setRepository(DbRepository repo) {
        this.repo = (MailRepository) repo;
    }

    public void unsetRepository(DbRepository repo) {
        this.repo = null;
    }

    public MailRepository getRepository() {
        return repo;
    }

	@Override
	protected void attach() {
	    super.attach();

		router.attach(new RouteBuilder("", MailRootResource.class));
		router.attach(new RouteBuilder("/mail/", SendMailResource.class));
		router.attach(new RouteBuilder("/accounts", AccountsResource.class));
		router.attach(new RouteBuilder("/accounts/", PostAccountResource.class));
		router.attach(new RouteBuilder("/accounts/{id}", AccountResource.class));
        router.attach(new RouteBuilder("/accounts/{id}/", PutAccountResource.class));
		router.attach(new RouteBuilder("/accounts/{id}/folders", FoldersResource.class));

        router.attach(new RouteBuilder("/folders/{id}", FolderResource.class));
		router.attach(new RouteBuilder("/folders/{id}/mails", MailsResource.class));
	}

	public void send(String subject, String body) {
		send("to", subject, body);
	}

	public void send(String to, String subject, String body) {
		String from = "notes@evandor.de";

		String host = "XXX";//serverConfiguration.getConfigForKey(ServerConfiguration.EMAIL_HOST);

		Properties properties = System.getProperties();
		properties.setProperty("mail.smtp.host", host);
		properties.put("mail.debug", "true");
		properties.put("mail.smtp.auth", "true");
		properties.put("mail.smtp.user", "carsten");
		properties.put("mail.smtp.password", "23");

//		props.put("mail.smtp.host", "SMTPHOST");
//		props.put("mail.smtp.port", "PORTNUMBER");
//		props.put("mail.transport.protocol","smtp");
//		props.put("mail.smtp.starttls.enable", "true");
//		props.put("mail.smtp.tls", "true");
//		props.put("mail.smtp.user", "EXAMPLENAME@PROVIDER.COM");
//		props.put("mail.password", "PASSWORD");

		Session session = Session.getDefaultInstance(properties);
		try {
			session.getTransport();
		} catch (NoSuchProviderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			MimeMessage message = new MimeMessage(session);
			message.setFrom(new InternetAddress(from));
			message.addRecipient(Message.RecipientType.TO, new InternetAddress("evandor@gmail.com"));
			message.setSubject(subject);
			message.setText(body);

			//			Transport.send(message);

			Transport tr = session.getTransport("smtp");
			tr.connect(host, "", "");
			message.saveChanges();      // don't forget this
			tr.sendMessage(message, message.getAllRecipients());
			tr.close();

		} catch (MessagingException mex) {
			mex.printStackTrace();
		}

	}

    @Override
    public List<MenuItem> getMenuEntries() {
        MenuItem menuItem = new MenuItem(APP_NAME, "/" + APP_NAME  + getApiVersion().getVersionPath());
        menuItem.setCategory(MenuItem.Category.APPLICATION_MAIN_MENU);
        return Arrays.asList(menuItem);    }

}


text:
from fylm.model.registration import RegistrationSet
from fylm.model.rotation import RotationSet
from fylm.model.timestamp import TimestampSet
from fylm.model.image import ImageSet as FylmImageSet, Image
from itertools import izip
import logging
from nd2reader import Nd2
from fylm.service.base import BaseSetService

log = logging.getLogger(__name__)

class ImageReader(object):
    """
    Gets ImageSet objects from the nd2reader library, sets metadata as appropriate, applies transformations to each image
    to correct for rotational and translational distortion, and yields the ImageSet. Seamlessly gets images from separate
    ND2 files for an unlimited number of time_periods, so the application will behave as if there was only a single acquisition.

    The registration and rotation can be optionally deactivated in case they misbehave, and the start point can also be set.

    """
    def __init__(self, experiment, register_images=True, rotate_images=True):
        self._experiment = experiment
        self._field_of_view = None
        self._register_images = register_images
        self._rotate_images = rotate_images
        self._registration_set = RegistrationSet(experiment)
        self._rotation_set = RotationSet(experiment)
        self._timestamp_set = TimestampSet(experiment)
        self._time_period = None
        self._nd2 = None

        set_service = BaseSetService()
        for model_set in (self._registration_set, self._rotation_set, self._timestamp_set):
            set_service.load_existing_models(model_set)

    def __len__(self):
        return self.nd2.time_index_count

    @property
    def time_period(self):
        return self._time_period

    @time_period.setter
    def time_period(self, value):
        self._time_period = int(value)
        self._nd2 = None

    @property
    def nd2(self):
        if self._nd2 is None:
            filename = self._experiment.get_nd2_from_time_period(self._time_period)
            self._nd2 = Nd2(filename)
        return self._nd2

    def get_image(self, index, channel="", z_level=1):
        rotation_offset = self._rotation_set.existing[index].offset
        dx, dy = next(self._registration_set.get_data(self.field_of_view, self.time_period))
        timestamp = next(self._timestamp_set.get_data(self.field_of_view, self.time_period))
        raw_image = self.nd2.get_image(index, self.field_of_view, channel, z_level)
        return Image(raw_image.data, rotation_offset, dx, dy, timestamp)

    @property
    def channel_names(self):
        return self.nd2.channel_names

    @property
    def field_of_view(self):
        return self._field_of_view

    @field_of_view.setter
    def field_of_view(self, value):
        self._field_of_view = int(value)

    def __iter__(self):
        """
        Provides image sets for a single time_period.

        """
        rotation_offset = self._rotation_set.get_data(self.field_of_view)
        registration_data = self._registration_set.get_data(self.field_of_view, self.time_period)
        timestamp_data = self._timestamp_set.get_data(self.field_of_view, self.time_period)
        for nd2_image_set, registration_offset, (time_index, timestamp) in izip(self.nd2.image_sets(self.field_of_view),
                                                                                registration_data,
                                                                                timestamp_data):
            yield FylmImageSet(nd2_image_set, rotation_offset, registration_offset, time_index - 1, timestamp)


text:
package com.rjfun.cordova.plugin;

import java.lang.reflect.Method;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.apache.cordova.PluginResult.Status;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.WifiLock;
import android.util.Log;

public class WifiAdmin extends CordovaPlugin {

	private static final String LOGTAG = "WifiAdmin";

    /** Cordova Actions. */
    private static final String ACTION_GET_WIFI_INFO = "getWifiInfo";
    private static final String ACTION_ENABLE_WIFI = "enableWifi";
    private static final String ACTION_CONNECT_WIFI = "connectWifi";
    private static final String ACTION_ENABLE_WIFI_AP = "enableWifiAP";
    private static final String ACTION_ENABLE_WIFI_LOCK = "enableWifiLock";

    private WifiLock wifiLock = null;

    @Override
    public boolean execute(String action, JSONArray inputs, CallbackContext callbackContext) throws JSONException {
        PluginResult result = null;
        if (ACTION_GET_WIFI_INFO.equals(action)) {
            result = executeGetWifiInfo(inputs, callbackContext);

        } else if (ACTION_ENABLE_WIFI.equals(action)) {
            result = executeEnableWifi(inputs, callbackContext);

        } else if (ACTION_CONNECT_WIFI.equals(action)) {
            result = executeConnectWifi(inputs, callbackContext);

        } else if (ACTION_ENABLE_WIFI_AP.equals(action)) {
            result = executeEnableWifiAP(inputs, callbackContext);

        } else if (ACTION_ENABLE_WIFI_LOCK.equals(action)) {
            result = executeEnableWifiLock(inputs, callbackContext);

        } else {
            Log.d(LOGTAG, String.format("Invalid action passed: %s", action));
            result = new PluginResult(Status.INVALID_ACTION);
        }

        if(result != null) callbackContext.sendPluginResult( result );

        return true;
    }

    private PluginResult executeGetWifiInfo(JSONArray inputs, CallbackContext callbackContext) {
    	Log.w(LOGTAG, "executeGetWifiInfo");

		Context context = cordova.getActivity().getApplicationContext();
		WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
		WifiInfo wifiInfo = wifiManager.getConnectionInfo();

		JSONObject obj = new JSONObject();
		try {
			JSONObject activity = new JSONObject();
			activity.put("BSSID", wifiInfo.getBSSID());
			activity.put("HiddenSSID", wifiInfo.getHiddenSSID());
			activity.put("SSID", wifiInfo.getSSID());
			activity.put("MacAddress", wifiInfo.getMacAddress());
			activity.put("IpAddress", wifiInfo.getIpAddress());
			activity.put("NetworkId", wifiInfo.getNetworkId());
			activity.put("RSSI", wifiInfo.getRssi());
			activity.put("LinkSpeed", wifiInfo.getLinkSpeed());
			obj.put("activity", activity);

			JSONArray available = new JSONArray();
	        for (ScanResult scanResult : wifiManager.getScanResults()) {
	        	JSONObject ap = new JSONObject();
	        	ap.put("BSSID", scanResult.BSSID);
	        	ap.put("SSID", scanResult.SSID);
	        	ap.put("frequency", scanResult.frequency);
	        	ap.put("level", scanResult.level);
	        	//netwrok.put("timestamp", String.valueOf(scanResult.timestamp));
	        	ap.put("capabilities", scanResult.capabilities);
	        	available.put(ap);
	        }
	        obj.put("available", available);

		} catch (JSONException e) {
			e.printStackTrace();
			callbackContext.error("JSON Exception");
		}
		callbackContext.success(obj);

    	return null;
    }

    private PluginResult executeEnableWifi(JSONArray inputs, CallbackContext callbackContext) {
    	Log.w(LOGTAG, "executeEnableWifi");

		Context context = cordova.getActivity().getApplicationContext();
		WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

		boolean toEnable = true;
		try {
			toEnable = inputs.getBoolean( 0 );
		} catch (JSONException e) {
		      Log.w(LOGTAG, String.format("Got JSON Exception: %s", e.getMessage()));
		      return new PluginResult(Status.JSON_EXCEPTION);
		}

		wifiManager.setWifiEnabled( toEnable );
		callbackContext.success();

    	return null;
    }

    private PluginResult executeConnectWifi(JSONArray inputs, CallbackContext callbackContext) {
    	Log.w(LOGTAG, "executeConnectWifi");

		boolean toEnable = true;
		try {
			toEnable = inputs.getBoolean( 0 );
		} catch (JSONException e) {
		      Log.w(LOGTAG, String.format("Got JSON Exception: %s", e.getMessage()));
		      return new PluginResult(Status.JSON_EXCEPTION);
		}

		return null;
    }
    /**
     * 创建一个wifi信息
     * @param ssid 名称
     * @param passawrd 密码
     * @param paramInt 有3个参数,1是无密码,2是简单密码,3是wap加密
     * @return
     */
    public WifiConfiguration createWifiAPInfo(boolean enabled,String ssid, String password) {
        //配置网络信息类
        WifiConfiguration localWifiConfiguration1 = new WifiConfiguration();
        //设置配置网络属性
        localWifiConfiguration1.allowedAuthAlgorithms.clear();
        localWifiConfiguration1.allowedGroupCiphers.clear();
        localWifiConfiguration1.allowedKeyManagement.clear();
        localWifiConfiguration1.allowedPairwiseCiphers.clear();
        localWifiConfiguration1.allowedProtocols.clear();

        localWifiConfiguration1.SSID = ssid;
        localWifiConfiguration1.allowedAuthAlgorithms.set(1);
        localWifiConfiguration1.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        localWifiConfiguration1.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
        localWifiConfiguration1.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
        localWifiConfiguration1.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
        localWifiConfiguration1.allowedKeyManagement.set(0);
        localWifiConfiguration1.wepTxKeyIndex = 0;
        if (password==null || password.equals("")) {  //没有密码
            localWifiConfiguration1.wepKeys[0] = "";
            localWifiConfiguration1.allowedKeyManagement.set(0);
            localWifiConfiguration1.wepTxKeyIndex = 0;
        } else{//wap加密
            localWifiConfiguration1.preSharedKey = password;
            localWifiConfiguration1.allowedAuthAlgorithms.set(0);
            localWifiConfiguration1.allowedProtocols.set(1);
            localWifiConfiguration1.allowedProtocols.set(0);
            localWifiConfiguration1.allowedKeyManagement.set(1);
            localWifiConfiguration1.allowedPairwiseCiphers.set(2);
            localWifiConfiguration1.allowedPairwiseCiphers.set(1);
        }
        return localWifiConfiguration1;
    }
    /**
     * 根据wifi信息创建或关闭一个热点
     * @param paramWifiConfiguration
     * @param paramBoolean 关闭标志
     */
    public void createWifiAP(boolean paramBoolean,String ssid,String password) {
        try {
            WifiConfiguration paramWifiConfiguration = createWifiAPInfo(paramBoolean,ssid,password);
            Context context = cordova.getActivity().getApplicationContext();
	    WifiApManager wifiAp = new WifiApManager(context);
            wifiAp.setWifiApEnabled(paramWifiConfiguration,paramBoolean);
            return;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private PluginResult executeEnableWifiAP(JSONArray inputs, CallbackContext callbackContext) {
    	Log.w(LOGTAG, "executeEnableWifiAP");

		boolean toEnable = true;
		try {
			toEnable = inputs.getBoolean( 0 );
			JSONObject jsobj = inputs.getJSONObject(1);
			String ssid = jsobj.getString("ssid");
			String password = jsobj.getString("password");
	    	Log.w(LOGTAG, "ssid: "+ssid+" password: "+password);
	    	createWifiAP(toEnable,ssid,password);
		} catch (JSONException e) {
		      Log.w(LOGTAG, String.format("Got JSON Exception: %s", e.getMessage()));
		      return new PluginResult(Status.JSON_EXCEPTION);
		}

		return new PluginResult(Status.OK);
    }

    private PluginResult executeEnableWifiLock(JSONArray inputs, CallbackContext callbackContext) {
    	Log.w(LOGTAG, "executeEnableWifiLock");

		boolean toEnable = true;
		try {
			toEnable = inputs.getBoolean( 0 );
		} catch (JSONException e) {
		      Log.w(LOGTAG, String.format("Got JSON Exception: %s", e.getMessage()));
		      return new PluginResult(Status.JSON_EXCEPTION);
		}

		Context context = cordova.getActivity().getApplicationContext();
		WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

		if(wifiLock == null) {
			wifiLock = wifiManager.createWifiLock("Test");
		}

		if(wifiLock != null) {
			if(toEnable) {
				wifiLock.acquire();
			} else {
		        if(wifiLock.isHeld()) {
		        	wifiLock.release();
		        }
			}
		}

		callbackContext.success();

    	return null;
    }
}


text:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xunit;

namespace Alpheus
{
    public partial class SSHDTests
    {
        public SSHD sshd_1;

        public SSHDTests()
        {
            sshd_1 = new SSHD(Path.Combine("SSHD", "sshd_config"), true);
        }

        [Fact]
        public void CanReadFile()
        {
            Assert.True(sshd_1.File.Exists);
            Assert.True(!string.IsNullOrEmpty(sshd_1.FileContents));
            SSHD sshd_2 = new SSHD("dynamic file", true, true, (parent, path) =>
                {
                    return parent.FilePath + " dynamic file contents";

                });
            Assert.Equal(sshd_2.FileContents, sshd_2.FilePath + " dynamic file contents");
        }

    }
}