Show More
@@ -30,7 +30,7 b' from string import Formatter' | |||||
30 |
|
30 | |||
31 | from IPython.config.configurable import Configurable |
|
31 | from IPython.config.configurable import Configurable | |
32 | from IPython.core import release |
|
32 | from IPython.core import release | |
33 | from IPython.utils import coloransi |
|
33 | from IPython.utils import coloransi, py3compat | |
34 | from IPython.utils.traitlets import (Unicode, Instance, Dict, Bool, Int) |
|
34 | from IPython.utils.traitlets import (Unicode, Instance, Dict, Bool, Int) | |
35 |
|
35 | |||
36 | #----------------------------------------------------------------------------- |
|
36 | #----------------------------------------------------------------------------- | |
@@ -96,6 +96,12 b' class LazyEvaluate(object):' | |||||
96 |
|
96 | |||
97 | def __str__(self): |
|
97 | def __str__(self): | |
98 | return str(self()) |
|
98 | return str(self()) | |
|
99 | ||||
|
100 | def __unicode__(self): | |||
|
101 | return unicode(self()) | |||
|
102 | ||||
|
103 | def __format__(self, format_spec): | |||
|
104 | return format(self(), format_spec) | |||
99 |
|
105 | |||
100 | def multiple_replace(dict, text): |
|
106 | def multiple_replace(dict, text): | |
101 | """ Replace in 'text' all occurences of any key in the given |
|
107 | """ Replace in 'text' all occurences of any key in the given | |
@@ -129,13 +135,13 b' def multiple_replace(dict, text):' | |||||
129 | # - I also need to split up the color schemes from the prompt specials |
|
135 | # - I also need to split up the color schemes from the prompt specials | |
130 | # somehow. I don't have a clean design for that quite yet. |
|
136 | # somehow. I don't have a clean design for that quite yet. | |
131 |
|
137 | |||
132 | HOME = os.environ.get("HOME","//////:::::ZZZZZ,,,~~~") |
|
138 | HOME = py3compat.str_to_unicode(os.environ.get("HOME","//////:::::ZZZZZ,,,~~~")) | |
133 |
|
139 | |||
134 | # We precompute a few more strings here for the prompt_specials, which are |
|
140 | # We precompute a few more strings here for the prompt_specials, which are | |
135 | # fixed once ipython starts. This reduces the runtime overhead of computing |
|
141 | # fixed once ipython starts. This reduces the runtime overhead of computing | |
136 | # prompt strings. |
|
142 | # prompt strings. | |
137 | USER = os.environ.get("USER") |
|
143 | USER = py3compat.str_to_unicode(os.environ.get("USER",'')) | |
138 | HOSTNAME = socket.gethostname() |
|
144 | HOSTNAME = py3compat.str_to_unicode(socket.gethostname()) | |
139 | HOSTNAME_SHORT = HOSTNAME.split(".")[0] |
|
145 | HOSTNAME_SHORT = HOSTNAME.split(".")[0] | |
140 | ROOT_SYMBOL = "#" if (os.name=='nt' or os.getuid()==0) else "$" |
|
146 | ROOT_SYMBOL = "#" if (os.name=='nt' or os.getuid()==0) else "$" | |
141 |
|
147 | |||
@@ -202,7 +208,7 b' def cwd_filt(depth):' | |||||
202 | $HOME is always replaced with '~'. |
|
208 | $HOME is always replaced with '~'. | |
203 | If depth==0, the full path is returned.""" |
|
209 | If depth==0, the full path is returned.""" | |
204 |
|
210 | |||
205 | cwd = os.getcwd().replace(HOME,"~") |
|
211 | cwd = os.getcwdu().replace(HOME,"~") | |
206 | out = os.sep.join(cwd.split(os.sep)[-depth:]) |
|
212 | out = os.sep.join(cwd.split(os.sep)[-depth:]) | |
207 | return out or os.sep |
|
213 | return out or os.sep | |
208 |
|
214 | |||
@@ -212,7 +218,7 b' def cwd_filt2(depth):' | |||||
212 | $HOME is always replaced with '~'. |
|
218 | $HOME is always replaced with '~'. | |
213 | If depth==0, the full path is returned.""" |
|
219 | If depth==0, the full path is returned.""" | |
214 |
|
220 | |||
215 | full_cwd = os.getcwd() |
|
221 | full_cwd = os.getcwdu() | |
216 | cwd = full_cwd.replace(HOME,"~").split(os.sep) |
|
222 | cwd = full_cwd.replace(HOME,"~").split(os.sep) | |
217 | if '~' in cwd and len(cwd) == depth+1: |
|
223 | if '~' in cwd and len(cwd) == depth+1: | |
218 | depth += 1 |
|
224 | depth += 1 | |
@@ -228,9 +234,9 b' def cwd_filt2(depth):' | |||||
228 | #----------------------------------------------------------------------------- |
|
234 | #----------------------------------------------------------------------------- | |
229 |
|
235 | |||
230 | lazily_evaluate = {'time': LazyEvaluate(time.strftime, "%H:%M:%S"), |
|
236 | lazily_evaluate = {'time': LazyEvaluate(time.strftime, "%H:%M:%S"), | |
231 | 'cwd': LazyEvaluate(os.getcwd), |
|
237 | 'cwd': LazyEvaluate(os.getcwdu), | |
232 | 'cwd_last': LazyEvaluate(lambda: os.getcwd().split(os.sep)[-1]), |
|
238 | 'cwd_last': LazyEvaluate(lambda: os.getcwdu().split(os.sep)[-1]), | |
233 |
'cwd_x': [LazyEvaluate(lambda: os.getcwd().replace( |
|
239 | 'cwd_x': [LazyEvaluate(lambda: os.getcwdu().replace(HOME,"~"))] +\ | |
234 | [LazyEvaluate(cwd_filt, x) for x in range(1,6)], |
|
240 | [LazyEvaluate(cwd_filt, x) for x in range(1,6)], | |
235 | 'cwd_y': [LazyEvaluate(cwd_filt2, x) for x in range(6)] |
|
241 | 'cwd_y': [LazyEvaluate(cwd_filt2, x) for x in range(6)] | |
236 | } |
|
242 | } |
@@ -1,12 +1,16 b'' | |||||
|
1 | # -*- coding: utf-8 | |||
1 | """Tests for prompt generation.""" |
|
2 | """Tests for prompt generation.""" | |
2 |
|
3 | |||
3 | import unittest |
|
4 | import unittest | |
4 |
|
5 | |||
|
6 | import os | |||
5 | import nose.tools as nt |
|
7 | import nose.tools as nt | |
6 |
|
8 | |||
7 | from IPython.testing import tools as tt, decorators as dec |
|
9 | from IPython.testing import tools as tt, decorators as dec | |
8 | from IPython.core.prompts import PromptManager |
|
10 | from IPython.core.prompts import PromptManager, LazyEvaluate | |
9 | from IPython.testing.globalipapp import get_ipython |
|
11 | from IPython.testing.globalipapp import get_ipython | |
|
12 | from IPython.utils import py3compat | |||
|
13 | from IPython.utils.tempdir import TemporaryDirectory | |||
10 |
|
14 | |||
11 | ip = get_ipython() |
|
15 | ip = get_ipython() | |
12 |
|
16 | |||
@@ -60,3 +64,47 b' class PromptTests(unittest.TestCase):' | |||||
60 | def test_render(self): |
|
64 | def test_render(self): | |
61 | self.pm.in_template = r'\#>' |
|
65 | self.pm.in_template = r'\#>' | |
62 | self.assertEqual(self.pm.render('in',color=False), '%d>' % ip.execution_count) |
|
66 | self.assertEqual(self.pm.render('in',color=False), '%d>' % ip.execution_count) | |
|
67 | ||||
|
68 | def test_render_unicode_cwd(self): | |||
|
69 | save = os.getcwdu() | |||
|
70 | with TemporaryDirectory(u'ünicødé') as td: | |||
|
71 | os.chdir(td) | |||
|
72 | self.pm.in_template = r'\w [\#]' | |||
|
73 | p = self.pm.render('in', color=False) | |||
|
74 | self.assertEquals(p, u"%s [%i]" % (os.getcwdu(), ip.execution_count)) | |||
|
75 | os.chdir(save) | |||
|
76 | ||||
|
77 | def test_lazy_eval_unicode(self): | |||
|
78 | u = u'ünicødé' | |||
|
79 | lz = LazyEvaluate(lambda : u) | |||
|
80 | # str(lz) would fail | |||
|
81 | self.assertEquals(unicode(lz), u) | |||
|
82 | self.assertEquals(format(lz), u) | |||
|
83 | ||||
|
84 | def test_lazy_eval_nonascii_bytes(self): | |||
|
85 | u = u'ünicødé' | |||
|
86 | b = u.encode('utf8') | |||
|
87 | lz = LazyEvaluate(lambda : b) | |||
|
88 | # unicode(lz) would fail | |||
|
89 | self.assertEquals(str(lz), str(b)) | |||
|
90 | self.assertEquals(format(lz), str(b)) | |||
|
91 | ||||
|
92 | def test_lazy_eval_float(self): | |||
|
93 | f = 0.503 | |||
|
94 | lz = LazyEvaluate(lambda : f) | |||
|
95 | ||||
|
96 | self.assertEquals(str(lz), str(f)) | |||
|
97 | self.assertEquals(unicode(lz), unicode(f)) | |||
|
98 | self.assertEquals(format(lz), str(f)) | |||
|
99 | self.assertEquals(format(lz, '.1'), '0.5') | |||
|
100 | ||||
|
101 | def test_cwd_x(self): | |||
|
102 | self.pm.in_template = r"\X0" | |||
|
103 | save = os.getcwdu() | |||
|
104 | os.chdir(os.path.expanduser('~')) | |||
|
105 | p = self.pm.render('in', color=False) | |||
|
106 | try: | |||
|
107 | self.assertEquals(p, '~') | |||
|
108 | finally: | |||
|
109 | os.chdir(save) | |||
|
110 |
@@ -565,12 +565,15 b' class TerminalInteractiveShell(InteractiveShell):' | |||||
565 |
|
565 | |||
566 | if self.has_readline: |
|
566 | if self.has_readline: | |
567 | self.set_readline_completer() |
|
567 | self.set_readline_completer() | |
|
568 | ||||
|
569 | # raw_input expects str, but we pass it unicode sometimes | |||
|
570 | prompt = py3compat.cast_bytes_py2(prompt) | |||
568 |
|
571 | |||
569 | try: |
|
572 | try: | |
570 | line = py3compat.str_to_unicode(self.raw_input_original(prompt)) |
|
573 | line = py3compat.str_to_unicode(self.raw_input_original(prompt)) | |
571 | except ValueError: |
|
574 | except ValueError: | |
572 | warn("\n********\nYou or a %run:ed script called sys.stdin.close()" |
|
575 | warn("\n********\nYou or a %run:ed script called sys.stdin.close()" | |
573 | " or sys.stdout.close()!\nExiting IPython!") |
|
576 | " or sys.stdout.close()!\nExiting IPython!\n") | |
574 | self.ask_exit() |
|
577 | self.ask_exit() | |
575 | return "" |
|
578 | return "" | |
576 |
|
579 |
@@ -102,6 +102,7 b' class NoColors:' | |||||
102 | """This defines all the same names as the colour classes, but maps them to |
|
102 | """This defines all the same names as the colour classes, but maps them to | |
103 | empty strings, so it can easily be substituted to turn off colours.""" |
|
103 | empty strings, so it can easily be substituted to turn off colours.""" | |
104 | NoColor = '' |
|
104 | NoColor = '' | |
|
105 | Normal = '' | |||
105 |
|
106 | |||
106 | for name, value in color_templates: |
|
107 | for name, value in color_templates: | |
107 | setattr(NoColors, name, '') |
|
108 | setattr(NoColors, name, '') |
General Comments 0
You need to be logged in to leave comments.
Login now