app/app_profiler/ppstats.py
changeset 2857 bc793800116e
parent 2832 2a0a7e081caf
equal deleted inserted replaced
2856:f446e019825c 2857:bc793800116e
     1 import pstats
     1 import pstats
     2 import cPickle
     2 import cPickle
     3 import zlib
     3 import zlib
       
     4 import re
     4 
     5 
     5 class PickleStats(object):
     6 class PickleStats(object):
     6     def __init__(self, stats):
     7     def __init__(self, stats):
     7         self.stats = stats
     8         self.stats = stats
     8 
     9 
    46 
    47 
    47 class Stats(pstats.Stats):
    48 class Stats(pstats.Stats):
    48     def __init__(self, *args, **kwargs):
    49     def __init__(self, *args, **kwargs):
    49         pstats.Stats.__init__(self, *args)
    50         pstats.Stats.__init__(self, *args)
    50         self.replace_dirs = {}
    51         self.replace_dirs = {}
       
    52         self.replace_regexes = {}
    51 
    53 
    52     def set_output(self, stream):
    54     def set_output(self, stream):
    53         "redirect output of print_stats to the file object <stream>"
    55         "redirect output of print_stats to the file object <stream>"
    54         self.stream = stream
    56         self.stream = stream
    55 
    57 
    56     def hide_directory(self, dirname, replacement=''):
    58     def hide_directory(self, dirname, replacement=''):
    57         "replace occurences of <dirname> in filenames with <replacement>"
    59         "replace occurences of <dirname> in filenames with <replacement>"
    58         self.replace_dirs[dirname] = replacement
    60         self.replace_dirs[dirname] = replacement
       
    61         
       
    62     def hide_regex(self, pattern, replacement=''):
       
    63         "call re.sub(pattern, replacement) on each filename"
       
    64         self.replace_regexes[pattern] = replacement
    59 
    65 
    60     def func_strip_path(self, func_name):
    66     def func_strip_path(self, func_name):
    61         "take a filename, line, name tuple and mangle appropiately"
    67         "take a filename, line, name tuple and mangle appropiately"
    62         filename, line, name = func_name
    68         filename, line, name = func_name
    63 
    69 
    64         for dirname in self.replace_dirs:
    70         for dirname in self.replace_dirs:
    65             filename = filename.replace(dirname, self.replace_dirs[dirname])
    71             filename = filename.replace(dirname, self.replace_dirs[dirname])
    66 
    72 
       
    73         for pattern in self.replace_regexes:
       
    74             filename = re.sub(pattern, self.replace_regexes[pattern], filename)
       
    75 
    67         return filename, line, name
    76         return filename, line, name
    68 
    77 
    69     def strip_dirs(self):
    78     def strip_dirs(self):
    70         "strip irrelevant/redundant directories from filenames in profile data"
    79         "strip irrelevant/redundant directories from filenames in profile data"
    71         func_std_string = pstats.func_std_string
    80         func_std_string = pstats.func_std_string
       
    81         add_func_stats = pstats.add_func_stats
    72 
    82 
    73         oldstats = self.stats
    83         oldstats = self.stats
    74         self.stats = newstats = {}
    84         self.stats = newstats = {}
    75         max_name_len = 0
    85         max_name_len = 0
    76         for func, (cc, nc, tt, ct, callers) in oldstats.iteritems():
    86         for func, (cc, nc, tt, ct, callers) in oldstats.iteritems():