Файловый менеджер - Редактировать - /opt/imh-python/lib/python3.9/site-packages/IPython/sphinxext/__pycache__/ipython_directive.cpython-39.pyc
Ðазад
a o�h]� � @ sR d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddlmZ ddl mZ ddl mZ ddlmZ ddlmZ ddlmZ dd lmZ d ZzddlZdZW n ey� Y n0 ed�\ZZZg d �Zdd� Z G dd� de!�Z"G dd� de�Z#dd� Z$dd� Z%e&dk�rNej'�(d��s@e�)d� e%� e*d� dS )aA Sphinx directive to support embedded IPython code. IPython provides an extension for `Sphinx <http://www.sphinx-doc.org/>`_ to highlight and run code. This directive allows pasting of entire interactive IPython sessions, prompts and all, and their code will actually get re-executed at doc build time, with all prompts renumbered sequentially. It also allows you to input code as a pure python input by giving the argument python to the directive. The output looks like an interactive ipython section. Here is an example of how the IPython directive can **run** python code, at build time. .. ipython:: In [1]: 1+1 In [1]: import datetime ...: datetime.date.fromisoformat('2022-02-22') It supports IPython construct that plain Python does not understand (like magics): .. ipython:: In [0]: import time In [0]: %pdoc time.sleep This will also support top-level async when using IPython 7.0+ .. ipython:: In [2]: import asyncio ...: print('before') ...: await asyncio.sleep(1) ...: print('after') The namespace will persist across multiple code chucks, Let's define a variable: .. ipython:: In [0]: who = "World" And now say hello: .. ipython:: In [0]: print('Hello,', who) If the current section raises an exception, you can add the ``:okexcept:`` flag to the current block, otherwise the build will fail. .. ipython:: :okexcept: In [1]: 1/0 IPython Sphinx directive module =============================== To enable this directive, simply list it in your Sphinx ``conf.py`` file (making sure the directory where you placed it is visible to sphinx, as is needed for all Sphinx directives). For example, to enable syntax highlighting and the IPython directive:: extensions = ['IPython.sphinxext.ipython_console_highlighting', 'IPython.sphinxext.ipython_directive'] The IPython directive outputs code-blocks with the language 'ipython'. So if you do not have the syntax highlighting extension enabled as well, then all rendered code-blocks will be uncolored. By default this directive assumes that your prompts are unchanged IPython ones, but this can be customized. The configurable options that can be placed in conf.py are: ipython_savefig_dir: The directory in which to save the figures. This is relative to the Sphinx source directory. The default is `html_static_path`. ipython_rgxin: The compiled regular expression to denote the start of IPython input lines. The default is ``re.compile('In \[(\d+)\]:\s?(.*)\s*')``. You shouldn't need to change this. ipython_warning_is_error: [default to True] Fail the build if something unexpected happen, for example if a block raise an exception but does not have the `:okexcept:` flag. The exact behavior of what is considered strict, may change between the sphinx directive version. ipython_rgxout: The compiled regular expression to denote the start of IPython output lines. The default is ``re.compile('Out\[(\d+)\]:\s?(.*)\s*')``. You shouldn't need to change this. ipython_promptin: The string to represent the IPython input prompt in the generated ReST. The default is ``'In [%d]:'``. This expects that the line numbers are used in the prompt. ipython_promptout: The string to represent the IPython prompt in the generated ReST. The default is ``'Out [%d]:'``. This expects that the line numbers are used in the prompt. ipython_mplbackend: The string which specifies if the embedded Sphinx shell should import Matplotlib and set the backend. The value specifies a backend that is passed to `matplotlib.use()` before any lines in `ipython_execlines` are executed. If not specified in conf.py, then the default value of 'agg' is used. To use the IPython directive without matplotlib as a dependency, set the value to `None`. It may end up that matplotlib is still imported if the user specifies so in `ipython_execlines` or makes use of the @savefig pseudo decorator. ipython_execlines: A list of strings to be exec'd in the embedded Sphinx shell. Typical usage is to make certain packages always available. Set this to an empty list if you wish to have no imports always available. If specified in ``conf.py`` as `None`, then it has the effect of making no imports available. If omitted from conf.py altogether, then the default value of ['import numpy as np', 'import matplotlib.pyplot as plt'] is used. ipython_holdcount When the @suppress pseudo-decorator is used, the execution count can be incremented or not. The default behavior is to hold the execution count, corresponding to a value of `True`. Set this to `False` to increment the execution count after each suppressed command. As an example, to use the IPython directive when `matplotlib` is not available, one sets the backend to `None`:: ipython_mplbackend = None An example usage of the directive is: .. code-block:: rst .. ipython:: In [1]: x = 1 In [2]: y = x**2 In [3]: print(y) See http://matplotlib.org/sampledoc/ipython_directive.html for additional documentation. Pseudo-Decorators ================= Note: Only one decorator is supported per input. If more than one decorator is specified, then only the last one is used. In addition to the Pseudo-Decorators/options described at the above link, several enhancements have been made. The directive will emit a message to the console at build-time if code-execution resulted in an exception or warning. You can suppress these on a per-block basis by specifying the :okexcept: or :okwarning: options: .. code-block:: rst .. ipython:: :okexcept: :okwarning: In [1]: 1/0 In [2]: # raise warning. To Do ===== - Turn the ad-hoc test() function into a real test suite. - Break up ipython-specific functionality from matplotlib stuff into better separated code. � N)�StringIO)� directives)� Directive)�logging)�Config)�InteractiveShell)� ProfileDirFT� )�suppress�verbatim�savefig�doctestc s� g }| � d�}t|�}d}d} ||kr*�q�|| } |d7 }| �� � � �d�r\|�t| f� qt� fdd�tD ��r�| r�td��n� } q|� | �}|�r�t |�d��|�d � }} d d�dgtt |��d � }t|�}g }||k �rj|| }|� |�}|�sj|�d��r�qjnP|�|��rV||d� }|�rH|d d k�rH|dd� }| d| 7 } n |�|� |d7 }q�|�t| | d�|�ff� q|� | �}|rt |�d��|�d � }}||d k �r�d�|g||d� �}|�t|f� �q�q|S )a� part is a string of ipython text, comprised of at most one input, one output, comments, and blank lines. The block parser parses the text into a list of:: blocks = [ (TOKEN0, data0), (TOKEN1, data1), ...] where TOKEN is one of [COMMENT | INPUT | OUTPUT ] and data is, depending on the type of token:: COMMENT : the comment string INPUT: the (DECORATOR, INPUT_LINE, REST) where DECORATOR: the input decorator (or None) INPUT_LINE: the input as string (possibly multi-line) REST : any stdout generated by the input line (not OUTPUT) OUTPUT: the output string, possibly multi-line � r N� �#c 3 s | ]}� � d | �V qdS ��@N�� startswith��.0Zpseudo_decorator�Z line_stripped� ��/root/rpmbuild/BUILDROOT/imh-python39-modules-3.9.7-92.el8.x86_64/opt/imh-python/lib/python3.9/site-packages/IPython/sphinxext/ipython_directive.py� <genexpr> s �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� sd �� r>