Fix non-ASCII directories processing

Fixes #788
This commit is contained in:
ZyX 2014-02-08 23:39:02 +04:00
parent faeab131f1
commit 9e8471be9e
3 changed files with 33 additions and 31 deletions

13
powerline/lib/unicode.py Normal file
View File

@ -0,0 +1,13 @@
# vim:fileencoding=utf-8:noet
try:
from __builtin__ import unicode
except ImportError:
unicode = str # NOQA
def u(s):
if type(s) is unicode:
return s
else:
return unicode(s, 'utf-8')

View File

@ -17,6 +17,7 @@ from powerline.lib.vcs import guess, tree_status
from powerline.lib.threaded import ThreadedSegment, KwThreadedSegment, with_docstring from powerline.lib.threaded import ThreadedSegment, KwThreadedSegment, with_docstring
from powerline.lib.monotonic import monotonic from powerline.lib.monotonic import monotonic
from powerline.lib.humanize_bytes import humanize_bytes from powerline.lib.humanize_bytes import humanize_bytes
from powerline.lib.unicode import u
from powerline.theme import requires_segment_info from powerline.theme import requires_segment_info
from collections import namedtuple from collections import namedtuple
@ -91,7 +92,7 @@ def cwd(pl, segment_info, dir_shorten_len=None, dir_limit_depth=None, use_path_s
Highlight groups used: ``cwd:current_folder`` or ``cwd``. It is recommended to define all highlight groups. Highlight groups used: ``cwd:current_folder`` or ``cwd``. It is recommended to define all highlight groups.
''' '''
try: try:
cwd = segment_info['getcwd']() cwd = u(segment_info['getcwd']())
except OSError as e: except OSError as e:
if e.errno == 2: if e.errno == 2:
# user most probably deleted the directory # user most probably deleted the directory
@ -100,7 +101,7 @@ def cwd(pl, segment_info, dir_shorten_len=None, dir_limit_depth=None, use_path_s
cwd = "[not found]" cwd = "[not found]"
else: else:
raise raise
home = segment_info['home'] home = u(segment_info['home'])
if home: if home:
cwd = re.sub('^' + re.escape(home), '~', cwd, 1) cwd = re.sub('^' + re.escape(home), '~', cwd, 1)
cwd_split = cwd.split(os.sep) cwd_split = cwd.split(os.sep)
@ -335,7 +336,7 @@ class WeatherSegment(ThreadedSegment):
import json import json
if not self.url: if not self.url:
# Do not lock attribute assignments in this branch: they are used # Do not lock attribute assignments in this branch: they are used
# only in .update() # only in .update()
if not self.location: if not self.location:
location_data = json.loads(urllib_read('http://freegeoip.net/json/')) location_data = json.loads(urllib_read('http://freegeoip.net/json/'))
@ -428,12 +429,12 @@ weather conditions.
:param str temp_format: :param str temp_format:
format string, receives ``temp`` as an argument. Should also hold unit. format string, receives ``temp`` as an argument. Should also hold unit.
:param float temp_coldest: :param float temp_coldest:
coldest temperature. Any temperature below it will have gradient level equal coldest temperature. Any temperature below it will have gradient level equal
to zero. to zero.
:param float temp_hottest: :param float temp_hottest:
hottest temperature. Any temperature above it will have gradient level equal hottest temperature. Any temperature above it will have gradient level equal
to 100. Temperatures between ``temp_coldest`` and ``temp_hottest`` receive to 100. Temperatures between ``temp_coldest`` and ``temp_hottest`` receive
gradient level that indicates relative position in this interval gradient level that indicates relative position in this interval
(``100 * (cur-coldest) / (hottest-coldest)``). (``100 * (cur-coldest) / (hottest-coldest)``).
Divider highlight group used: ``background:divider``. Divider highlight group used: ``background:divider``.
@ -453,17 +454,17 @@ def system_load(pl, format='{avg:.1f}', threshold_good=1, threshold_bad=2, track
:param str format: :param str format:
format string, receives ``avg`` as an argument format string, receives ``avg`` as an argument
:param float threshold_good: :param float threshold_good:
threshold for gradient level 0: any normalized load average below this threshold for gradient level 0: any normalized load average below this
value will have this gradient level. value will have this gradient level.
:param float threshold_bad: :param float threshold_bad:
threshold for gradient level 100: any normalized load average above this threshold for gradient level 100: any normalized load average above this
value will have this gradient level. Load averages between value will have this gradient level. Load averages between
``threshold_good`` and ``threshold_bad`` receive gradient level that ``threshold_good`` and ``threshold_bad`` receive gradient level that
indicates relative position in this interval: indicates relative position in this interval:
(``100 * (cur-good) / (bad-good)``). (``100 * (cur-good) / (bad-good)``).
Note: both parameters are checked against normalized load averages. Note: both parameters are checked against normalized load averages.
:param bool track_cpu_count: :param bool track_cpu_count:
if True powerline will continuously poll the system to detect changes if True powerline will continuously poll the system to detect changes
in the number of CPUs. in the number of CPUs.
Divider highlight group used: ``background:divider``. Divider highlight group used: ``background:divider``.
@ -629,7 +630,7 @@ elif 'psutil' in globals():
from time import time from time import time
def _get_uptime(): # NOQA def _get_uptime(): # NOQA
# psutil.BOOT_TIME is not subject to clock adjustments, but time() is. # psutil.BOOT_TIME is not subject to clock adjustments, but time() is.
# Thus it is a fallback to /proc/uptime reading and not the reverse. # Thus it is a fallback to /proc/uptime reading and not the reverse.
return int(time() - psutil.BOOT_TIME) return int(time() - psutil.BOOT_TIME)
else: else:
@ -780,10 +781,10 @@ falls back to reading
:param str sent_format: :param str sent_format:
format string, receives ``value`` as argument format string, receives ``value`` as argument
:param float recv_max: :param float recv_max:
maximum number of received bytes per second. Is only used to compute maximum number of received bytes per second. Is only used to compute
gradient level gradient level
:param float sent_max: :param float sent_max:
maximum number of sent bytes per second. Is only used to compute gradient maximum number of sent bytes per second. Is only used to compute gradient
level level
Divider highlight group used: ``background:divider``. Divider highlight group used: ``background:divider``.
@ -855,8 +856,8 @@ email_imap_alert = with_docstring(EmailIMAPSegment(),
:param str folder: :param str folder:
folder to check for e-mails folder to check for e-mails
:param int max_msgs: :param int max_msgs:
Maximum number of messages. If there are more messages then max_msgs then it Maximum number of messages. If there are more messages then max_msgs then it
will use gradient level equal to 100, otherwise gradient level is equal to will use gradient level equal to 100, otherwise gradient level is equal to
``100 * msgs_num / max_msgs``. If not present gradient is not computed. ``100 * msgs_num / max_msgs``. If not present gradient is not computed.
Highlight groups used: ``email_alert_gradient`` (gradient), ``email_alert``. Highlight groups used: ``email_alert_gradient`` (gradient), ``email_alert``.

View File

@ -1,19 +1,7 @@
# vim:fileencoding=utf-8:noet # vim:fileencoding=utf-8:noet
from .segment import gen_segment_getter from powerline.segment import gen_segment_getter
from powerline.lib.unicode import u, unicode
try:
from __builtin__ import unicode
except ImportError:
unicode = str # NOQA
def u(s):
if type(s) is unicode:
return s
else:
return unicode(s, 'utf-8')
def requires_segment_info(func): def requires_segment_info(func):