Skip to content

Commit 08f400c

Browse files
0.7
1 parent b83e481 commit 08f400c

File tree

7 files changed

+65
-15
lines changed

7 files changed

+65
-15
lines changed

doc/configuration.rst

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
Installation and Configuration
22
==================================
3+
4+
Installation
5+
----------------------------------
6+
7+
.. code-block::
8+
9+
pip install sphinx-exec-code
10+
11+
12+
313
To use this extension just add it to the ``extensions`` in your ``conf.py``
414

515
.. code-block::
@@ -8,7 +18,10 @@ To use this extension just add it to the ``extensions`` in your ``conf.py``
818
'sphinx_exec_code',
919
]
1020
11-
Additionally the following configuration parameters are available:
21+
Configuration
22+
----------------------------------
23+
24+
The following configuration parameters are available:
1225

1326
.. _config_options:
1427

@@ -35,11 +48,17 @@ Additionally the following configuration parameters are available:
3548
- | The directory that is used to create the path to the
3649
| example files. Defaults to the parent folder of the ``conf.py``.
3750
51+
* - ``exec_code_stdout_encoding``
52+
- ``str``
53+
- | Encoding used to decode stdout.
54+
| The default depends on the operating system but should be ``utf-8``.
55+
56+
3857
If it's a relative path it will be resolved relative to the parent folder of the ``conf.py``
3958

4059
Example:
4160

42-
.. code-block::
61+
.. code-block:: python
4362
4463
exec_code_working_dir = '..'
4564
exec_code_folders = ['../my_src']
@@ -50,8 +69,9 @@ The configured values are logged.
5069

5170
Log output for Example:
5271

53-
::
72+
.. code-block:: text
5473
5574
[exec-code] Working dir: C:\Python\sphinx-exec-code
5675
[exec-code] Folders: C:\Python\sphinx-exec-code\my_src
5776
[exec-code] Example dir: C:\Python\sphinx-exec-code\doc
77+
[exec-code] Stdout encoding: utf-8

readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ This code will be executed
3636
```
3737

3838
# Changelog
39+
#### 0.7 (15.07.2022)
40+
- Added config parameter to specify stdout encoding
41+
- Only empty lines of the output get trimmed
42+
3943
#### 0.6 (04.04.2022)
4044
- Fixed an issue where the line numbers for error messages were not correct
4145

requirements.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pytest==7.0.1
2-
pre-commit==2.17.0
3-
sphinx==4.4.0
1+
pytest==7.1.2
2+
pre-commit==2.20.0
3+
sphinx==5.0.2
44
sphinx-rtd-theme==1.0.0

src/sphinx_exec_code/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.6'
1+
__version__ = '0.7'

src/sphinx_exec_code/code_exec.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import subprocess
33
import sys
4+
from itertools import dropwhile
45
from pathlib import Path
56
from typing import Iterable, Optional
67

@@ -10,12 +11,14 @@
1011

1112
WORKING_DIR: Optional[str] = None
1213
ADDITIONAL_FOLDERS: Optional[Iterable[str]] = None
14+
STDOUT_ENCODING: str = sys.stdout.encoding
1315

1416

15-
def setup_code_env(cwd: Path, folders: Iterable[Path]):
16-
global WORKING_DIR, ADDITIONAL_FOLDERS
17+
def setup_code_env(cwd: Path, folders: Iterable[Path], encoding: str):
18+
global WORKING_DIR, ADDITIONAL_FOLDERS, STDOUT_ENCODING
1719
WORKING_DIR = str(cwd)
1820
ADDITIONAL_FOLDERS = tuple(map(str, folders))
21+
STDOUT_ENCODING = encoding
1922

2023

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

35-
ret = (run.stdout.decode() + run.stderr.decode()).strip()
38+
# decode output and drop tailing spaces
39+
ret_str = (run.stdout.decode(encoding=STDOUT_ENCODING) + run.stderr.decode(encoding=STDOUT_ENCODING)).rstrip()
40+
41+
# drop leading empty lines
42+
ret_lines = list(dropwhile(lambda x: not x.strip(), ret_str.splitlines()))
3643

3744
# Normalize newlines
38-
return '\n'.join(ret.splitlines())
45+
return '\n'.join(ret_lines)

src/sphinx_exec_code/sphinx_api.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
from pathlib import Path
23

34
from sphinx_exec_code import __version__
@@ -8,6 +9,7 @@
89
CONF_NAME_CWD = 'exec_code_working_dir'
910
CONF_NAME_DIRS = 'exec_code_folders'
1011
CONF_NAME_SAMPLE_DIR = 'exec_code_example_dir'
12+
CONF_NAME_STDOUT_ENCODING = 'exec_code_stdout_encoding'
1113

1214

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

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

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

5862
setup_example_dir(example_dir)
59-
setup_code_env(cwd, folders)
63+
setup_code_env(cwd, folders, stdout_encoding)
6064
return None
6165

6266

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

7579
# config options
76-
app.add_config_value(CONF_NAME_CWD, cwd, 'env',)
77-
app.add_config_value(CONF_NAME_DIRS, code_folders, 'env')
78-
app.add_config_value(CONF_NAME_SAMPLE_DIR, confdir, 'env')
80+
app.add_config_value(CONF_NAME_CWD, cwd, 'env', (Path, str))
81+
app.add_config_value(CONF_NAME_DIRS, code_folders, 'env', (Path, str))
82+
app.add_config_value(CONF_NAME_SAMPLE_DIR, confdir, 'env', (Path, str))
83+
app.add_config_value(CONF_NAME_STDOUT_ENCODING, sys.stdout.encoding, 'env', str)
7984

8085
app.connect('builder-inited', builder_ready)
8186
app.add_directive('exec_code', ExecCode)

tests/test_code_exec.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ def test_print(setup_env):
2121
assert output == 'Line1\nLine2'
2222

2323

24+
def test_print_table(setup_env):
25+
code = "\n \n \n\n" \
26+
"print(' | A | B |')\n" \
27+
"print(' Col1 | 1 | 2 |')"
28+
output = execute_code(code, 'my_file', 1)
29+
assert output == ' | A | B |\n Col1 | 1 | 2 |'
30+
31+
2432
def test_err(setup_env):
2533
code = "print('Line1')\nprint('Line2')\n1/0"
2634

@@ -37,3 +45,9 @@ def test_err(setup_env):
3745
' File "my_file", line 7',
3846
'ZeroDivisionError: division by zero'
3947
]
48+
49+
50+
def test_unicode(setup_env):
51+
code = "print('●')"
52+
output = execute_code(code, 'my_file', 1)
53+
assert output == '●'

0 commit comments

Comments
 (0)