Skip to content

Commit dfbe9aa

Browse files
authored
Merge branch 'main' into windows-gha
2 parents f8cfa02 + d33322d commit dfbe9aa

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

docs/conf.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,38 @@
99

1010
# Only for windows compatibility - Forces default encoding to UTF8, which it may not be on windows
1111
if os.name == 'nt':
12-
reload(sys)
13-
sys.setdefaultencoding('UTF8')
12+
# monkeypatches sphinxcontrib.datatemplates so it uses utf-8 as the encoding
13+
# instead of cp1252 when opening a file. using sys.setdefaultencoding does
14+
# not alter the encoding when opening a file for reading.
15+
16+
from sphinxcontrib.datatemplates import directive
17+
from xml.etree import ElementTree as ET
18+
import mimetypes
19+
20+
old_load_data = directive.DataTemplateLegacy._load_data
21+
22+
def _load_data(self, env, data_source, encoding):
23+
rel_filename, filename = env.relfn2path(data_source)
24+
if data_source.endswith('.yaml'):
25+
return self._load_yaml(filename, 'utf-8')
26+
elif data_source.endswith('.json'):
27+
return self._load_json(filename, 'utf-8')
28+
elif data_source.endswith('.csv'):
29+
return self._load_csv(filename, 'utf-8')
30+
elif "xml" in mimetypes.guess_type(data_source)[0]:
31+
# there are many XML based formats
32+
return ET.parse(filename).getroot()
33+
else:
34+
raise NotImplementedError('cannot load file type of %s' %
35+
data_source)
36+
37+
38+
directive.DataTemplateLegacy._load_data = _load_data
39+
setattr(
40+
sys.modules['sphinxcontrib.datatemplates.directive'].DataTemplateLegacy,
41+
'_load_data',
42+
_load_data
43+
)
1444

1545

1646
sys.path.append(os.getcwd()) # noqa

0 commit comments

Comments
 (0)