mirror of
https://github.com/nicolargo/glances.git
synced 2026-03-17 21:37:27 -04:00
Improve string encodeing/deconding
This commit is contained in:
@@ -21,7 +21,8 @@
|
||||
# pylint: skip-file
|
||||
"""Python 2/3 compatibility shims."""
|
||||
|
||||
from __future__ import print_function
|
||||
from __future__ import print_function, unicode_literals
|
||||
|
||||
import operator
|
||||
import sys
|
||||
import unicodedata
|
||||
@@ -84,23 +85,29 @@ if PY3:
|
||||
def itervalues(d):
|
||||
return iter(d.values())
|
||||
|
||||
def u(s):
|
||||
def u(s, errors='replace'):
|
||||
if isinstance(s, text_type):
|
||||
return s
|
||||
return s.decode('utf-8', 'replace')
|
||||
return s.decode('utf-8', errors=errors)
|
||||
|
||||
def b(s):
|
||||
def b(s, errors='replace'):
|
||||
if isinstance(s, binary_type):
|
||||
return s
|
||||
return s.encode('utf-8')
|
||||
return s.encode('utf-8', errors=errors)
|
||||
|
||||
def nativestr(s):
|
||||
def n(s):
|
||||
'''Only in Python 2...
|
||||
from future.utils import bytes_to_native_str as n
|
||||
'''
|
||||
return s
|
||||
|
||||
def nativestr(s, errors='replace'):
|
||||
if isinstance(s, text_type):
|
||||
return s
|
||||
elif isinstance(s, (int, float)):
|
||||
return s.__str__()
|
||||
else:
|
||||
return s.decode('utf-8', 'replace')
|
||||
return s.decode('utf-8', errors=errors)
|
||||
|
||||
def system_exec(command):
|
||||
"""Execute a system command and return the resul as a str"""
|
||||
@@ -113,6 +120,7 @@ if PY3:
|
||||
return res.rstrip()
|
||||
|
||||
else:
|
||||
from future.utils import bytes_to_native_str as n
|
||||
import Queue as queue
|
||||
from itertools import imap as map
|
||||
from ConfigParser import SafeConfigParser as ConfigParser, NoOptionError, NoSectionError
|
||||
@@ -167,23 +175,23 @@ else:
|
||||
def itervalues(d):
|
||||
return d.itervalues()
|
||||
|
||||
def u(s):
|
||||
def u(s, errors='replace'):
|
||||
if isinstance(s, text_type):
|
||||
return s
|
||||
return s.decode('utf-8')
|
||||
return s.decode('utf-8', errors=errors)
|
||||
|
||||
def b(s):
|
||||
def b(s, errors='replace'):
|
||||
if isinstance(s, binary_type):
|
||||
return s
|
||||
return s.encode('utf-8', 'replace')
|
||||
return s.encode('utf-8', errors=errors)
|
||||
|
||||
def nativestr(s):
|
||||
def nativestr(s, errors='replace'):
|
||||
if isinstance(s, binary_type):
|
||||
return s
|
||||
elif isinstance(s, (int, float)):
|
||||
return s.__str__()
|
||||
else:
|
||||
return s.encode('utf-8', 'replace')
|
||||
return s.encode('utf-8', errors=errors)
|
||||
|
||||
def system_exec(command):
|
||||
"""Execute a system command and return the resul as a str"""
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
"""Manage the folder list."""
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import os
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
"""Curses interface class."""
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import re
|
||||
import sys
|
||||
|
||||
@@ -18,8 +18,9 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
"""Disk I/O plugin."""
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from glances.compat import nativestr
|
||||
from glances.compat import nativestr, n
|
||||
from glances.timer import getTimeSinceLastUpdate
|
||||
from glances.plugins.glances_plugin import GlancesPlugin
|
||||
|
||||
@@ -110,7 +111,7 @@ class Plugin(GlancesPlugin):
|
||||
self.diskio_old[disk].write_bytes)
|
||||
diskstat = {
|
||||
'time_since_update': time_since_update,
|
||||
'disk_name': disk,
|
||||
'disk_name': n(disk),
|
||||
'read_count': read_count,
|
||||
'write_count': write_count,
|
||||
'read_bytes': read_bytes,
|
||||
|
||||
@@ -18,10 +18,11 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
"""Folder plugin."""
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import numbers
|
||||
|
||||
from glances.compat import nativestr
|
||||
from glances.compat import nativestr, n
|
||||
from glances.folder_list import FolderList as glancesFolderList
|
||||
from glances.plugins.glances_plugin import GlancesPlugin
|
||||
from glances.logger import logger
|
||||
|
||||
@@ -18,10 +18,11 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
"""File system plugin."""
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import operator
|
||||
|
||||
from glances.compat import u, nativestr
|
||||
from glances.compat import u, nativestr, n
|
||||
from glances.plugins.glances_plugin import GlancesPlugin
|
||||
|
||||
import psutil
|
||||
|
||||
@@ -18,12 +18,14 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
"""Network plugin."""
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import base64
|
||||
import operator
|
||||
|
||||
from glances.timer import getTimeSinceLastUpdate
|
||||
from glances.plugins.glances_plugin import GlancesPlugin
|
||||
from glances.compat import n, u, b, nativestr
|
||||
|
||||
import psutil
|
||||
|
||||
@@ -119,7 +121,7 @@ class Plugin(GlancesPlugin):
|
||||
rx = cumulative_rx - self.network_old[net].bytes_recv
|
||||
tx = cumulative_tx - self.network_old[net].bytes_sent
|
||||
cx = rx + tx
|
||||
netstat = {'interface_name': net,
|
||||
netstat = {'interface_name': n(net),
|
||||
'time_since_update': time_since_update,
|
||||
'cumulative_rx': cumulative_rx,
|
||||
'rx': rx,
|
||||
|
||||
Reference in New Issue
Block a user