def _get_text(self, size): maxcol = size[0] - len(self.prefix) # self.prefix is a padding var_label = self.var_label or '' value_str = self.value_str or '' alltext = var_label + ": " + value_str # The first line is not indented firstline = self.prefix + alltext[:maxcol] if not alltext[maxcol:]: return [firstline] fulllines, rest = divmod(text_width(alltext) - maxcol, maxcol - 2) restlines = [alltext[(maxcol - 2)*i + maxcol:(maxcol - 2)*i + 2*maxcol - 2] for i in xrange(fulllines + bool(rest))] return [firstline] + [" " + self.prefix + i for i in restlines]
def _get_text(self, size): maxcol = size[0] - len(self.prefix) # self.prefix is a padding var_label = self.var_label or '' value_str = self.value_str or '' alltext = var_label + ": " + value_str # The first line is not indented firstline = self.prefix + alltext[:maxcol] if not alltext[maxcol:]: return [firstline] fulllines, rest = divmod(len(alltext) - maxcol, maxcol - 2) restlines = [alltext[(maxcol - 2)*i + maxcol:(maxcol - 2)*i + 2*maxcol - 2] for i in xrange(fulllines + bool(rest))] return [firstline] + [" " + self.prefix + i for i in restlines]
def _get_wrapped_lines(self, maxcol: int) -> List[str]: """ :param maxcol: the number of columns available to this widget :return: list of string lines, including prefixes, wrapped to fit in the available space """ maxcol -= len(self.prefix) # self.prefix is padding var_label = self.var_label or "" value_str = self.value_str or "" alltext = var_label + ": " + value_str # The first line is not indented firstline = self.prefix + alltext[:maxcol] if not alltext[maxcol:]: return [firstline] fulllines, rest = divmod(text_width(alltext) - maxcol, maxcol - 2) restlines = [alltext[(maxcol - 2)*i + maxcol:(maxcol - 2)*i + 2*maxcol - 2] for i in xrange(fulllines + bool(rest))] return [firstline] + [self.prefix + " " + i for i in restlines]
def walk_value(self, prefix, label, value, id_path=None, attr_prefix=None): if id_path is None: id_path = label iinfo = self.frame_var_info.get_inspect_info(id_path, read_only=True) if isinstance(value, integer_types + (float, complex)): self.add_item(prefix, label, repr(value), id_path, attr_prefix) elif isinstance(value, string_types): self.add_item(prefix, label, repr(value), id_path, attr_prefix) elif value is None: self.add_item(prefix, label, repr(value), id_path, attr_prefix) else: try: displayed_value = get_stringifier(iinfo)(value) except Exception: # Unfortunately, anything can happen when calling str() or # repr() on a random object. displayed_value = type_stringifier(value) \ + " (!! %s error !!)" % iinfo.display_type if iinfo.show_detail: if iinfo.access_level == "public": marker = "pub" elif iinfo.access_level == "private": marker = "pri" else: marker = "all" if iinfo.show_methods: marker += "+()" displayed_value += " [%s]" % marker self.add_item(prefix, label, displayed_value, id_path, attr_prefix) if not iinfo.show_detail: return # set --------------------------------------------------------- if isinstance(value, (set, frozenset)): for i, entry in enumerate(value): if i % 10 == 0 and i: cont_id_path = "%s.cont-%d" % (id_path, i) if not self.frame_var_info.get_inspect_info( cont_id_path, read_only=True).show_detail: self.add_item(prefix+self.PREFIX, "...", None, cont_id_path) break self.walk_value(prefix+self.PREFIX, None, entry, "%s[%d]" % (id_path, i)) if not value: self.add_item(prefix+self.PREFIX, "<empty>", None) return # containers -------------------------------------------------- key_it = None try: if PY3: key_it = value.keys() else: key_it = value.iterkeys() except Exception: pass if key_it is None: try: len_value = len(value) except Exception: pass else: try: value[0] except IndexError: key_it = [] except Exception: pass else: key_it = xrange(len_value) if key_it is not None: cnt = 0 for key in key_it: if cnt % 10 == 0 and cnt: cont_id_path = "%s.cont-%d" % (id_path, cnt) if not self.frame_var_info.get_inspect_info( cont_id_path, read_only=True).show_detail: self.add_item( prefix+self.PREFIX, "...", None, cont_id_path) break self.walk_value(prefix+self.PREFIX, repr(key), value[key], "%s[%r]" % (id_path, key)) cnt += 1 if not cnt: self.add_item(prefix+self.PREFIX, "<empty>", None) return # class types ------------------------------------------------- key_its = [] try: key_its.append(dir(value)) except Exception: pass keys = [key for ki in key_its for key in ki] keys.sort() cnt_omitted_private = cnt_omitted_methods = 0 for key in keys: if iinfo.access_level == "public": if key.startswith("_"): cnt_omitted_private += 1 continue elif iinfo.access_level == "private": if key.startswith("__") and key.endswith("__"): cnt_omitted_private += 1 continue try: attr_value = getattr(value, key) if callable(attr_value) and not iinfo.show_methods: cnt_omitted_methods += 1 continue except Exception: attr_value = WatchEvalError() self.walk_value(prefix+self.PREFIX, ".%s" % key, attr_value, "%s.%s" % (id_path, key)) if not keys: if cnt_omitted_private: label = "<omitted private attributes>" elif cnt_omitted_methods: label = "<omitted methods>" else: label = "<empty>" self.add_item(prefix+self.PREFIX, label, None) if not key_its: self.add_item(prefix+self.PREFIX, "<?>", None)
def render(self, size, focus=False): from pudb.ui_tools import make_canvas maxcol = size[0] if focus: apfx = "focused "+self.attr_prefix+" " else: apfx = self.attr_prefix+" " var_label = self.var_label or '' if self.wrap: text = self._get_text(size) extralabel_full, extralabel_rem = divmod( text_width(var_label[maxcol:]), maxcol) totallen = sum([text_width(i) for i in text]) labellen = ( len(self.prefix) # Padding of first line + (len(self.prefix) + 2) # Padding of subsequent lines * (extralabel_full + bool(extralabel_rem)) + text_width(var_label) + 2 # for ": " ) _attr = [(apfx+"label", labellen), (apfx+"value", totallen - labellen)] from urwid.util import rle_subseg fullcols, rem = divmod(totallen, maxcol) attr = [rle_subseg(_attr, i*maxcol, (i + 1)*maxcol) for i in xrange(fullcols + bool(rem))] return make_canvas(text, attr, maxcol, apfx+"value") lprefix = len(self.prefix) if self.value_str is not None: if self.var_label is not None: if len(self.prefix) + text_width(self.var_label) > self.SIZE_LIMIT: # label too long? generate separate value line text = [self.prefix + self.var_label, self.prefix+" " + self.value_str] attr = [ [(apfx+"label", lprefix+text_width(self.var_label))], [(apfx+"value", lprefix+2+text_width(self.value_str))] ] else: text = [self.prefix + self.var_label + ": " + self.value_str] attr = [[ (apfx+"label", lprefix+text_width(self.var_label)+2), (apfx+"value", text_width(self.value_str)), ]] else: text = [self.prefix + self.value_str] attr = [[ (apfx+"label", len(self.prefix)), (apfx+"value", text_width(self.value_str)), ]] else: text = [self.prefix + self.var_label] attr = [[(apfx+"label", lprefix + text_width(self.var_label)), ]] # Ellipses to show text was cut off #encoding = urwid.util.detected_encoding if False: # encoding[:3] == "UTF": # Unicode is supported, use single character ellipsis for i in xrange(len(text)): if len(text[i]) > maxcol: text[i] = (unicode(text[i][:maxcol-1]) # noqa: F821 + ELLIPSIS + unicode(text[i][maxcol:])) # noqa: F821 # XXX: This doesn't work. It just gives a ? # Strangely, the following does work (it gives the … # three characters from the right): # # text[i] = (unicode(text[i][:maxcol-3]) # + unicode(u'…')) + unicode(text[i][maxcol-2:]) else: for i in xrange(len(text)): if text_width(text[i]) > maxcol: text[i] = text[i][:maxcol-3] + "..." return make_canvas(text, attr, maxcol, apfx+"value")
def walk_value(self, prefix, label, value, id_path=None, attr_prefix=None): if id_path is None: id_path = label iinfo = self.frame_var_info.get_inspect_info(id_path, read_only=True) if isinstance(value, integer_types + (float, complex)): self.add_item(prefix, label, repr(value), id_path, attr_prefix) elif isinstance(value, string_types): self.add_item(prefix, label, repr(value), id_path, attr_prefix) else: try: displayed_value = get_stringifier(iinfo)(value) except Exception: ## Unfortunately, anything can happen when calling str() or ## repr() on a random object. displayed_value = type_stringifier(value) \ + " (!! %s error !!)" % iinfo.display_type self.add_item(prefix, label, displayed_value, id_path, attr_prefix) if not iinfo.show_detail: return # set --------------------------------------------------------- if isinstance(value, (set, frozenset)): for i, entry in enumerate(value): if i % 10 == 0 and i: cont_id_path = "%s.cont-%d" % (id_path, i) if not self.frame_var_info.get_inspect_info( cont_id_path, read_only=True).show_detail: self.add_item(prefix+self.PREFIX, "...", None, cont_id_path) break self.walk_value(prefix+self.PREFIX, None, entry, "%s[%d]" % (id_path, i)) if not value: self.add_item(prefix+self.PREFIX, "<empty>", None) return # containers -------------------------------------------------- key_it = None try: l = len(value) except: pass else: try: value[0] except IndexError: key_it = [] except: pass else: key_it = xrange(l) try: key_it = value.iterkeys() except: pass if key_it is not None: cnt = 0 for key in key_it: if cnt % 10 == 0 and cnt: cont_id_path = "%s.cont-%d" % (id_path, cnt) if not self.frame_var_info.get_inspect_info( cont_id_path, read_only=True).show_detail: self.add_item( prefix+self.PREFIX, "...", None, cont_id_path) break self.walk_value(prefix+self.PREFIX, repr(key), value[key], "%s[%r]" % (id_path, key)) cnt += 1 if not cnt: self.add_item(prefix+self.PREFIX, "<empty>", None) return # class types ------------------------------------------------- key_its = [] try: key_its.append(dir(value)) except: pass keys = [key for key_it in key_its for key in key_it] keys.sort() cnt_omitted = 0 for key in keys: if key[0] == "_" and not iinfo.show_private_members: cnt_omitted += 1 continue try: attr_value = getattr(value, key) except: attr_value = WatchEvalError() self.walk_value(prefix+self.PREFIX, ".%s" % key, attr_value, "%s.%s" % (id_path, key)) if not keys: if cnt_omitted: self.add_item(prefix+self.PREFIX, "<omitted private attributes>", None) else: self.add_item(prefix+self.PREFIX, "<empty>", None) if not key_its: self.add_item(prefix+self.PREFIX, "<?>", None)
def walk_value(self, parent, label, value, id_path=None, attr_prefix=None): if id_path is None: id_path = label iinfo = self.frame_var_info.get_inspect_info(id_path, read_only=True) if isinstance(value, integer_types + (float, complex)): self.add_item(parent, label, repr(value), id_path, attr_prefix) elif isinstance(value, string_types): self.add_item(parent, label, repr(value), id_path, attr_prefix) elif value is None: self.add_item(parent, label, repr(value), id_path, attr_prefix) else: try: displayed_value = get_stringifier(iinfo)(value) except Exception: # Unfortunately, anything can happen when calling str() or # repr() on a random object. displayed_value = type_stringifier(value) \ + " (!! %s error !!)" % iinfo.display_type if iinfo.show_detail: if iinfo.access_level == "public": marker = "pub" elif iinfo.access_level == "private": marker = "pri" else: marker = "all" if iinfo.show_methods: marker += "+()" displayed_value += " [%s]" % marker new_parent_item = self.add_item(parent, label, displayed_value, id_path, attr_prefix) if not iinfo.show_detail: return # set --------------------------------------------------------- if isinstance(value, (set, frozenset)): for i, entry in enumerate(value): if i % 10 == 0 and i: cont_id_path = "%s.cont-%d" % (id_path, i) if not self.frame_var_info.get_inspect_info( cont_id_path, read_only=True).show_detail: self.add_item(new_parent_item, "...", None, cont_id_path) break self.walk_value(new_parent_item, None, entry, "%s[%d]" % (id_path, i)) if not value: self.add_item(new_parent_item, "<empty>", None) return # containers -------------------------------------------------- key_it = None try: if PY3: key_it = value.keys() else: key_it = value.iterkeys() except Exception: pass if key_it is None: try: len_value = len(value) except Exception: pass else: try: value[0] except IndexError: key_it = [] except Exception: pass else: key_it = xrange(len_value) if key_it is not None: cnt = 0 for key in key_it: if cnt % 10 == 0 and cnt: cont_id_path = "%s.cont-%d" % (id_path, cnt) if not self.frame_var_info.get_inspect_info( cont_id_path, read_only=True).show_detail: self.add_item(new_parent_item, "...", None, cont_id_path) break self.walk_value(new_parent_item, repr(key), value[key], "%s[%r]" % (id_path, key)) cnt += 1 if not cnt: self.add_item(new_parent_item, "<empty>", None) return # class types ------------------------------------------------- key_its = [] try: key_its.append(dir(value)) except Exception: pass keys = [key for ki in key_its for key in ki] keys.sort() cnt_omitted_private = cnt_omitted_methods = 0 for key in keys: if iinfo.access_level == "public": if key.startswith("_"): cnt_omitted_private += 1 continue elif iinfo.access_level == "private": if key.startswith("__") and key.endswith("__"): cnt_omitted_private += 1 continue try: attr_value = getattr(value, key) if inspect.isroutine( attr_value) and not iinfo.show_methods: cnt_omitted_methods += 1 continue except Exception: attr_value = WatchEvalError() self.walk_value(new_parent_item, ".%s" % key, attr_value, "%s.%s" % (id_path, key)) if not keys: if cnt_omitted_private: label = "<omitted private attributes>" elif cnt_omitted_methods: label = "<omitted methods>" else: label = "<empty>" self.add_item(new_parent_item, label, None) if not key_its: self.add_item(new_parent_item, "<?>", None)
def render(self, size, focus=False): from pudb.ui_tools import make_canvas maxcol = size[0] if focus: apfx = "focused " + self.attr_prefix + " " else: apfx = self.attr_prefix + " " var_label = self.var_label or '' if self.wrap: text = self._get_text(size) extralabel_full, extralabel_rem = divmod( text_width(var_label[maxcol:]), maxcol) totallen = sum([text_width(i) for i in text]) labellen = ( len(self.prefix) # Padding of first line + (len(self.prefix) + 2) # Padding of subsequent lines * (extralabel_full + bool(extralabel_rem)) + text_width(var_label) + 2 # for ": " ) _attr = [(apfx + "label", labellen), (apfx + "value", totallen - labellen)] from urwid.util import rle_subseg fullcols, rem = divmod(totallen, maxcol) attr = [ rle_subseg(_attr, i * maxcol, (i + 1) * maxcol) for i in xrange(fullcols + bool(rem)) ] return make_canvas(text, attr, maxcol, apfx + "value") lprefix = len(self.prefix) if self.value_str is not None: if self.var_label is not None: if len(self.prefix) + text_width( self.var_label) > self.SIZE_LIMIT: # label too long? generate separate value line text = [ self.prefix + self.var_label, self.prefix + " " + self.value_str ] attr = [[(apfx + "label", lprefix + text_width(self.var_label))], [(apfx + "value", lprefix + 2 + text_width(self.value_str))]] else: text = [ self.prefix + self.var_label + ": " + self.value_str ] attr = [[ (apfx + "label", lprefix + text_width(self.var_label) + 2), (apfx + "value", text_width(self.value_str)), ]] else: text = [self.prefix + self.value_str] attr = [[ (apfx + "label", len(self.prefix)), (apfx + "value", text_width(self.value_str)), ]] else: text = [self.prefix + self.var_label] attr = [[ (apfx + "label", lprefix + text_width(self.var_label)), ]] # Ellipses to show text was cut off #encoding = urwid.util.detected_encoding if False: # encoding[:3] == "UTF": # Unicode is supported, use single character ellipsis for i in xrange(len(text)): if len(text[i]) > maxcol: text[i] = ( unicode(text[i][:maxcol - 1]) # noqa: F821 + ELLIPSIS + unicode(text[i][maxcol:])) # noqa: F821 # XXX: This doesn't work. It just gives a ? # Strangely, the following does work (it gives the … # three characters from the right): # # text[i] = (unicode(text[i][:maxcol-3]) # + unicode(u'…')) + unicode(text[i][maxcol-2:]) else: for i in xrange(len(text)): if text_width(text[i]) > maxcol: text[i] = text[i][:maxcol - 3] + "..." return make_canvas(text, attr, maxcol, apfx + "value")
def render(self, size: Tuple[int], focus: bool = False) -> urwid.Canvas: """ :param size: (maxcol,) the number of columns available to this widget :param focus: True if this widget or one of its children is in focus :return: A Canvas subclass instance containing the rendered content of this widget """ from pudb.ui_tools import make_canvas maxcol = size[0] if focus: apfx = "focused " + self.attr_prefix + " " else: apfx = self.attr_prefix + " " var_label = self.var_label or "" if self.wrap: text = self._get_wrapped_lines(maxcol) extralabel_full, extralabel_rem = divmod( text_width(var_label[maxcol:]), maxcol) totallen = sum([text_width(i) for i in text]) labellen = ( len(self.prefix) # Padding of first line + (len(self.prefix) + 2) # Padding of subsequent lines * (extralabel_full + bool(extralabel_rem)) + text_width(var_label) + 2 # for ": " ) _attr = [(apfx + "label", labellen), (apfx + "value", totallen - labellen)] from urwid.util import rle_subseg fullcols, rem = divmod(totallen, maxcol) attr = [ rle_subseg(_attr, i * maxcol, (i + 1) * maxcol) for i in xrange(fullcols + bool(rem)) ] return make_canvas(text, attr, maxcol, apfx + "value") lprefix = len(self.prefix) if self.value_str is not None: if self.var_label is not None: if len(self._get_wrapped_lines(maxcol)) > 1: # label too long? generate separate value line text = [ self.prefix + self.var_label + ":", self.prefix + " " + self.value_str ] attr = [[(apfx + "label", lprefix + text_width(self.var_label) + 1)], [(apfx + "value", lprefix + 2 + text_width(self.value_str))]] else: text = [ self.prefix + self.var_label + ": " + self.value_str ] attr = [[ (apfx + "label", lprefix + text_width(self.var_label) + 2), (apfx + "value", text_width(self.value_str)), ]] else: text = [self.prefix + self.value_str] attr = [[ (apfx + "label", len(self.prefix)), (apfx + "value", text_width(self.value_str)), ]] else: text = [self.prefix + self.var_label] attr = [[ (apfx + "label", lprefix + text_width(self.var_label)), ]] # Ellipses to show text was cut off #encoding = urwid.util.detected_encoding if False: # encoding[:3] == "UTF": # Unicode is supported, use single character ellipsis for i in xrange(len(text)): if len(text[i]) > maxcol: text[i] = ( unicode(text[i][:maxcol - 1]) # noqa: F821 + ELLIPSIS + unicode(text[i][maxcol:])) # noqa: F821 # XXX: This doesn't work. It just gives a ? # Strangely, the following does work (it gives the … # three characters from the right): # # text[i] = (unicode(text[i][:maxcol-3]) # + unicode(u'…')) + unicode(text[i][maxcol-2:]) else: for i in xrange(len(text)): if text_width(text[i]) > maxcol: text[i] = text[i][:maxcol - 3] + "..." return make_canvas(text, attr, maxcol, apfx + "value")
def walk_value(self, prefix, label, value, id_path=None, attr_prefix=None): if id_path is None: id_path = label iinfo = self.frame_var_info.get_inspect_info(id_path, read_only=True) if isinstance(value, integer_types + (float, complex)): self.add_item(prefix, label, repr(value), id_path, attr_prefix) elif isinstance(value, string_types): self.add_item(prefix, label, repr(value), id_path, attr_prefix) else: try: displayed_value = get_stringifier(iinfo)(value) except Exception: ## Unfortunately, anything can happen when calling str() or ## repr() on a random object. displayed_value = type_stringifier(value) \ + " (!! %s error !!)" % iinfo.display_type self.add_item(prefix, label, displayed_value, id_path, attr_prefix) if not iinfo.show_detail: return # set --------------------------------------------------------- if isinstance(value, (set, frozenset)): for i, entry in enumerate(value): if i % 10 == 0 and i: cont_id_path = "%s.cont-%d" % (id_path, i) if not self.frame_var_info.get_inspect_info( cont_id_path, read_only=True).show_detail: self.add_item(prefix + self.PREFIX, "...", None, cont_id_path) break self.walk_value(prefix + self.PREFIX, None, entry, "%s[%d]" % (id_path, i)) if not value: self.add_item(prefix + self.PREFIX, "<empty>", None) return # containers -------------------------------------------------- key_it = None try: l = len(value) except: pass else: try: value[0] except IndexError: key_it = [] except: pass else: key_it = xrange(l) try: key_it = value.iterkeys() except: pass if key_it is not None: cnt = 0 for key in key_it: if cnt % 10 == 0 and cnt: cont_id_path = "%s.cont-%d" % (id_path, cnt) if not self.frame_var_info.get_inspect_info( cont_id_path, read_only=True).show_detail: self.add_item(prefix + self.PREFIX, "...", None, cont_id_path) break self.walk_value(prefix + self.PREFIX, repr(key), value[key], "%s[%r]" % (id_path, key)) cnt += 1 if not cnt: self.add_item(prefix + self.PREFIX, "<empty>", None) return # class types ------------------------------------------------- key_its = [] try: key_its.append(dir(value)) except: pass keys = [key for ki in key_its for key in ki] keys.sort() cnt_omitted = 0 for key in keys: if key[0] == "_" and not iinfo.show_private_members: cnt_omitted += 1 continue try: attr_value = getattr(value, key) except: attr_value = WatchEvalError() self.walk_value(prefix + self.PREFIX, ".%s" % key, attr_value, "%s.%s" % (id_path, key)) if not keys: if cnt_omitted: self.add_item(prefix + self.PREFIX, "<omitted private attributes>", None) else: self.add_item(prefix + self.PREFIX, "<empty>", None) if not key_its: self.add_item(prefix + self.PREFIX, "<?>", None)