code
stringlengths
1
1.49M
vector
listlengths
0
7.38k
snippet
listlengths
0
7.38k
'''Plugin Performance profiling module for ClearCutter Loads an OSSIM plugin and sample log data, and identifies the CPU cost for each SID in a plugin as a percentage of total runtime to process the entire file ''' __author__ = "CP Constantine" __email__ = "conrad@alienvault.com" __copyright__ = 'Copyright:Alienvault 2012' __credits__ = ["Conrad Constantine"] __version__ = "0.1" __license__ = "BSD" __status__ = "Prototype" __maintainer__ = "CP Constantine" import cProfile, pstats, re logdata = '' aliases = { 'IPV4' :"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}", 'IPV6_MAP' : "::ffff:\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}", 'MAC': "\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}", 'PORT': "\d{1,5}", 'HOSTNAME' : "((([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)([a-zA-Z])+)", 'TIME' : "\d\d:\d\d:\d\d", 'SYSLOG_DATE' : "\w{3}\s+\d{1,2}\s\d\d:\d\d:\d\d", 'SYSLOG_WY_DATE' : "\w+\s+\d{1,2}\s\d{4}\s\d\d:\d\d:\d\d", } def __init__(self, log): self.logdata = open(log, 'r').readlines() def ProfileRegexp(self, regexp): cProfile.run('self.Profilewrap(regexp)', 'profiler.out') profstats = pstats.Stats('profiler.out') profstats.print_stats() def ProfileWrap(self, regexp): for line in self.logdata: for alias in self.aliases: tmp_al = "" tmp_al = "\\" + alias; regexp = regexp.replace(tmp_al, self.aliases[alias]) result = re.findall(regexp, line) try: tmp = result[0] except IndexError: continue
[ [ 8, 0, 0.0577, 0.0962, 0, 0.66, 0, 0, 1, 0, 0, 0, 0, 0, 0 ], [ 14, 0, 0.1346, 0.0192, 0, 0.66, 0.0714, 777, 1, 0, 0, 0, 0, 3, 0 ], [ 14, 0, 0.1538, 0.0192, 0, 0.66...
[ "'''Plugin Performance profiling module for ClearCutter\n\nLoads an OSSIM plugin and sample log data, and identifies the CPU cost for each SID in a plugin\nas a percentage of total runtime to process the entire file\n'''", "__author__ = \"CP Constantine\"", "__email__ = \"conrad@alienvault.com\"", "__copyrigh...
#!/usr/bin/python # -*- coding: iso-8859-1 -*- # # progressbar - Text progressbar library for python. # Copyright (c) 2005 Nilton Volpato # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA """Text progressbar library for python. This library provides a text mode progressbar. This is tipically used to display the progress of a long running operation, providing a visual clue that processing is underway. The ProgressBar class manages the progress, and the format of the line is given by a number of widgets. A widget is an object that may display diferently depending on the state of the progress. There are three types of widget: - a string, which always shows itself; - a ProgressBarWidget, which may return a diferent value every time it's update method is called; and - a ProgressBarWidgetHFill, which is like ProgressBarWidget, except it expands to fill the remaining width of the line. The progressbar module is very easy to use, yet very powerful. And automatically supports features like auto-resizing when available. """ __author__ = "Nilton Volpato" __author_email__ = "first-name dot last-name @ gmail.com" __date__ = "2006-05-07" __version__ = "2.2" # Changelog # # 2006-05-07: v2.2 fixed bug in windows # 2005-12-04: v2.1 autodetect terminal width, added start method # 2005-12-04: v2.0 everything is now a widget (wow!) # 2005-12-03: v1.0 rewrite using widgets # 2005-06-02: v0.5 rewrite # 2004-??-??: v0.1 first version import sys, time from array import array try: from fcntl import ioctl import termios except ImportError: pass import signal class ProgressBarWidget(object): """This is an element of ProgressBar formatting. The ProgressBar object will call it's update value when an update is needed. It's size may change between call, but the results will not be good if the size changes drastically and repeatedly. """ def update(self, pbar): """Returns the string representing the widget. The parameter pbar is a reference to the calling ProgressBar, where one can access attributes of the class for knowing how the update must be made. At least this function must be overriden.""" pass class ProgressBarWidgetHFill(object): """This is a variable width element of ProgressBar formatting. The ProgressBar object will call it's update value, informing the width this object must the made. This is like TeX \\hfill, it will expand to fill the line. You can use more than one in the same line, and they will all have the same width, and together will fill the line. """ def update(self, pbar, width): """Returns the string representing the widget. The parameter pbar is a reference to the calling ProgressBar, where one can access attributes of the class for knowing how the update must be made. The parameter width is the total horizontal width the widget must have. At least this function must be overriden.""" pass class ETA(ProgressBarWidget): "Widget for the Estimated Time of Arrival" def format_time(self, seconds): return time.strftime('%H:%M:%S', time.gmtime(seconds)) def update(self, pbar): if pbar.currval == 0: return 'ETA: --:--:--' elif pbar.finished: return 'Time: %s' % self.format_time(pbar.seconds_elapsed) else: elapsed = pbar.seconds_elapsed eta = elapsed * pbar.maxval / pbar.currval - elapsed return 'ETA: %s' % self.format_time(eta) class FileTransferSpeed(ProgressBarWidget): "Widget for showing the transfer speed (useful for file transfers)." def __init__(self): self.fmt = '%6.2f %s' self.units = ['B', 'K', 'M', 'G', 'T', 'P'] def update(self, pbar): if pbar.seconds_elapsed < 2e-6:#== 0: bps = 0.0 else: bps = float(pbar.currval) / pbar.seconds_elapsed spd = bps for u in self.units: if spd < 1000: break spd /= 1000 return self.fmt % (spd, u + '/s') class RotatingMarker(ProgressBarWidget): "A rotating marker for filling the bar of progress." def __init__(self, markers='|/-\\'): self.markers = markers self.curmark = -1 def update(self, pbar): if pbar.finished: return self.markers[0] self.curmark = (self.curmark + 1) % len(self.markers) return self.markers[self.curmark] class Percentage(ProgressBarWidget): "Just the percentage done." def update(self, pbar): return '%3d%%' % pbar.percentage() class SimpleProgress(ProgressBarWidget): "Simple Progress: returns what is already done and the total, e.g. '5 of 47'" def update(self, pbar): return '%d of %d' % (pbar.currval, pbar.maxval) class Bar(ProgressBarWidgetHFill): "The bar of progress. It will strech to fill the line." def __init__(self, marker='#', left='|', right='|'): self.marker = marker self.left = left self.right = right def _format_marker(self, pbar): if isinstance(self.marker, (str, unicode)): return self.marker else: return self.marker.update(pbar) def update(self, pbar, width): percent = pbar.percentage() cwidth = width - len(self.left) - len(self.right) marked_width = int(percent * cwidth / 100) m = self._format_marker(pbar) bar = (self.left + (m * marked_width).ljust(cwidth) + self.right) return bar class ReverseBar(Bar): "The reverse bar of progress, or bar of regress. :)" def update(self, pbar, width): percent = pbar.percentage() cwidth = width - len(self.left) - len(self.right) marked_width = int(percent * cwidth / 100) m = self._format_marker(pbar) bar = (self.left + (m * marked_width).rjust(cwidth) + self.right) return bar default_widgets = [Percentage(), ' ', Bar()] class ProgressBar(object): """This is the ProgressBar class, it updates and prints the bar. The term_width parameter may be an integer. Or None, in which case it will try to guess it, if it fails it will default to 80 columns. The simple use is like this: >>> pbar = ProgressBar().start() >>> for i in xrange(100): ... # do something ... pbar.update(i+1) ... >>> pbar.finish() But anything you want to do is possible (well, almost anything). You can supply different widgets of any type in any order. And you can even write your own widgets! There are many widgets already shipped and you should experiment with them. When implementing a widget update method you may access any attribute or function of the ProgressBar object calling the widget's update method. The most important attributes you would like to access are: - currval: current value of the progress, 0 <= currval <= maxval - maxval: maximum (and final) value of the progress - finished: True if the bar is have finished (reached 100%), False o/w - start_time: first time update() method of ProgressBar was called - seconds_elapsed: seconds elapsed since start_time - percentage(): percentage of the progress (this is a method) """ def __init__(self, maxval=100, widgets=default_widgets, term_width=None, fd=sys.stderr): assert maxval > 0 self.maxval = maxval self.widgets = widgets self.fd = fd self.signal_set = False if term_width is None: try: self.handle_resize(None, None) signal.signal(signal.SIGWINCH, self.handle_resize) self.signal_set = True except: self.term_width = 79 else: self.term_width = term_width self.currval = 0 self.finished = False self.prev_percentage = -1 self.start_time = None self.seconds_elapsed = 0 def handle_resize(self, signum, frame): h, w = array('h', ioctl(self.fd, termios.TIOCGWINSZ, '\0' * 8))[:2] self.term_width = w def percentage(self): "Returns the percentage of the progress." return self.currval * 100.0 / self.maxval def _format_widgets(self): r = [] hfill_inds = [] num_hfill = 0 currwidth = 0 for i, w in enumerate(self.widgets): if isinstance(w, ProgressBarWidgetHFill): r.append(w) hfill_inds.append(i) num_hfill += 1 elif isinstance(w, (str, unicode)): r.append(w) currwidth += len(w) else: weval = w.update(self) currwidth += len(weval) r.append(weval) for iw in hfill_inds: r[iw] = r[iw].update(self, (self.term_width - currwidth) / num_hfill) return r def _format_line(self): return ''.join(self._format_widgets()).ljust(self.term_width) def _need_update(self): return int(self.percentage()) != int(self.prev_percentage) def update(self, value): "Updates the progress bar to a new value." assert 0 <= value <= self.maxval self.currval = value if not self._need_update() or self.finished: return if not self.start_time: self.start_time = time.time() self.seconds_elapsed = time.time() - self.start_time self.prev_percentage = self.percentage() if value != self.maxval: self.fd.write(self._format_line() + '\r') else: self.finished = True self.fd.write(self._format_line() + '\n') def start(self): """Start measuring time, and prints the bar at 0%. It returns self so you can use it like this: >>> pbar = ProgressBar().start() >>> for i in xrange(100): ... # do something ... pbar.update(i+1) ... >>> pbar.finish() """ self.update(0) return self def finish(self): """Used to tell the progress is finished.""" self.update(self.maxval) if self.signal_set: signal.signal(signal.SIGWINCH, signal.SIG_DFL) if __name__ == '__main__': import os def example1(): widgets = ['Test: ', Percentage(), ' ', Bar(marker=RotatingMarker()), ' ', ETA(), ' ', FileTransferSpeed()] pbar = ProgressBar(widgets=widgets, maxval=10000000).start() for i in range(1000000): # do something pbar.update(10 * i + 1) pbar.finish() print def example2(): class CrazyFileTransferSpeed(FileTransferSpeed): "It's bigger between 45 and 80 percent" def update(self, pbar): if 45 < pbar.percentage() < 80: return 'Bigger Now ' + FileTransferSpeed.update(self, pbar) else: return FileTransferSpeed.update(self, pbar) widgets = [CrazyFileTransferSpeed(), ' <<<', Bar(), '>>> ', Percentage(), ' ', ETA()] pbar = ProgressBar(widgets=widgets, maxval=10000000) # maybe do something pbar.start() for i in range(2000000): # do something pbar.update(5 * i + 1) pbar.finish() print def example3(): widgets = [Bar('>'), ' ', ETA(), ' ', ReverseBar('<')] pbar = ProgressBar(widgets=widgets, maxval=10000000).start() for i in range(1000000): # do something pbar.update(10 * i + 1) pbar.finish() print def example4(): widgets = ['Test: ', Percentage(), ' ', Bar(marker='0', left='[', right=']'), ' ', ETA(), ' ', FileTransferSpeed()] pbar = ProgressBar(widgets=widgets, maxval=500) pbar.start() for i in range(100, 500 + 1, 50): time.sleep(0.2) pbar.update(i) pbar.finish() print example1() example2() example3() example4()
[ [ 8, 0, 0.0831, 0.0509, 0, 0.66, 0, 0, 1, 0, 0, 0, 0, 0, 0 ], [ 14, 0, 0.1126, 0.0027, 0, 0.66, 0.05, 777, 1, 0, 0, 0, 0, 3, 0 ], [ 14, 0, 0.1153, 0.0027, 0, 0.66, ...
[ "\"\"\"Text progressbar library for python.\n\nThis library provides a text mode progressbar. This is tipically used\nto display the progress of a long running operation, providing a\nvisual clue that processing is underway.\n\nThe ProgressBar class manages the progress, and the format of the line\nis given by a nu...
'''Levenshtein Distance Calculator for Clearcutter log identification module''' __author__ = "CP Constantine" __email__ = "conrad@alienvault.com" __copyright__ = 'Copyright:Alienvault 2012' __credits__ = ["Conrad Constantine"] __version__ = "0.1" __license__ = "BSD" __status__ = "Prototype" __maintainer__ = "CP Constantine" def levenshtein(s1, s2): l1 = len(s1) l2 = len(s2) matrix = [range(l1 + 1)] * (l2 + 1) for zz in range(l2 + 1): matrix[zz] = range(zz, zz + l1 + 1) for zz in range(0, l2): for sz in range(0, l1): if s1[sz] == s2[zz]: matrix[zz + 1][sz + 1] = min(matrix[zz + 1][sz] + 1, matrix[zz][sz + 1] + 1, matrix[zz][sz]) else: matrix[zz + 1][sz + 1] = min(matrix[zz + 1][sz] + 1, matrix[zz][sz + 1] + 1, matrix[zz][sz] + 1) return matrix[l2][l1]
[ [ 8, 0, 0.037, 0.037, 0, 0.66, 0, 0, 1, 0, 0, 0, 0, 0, 0 ], [ 14, 0, 0.1111, 0.037, 0, 0.66, 0.1111, 777, 1, 0, 0, 0, 0, 3, 0 ], [ 14, 0, 0.1481, 0.037, 0, 0.66, ...
[ "'''Levenshtein Distance Calculator for Clearcutter log identification module'''", "__author__ = \"CP Constantine\"", "__email__ = \"conrad@alienvault.com\"", "__copyright__ = 'Copyright:Alienvault 2012'", "__credits__ = [\"Conrad Constantine\"]", "__version__ = \"0.1\"", "__license__ = \"BSD\"", "__s...
'''Extract calls to logging libraries from code Trees''' class CodeScrape(object): ''' classdocs ''' def __init__(self,params): ''' Constructor ''' pass
[ [ 8, 0, 0.0667, 0.0667, 0, 0.66, 0, 0, 1, 0, 0, 0, 0, 0, 0 ], [ 3, 0, 0.5333, 0.7333, 0, 0.66, 1, 888, 0, 1, 0, 0, 186, 0, 0 ], [ 8, 1, 0.3333, 0.2, 1, 0.72, 0,...
[ "'''Extract calls to logging libraries from code Trees'''", "class CodeScrape(object):\n '''\n classdocs\n '''\n\n\n def __init__(self,params):\n '''", " '''\n classdocs\n '''", " def __init__(self,params):\n '''\n Constructor\n '''\n pass", " ...
""" Clusters Locate clusters of test in Logfiles, to assist in processing discrete log messages, from any given log data sample and assist in the creation of Regular Expression to parse those log entries """ __author__ = "CP Constantine" __email__ = "conrad@alienvault.com" __copyright__ = 'Copyright:Alienvault 2012' __credits__ = ["Conrad Constantine"] __version__ = "0.2" __license__ = "BSD" __status__ = "Prototype" __maintainer__ = "CP Constantine" #TODO: More Regexp Patterns #TODO: Levenshtein distance grouping (recurse window groupings #TODO: Extract all unique words from a file #cat comment_file.txt | tr " " "\n" | sort | uniq -c #TODO: Print total matches for each identified log entry. import sys, progressbar, commonvars, levenshtein, plugingenerate from logfile import LogFile class ClusterNode(object): """ Linked list node for log patterns """ Children = [] Content = "" Parent = None ContentHash = "" def __init__(self, NodeContent="Not Provided"): self.Children = [] self.Content = NodeContent #if verbose > 3 : print "Created new Node " + str(id(self)) + " with content : " + self.Content self.ContentHash = hash(NodeContent) def GetChildren(self): return self.Children def GetContent(self): return self.Content def MatchChild(self, MatchContent): if len(self.Children) == 0: #print "No Children" return None else: for child in self.Children: if (child.ContentHash == hash(MatchContent)): #print "Found Child Match : " + child.Content return child else: return None def MatchNephew(self, MatchContent): """Find Nephew Match""" if self.Parent == None: #This node is the root node return None for sibling in self.Parent.Children: if len(sibling.Children) > 0 : # no point if sibling has no children for child in sibling.Children: #let's see which child node this matches if (child.Content == MatchContent): return child return None def AddChild(self, NodeContent): ChildContent = ClusterNode(NodeContent) ChildContent.Parent = self self.Children.append(ChildContent) return ChildContent def GeneratePath(self): #TODO: Compare siblings against regexps to suggest a regex replacement currentNode = self parentpath = "" while currentNode.Content != "ROOTNODE": if len(currentNode.Parent.Children) > ClusterGroup.VarThreshold: parentpath = "[VARIABLE]" + " " + parentpath else: parentpath = currentNode.Content + " " + parentpath currentNode = currentNode.Parent return parentpath class ClusterGroup(object): """ A Group of word cluster, representing the unique log types within a logfile """ Args = "" Log = "" VarThreshold = 10 #How many siblings a string node must have before it is considered to be variable data VarDistance = 20 rootNode = ClusterNode(NodeContent="ROOTNODE") entries = [] def __init__(self, args): self.rootNode = ClusterNode(NodeContent="ROOTNODE") self.Args = args def IsMatch(self, logline): ''' Test the incoming log line to see if it matches this clustergroup Return boolean match ''' logwords = commonvars.FindCommonRegex(logline).split() #TODO Split at '=' marks as well currentNode = self.rootNode for logword in logwords: #process logs a word at a time #match our own children first match = currentNode.MatchChild(MatchContent=logword) if match == None: #then try our siblings match = currentNode.MatchNephew(MatchContent=logword) if match == None: #then add a new child match = currentNode.AddChild(NodeContent=logword) if match == None: print "FAILED" else: currentNode = match def IsEndNode(self, Node): ''' Is This Node the final word of a log template? @return: True or False ''' endnode = False hasNephews = False if (len(Node.Children) is 0): #I'm an EndNode for a log wording cluster if Node.Parent is not None: #let's make sure our siblings are all endnodes too, and this is really var data for sibling in Node.Parent.Children: if len(sibling.Children) > 0 : hasNephews = True if (hasNephews is False) and (len(Node.Parent.Children) >= ClusterGroup.VarThreshold): #log event ends in a variable endnode = True if (hasNephews is False) and (len(Node.Parent.Children) == 1) : #log event ends in a fixed string endnode = True if endnode is True: entry = Node.GeneratePath() if entry not in self.entries: self.entries.append(entry) def BuildResultsTree(self, node): ''' Recurse through the Node Tree, identifying and printing complete log patterns' @return: None (recursive function) ''' if self.IsEndNode(node) == True : return None # no children so back up a level for childnode in node.Children: self.BuildResultsTree(childnode) def Results(self): ''' Display all identified unique log event types @return None ''' #if options.outfile == true: dump to file print "\n========== Potential Unique Log Events ==========\n" self.BuildResultsTree(self.rootNode) #Todo - commandline args to toggle levenshtein identification of dupes previous = '' for entry in self.entries: if levenshtein.levenshtein(entry, previous) < ClusterGroup.VarDistance : print "\t" + entry else: print entry previous = entry def Run(self): try: self.Log = LogFile(self.Args.logfile) except IOError: print "File: " + self.Log.Filename + " cannot be opened : " + str(sys.exc_info()[1]) #TODO: log to stderr raise IOError() #if args.v > 0 : print "Processing Log File " + log.Filename + ":" + str(log.Length) + " bytes" logline = self.Log.RetrieveCurrentLine() widgets = ['Processing potential messages: ', progressbar.Percentage(), ' ', progressbar.Bar(marker=progressbar.RotatingMarker()), ' ', progressbar.ETA()] if self.Args.quiet is False : pbar = progressbar.ProgressBar(widgets=widgets, maxval=100).start() while logline != "": #TODO: Make this actually exit on EOF self.IsMatch(logline) if self.Args.quiet is False : pbar.update((1.0 * self.Log.Position / self.Log.Length) * 100) logline = self.Log.RetrieveCurrentLine() if self.Args.quiet is False : pbar.finish() def GenPlugin(self): ''' Create a Template OSSIM agent plugin file using the identified log templates as SIDs @return: The filename of the generated plugin ''' generator = plugingenerate.Generator(self.entries) generator.WritePlugin() return generator.PluginFile #Take EndNode Strings #Calculate Levenshtein distance between them #Deduplicate from there.
[ [ 8, 0, 0.0111, 0.0177, 0, 0.66, 0, 0, 1, 0, 0, 0, 0, 0, 0 ], [ 14, 0, 0.0265, 0.0044, 0, 0.66, 0.0833, 777, 1, 0, 0, 0, 0, 3, 0 ], [ 14, 0, 0.031, 0.0044, 0, 0.66,...
[ "\"\"\"\nClusters Locate clusters of test in Logfiles, to assist in processing discrete log messages,\nfrom any given log data sample and assist in the creation of Regular Expression to parse those log entries\n\"\"\"", "__author__ = \"CP Constantine\"", "__email__ = \"conrad@alienvault.com\"", "__copyright__...
#!/usr/bin/python # -*- coding: iso-8859-1 -*- # # progressbar - Text progressbar library for python. # Copyright (c) 2005 Nilton Volpato # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA """Text progressbar library for python. This library provides a text mode progressbar. This is tipically used to display the progress of a long running operation, providing a visual clue that processing is underway. The ProgressBar class manages the progress, and the format of the line is given by a number of widgets. A widget is an object that may display diferently depending on the state of the progress. There are three types of widget: - a string, which always shows itself; - a ProgressBarWidget, which may return a diferent value every time it's update method is called; and - a ProgressBarWidgetHFill, which is like ProgressBarWidget, except it expands to fill the remaining width of the line. The progressbar module is very easy to use, yet very powerful. And automatically supports features like auto-resizing when available. """ __author__ = "Nilton Volpato" __author_email__ = "first-name dot last-name @ gmail.com" __date__ = "2006-05-07" __version__ = "2.2" # Changelog # # 2006-05-07: v2.2 fixed bug in windows # 2005-12-04: v2.1 autodetect terminal width, added start method # 2005-12-04: v2.0 everything is now a widget (wow!) # 2005-12-03: v1.0 rewrite using widgets # 2005-06-02: v0.5 rewrite # 2004-??-??: v0.1 first version import sys, time from array import array try: from fcntl import ioctl import termios except ImportError: pass import signal class ProgressBarWidget(object): """This is an element of ProgressBar formatting. The ProgressBar object will call it's update value when an update is needed. It's size may change between call, but the results will not be good if the size changes drastically and repeatedly. """ def update(self, pbar): """Returns the string representing the widget. The parameter pbar is a reference to the calling ProgressBar, where one can access attributes of the class for knowing how the update must be made. At least this function must be overriden.""" pass class ProgressBarWidgetHFill(object): """This is a variable width element of ProgressBar formatting. The ProgressBar object will call it's update value, informing the width this object must the made. This is like TeX \\hfill, it will expand to fill the line. You can use more than one in the same line, and they will all have the same width, and together will fill the line. """ def update(self, pbar, width): """Returns the string representing the widget. The parameter pbar is a reference to the calling ProgressBar, where one can access attributes of the class for knowing how the update must be made. The parameter width is the total horizontal width the widget must have. At least this function must be overriden.""" pass class ETA(ProgressBarWidget): "Widget for the Estimated Time of Arrival" def format_time(self, seconds): return time.strftime('%H:%M:%S', time.gmtime(seconds)) def update(self, pbar): if pbar.currval == 0: return 'ETA: --:--:--' elif pbar.finished: return 'Time: %s' % self.format_time(pbar.seconds_elapsed) else: elapsed = pbar.seconds_elapsed eta = elapsed * pbar.maxval / pbar.currval - elapsed return 'ETA: %s' % self.format_time(eta) class FileTransferSpeed(ProgressBarWidget): "Widget for showing the transfer speed (useful for file transfers)." def __init__(self): self.fmt = '%6.2f %s' self.units = ['B', 'K', 'M', 'G', 'T', 'P'] def update(self, pbar): if pbar.seconds_elapsed < 2e-6:#== 0: bps = 0.0 else: bps = float(pbar.currval) / pbar.seconds_elapsed spd = bps for u in self.units: if spd < 1000: break spd /= 1000 return self.fmt % (spd, u + '/s') class RotatingMarker(ProgressBarWidget): "A rotating marker for filling the bar of progress." def __init__(self, markers='|/-\\'): self.markers = markers self.curmark = -1 def update(self, pbar): if pbar.finished: return self.markers[0] self.curmark = (self.curmark + 1) % len(self.markers) return self.markers[self.curmark] class Percentage(ProgressBarWidget): "Just the percentage done." def update(self, pbar): return '%3d%%' % pbar.percentage() class SimpleProgress(ProgressBarWidget): "Simple Progress: returns what is already done and the total, e.g. '5 of 47'" def update(self, pbar): return '%d of %d' % (pbar.currval, pbar.maxval) class Bar(ProgressBarWidgetHFill): "The bar of progress. It will strech to fill the line." def __init__(self, marker='#', left='|', right='|'): self.marker = marker self.left = left self.right = right def _format_marker(self, pbar): if isinstance(self.marker, (str, unicode)): return self.marker else: return self.marker.update(pbar) def update(self, pbar, width): percent = pbar.percentage() cwidth = width - len(self.left) - len(self.right) marked_width = int(percent * cwidth / 100) m = self._format_marker(pbar) bar = (self.left + (m * marked_width).ljust(cwidth) + self.right) return bar class ReverseBar(Bar): "The reverse bar of progress, or bar of regress. :)" def update(self, pbar, width): percent = pbar.percentage() cwidth = width - len(self.left) - len(self.right) marked_width = int(percent * cwidth / 100) m = self._format_marker(pbar) bar = (self.left + (m * marked_width).rjust(cwidth) + self.right) return bar default_widgets = [Percentage(), ' ', Bar()] class ProgressBar(object): """This is the ProgressBar class, it updates and prints the bar. The term_width parameter may be an integer. Or None, in which case it will try to guess it, if it fails it will default to 80 columns. The simple use is like this: >>> pbar = ProgressBar().start() >>> for i in xrange(100): ... # do something ... pbar.update(i+1) ... >>> pbar.finish() But anything you want to do is possible (well, almost anything). You can supply different widgets of any type in any order. And you can even write your own widgets! There are many widgets already shipped and you should experiment with them. When implementing a widget update method you may access any attribute or function of the ProgressBar object calling the widget's update method. The most important attributes you would like to access are: - currval: current value of the progress, 0 <= currval <= maxval - maxval: maximum (and final) value of the progress - finished: True if the bar is have finished (reached 100%), False o/w - start_time: first time update() method of ProgressBar was called - seconds_elapsed: seconds elapsed since start_time - percentage(): percentage of the progress (this is a method) """ def __init__(self, maxval=100, widgets=default_widgets, term_width=None, fd=sys.stderr): assert maxval > 0 self.maxval = maxval self.widgets = widgets self.fd = fd self.signal_set = False if term_width is None: try: self.handle_resize(None, None) signal.signal(signal.SIGWINCH, self.handle_resize) self.signal_set = True except: self.term_width = 79 else: self.term_width = term_width self.currval = 0 self.finished = False self.prev_percentage = -1 self.start_time = None self.seconds_elapsed = 0 def handle_resize(self, signum, frame): h, w = array('h', ioctl(self.fd, termios.TIOCGWINSZ, '\0' * 8))[:2] self.term_width = w def percentage(self): "Returns the percentage of the progress." return self.currval * 100.0 / self.maxval def _format_widgets(self): r = [] hfill_inds = [] num_hfill = 0 currwidth = 0 for i, w in enumerate(self.widgets): if isinstance(w, ProgressBarWidgetHFill): r.append(w) hfill_inds.append(i) num_hfill += 1 elif isinstance(w, (str, unicode)): r.append(w) currwidth += len(w) else: weval = w.update(self) currwidth += len(weval) r.append(weval) for iw in hfill_inds: r[iw] = r[iw].update(self, (self.term_width - currwidth) / num_hfill) return r def _format_line(self): return ''.join(self._format_widgets()).ljust(self.term_width) def _need_update(self): return int(self.percentage()) != int(self.prev_percentage) def update(self, value): "Updates the progress bar to a new value." assert 0 <= value <= self.maxval self.currval = value if not self._need_update() or self.finished: return if not self.start_time: self.start_time = time.time() self.seconds_elapsed = time.time() - self.start_time self.prev_percentage = self.percentage() if value != self.maxval: self.fd.write(self._format_line() + '\r') else: self.finished = True self.fd.write(self._format_line() + '\n') def start(self): """Start measuring time, and prints the bar at 0%. It returns self so you can use it like this: >>> pbar = ProgressBar().start() >>> for i in xrange(100): ... # do something ... pbar.update(i+1) ... >>> pbar.finish() """ self.update(0) return self def finish(self): """Used to tell the progress is finished.""" self.update(self.maxval) if self.signal_set: signal.signal(signal.SIGWINCH, signal.SIG_DFL) if __name__ == '__main__': import os def example1(): widgets = ['Test: ', Percentage(), ' ', Bar(marker=RotatingMarker()), ' ', ETA(), ' ', FileTransferSpeed()] pbar = ProgressBar(widgets=widgets, maxval=10000000).start() for i in range(1000000): # do something pbar.update(10 * i + 1) pbar.finish() print def example2(): class CrazyFileTransferSpeed(FileTransferSpeed): "It's bigger between 45 and 80 percent" def update(self, pbar): if 45 < pbar.percentage() < 80: return 'Bigger Now ' + FileTransferSpeed.update(self, pbar) else: return FileTransferSpeed.update(self, pbar) widgets = [CrazyFileTransferSpeed(), ' <<<', Bar(), '>>> ', Percentage(), ' ', ETA()] pbar = ProgressBar(widgets=widgets, maxval=10000000) # maybe do something pbar.start() for i in range(2000000): # do something pbar.update(5 * i + 1) pbar.finish() print def example3(): widgets = [Bar('>'), ' ', ETA(), ' ', ReverseBar('<')] pbar = ProgressBar(widgets=widgets, maxval=10000000).start() for i in range(1000000): # do something pbar.update(10 * i + 1) pbar.finish() print def example4(): widgets = ['Test: ', Percentage(), ' ', Bar(marker='0', left='[', right=']'), ' ', ETA(), ' ', FileTransferSpeed()] pbar = ProgressBar(widgets=widgets, maxval=500) pbar.start() for i in range(100, 500 + 1, 50): time.sleep(0.2) pbar.update(i) pbar.finish() print example1() example2() example3() example4()
[ [ 8, 0, 0.0831, 0.0509, 0, 0.66, 0, 0, 1, 0, 0, 0, 0, 0, 0 ], [ 14, 0, 0.1126, 0.0027, 0, 0.66, 0.05, 777, 1, 0, 0, 0, 0, 3, 0 ], [ 14, 0, 0.1153, 0.0027, 0, 0.66, ...
[ "\"\"\"Text progressbar library for python.\n\nThis library provides a text mode progressbar. This is tipically used\nto display the progress of a long running operation, providing a\nvisual clue that processing is underway.\n\nThe ProgressBar class manages the progress, and the format of the line\nis given by a nu...