##// END OF EJS Templates
Backport PR #4163: Fix for incorrect default encoding on Windows....
Backport PR #4163: Fix for incorrect default encoding on Windows. Whilst trying out rendering notebooks in a flask app under Apache on Windows I got the below error when simply trying to import `SlidesExporter` ```python mod_wsgi (pid=6260): Exception occurred processing WSGI script 'flask_test.wsgi'. Traceback (most recent call last): File "flask_test.py", line 81, in render_notebook from IPython.nbconvert.exporters import SlidesExporter File "c:\\dev\\code\\ipython\\IPython\\__init__.py", line 47, in <module> from .terminal.embed import embed File "c:\\dev\\code\\ipython\\IPython\\terminal\\embed.py", line 32, in <module> from IPython.terminal.interactiveshell import TerminalInteractiveShell File "c:\\dev\\code\\ipython\\IPython\\terminal\\interactiveshell.py", line 25, in <module> from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC File "c:\\dev\\code\\ipython\\IPython\\core\\interactiveshell.py", line 59, in <module> from IPython.core.prompts import PromptManager File "c:\\dev\\code\\ipython\\IPython\\core\\prompts.py", line 138, in <module> HOME = py3compat.str_to_unicode(os.environ.get("HOME","//////:::::ZZZZZ,,,~~~")) File "c:\\dev\\code\\ipython\\IPython\\utils\\py3compat.py", line 18, in decode return s.decode(encoding, "replace") LookupError: unknown encoding: cp0 ``` A little bit of [googling](http://bugs.python.org/issue6501) suggests that Windows returns 'cp0' to indicate there is no code page. This fix simply looks for this invalid value and replaces it with something valid. With this change it works for me.

File last commit:

