STUBS = {

# ______________________________________________________________________________________________ Class #
"ClassDefinition":"""
/// DO NOT EDIT. Generated by {ClassName} python class
Class {PackageName}.{ClassName} Extends {Superclass}
{{

{props}

{params}

{methods} 

}}

""",
# ______________________________________________________________________________________________ Property #
"Property":"""
Property {name} As {type}{init};{doc}
""",

# ______________________________________________________________________________________________ Parameter #
"Parameter":"""
Parameter {name} = "{default}";
""",

# ______________________________________________________________________________________________ Methods #

"Common":{

######## OnInit
    "OnInit":
    """

Property PythonClassObject As %SYS.Python;

Method OnInit() As %Status
{{
    s status = $$$OK
    try{{
        set sys = ##class(%SYS.Python).Import("sys")
        if (sys.path."__getitem__"(0) '= "{ScriptPath}") {{
           do sys.path.insert(0, "{ScriptPath}")
        }}
        set PyLib = ##class(%SYS.Python).Import("{ScriptName}")
        set ..PythonClassObject = PyLib.{ClassName}($this)
    }} catch e {{
        set status = $system.Status.Error(5001, e.AsSystemError())
        $$$LOGERROR("Error while importing the class {ClassName}: "_status)
    }}
    Quit status
}}

""",

######## OnInitBP
    "OnInitBP":
    """

Property PythonClassName As %String(MAXLEN = "") [ InitialExpression = "{ClassName}" ];

Property PythonModuleOrScript As %String(MAXLEN = "") [ InitialExpression = "{ScriptName}" ];

Method OnInit() As %Status
{{
    s status = $$$OK
    try{{
        set sys = ##class(%SYS.Python).Import("sys")
        if (sys.path."__getitem__"(0) '= "{ScriptPath}") {{
           do sys.path.insert(0, "{ScriptPath}")
        }}
    }} catch e {{
        set status = $system.Status.Error(5001, e.AsSystemError())
        $$$LOGERROR("Error while importing the {Superclass} {ClassName}: "_status)
    }}
    Quit status
}}

"""

},

# ______________________________________________________________________________________________ InboundAdapter # 
"InboundAdapter": {
    # OnTask
    "OnTask": 
            """

Method OnTask() As %Status
{{
    try{{
        s status = ..PythonClassObject.OnTaskHelper()
    }} catch e {{
        set status = $system.Status.Error(5001, e.AsSystemError())
        $$$LOGERROR(status)
    }}
    Quit status
}}

            """,
    # OnTearDown
    "OnTearDown": 
            """

Method OnTearDown() As %Status
{{
    Quit $$$OK
}}

            """
    },

# ______________________________________________________________________________________________ BusinessService # 
"BusinessService": {

    "OnProcessInput": 
            """

Method OnProcessInput(pInput As %RegisteredObject, Output pOutput As %RegisteredObject) As %Status
{{
    try{{
        set arr = ..PythonClassObject.OnProcessInputHelper(pInput)
        set status = arr."__getitem__"("status")
        if arr."__getitem__"("pOutput_available") {{
            set pOutput = arr."__getitem__"("pOutput")
        }}
    }} catch e{{
        set status = $system.Status.Error(5001, e.AsSystemError())
        $$$LOGERROR(status)
    }}

    Quit status
}}

            """,
    "OnTearDown": 
            """

Method OnTearDown() As %Status
{{
    Quit $$$OK
}}

            """
    },
    
# ______________________________________________________________________________________________ BusinessProcess # 
"BusinessProcess": {
    # OnRequest
    "OnRequest": 
            """
Method OnRequest(request As %Library.Persistent, Output response As %Library.Persistent) As %Status
{{
      try{{
        set PyLib = ##class(%SYS.Python).Import(..PythonModuleOrScript)
        set PythonClassObject = $method(PyLib,..PythonClassName, $this)
        Set arr = PythonClassObject.OnRequestHelper(request)
        set status = arr."__getitem__"("status")
        if arr."__getitem__"("response_available"){{
          set response = arr."__getitem__"("response") 
        }}

      }} catch e {{
        set status = $system.Status.Error(5001, e.AsSystemError())
        $$$LOGERROR("ERROR IN BP"_status)
      }}
    Quit status
}}
            """,

    # OnResponse
    "OnResponse": """

Method OnResponse(request As %Library.Persistent, ByRef response As %Library.Persistent, callrequest As %Library.Persistent, callresponse As %Library.Persistent, pCompletionKey As %String) As %Status
{{
    try{{
          set PyLib = ##class(%SYS.Python).Import(..PythonModuleOrScript)
          set PythonClassObject = $method(PyLib,..PythonClassName, $this)
          set arr = PythonClassObject.OnResponseHelper(request,response,callrequest,callresponse,pCompletionKey)
          set status = arr."__getitem__"("status")
          if arr."__getitem__"("response_available"){{
            set response = arr."__getitem__"("response") 
            }}
      }} catch e {{
          set status = $system.Status.Error(5001, e.AsSystemError())
          $$$LOGERROR(status)
      }}
    Quit status
}}

""",
        "OnTearDown": """

Method OnTearDown() As %Status
{{
    Quit $$$OK
}}

"""
    },


# ______________________________________________________________________________________________ BusinessOperation #  
"BusinessOperation": {
    # Any method for a business operation will have the same custom stub as follows.
        "AnyMethod": """
Method {MethodNameModified}(pRequest As %Library.Persistent, Output pResponse As %Library.Persistent) As %Status
{{
    try{{
          Set arr = ..PythonClassObject.AnyMethodHelper(pRequest,"{MethodName}")
          set status = arr."__getitem__"("status")
          if arr."__getitem__"("response_available"){{
            set pResponse = arr."__getitem__"("response") 
            }}
      }} catch e {{
          set status = $system.Status.Error(5001, e.AsSystemError())
          $$$LOGERROR(status)
      }}
    Quit status
}}


"""
,
"OnMessage": """
Method OnMessage(pRequest As %Library.Persistent, Output pResponse As %Library.Persistent) As %Status
{{
    try{{
          Set arr = ..PythonClassObject.OnMessageHelper(pRequest,pResponse)
          set status = arr."__getitem__"("status")
          if arr."__getitem__"("response_available"){{
            set pResponse = arr."__getitem__"("response") 
            }}
      }} catch e {{
          set status = $system.Status.Error(5001, e.AsSystemError())
          $$$LOGERROR(status)
      }}
    Quit status
}}


"""
    },

# ______________________________________________________________________________________________ OutboundAdapter #  
    "OutboundAdapter":{
        # Any method for an outbound adapter will have the same custom stub as follows.
        "AnyMethod":"""

Method {MethodNameModified}(arguments As %Library.RegisteredObject, Output pResponse As %Library.RegisteredObject) As %Status
{{
    try{{
          Set arr = ..PythonClassObject.AnyMethodHelper(arguments,"{MethodName}")
          set status = arr."__getitem__"("status")
          if arr."__getitem__"("response_available"){{
            set pResponse = arr."__getitem__"("response") 
            }}
      }} catch e {{
          set status = $system.Status.Error(5001, e.AsSystemError())
          $$$LOGERROR(status)
      }}
    Quit status
}}

"""
    },

# ______________________________________________________________________________________________ PickleSerialize # 
"PickleSerialize": """
/// DO NOT EDIT. Generated by {ClassName} python class
Class {PackageName}.{ClassName} Extends (%Persistent, Ens.Request)
{{

Property SerializedStream As %Stream.GlobalBinary;

{props}

{indices}

/// Used by Business Process and BusinessOperation callbacks to send in a python object
/// in case the incoming request/response if of a user defined persistable type..
Method chunksFromIRIS(iteration As %Integer, chunkSize As %Integer = 1048576) As %SYS.Python
{{
    if iteration = 0 {{
        do ..SerializedStream.Rewind()
    }}
    if ..SerializedStream.AtEnd {{
        return ##class(%SYS.Python).Bytes("")
    }}
    Set chunkfromstream = ..SerializedStream.Read(chunkSize)
    return ##class(%SYS.Python).Bytes(chunkfromstream)
    
}}


}}
"""
,
# ______________________________________________________________________________________________ JsonSerialize # 
"JsonSerialize": """
/// DO NOT EDIT. Generated by {ClassName} python class
Class {PackageName}.{ClassName} Extends (%Persistent,Ens.Request)
{{

Property SerializedStream As %Stream.GlobalCharacter;

{props}

{indices}

/// Used by Business Process and BusinessOperation callbacks to send in a python object
/// in case the incoming request/response if of a user defined persistable type..
Method chunksFromIRIS(iteration As %Integer, chunkSize As %Integer = 1048576) As %String
{{
    if iteration = 0 {{
        do ..SerializedStream.Rewind()
    }}
    if ..SerializedStream.AtEnd {{
        return ""
    }}
    return ..SerializedStream.Read(chunkSize)
}}

Method %GetContentType() As %String
{{
    if ..SerializedStream.Size > 0 {{
        Quit "application/json"
    }}
    Quit ##super()
}}

Method OnShowJSONContents(pZenOutput As %Boolean = 0)
{{
	Set tSC = $$$OK
    Do ..OutputFormattedJSON(..SerializedStream)
}}


}}
"""
,
# ______________________________________________________________________________________________ JsonSmall # 
"JsonSmall": """
/// DO NOT EDIT. Generated by {ClassName} python class
Class {PackageName}.{ClassName} Extends (%Persistent,Ens.Request)
{{

Property SerializedData As %String (MAXLEN = "");

{props}

{indices}

Method chunksFromIRIS(iteration As %Integer, chunkSize As %Integer = 1048576) As %String
{{
    if iteration = 0 {{
        return ..SerializedData
    }}

    return ""
}}


}}

"""
,
# ______________________________________________________________________________________________ BinarySerialized # 
"BinarySerialized": """
/// DO NOT EDIT. Generated by {ClassName} python class
Class {PackageName}.{ClassName} Extends (%Persistent,Ens.Request)
{{

Property SerializedData As %Binary (MAXLEN = "");

{props}

{indices}

Method chunksFromIRIS(iteration As %Integer, chunkSize As %Integer = 1048576) As %String
{{
    if iteration = 0 {{
        return ##class(%SYS.Python).Bytes(..SerializedData)
    }}

    return ""
}}

}}

"""
,
# ______________________________________________________________________________________________ "MsgSubclass": """
 # 
"MsgSubclass": """
/// DO NOT EDIT. Generated by {ClassName} python class
Class {PackageName}.{ClassName} Extends ({Superclass})
{{

{props}

{indices}

}}

"""

# "ProductionMessage" will be generated using ClassDefinition and the above superclass. 
# Properties can be generated for Indexes and newcols
}
