Skip to content

Commit

Permalink
0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
spacemanspiff2007 committed Jul 15, 2022
1 parent b83e481 commit 08f400c
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 15 deletions.
26 changes: 23 additions & 3 deletions doc/configuration.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
Installation and Configuration
==================================

Installation
----------------------------------

.. code-block::
pip install sphinx-exec-code
To use this extension just add it to the ``extensions`` in your ``conf.py``

.. code-block::
Expand All @@ -8,7 +18,10 @@ To use this extension just add it to the ``extensions`` in your ``conf.py``
'sphinx_exec_code',
]
Additionally the following configuration parameters are available:
Configuration
----------------------------------

The following configuration parameters are available:

.. _config_options:

Expand All @@ -35,11 +48,17 @@ Additionally the following configuration parameters are available:
- | The directory that is used to create the path to the
| example files. Defaults to the parent folder of the ``conf.py``.
* - ``exec_code_stdout_encoding``
- ``str``
- | Encoding used to decode stdout.
| The default depends on the operating system but should be ``utf-8``.

If it's a relative path it will be resolved relative to the parent folder of the ``conf.py``

Example:

.. code-block::
.. code-block:: python
exec_code_working_dir = '..'
exec_code_folders = ['../my_src']
Expand All @@ -50,8 +69,9 @@ The configured values are logged.

Log output for Example:

::
.. code-block:: text
[exec-code] Working dir: C:\Python\sphinx-exec-code
[exec-code] Folders: C:\Python\sphinx-exec-code\my_src
[exec-code] Example dir: C:\Python\sphinx-exec-code\doc
[exec-code] Stdout encoding: utf-8
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ This code will be executed
```

# Changelog
#### 0.7 (15.07.2022)
- Added config parameter to specify stdout encoding
- Only empty lines of the output get trimmed

#### 0.6 (04.04.2022)
- Fixed an issue where the line numbers for error messages were not correct

Expand Down
6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pytest==7.0.1
pre-commit==2.17.0
sphinx==4.4.0
pytest==7.1.2
pre-commit==2.20.0
sphinx==5.0.2
sphinx-rtd-theme==1.0.0
2 changes: 1 addition & 1 deletion src/sphinx_exec_code/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.6'
__version__ = '0.7'
15 changes: 11 additions & 4 deletions src/sphinx_exec_code/code_exec.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import subprocess
import sys
from itertools import dropwhile
from pathlib import Path
from typing import Iterable, Optional

Expand All @@ -10,12 +11,14 @@

WORKING_DIR: Optional[str] = None
ADDITIONAL_FOLDERS: Optional[Iterable[str]] = None
STDOUT_ENCODING: str = sys.stdout.encoding


def setup_code_env(cwd: Path, folders: Iterable[Path]):
global WORKING_DIR, ADDITIONAL_FOLDERS
def setup_code_env(cwd: Path, folders: Iterable[Path], encoding: str):
global WORKING_DIR, ADDITIONAL_FOLDERS, STDOUT_ENCODING
WORKING_DIR = str(cwd)
ADDITIONAL_FOLDERS = tuple(map(str, folders))
STDOUT_ENCODING = encoding


def execute_code(code: str, file: Path, first_loc: int) -> str:
Expand All @@ -32,7 +35,11 @@ def execute_code(code: str, file: Path, first_loc: int) -> str:
if run.returncode != 0:
raise CodeException(code, file, first_loc, run.returncode, run.stderr.decode()) from None

ret = (run.stdout.decode() + run.stderr.decode()).strip()
# decode output and drop tailing spaces
ret_str = (run.stdout.decode(encoding=STDOUT_ENCODING) + run.stderr.decode(encoding=STDOUT_ENCODING)).rstrip()

# drop leading empty lines
ret_lines = list(dropwhile(lambda x: not x.strip(), ret_str.splitlines()))

# Normalize newlines
return '\n'.join(ret.splitlines())
return '\n'.join(ret_lines)
13 changes: 9 additions & 4 deletions src/sphinx_exec_code/sphinx_api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from pathlib import Path

from sphinx_exec_code import __version__
Expand All @@ -8,6 +9,7 @@
CONF_NAME_CWD = 'exec_code_working_dir'
CONF_NAME_DIRS = 'exec_code_folders'
CONF_NAME_SAMPLE_DIR = 'exec_code_example_dir'
CONF_NAME_STDOUT_ENCODING = 'exec_code_stdout_encoding'


def mk_path(app, obj) -> Path:
Expand All @@ -22,10 +24,12 @@ def builder_ready(app):
cwd = mk_path(app, getattr(app.config, CONF_NAME_CWD))
folders = tuple(mk_path(app, _p) for _p in getattr(app.config, CONF_NAME_DIRS))
example_dir = mk_path(app, getattr(app.config, CONF_NAME_SAMPLE_DIR))
stdout_encoding = getattr(app.config, CONF_NAME_STDOUT_ENCODING)

log.debug(f'[exec-code] Working dir: {cwd}')
log.debug(f'[exec-code] Folders: {", ".join(map(str, folders))}')
log.debug(f'[exec-code] Example dir: {example_dir}')
log.debug(f'[exec-code] Stdout encoding: {stdout_encoding}')

# Ensure dirs are valid
if not cwd.is_dir():
Expand Down Expand Up @@ -56,7 +60,7 @@ def builder_ready(app):
log.warning(f'[exec-code] No Python packages found in {_f}')

setup_example_dir(example_dir)
setup_code_env(cwd, folders)
setup_code_env(cwd, folders, stdout_encoding)
return None


Expand All @@ -73,9 +77,10 @@ def setup(app):
code_folders.append(str(src_dir))

# config options
app.add_config_value(CONF_NAME_CWD, cwd, 'env',)
app.add_config_value(CONF_NAME_DIRS, code_folders, 'env')
app.add_config_value(CONF_NAME_SAMPLE_DIR, confdir, 'env')
app.add_config_value(CONF_NAME_CWD, cwd, 'env', (Path, str))
app.add_config_value(CONF_NAME_DIRS, code_folders, 'env', (Path, str))
app.add_config_value(CONF_NAME_SAMPLE_DIR, confdir, 'env', (Path, str))
app.add_config_value(CONF_NAME_STDOUT_ENCODING, sys.stdout.encoding, 'env', str)

app.connect('builder-inited', builder_ready)
app.add_directive('exec_code', ExecCode)
Expand Down
14 changes: 14 additions & 0 deletions tests/test_code_exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ def test_print(setup_env):
assert output == 'Line1\nLine2'


def test_print_table(setup_env):
code = "\n \n \n\n" \
"print(' | A | B |')\n" \
"print(' Col1 | 1 | 2 |')"
output = execute_code(code, 'my_file', 1)
assert output == ' | A | B |\n Col1 | 1 | 2 |'


def test_err(setup_env):
code = "print('Line1')\nprint('Line2')\n1/0"

Expand All @@ -37,3 +45,9 @@ def test_err(setup_env):
' File "my_file", line 7',
'ZeroDivisionError: division by zero'
]


def test_unicode(setup_env):
code = "print('●')"
output = execute_code(code, 'my_file', 1)
assert output == '●'

0 comments on commit 08f400c

Please sign in to comment.