
    cwi"              	          % S r SSKJrJr  SSKJr  SSKJr  SSK	J
r
  SSKJrJrJrJrJr  SSKJrJrJrJr  SSKJrJr  SS	KJr  SS
KJr   SSKr/ SQr"S r# " S S\S9r$ " S S\$5      r%\%" 5       r&/ r'\\\$\4      \(S'   \RR                  " \#SS9S 5       r*\RR                  " \#SS9S 5       r+\RR                  " \#SS9S 5       r,\RR                  " \#SS9S 5       r-\RR                  " \#SS9S  5       r.\RR                  " \#S!S9S" 5       r/\RR                  " \#S#S9S$ 5       r0\RR                  " \#S%S9S& 5       r1S-S'\2S(\$S)\2S*S4S+ jjr3S.S, jr4g! \ a0    SSKrSSKr\R@                  " SS5      \RB                  S'   SSKr GN3f = f)/a  
This module contains functions to add custom scripts, which can be embedded
into logic programs.

Examples
--------
The following example shows how to add a script that works the same way as
clingo's embedded Python script:
```python
>>> from clingo.script import Script, register_script
>>> from clingo.control import Control
>>>
>>> import __main__
>>>
>>> class MyPythonScript(Script):
...     def execute(self, location, code):
...         exec(code, __main__.__dict__, __main__.__dict__)
...     def call(self, location, name, arguments):
...         return getattr(__main__, name)(*arguments)
...     def callable(self, name):
...         return name in __main__.__dict__ and callable(__main__.__dict__[name])
...
>>> register_script('mypython', MyPythonScript())
>>>
>>> ctl = Control()
>>> ctl.add('base', [], """
... #script(mypython)
... from clingo.symbol import Number
... def func(a):
...     return Number(a.number + 1)
... #end.
... a(@func(1)).
... """)
>>>
>>> ctl.ground([('base',[])])
>>> ctl.solve(on_model=print)
a(2)
```
    )ABCMetaabstractmethod)Iterable)python_version)format_exception)Anyr   ListTupleUnion)_c_call_ffi_handle_error_lib)Location_py_location)Control)SymbolN__main__zthe main module)Scriptenable_pythonregister_scriptc                     SR                  [        XU5      5      n[        R                  " [        R                  UR                  5       5        g)N F)joinr   r   clingo_set_errorclingo_error_runtimeencode)	exception	exc_value	tracebackmsgs       G/home/james-whalen/.local/lib/python3.13/site-packages/clingo/script.py_cb_error_top_levelr#   A   s7    
''"9C
DC$33SZZ\B    c                       \ rS rSrSr\S\S\SS4S j5       r\S\S\S	\	\
   S\\	\
   \
4   4S
 j5       r\S\S\4S j5       rS\SS4S jrSrg)r   G   z\
This interface can be implemented to embed custom scripting languages into
logic programs.
locationcodereturnNc                     g)z|
Execute the given source code.

Parameters
----------
location
    The location of the code.
code
    The code to execute.
N selfr'   r(   s      r"   executeScript.executeM       r$   name	argumentsc                     g)a  
Call the function with the given name and arguments.

Parameters
----------
location
    From where in the logic program the function was called.
name
    The name of the function.
arguments
    The arguments to the function.

Returns
-------
The resulting pool of symbols.
Nr+   )r-   r'   r1   r2   s       r"   callScript.callZ   r0   r$   c                     g)z
Check there is a function with the given name.

Parameters
----------
name
    The name of the function.

Returns
-------
Whether the function is callable.
Nr+   r-   r1   s     r"   callableScript.callableo   r0   r$   controlc                     g)z
Run the main function.

This function exisits primarily for internal purposes and does not need
to be implemented.

Parameters
----------
control
    Control object to pass to the main function.
