modinfo in the translate toolkit is now a 2-tuple containing the mtime and
[pootle.git] / statistics.py
blob920767ec5b33fdaf7d8c22db4ac488fb89a9b5e7
1 import os
2 from translate.storage import statsdb
3 from translate.filters import checks
4 from translate.misc.multistring import multistring
6 STATS_DB_FILE = None
8 def getmodtime(filename):
9 mtime, size = statsdb.get_mod_info(filename, errors_return_empty=True, empty_return=None)
10 return mtime
12 class pootlestatistics:
13 """this represents the statistics known about a file"""
14 def __init__(self, basefile):
15 """constructs statistic object for the given file"""
16 # TODO: try and remove circular references between basefile and this class
17 self.basefile = basefile
18 self._stats = None
19 self._totals = None
20 self._unitstats = None
21 self.statscache = statsdb.StatsCache(STATS_DB_FILE)
23 def getquickstats(self):
24 """returns the quick statistics (totals only)"""
25 if not self._totals:
26 self._totals = self.statscache.filetotals(self.basefile.filename, errors_return_empty=True)
27 return self._totals
29 def getstats(self, checker=None):
30 """reads the stats if neccessary or returns them from the cache"""
31 if checker == None:
32 checker = self.basefile.checker
33 if not self._stats:
34 self._stats = self.statscache.filestats(self.basefile.filename, checker, errors_return_empty=True)
35 return self._stats
37 def purge_totals(self):
38 """Temporary helper to clean up where needed. We might be able to remove
39 this after the move to cpo."""
40 self._totals = None
41 self._stats = None
42 self._unitstats = None
44 def getunitstats(self):
45 if not self._unitstats:
46 self._unitstats = self.statscache.unitstats(self.basefile.filename, errors_return_empty=True)
47 return self._unitstats
49 def updatequickstats(self, save=True):
50 """updates the project's quick stats on this file"""
51 totals = self.getquickstats()
52 self.basefile.project.updatequickstats(self.basefile.pofilename,
53 totals.get("translatedsourcewords", 0), totals.get("translated", 0),
54 totals.get("fuzzysourcewords", 0), totals.get("fuzzy", 0),
55 totals.get("totalsourcewords", 0), totals.get("total", 0),
56 save)
58 def reclassifyunit(self, item):
59 """Reclassifies all the information in the database and self._stats about
60 the given unit"""
61 unit = self.basefile.getitem(item)
62 item = self.getstats()["total"][item]
64 classes = self.statscache.recacheunit(self.basefile.filename, self.basefile.checker, unit)
65 for classname, matchingitems in self.getstats().items():
66 if (classname in classes) != (item in matchingitems):
67 if classname in classes:
68 self.getstats()[classname].append(item)
69 else:
70 self.getstats()[classname].remove(item)
71 self.getstats()[classname].sort()
72 # We should be doing better, but for now it is easiet to simply force a
73 # reload from the database
74 self.purge_totals()
76 def getitemslen(self):
77 """gets the number of items in the file"""
78 return self.getquickstats()["total"]