From f1e86ef417055b7331d82c3ce46b2ecad0b74289 Mon Sep 17 00:00:00 2001 From: Phil <phil@secdev.org> Date: Tue, 23 Dec 2008 19:25:11 +0100 Subject: [PATCH] Fixed NoTheme and factorized some code (ticket #143) --- scapy/plist.py | 10 +++++----- scapy/themes.py | 34 +++++++++++++++------------------- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/scapy/plist.py b/scapy/plist.py index 9f57beea..f08350f3 100644 --- a/scapy/plist.py +++ b/scapy/plist.py @@ -104,7 +104,7 @@ lfilter: truth function to apply to each packet to decide whether it will be dis if lfilter is not None: if not lfilter(self.res[i]): continue - print conf.color_theme.id(i,"%04i"), + print conf.color_theme.id(i,fmt="%04i"), if prn is None: print self._elt2sum(self.res[i]) else: @@ -187,7 +187,7 @@ lfilter: truth function to apply to each packet to decide whether it will be dis p = self._elt2pkt(self.res[i]) if lfilter is not None and not lfilter(p): continue - print "%s %s %s" % (conf.color_theme.id(i,"%04i"), + print "%s %s %s" % (conf.color_theme.id(i,fmt="%04i"), p.sprintf("%.time%"), self._elt2sum(self.res[i])) if p.haslayer(conf.raw_layer): @@ -200,7 +200,7 @@ lfilter: truth function to apply to each packet to decide whether it will be dis p = self._elt2pkt(self.res[i]) if lfilter is not None and not lfilter(p): continue - print "%s %s %s" % (conf.color_theme.id(i,"%04i"), + print "%s %s %s" % (conf.color_theme.id(i,fmt="%04i"), p.sprintf("%.time%"), self._elt2sum(self.res[i])) hexdump(p) @@ -211,7 +211,7 @@ lfilter: truth function to apply to each packet to decide whether it will be dis p = self._elt2pkt(self.res[i]) if p.haslayer(Padding): if lfilter is None or lfilter(p): - print "%s %s %s" % (conf.color_theme.id(i,"%04i"), + print "%s %s %s" % (conf.color_theme.id(i,fmt="%04i"), p.sprintf("%.time%"), self._elt2sum(self.res[i])) hexdump(p.getlayer(Padding).load) @@ -225,7 +225,7 @@ lfilter: truth function to apply to each packet to decide whether it will be dis if pad == pad[0]*len(pad): continue if lfilter is None or lfilter(p): - print "%s %s %s" % (conf.color_theme.id(i,"%04i"), + print "%s %s %s" % (conf.color_theme.id(i,fmt="%04i"), p.sprintf("%.time%"), self._elt2sum(self.res[i])) hexdump(p.getlayer(Padding).load) diff --git a/scapy/themes.py b/scapy/themes.py index 0a0ebdd5..43257142 100644 --- a/scapy/themes.py +++ b/scapy/themes.py @@ -26,11 +26,21 @@ class Color: invert = "\033[7m" +def create_styler(fmt=None, before="", after="", fmt2="%s"): + def do_style(val, fmt=fmt, before=before, after=after, fmt2=fmt2): + if fmt is None: + if type(val) is not str: + val = str(val) + else: + val = fmt % val + return fmt2 % (before+val+after) + return do_style + class ColorTheme: def __repr__(self): return "<%s>" % self.__class__.__name__ def __getattr__(self, attr): - return lambda x:x + return create_styler() class NoTheme(ColorTheme): @@ -48,14 +58,7 @@ class AnsiColorTheme(ColorTheme): else: before = after = "" - def do_style(val, fmt=None, before=before, after=after): - if fmt is None: - if type(val) is not str: - val = str(val) - else: - val = fmt % val - return before+val+after - return do_style + return create_styler(before=before, after=after) style_normal = "" @@ -174,20 +177,13 @@ class ColorOnBlackTheme(AnsiColorTheme): style_left = Color.cyan+Color.bold style_right = Color.red+Color.bold + class FormatTheme(ColorTheme): def __getattr__(self, attr): if attr.startswith("__"): raise AttributeError(attr) - col = self.__class__.__dict__.get("style_%s" % attr, "%s") - def do_style(val, fmt=None, col=col): - if fmt is None: - if type(val) is not str: - val = str(val) - else: - val = fmt % val - return col % val - return do_style - + colfmt = self.__class__.__dict__.get("style_%s" % attr, "%s") + return create_styler(fmt2 = colfmt) class LatexTheme(FormatTheme): style_prompt = r"\textcolor{blue}{%s}" -- GitLab