How to use the glances.compat.u function in Glances

To help you get started, we’ve selected a few Glances examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github nicolargo / glances / glances / outputs / glances_curses.py View on Github external
def get_stats_display_width(self, curse_msg, without_option=False):
        """Return the width of the formatted curses message."""
        try:
            if without_option:
                # Size without options
                c = len(max(''.join([(u(u(nativestr(i['msg'])).encode('ascii', 'replace')) if not i['optional'] else "")
                                     for i in curse_msg['msgdict']]).split('\n'), key=len))
            else:
                # Size with all options
                c = len(max(''.join([u(u(nativestr(i['msg'])).encode('ascii', 'replace'))
                                     for i in curse_msg['msgdict']]).split('\n'), key=len))
        except Exception as e:
            logger.debug('ERROR: Can not compute plugin width ({})'.format(e))
            return 0
        else:
            return c
github nicolargo / glances / glances / amps / glances_default.py View on Github external
logger.debug('{}: Error while executing command ({})'.format(self.NAME, e))
            return self.result()
        # No command found, use default message
        if res is None:
            # Set the default message if command return None
            # Default sum of CPU and MEM for the matching regex
            self.set_result('CPU: {:.1f}% | MEM: {:.1f}%'.format(
                sum([p['cpu_percent'] for p in process_list]),
                sum([p['memory_percent'] for p in process_list])))
            return self.result()
        # Run command(s)
        # Comman separated commands can be executed
        try:
            msg = ''
            for cmd in res.split(';'):
                msg += u(check_output(cmd.split(), stderr=STDOUT))
            self.set_result(to_ascii(msg.rstrip()))
        except CalledProcessError as e:
            self.set_result(e.output)
        return self.result()
github nicolargo / glances / glances / outputs / glances_curses.py View on Github external
def get_stats_display_width(self, curse_msg, without_option=False):
        """Return the width of the formatted curses message."""
        try:
            if without_option:
                # Size without options
                c = len(max(''.join([(u(u(nativestr(i['msg'])).encode('ascii', 'replace')) if not i['optional'] else "")
                                     for i in curse_msg['msgdict']]).split('\n'), key=len))
            else:
                # Size with all options
                c = len(max(''.join([u(u(nativestr(i['msg'])).encode('ascii', 'replace'))
                                     for i in curse_msg['msgdict']]).split('\n'), key=len))
        except Exception as e:
            logger.debug('ERROR: Can not compute plugin width ({})'.format(e))
            return 0
        else:
            return c
github nicolargo / glances / glances / amps / glances_amp.py View on Github external
def set_result(self, result, separator=''):
        """Store the result (string) into the result key of the AMP
        if one_line is true then replace \n by separator
        """
        if self.one_line():
            self.configs['result'] = u(result).replace('\n', separator)
        else:
            self.configs['result'] = u(result)
github nicolargo / glances / glances / outputs / glances_curses.py View on Github external
try:
                self.term_window.addnstr(y, x,
                                         m['msg'],
                                         # Do not disply outside the screen
                                         screen_x - x,
                                         self.colors_list[m['decoration']])
            except Exception:
                pass
            else:
                # New column
                # Python 2: we need to decode to get real screen size because
                # UTF-8 special tree chars occupy several bytes.
                # Python 3: strings are strings and bytes are bytes, all is
                # good.
                try:
                    x += len(u(m['msg']))
                except UnicodeDecodeError:
                    # Quick and dirty hack for issue #745
                    pass
                if x > x_max:
                    x_max = x

        # Compute the next Glances column/line position
        self.next_column = max(
            self.next_column, x_max + self.space_between_column)
        self.next_line = max(self.next_line, y + self.space_between_line)

        # Have empty lines after the plugins
        self.next_line += add_space
github nicolargo / glances / glances / amps / glances_amp.py View on Github external
def result(self):
        """ Return the result of the AMP (as a string)"""
        ret = self.get('result')
        if ret is not None:
            ret = u(ret)
        return ret