Nr+   r-   r:   s     r"   mainScript.main~   r0   r$   r+   )__name__
__module____qualname____firstlineno____doc__r   r   strr.   r   r   r   r4   boolr8   r   r=   __static_attributes__r+   r$   r"   r   r   G   s    
 
 
 
 
 
  (+8@8H	x'	( ( S T  G  r$   r   )	metaclassc                   ,    \ rS rSrS rS rS rS rSrg)_PythonScript   c                 V    [        U[        R                  [        R                  5        g N)execr   __dict__r,   s      r"   r.   _PythonScript.execute   s    T8$$h&7&78r$   c                 ,    [        [        U5      nU" U6 $ rL   )getattrr   )r-   r'   r1   r2   funs        r"   r4   _PythonScript.call   s    h%Ir$   c                 n    U[         R                  ;   =(       a    [        [         R                  U   5      $ rL   )r   rN   r8   r7   s     r"   r8   _PythonScript.callable   s(    x(((NXh6G6G6M-NNr$   c                 0    [         R                  " U5        g rL   )r   r=   r<   s     r"   r=   _PythonScript.main   s    gr$   r+   N)	r?   r@   rA   rB   r.   r4   r8   r=   rF   r+   r$   r"   rI   rI      s    9Or$   rI   _GLOBAL_SCRIPTSpyclingo_script_execute)onerrorr1   c                     [         R                  " U5      nUR                  [        U 5      [         R                  " U5      R                  5       5        g)NT)r   from_handler.   r   stringdecode)r'   r(   datascripts       r"   _pyclingo_script_executera      s:    %%d+F
NN<)4;;t+<+C+C+EFr$   pyclingo_script_callc                 b   [         R                  " U5      n[         R                  " SU5      n[         R                  " SU5      n[         R                  " U5      R	                  5       n[        U5       V	s/ s H  n	[        X)   5      PM     n
n	UR                  [        U 5      X5      n[        U[        5      (       a  [        U5      OU/n[         R                  " S[        U5      5      n[        U5       H  u  pUR                  X'   M     [!        U" U[        U5      U5      5        gs  sn	f )Nclingo_symbol_callback_tzclingo_symbol_t*zclingo_symbol_t[]T)r   r\   castr]   r^   ranger   r4   r   
isinstanceIterableABClistnewlen	enumerate_repr   )r'   r1   r2   sizesymbol_callbacksymbol_callback_datar_   r`   py_nameipy_argsretsymbols	c_symbolssyms                  r"   _pyclingo_script_callrx      s     %%d+Fii :OLO		,i8Ikk$&&(G-24[9[vil#[G9
++l8,g
?C%c;77d3icUG,c'l;IG$xx	 %/)S\;OPQ :s   6D,pyclingo_script_callablec                     [         R                  " U5      n[         R                  " U 5      R                  5       nUR	                  U5      US'   g)Nr   T)r   r\   r]   r^   r8   )r1   rt   r_   r`   rq   s        r"   _pyclingo_script_callabler{      s>    %%d+Fkk$&&(G__W%CFr$   pyclingo_script_mainc                     [         R                  " U5      nUR                  [        [         R                  " SU 5      5      5        g)Nclingo_control_t*T)r   r\   r=   r   re   )ctlr_   r`   s      r"   _pyclingo_script_mainr      s2    %%d+F
KK		"5s;<=r$   pyclingo_executec                     U[         R                  :X  d   e[        [         R                  " SU 5      U[         R                  " [
        5      5      $ Nzclingo_location_t*)r   NULLra   re   
new_handle_PYTHON_SCRIPT)r'   r(   r_   s      r"   _pyclingo_executer      s=    499#		&149X r$   pyclingo_callc                     U[         R                  :X  d   e[        [         R                  " SU 5      UUUUU[         R                  " [
        5      5      $ r   )r   r   rx   re   r   r   )r'   r1   r2   rn   ro   rp   r_   s          r"   _pyclingo_callr      sM     499 		&1' r$   pyclingo_callablec                 v    U[         R                  :X  d   e[        X[         R                  " [        5      5      $ rL   )r   r   r{   r   r   )r1   rt   r_   s      r"   _pyclingo_callabler      s*    499$T0OPPr$   pyclingo_mainc                     U[         R                  :X  d   e[        [         R                  " SU 5      [         R                  " [
        5      5      $ )Nr~   )r   r   r   re   r   r   )r   r_   s     r"   _pyclingo_mainr      s;    499 		%s+T__^-L r$   r1   r`   versionr)   c                    [        S[        R                  UR                  5       5      n[        R
                  " S5      n[        R                  " S[        R                  5      US   l        [        R                  " S[        R                  5      US   l
        [        R                  " S[        R                  5      US   l        [        R                  " S[        R                  5      US   l        [        R                  US   l        X4S   l        [        R$                  " U5      n[&        R)                  X45        [+        [        R,                  " U R                  5       XE5      5        g)a  
Registers a script language which can then be embedded into a logic
program.

Parameters
----------
name
    The name of the script. This name can then be used in the script
    statement in a logic program.
script
    The class to register.
version
    The version of the script.
char const*clingo_script_t*void*r   N)r   r   clingo_add_stringr   r   rj   re   rY   r.   rb   r4   ry   r8   r|   r=   r   freer   r   rX   appendr   clingo_register_script)r1   r`   r   	c_versionc_scriptr_   s         r"   r   r      s     t'='=w~~?OPIxx*+H))GT-I-IJHQKyy$*C*CDHQK99Wd.K.KLHQKyy$*C*CDHQKyyHQK#QK??6"DF>*$--dkkmXLMr$   c                     [        S[        R                  [        5       R	                  5       5      n [
        R                  " S5      n[
        R                  " S[        R                  5      US   l	        [
        R                  " S[        R                  5      US   l        [
        R                  " S[        R                  5      US   l        [
        R                  " S[        R                  5      US   l        [
        R                   US   l        XS   l        ['        [        R(                  " SR	                  5       U[
        R                   5      5        g)z
This function can be called to enable evaluation of Python scripts in logic
programs.

By default evaluation is only enabled in the clingo executable but not in
the Python module.
r   r   r   r   pythonN)r   r   r   r   r   r   rj   re   r   r.   r   r4   r   r8   r   r=   r   r   r   r   r   )r   r   s     r"   r   r     s     t--~/?/F/F/HI xx*+H))GT-B-BCHQKyy$*<*<=HQK99Wd.D.DEHQKyy$*<*<=HQKyyHQK#QK$--hoo.?499UVr$   )z1.0.0)r)   N)5rC   abcr   r   collections.abcr   rh   platformr   r    r   typingr   r	   r
   r   clingo._internalr   r   r   r   
clingo.astr   r   clingo.controlr   clingo.symbolr   r   ImportErrorsystypes
ModuleTypemodules__all__r#   r   rI   r   rX   __annotations__
def_externra   rx   r{   r   r   r   r   r   rD   r   r   r+   r$   r"   <module>r      s  &P ( 3 # & 4 4 ? ? - "   9Bw BJF  ,.eFCK() . ,3LM N ,3IJ K& ,3MN O ,3IJ K ,3EF G ,?C D ,3FGQ HQ
 ,?C DN# Nv N N$ N8Wk  #..z;LMCKK
s   E 2FF