r11685:1f458eea
r12463:516353d0
Show More
ansi.py
157 lines | 4.6 KiB | text/x-python | PythonLexer
Jonathan Frederic
Cleanup and refactor of filters
r10676 """Filters for processing ANSI colors within Jinja templates.
"""
#-----------------------------------------------------------------------------
# Copyright (c) 2013, the IPython Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
Jonathan Frederic
Post code-review, extended refactor.
r10485 import re
jakobgager
Update imports
r10946 from IPython.utils import coloransi
Jonathan Frederic
Post code-review, extended refactor.
r10485
Jonathan Frederic
Cleanup and refactor of filters
r10676 #-----------------------------------------------------------------------------
# Classes and functions
#-----------------------------------------------------------------------------
Jonathan Frederic
Post code-review, extended refactor.
r10485
Brian E. Granger
Fixing import logic.
r11088 __all__ = [
Jonathan Frederic
Filter names cleanup
r11685 'strip_ansi',
Brian E. Granger
Fixing import logic.
r11088 'ansi2html',
'single_ansi2latex',
'ansi2latex'
]
Jonathan Frederic
Filter names cleanup
r11685 def strip_ansi(source):
Jonathan Frederic
Cleanup and refactor of filters
r10676 """
Remove ansi from text
Parameters
----------
source : str
Source to remove the ansi from
"""
Jonathan Frederic
Fixed still broken remove_ansi, : to ;
r11435 return re.sub(r'\033\[(\d|;)+?m', '', source)
Jonathan Frederic
Post code-review, extended refactor.
r10485
Jonathan Frederic
Cleanup and refactor of filters
r10676
def ansi2html(text):
"""
Conver ansi colors to html colors.
Parameters
----------
text : str
Text containing ansi colors to convert to html
"""
Jonathan Frederic
Post code-review, extended refactor.
r10485 ansi_colormap = {
'30': 'ansiblack',
'31': 'ansired',
'32': 'ansigreen',
'33': 'ansiyellow',
'34': 'ansiblue',
'35': 'ansipurple',
'36': 'ansicyan',
'37': 'ansigrey',
'01': 'ansibold',
}
# do ampersand first
Jonathan Frederic
Cleanup and refactor of filters
r10676 text = text.replace('&', '&amp;')
Jonathan Frederic
Post code-review, extended refactor.
r10485 html_escapes = {
'<': '&lt;',
'>': '&gt;',
"'": '&apos;',
'"': '&quot;',
'`': '&#96;',
}
Jonathan Frederic
Fixed all broken references, refactored some stuff here and there,...
r10624
Jonathan Frederic
Post code-review, extended refactor.
r10485 for c, escape in html_escapes.iteritems():
Jonathan Frederic
Cleanup and refactor of filters
r10676 text = text.replace(c, escape)
Jonathan Frederic
Post code-review, extended refactor.
r10485
ansi_re = re.compile('\x1b' + r'\[([\dA-Fa-f;]*?)m')
Jonathan Frederic
Cleanup and refactor of filters
r10676 m = ansi_re.search(text)
Jonathan Frederic
Post code-review, extended refactor.
r10485 opened = False
cmds = []
opener = ''
closer = ''
while m:
cmds = m.groups()[0].split(';')
closer = '</span>' if opened else ''
Jonathan Frederic
Fixed all broken references, refactored some stuff here and there,...
r10624
Jonathan Frederic
Post code-review, extended refactor.
r10485 # True if there is there more than one element in cmds, *or*
# if there is only one but it is not equal to a string of zeroes.
opened = len(cmds) > 1 or cmds[0] != '0' * len(cmds[0])
classes = []
for cmd in cmds:
if cmd in ansi_colormap:
classes.append(ansi_colormap.get(cmd))
if classes:
opener = '<span class="%s">' % (' '.join(classes))
else:
opener = ''
Jonathan Frederic
Cleanup and refactor of filters
r10676 text = re.sub(ansi_re, closer + opener, text, 1)
Jonathan Frederic
Post code-review, extended refactor.
r10485
Jonathan Frederic
Cleanup and refactor of filters
r10676 m = ansi_re.search(text)
Jonathan Frederic
Post code-review, extended refactor.
r10485
if opened:
Jonathan Frederic
Cleanup and refactor of filters
r10676 text += '</span>'
return text
jakobgager
move Ansi related filters to ansi.py
r10943
def single_ansi2latex(code):
"""Converts single ansi markup to latex format
Return latex code and number of open brackets.
"""
for color in coloransi.color_templates:
Jonathan Frederic
Fix, ansi2latex style-less code support...
r11495
#Make sure to get the color code (which is a part of the overall style)
# i.e. 0;31 is valid
# 31 is also valid, and means the same thing
#coloransi.color_templates stores the longer of the two formats %d;%d
#Get the short format so we can parse that too. Short format only exist
#if no other formating is applied (the other number must be a 0)!
style_code = getattr(coloransi.TermColors, color[0])
color_code = style_code.split(';')[1]
is_normal = style_code.split(';')[0] == '0'
jakobgager
move Ansi related filters to ansi.py
r10943 # regular fonts
Jonathan Frederic
Fix, ansi2latex style-less code support...
r11495 if (code == style_code) or (is_normal and code == color_code):
jakobgager
move Ansi related filters to ansi.py
r10943 return '\\'+color[0].lower()+'{', 1
# bold fonts
Jonathan Frederic
Fix, ansi2latex style-less code support...
r11495 if code == style_code[:3]+str(1)+style_code[3:]:
jakobgager
move Ansi related filters to ansi.py
r10943 return '\\textbf{\\textcolor{'+color[0].lower()+'}{', 2
return '', 0
def ansi2latex(text):
"""Converts ansi formated text to latex version
based on https://bitbucket.org/birkenfeld/sphinx-contrib/ansi.py
"""
color_pattern = re.compile('\x1b\\[([^m]+)m')
last_end = 0
openbrack = 0
outstring = ''
for match in color_pattern.finditer(text):
head = text[last_end:match.start()]
outstring += head
if openbrack:
outstring += '}'*openbrack
openbrack = 0
Jonathan Frederic
Fixes for Py3.3
r11547 if not (match.group() == coloransi.TermColors.Normal or openbrack):
jakobgager
move Ansi related filters to ansi.py
r10943 texform, openbrack = single_ansi2latex(match.group())
outstring += texform
last_end = match.end()
Jonathan Frederic
Fix ansi2latex color remainer of string.
r11496
#Add the remainer of the string and THEN close any remaining color brackets.
outstring += text[last_end:]
jakobgager
move Ansi related filters to ansi.py
r10943 if openbrack:
outstring += '}'*openbrack
return outstring.strip()