mirror of
https://github.com/powerline/powerline.git
synced 2025-07-22 13:25:13 +02:00
Add some hacks to make code compatible with pdbpp
This commit is contained in:
parent
9bbec772e0
commit
b0d295301b
@ -1,8 +1,98 @@
|
|||||||
# vim:fileencoding=utf-8:noet
|
# vim:fileencoding=utf-8:noet
|
||||||
from __future__ import (unicode_literals, division, absolute_import, print_function)
|
from __future__ import (unicode_literals, division, absolute_import, print_function)
|
||||||
|
|
||||||
|
import sys
|
||||||
|
|
||||||
from powerline.pdb import PDBPowerline
|
from powerline.pdb import PDBPowerline
|
||||||
from powerline.lib.encoding import get_preferred_output_encoding
|
from powerline.lib.encoding import get_preferred_output_encoding
|
||||||
|
from powerline.lib.unicode import unicode
|
||||||
|
|
||||||
|
|
||||||
|
if sys.version_info < (3,):
|
||||||
|
# XXX The below classes make code compatible with PDBpp which uses pyrepl
|
||||||
|
# which does not expect unicode or something above ASCII. They are
|
||||||
|
# completely not needed if pdbpp is not used, but that’s not always the
|
||||||
|
# case.
|
||||||
|
class PowerlineRenderBytesResult(bytes):
|
||||||
|
def __new__(cls, s, encoding=None):
|
||||||
|
encoding = encoding or s.encoding
|
||||||
|
self = bytes.__new__(cls, s.encode(encoding) if isinstance(s, unicode) else s)
|
||||||
|
self.encoding = encoding
|
||||||
|
return self
|
||||||
|
|
||||||
|
for meth in (
|
||||||
|
'__contains__',
|
||||||
|
'partition', 'rpartition',
|
||||||
|
'split', 'rsplit',
|
||||||
|
'count', 'join',
|
||||||
|
):
|
||||||
|
exec((
|
||||||
|
'def {0}(self, *args):\n'
|
||||||
|
' if any((isinstance(arg, unicode) for arg in args)):\n'
|
||||||
|
' return self.__unicode__().{0}(*args)\n'
|
||||||
|
' else:\n'
|
||||||
|
' return bytes.{0}(self, *args)'
|
||||||
|
).format(meth))
|
||||||
|
|
||||||
|
for meth in (
|
||||||
|
'find', 'rfind',
|
||||||
|
'index', 'rindex',
|
||||||
|
):
|
||||||
|
exec((
|
||||||
|
'def {0}(self, *args):\n'
|
||||||
|
' if any((isinstance(arg, unicode) for arg in args)):\n'
|
||||||
|
' args = [arg.encode(self.encoding) if isinstance(arg, unicode) else arg for arg in args]\n'
|
||||||
|
' return bytes.{0}(self, *args)'
|
||||||
|
).format(meth))
|
||||||
|
|
||||||
|
def __len__(self):
|
||||||
|
return len(self.decode(self.encoding))
|
||||||
|
|
||||||
|
def __getitem__(self, *args):
|
||||||
|
return PowerlineRenderBytesResult(bytes.__getitem__(self, *args), encoding=self.encoding)
|
||||||
|
|
||||||
|
def __getslice__(self, *args):
|
||||||
|
return PowerlineRenderBytesResult(bytes.__getslice__(self, *args), encoding=self.encoding)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def add(encoding, *args):
|
||||||
|
if any((isinstance(arg, unicode) for arg in args)):
|
||||||
|
return ''.join((
|
||||||
|
arg
|
||||||
|
if isinstance(arg, unicode)
|
||||||
|
else arg.decode(encoding)
|
||||||
|
for arg in args
|
||||||
|
))
|
||||||
|
else:
|
||||||
|
return PowerlineRenderBytesResult(b''.join(args), encoding=encoding)
|
||||||
|
|
||||||
|
def __add__(self, other):
|
||||||
|
return self.add(self.encoding, self, other)
|
||||||
|
|
||||||
|
def __radd__(self, other):
|
||||||
|
return self.add(self.encoding, other, self)
|
||||||
|
|
||||||
|
def __unicode__(self):
|
||||||
|
return PowerlineRenderResult(self)
|
||||||
|
|
||||||
|
class PowerlineRenderResult(unicode):
|
||||||
|
def __new__(cls, s, encoding=None):
|
||||||
|
encoding = (
|
||||||
|
encoding
|
||||||
|
or getattr(s, 'encoding', None)
|
||||||
|
or get_preferred_output_encoding()
|
||||||
|
)
|
||||||
|
if isinstance(s, unicode):
|
||||||
|
self = unicode.__new__(cls, s)
|
||||||
|
else:
|
||||||
|
self = unicode.__new__(cls, s, encoding, 'replace')
|
||||||
|
self.encoding = encoding
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return PowerlineRenderBytesResult(self)
|
||||||
|
else:
|
||||||
|
PowerlineRenderResult = str
|
||||||
|
|
||||||
|
|
||||||
def use_powerline_prompt(cls):
|
def use_powerline_prompt(cls):
|
||||||
@ -15,8 +105,6 @@ def use_powerline_prompt(cls):
|
|||||||
``cls`` argument or a class derived from it. Latter is used to turn
|
``cls`` argument or a class derived from it. Latter is used to turn
|
||||||
old-style classes into new-style classes.
|
old-style classes into new-style classes.
|
||||||
'''
|
'''
|
||||||
encoding = get_preferred_output_encoding()
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def prompt(self):
|
def prompt(self):
|
||||||
try:
|
try:
|
||||||
@ -25,11 +113,7 @@ def use_powerline_prompt(cls):
|
|||||||
powerline = PDBPowerline()
|
powerline = PDBPowerline()
|
||||||
powerline.setup(self)
|
powerline.setup(self)
|
||||||
self.powerline = powerline
|
self.powerline = powerline
|
||||||
ret = powerline.render(side='left')
|
return PowerlineRenderResult(powerline.render(side='left'))
|
||||||
if not isinstance(ret, str):
|
|
||||||
# Python-2
|
|
||||||
ret = ret.encode(encoding)
|
|
||||||
return ret
|
|
||||||
|
|
||||||
@prompt.setter
|
@prompt.setter
|
||||||
def prompt(self, _):
|
def prompt(self, _):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user