Format: application/x-python-code
1 o
2
3 ��hj� � @ sf d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl
4 Z
5 ddlmZ ddl
6 mZmZmZ ddlmZ ddlmZ ddlmZ ddlmZ dd lmZ dd
7 lmZ dZzddlZdZW n eyq Y nw ed
8 �\Z Z!Z"g d�Z#dd� Z$G dd� de%�Z&G dd� de�Z'dd� Z(dd� Z)e*dkr�ej+�,d�s�e�-d� e)� e.d� dS dS )aA
9 Sphinx directive to support embedded IPython code.
10
11 IPython provides an extension for `Sphinx <http://www.sphinx-doc.org/>`_ to
12 highlight and run code.
13
14 This directive allows pasting of entire interactive IPython sessions, prompts
15 and all, and their code will actually get re-executed at doc build time, with
16 all prompts renumbered sequentially. It also allows you to input code as a pure
17 python input by giving the argument python to the directive. The output looks
18 like an interactive ipython section.
19
20 Here is an example of how the IPython directive can
21 **run** python code, at build time.
22
23 .. ipython::
24
25 In [1]: 1+1
26
27 In [1]: import datetime
28 ...: datetime.date.fromisoformat('2022-02-22')
29
30 It supports IPython construct that plain
31 Python does not understand (like magics):
32
33 .. ipython::
34
35 In [0]: import time
36
37 In [0]: %pdoc time.sleep
38
39 This will also support top-level async when using IPython 7.0+
40
41 .. ipython::
42
43 In [2]: import asyncio
44 ...: print('before')
45 ...: await asyncio.sleep(1)
46 ...: print('after')
47
48
49 The namespace will persist across multiple code chucks, Let's define a variable:
50
51 .. ipython::
52
53 In [0]: who = "World"
54
55 And now say hello:
56
57 .. ipython::
58
59 In [0]: print('Hello,', who)
60
61 If the current section raises an exception, you can add the ``:okexcept:`` flag
62 to the current block, otherwise the build will fail.
63
64 .. ipython::
65 :okexcept:
66
67 In [1]: 1/0
68
69 IPython Sphinx directive module
70 ===============================
71
72 To enable this directive, simply list it in your Sphinx ``conf.py`` file
73 (making sure the directory where you placed it is visible to sphinx, as is
74 needed for all Sphinx directives). For example, to enable syntax highlighting
75 and the IPython directive::
76
77 extensions = ['IPython.sphinxext.ipython_console_highlighting',
78 'IPython.sphinxext.ipython_directive']
79
80 The IPython directive outputs code-blocks with the language 'ipython'. So
81 if you do not have the syntax highlighting extension enabled as well, then
82 all rendered code-blocks will be uncolored. By default this directive assumes
83 that your prompts are unchanged IPython ones, but this can be customized.
84 The configurable options that can be placed in conf.py are:
85
86 ipython_savefig_dir:
87 The directory in which to save the figures. This is relative to the
88 Sphinx source directory. The default is `html_static_path`.
89 ipython_rgxin:
90 The compiled regular expression to denote the start of IPython input
91 lines. The default is ``re.compile('In \[(\d+)\]:\s?(.*)\s*')``. You
92 shouldn't need to change this.
93 ipython_warning_is_error: [default to True]
94 Fail the build if something unexpected happen, for example if a block raise
95 an exception but does not have the `:okexcept:` flag. The exact behavior of
96 what is considered strict, may change between the sphinx directive version.
97 ipython_rgxout:
98 The compiled regular expression to denote the start of IPython output
99 lines. The default is ``re.compile('Out\[(\d+)\]:\s?(.*)\s*')``. You
100 shouldn't need to change this.
101 ipython_promptin:
102 The string to represent the IPython input prompt in the generated ReST.
103 The default is ``'In [%d]:'``. This expects that the line numbers are used
104 in the prompt.
105 ipython_promptout:
106 The string to represent the IPython prompt in the generated ReST. The
107 default is ``'Out [%d]:'``. This expects that the line numbers are used
108 in the prompt.
109 ipython_mplbackend:
110 The string which specifies if the embedded Sphinx shell should import
111 Matplotlib and set the backend. The value specifies a backend that is
112 passed to `matplotlib.use()` before any lines in `ipython_execlines` are
113 executed. If not specified in conf.py, then the default value of 'agg' is
114 used. To use the IPython directive without matplotlib as a dependency, set
115 the value to `None`. It may end up that matplotlib is still imported
116 if the user specifies so in `ipython_execlines` or makes use of the
117 @savefig pseudo decorator.
118 ipython_execlines:
119 A list of strings to be exec'd in the embedded Sphinx shell. Typical
120 usage is to make certain packages always available. Set this to an empty
121 list if you wish to have no imports always available. If specified in
122 ``conf.py`` as `None`, then it has the effect of making no imports available.
123 If omitted from conf.py altogether, then the default value of
124 ['import numpy as np', 'import matplotlib.pyplot as plt'] is used.
125 ipython_holdcount
126 When the @suppress pseudo-decorator is used, the execution count can be
127 incremented or not. The default behavior is to hold the execution count,
128 corresponding to a value of `True`. Set this to `False` to increment
129 the execution count after each suppressed command.
130
131 As an example, to use the IPython directive when `matplotlib` is not available,
132 one sets the backend to `None`::
133
134 ipython_mplbackend = None
135
136 An example usage of the directive is:
137
138 .. code-block:: rst
139
140 .. ipython::
141
142 In [1]: x = 1
143
144 In [2]: y = x**2
145
146 In [3]: print(y)
147
148 See http://matplotlib.org/sampledoc/ipython_directive.html for additional
149 documentation.
150
151 Pseudo-Decorators
152 =================
153
154 Note: Only one decorator is supported per input. If more than one decorator
155 is specified, then only the last one is used.
156
157 In addition to the Pseudo-Decorators/options described at the above link,
158 several enhancements have been made. The directive will emit a message to the
159 console at build-time if code-execution resulted in an exception or warning.
160 You can suppress these on a per-block basis by specifying the :okexcept:
161 or :okwarning: options:
162
163 .. code-block:: rst
164
165 .. ipython::
166 :okexcept:
167 :okwarning:
168
169 In [1]: 1/0
170 In [2]: # raise warning.
171
172 To Do
173 =====
174
175 - Turn the ad-hoc test() function into a real test suite.
176 - Break up ipython-specific functionality from matplotlib stuff into better
177 separated code.
178
179 � N)�StringIO)�Any�Dict�Set)�
180 directives)� Directive)�logging)�Config)�InteractiveShell)�
181 ProfileDirFT� )�suppress�verbatim�savefig�doctestc s� g }| � d�}t|�}d}d} ||kr |S || }
182 |d7 }|
183 �� � � �d�r0|�t|
184 f� qt� fdd�tD ��rD| rAtd��� } q|� |
185 �}|r�t
186 |�d��|�d �}}
187 d
188 d�dgtt
189 |��d � }t|�}g }||k r�|| }|� |�}|s�|�d�r�n-|�|�r�||d� }|r�|d d
190 kr�|dd� }|
191 d| 7 }
192 n|�|� |d7 }||k sr|�t| |
193 d�|�ff� q|� |
194 �}|r�t
195 |�d��|�d �}}||d k r�d�|g||d� �}|�t|f� |S q)a�
196 part is a string of ipython text, comprised of at most one
197 input, one output, comments, and blank lines. The block parser
198 parses the text into a list of::
199
200 blocks = [ (TOKEN0, data0), (TOKEN1, data1), ...]
201
202 where TOKEN is one of [COMMENT | INPUT | OUTPUT ] and
203 data is, depending on the type of token::
204
205 COMMENT : the comment string
206
207 INPUT: the (DECORATOR, INPUT_LINE, REST) where
208 DECORATOR: the input decorator (or None)
209 INPUT_LINE: the input as string (possibly multi-line)
210 REST : any stdout generated by the input line (not OUTPUT)
211
212 OUTPUT: the output string, possibly multi-line
213
214 �
215 r N� �#c 3 � � | ]
216 }� � d | �V qdS ��@N��
217 startswith��.0Zpseudo_decorator�Z
218 line_stripped� �Yc:\Users\shubh\Videos\agent\nova\Lib\site-packages\IPython/sphinxext/ipython_directive.py� <genexpr>
219 �
220 � �
221 �zblock_parser.<locals>.<genexpr>z@Applying multiple pseudo-decorators on one line is not supported� � %s:� �.� )�split�len�stripr �append�COMMENT�any�PSEUDO_DECORATORS�RuntimeError�match�int�group�join�str�INPUT�OUTPUT)�part�rgxin�rgxout�fmtinZfmtout�block�lines�N�i� decorator�lineZmatchin�linenoZ inputline�continuationZNc�rest�nextlineZmatchout�outputr r r �block_parser� sl
222 M�
223 ��
224
225
226
227 �
228 �rC c @ s� e Zd ZdZd dd�Zdd� Zdd� Zd d
229 � Zd!dd
230 �Zdd� Z dd� Z
231 dd� Zdd� Zdd� Z
232 dd� Zdd� Zdd� Zdd� ZdS )"�EmbeddedSphinxShellz1An embedded IPython instance to run inside SphinxNc C s� t � | _|d u r
233 g }t� }d|j_d|j_d|j_d|j_t j
234 dd�}d}tj�
235 ||�}t�|�}tj||d�}t�| j� || _| jj| _| jj| _d| _d| _|| _d| _d| _d| _d | _d| _|D ] }| j|dd � qed S )
236 Nz:memory:FZNoColorZprofile_)�prefixZauto_profile_sphinx_build)�configZprofile_dirr"