bugfixing in parsers
[logparse.git] / logparse / formatting.py
index b2670f81ab89d013f4c3d312108fd796bebb834d..19eda9931596c074dc18511905dfe41ef09c9fd4 100644 (file)
@@ -28,6 +28,7 @@ CORNERCHARS_DOUBLE = ['╚', '╝', '╗', '╔']
 CORNERCHARS_SINGLE = ['└', '┘', '┐', '┌']
 LINECHARS_DOUBLE = ['║', '═']
 LINECHARS_SINGLE = ['│', '─']
+INDENT = "  "
 
 class Output:
     """
@@ -78,7 +79,7 @@ class PlaintextOutput(Output):
         self.append('\n'*2)
         for data in section.data:
             self.append(self._fmt_data(data.subtitle, data.items))
-        self.append('\n')
+            self.append('\n')
 
     def _fmt_data(self, subtitle, data = None):   # write title and data
         if (subtitle == ""):
@@ -97,20 +98,21 @@ class PlaintextOutput(Output):
                 itemoutput = subtitle + '\n'
                 for datum in data:
                     datum = '• ' + datum
-                    if len(datum) > config.prefs['linewidth']:
+                    if len(datum) > config.prefs['linewidth'] - 3:
                         words = datum.split()
-                        if max(map(len, words)) > config.prefs['linewidth']:
-                            raise ValueError("Content width is too small")
+                        if max(map(len, words)) > config.prefs['linewidth'] - len(INDENT):
+                            continue
                         res, part, others = [], words[0], words[1:]
                         for word in others:
-                            if len(' ') + len(word) > config.prefs['linewidth'] - len(part):
+                            if 1 + len(word) > config.prefs['linewidth'] - len(part):
                                 res.append(part)
                                 part = word
                             else:
                                 part += ' ' + word
                         if part:
                             res.append(part)
-                        datum = '\n'.join(res)
+                        datum = ('\n    ').join(res)
+                    datum = INDENT + datum
                     itemoutput += datum + '\n'
                 return itemoutput
 
@@ -214,7 +216,7 @@ class Section:
         self.title = title
         self.data = []
 
-    def add_data(self, data):
+    def append_data(self, data):
         self.data.append(data)
 
 class Data:
@@ -222,7 +224,7 @@ class Data:
     Each section (parser) can have one or more Data() objects which are essentially glorified lists.
     """
     
-    def __init__(self, subtitle=None, items=[]):
+    def __init__(self, subtitle="", items=[]):
         self.subtitle = subtitle
         self.items = items 
 
@@ -230,16 +232,18 @@ class Data:
         if (len(self.items) > limit):
             more = str(len(self.items) - limit)
             self.items = self.items[:limit]
-            self.items..append("+ " + more + " more")
+            self.items.append("+ " + more + " more")
 
-    def orderbyfreq(self, l):     # order a list by the frequency of its elements and remove duplicates
-        temp_l = l[:]
-        l = list(set(l))
-        l = [[i, temp_l.count(i)] for i in l]   # add count of each element
-        l.sort(key=lambda x:temp_l.count(x[0])) # sort by count
-        l = [i[0] + ' (' + str(i[1]) + ')' for i in l]  # put element and count into string
-        l = l[::-1]     # reverse
-        self.items = l 
+    def orderbyfreq(self):     # order a list by the frequency of its elements and remove duplicates
+#        temp = list(self.items)[:]
+#        logger.debug(self.items)
+#        self.items = list(set(self.items))
+#        self.items = [[i, temp.count(i)] for i in self.items]   # add count of each element
+#        self.items.sort(key=lambda x:temp.count(x[0])) # sort by count
+#        self.items  = [i[0] + ' (' + str(i[1]) + ')' for i in self.items]  # put element and count into string
+#        self.items = self.items[::-1]     # reverse
+        unsorted = list(self.items)
+        self.items = [ "{0} ({1})".format(y, unsorted.count(y)) for y in sorted(set(unsorted), key = lambda x: -unsorted.count(x)) ]
 
 
 class PlaintextLine:
@@ -298,7 +302,7 @@ class PlaintextBox:
             if len(line) > contentwidth:
                 words = line.split()
                 if max(map(len, words)) > contentwidth:
-                    raise ValueError("Content width is too small")
+                    continue
                 res, part, others = [], words[0], words[1:]
                 for word in others:
                     if len(' ') + len(word) > contentwidth - len(part):