Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
byext.Stats Class Reference

List of all members.

Public Member Functions

def __init__
def statargs
def statdir
def statfile
def addstats
def report

Public Attributes

 stats

Detailed Description

Definition at line 9 of file byext.py.


Constructor & Destructor Documentation

def byext.Stats.__init__ (   self)

Definition at line 11 of file byext.py.

00011 
00012     def __init__(self):
00013         self.stats = {}

Here is the caller graph for this function:


Member Function Documentation

def byext.Stats.addstats (   self,
  ext,
  key,
  n 
)

Definition at line 74 of file byext.py.

00074 
00075     def addstats(self, ext, key, n):
00076         d = self.stats.setdefault(ext, {})
00077         d[key] = d.get(key, 0) + n

Here is the caller graph for this function:

def byext.Stats.report (   self)

Definition at line 78 of file byext.py.

00078 
00079     def report(self):
00080         exts = sorted(self.stats)
00081         # Get the column keys
00082         columns = {}
00083         for ext in exts:
00084             columns.update(self.stats[ext])
00085         cols = sorted(columns)
00086         colwidth = {}
00087         colwidth["ext"] = max([len(ext) for ext in exts])
00088         minwidth = 6
00089         self.stats["TOTAL"] = {}
00090         for col in cols:
00091             total = 0
00092             cw = max(minwidth, len(col))
00093             for ext in exts:
00094                 value = self.stats[ext].get(col)
00095                 if value is None:
00096                     w = 0
00097                 else:
00098                     w = len("%d" % value)
00099                     total += value
00100                 cw = max(cw, w)
00101             cw = max(cw, len(str(total)))
00102             colwidth[col] = cw
00103             self.stats["TOTAL"][col] = total
00104         exts.append("TOTAL")
00105         for ext in exts:
00106             self.stats[ext]["ext"] = ext
00107         cols.insert(0, "ext")
00108 
00109         def printheader():
00110             for col in cols:
00111                 print("%*s" % (colwidth[col], col), end=' ')
00112             print()
00113 
00114         printheader()
00115         for ext in exts:
00116             for col in cols:
00117                 value = self.stats[ext].get(col, "")
00118                 print("%*s" % (colwidth[col], value), end=' ')
00119             print()
00120         printheader()  # Another header at the bottom
00121 

Here is the call graph for this function:

def byext.Stats.statargs (   self,
  args 
)

Definition at line 14 of file byext.py.

00014 
00015     def statargs(self, args):
00016         for arg in args:
00017             if os.path.isdir(arg):
00018                 self.statdir(arg)
00019             elif os.path.isfile(arg):
00020                 self.statfile(arg)
00021             else:
00022                 sys.stderr.write("Can't find %s\n" % arg)
00023                 self.addstats("<???>", "unknown", 1)

Here is the call graph for this function:

def byext.Stats.statdir (   self,
  dir 
)

Definition at line 24 of file byext.py.

00024 
00025     def statdir(self, dir):
00026         self.addstats("<dir>", "dirs", 1)
00027         try:
00028             names = os.listdir(dir)
00029         except os.error as err:
00030             sys.stderr.write("Can't list %s: %s\n" % (dir, err))
00031             self.addstats("<dir>", "unlistable", 1)
00032             return
00033         for name in sorted(names):
00034             if name.startswith(".#"):
00035                 continue  # Skip CVS temp files
00036             if name.endswith("~"):
00037                 continue  # Skip Emacs backup files
00038             full = os.path.join(dir, name)
00039             if os.path.islink(full):
00040                 self.addstats("<lnk>", "links", 1)
00041             elif os.path.isdir(full):
00042                 self.statdir(full)
00043             else:
00044                 self.statfile(full)

Here is the call graph for this function:

Here is the caller graph for this function:

def byext.Stats.statfile (   self,
  filename 
)

Definition at line 45 of file byext.py.

00045 
00046     def statfile(self, filename):
00047         head, ext = os.path.splitext(filename)
00048         head, base = os.path.split(filename)
00049         if ext == base:
00050             ext = ""  # E.g. .cvsignore is deemed not to have an extension
00051         ext = os.path.normcase(ext)
00052         if not ext:
00053             ext = "<none>"
00054         self.addstats(ext, "files", 1)
00055         try:
00056             with open(filename, "rb") as f:
00057                 data = f.read()
00058         except IOError as err:
00059             sys.stderr.write("Can't open %s: %s\n" % (filename, err))
00060             self.addstats(ext, "unopenable", 1)
00061             return
00062         self.addstats(ext, "bytes", len(data))
00063         if b'\0' in data:
00064             self.addstats(ext, "binary", 1)
00065             return
00066         if not data:
00067             self.addstats(ext, "empty", 1)
00068         # self.addstats(ext, "chars", len(data))
00069         lines = str(data, "latin-1").splitlines()
00070         self.addstats(ext, "lines", len(lines))
00071         del lines
00072         words = data.split()
00073         self.addstats(ext, "words", len(words))

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 12 of file byext.py.


The documentation for this class was generated from the following file: